]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/console.cpp
Merge branch 'transfilterfix' into 'master'
[xonotic/netradiant.git] / radiant / console.cpp
1 /*
2    Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3    For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5    This file is part of GtkRadiant.
6
7    GtkRadiant is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    GtkRadiant is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GtkRadiant; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 #include "console.h"
23
24 #include <time.h>
25 #include <uilib/uilib.h>
26 #include <gtk/gtk.h>
27
28 #include "gtkutil/accelerator.h"
29 #include "gtkutil/messagebox.h"
30 #include "gtkutil/container.h"
31 #include "gtkutil/menu.h"
32 #include "gtkutil/nonmodal.h"
33 #include "stream/stringstream.h"
34 #include "convert.h"
35
36 #include "version.h"
37 #include "aboutmsg.h"
38 #include "gtkmisc.h"
39 #include "mainframe.h"
40
41 // handle to the console log file
42 namespace {
43     FILE *g_hLogFile;
44 }
45
46 bool g_Console_enableLogging = false;
47
48 // called whenever we need to open/close/check the console log file
49 void Sys_LogFile(bool enable)
50 {
51     if (enable && !g_hLogFile) {
52         // settings say we should be logging and we don't have a log file .. so create it
53         if (!SettingsPath_get()[0]) {
54             return; // cannot open a log file yet
55         }
56         // open a file to log the console (if user prefs say so)
57         // the file handle is g_hLogFile
58         // the log file is erased
59         StringOutputStream name(256);
60         name << SettingsPath_get() << "radiant.log";
61         g_hLogFile = fopen(name.c_str(), "w");
62         if (g_hLogFile != 0) {
63             globalOutputStream() << "Started logging to " << name.c_str() << "\n";
64             time_t localtime;
65             time(&localtime);
66             globalOutputStream() << "Today is: " << ctime(&localtime)
67                                  << "This is NetRadiant '" RADIANT_VERSION "' compiled " __DATE__ "\n" RADIANT_ABOUTMSG "\n";
68         } else {
69             ui::alert(ui::root, "Failed to create log file, check write permissions in Radiant directory.\n",
70                       "Console logging", ui::alert_type::OK, ui::alert_icon::Error);
71         }
72     } else if (!enable && g_hLogFile != 0) {
73         // settings say we should not be logging but still we have an active logfile .. close it
74         time_t localtime;
75         time(&localtime);
76         globalOutputStream() << "Closing log file at " << ctime(&localtime) << "\n";
77         fclose(g_hLogFile);
78         g_hLogFile = 0;
79     }
80 }
81
82 ui::TextView g_console{ui::null};
83
84 void console_clear()
85 {
86     g_console.text("");
87 }
88
89 void console_populate_popup(ui::TextView textview, ui::Menu menu, gpointer user_data)
90 {
91     menu_separator(menu);
92
93     ui::Widget item(ui::MenuItem("Clear"));
94     item.connect("activate", G_CALLBACK(console_clear), 0);
95     item.show();
96     menu.add(item);
97 }
98
99 gboolean destroy_set_null(ui::Window widget, ui::Widget *p)
100 {
101     *p = ui::Widget{ui::null};
102     return FALSE;
103 }
104
105 WidgetFocusPrinter g_consoleWidgetFocusPrinter("console");
106
107 ui::Widget Console_constructWindow(ui::Window toplevel)
108 {
109     auto scr = ui::ScrolledWindow(ui::New);
110     scr.overflow(ui::Policy::AUTOMATIC, ui::Policy::AUTOMATIC);
111     gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scr), GTK_SHADOW_IN);
112     scr.show();
113
114     {
115         auto text = ui::TextView(ui::New);
116         text.dimensions(0, -1); // allow shrinking
117         gtk_text_view_set_wrap_mode(text, GTK_WRAP_WORD);
118         gtk_text_view_set_editable(text, FALSE);
119         scr.add(text);
120         text.show();
121         g_console = text;
122
123         //globalExtendedASCIICharacterSet().print();
124
125         widget_connect_escape_clear_focus_widget(g_console);
126
127         //g_consoleWidgetFocusPrinter.connect(g_console);
128
129         g_console.connect("populate-popup", G_CALLBACK(console_populate_popup), 0);
130         g_console.connect("destroy", G_CALLBACK(destroy_set_null), &g_console);
131     }
132
133     gtk_container_set_focus_chain(GTK_CONTAINER(scr), NULL);
134
135     return scr;
136 }
137
138 class GtkTextBufferOutputStream : public TextOutputStream {
139     GtkTextBuffer *textBuffer;
140     GtkTextIter *iter;
141     GtkTextTag *tag;
142 public:
143     GtkTextBufferOutputStream(GtkTextBuffer *textBuffer, GtkTextIter *iter, GtkTextTag *tag) : textBuffer(textBuffer),
144                                                                                                iter(iter), tag(tag)
145     {
146     }
147
148     std::size_t write(const char *buffer, std::size_t length)
149     {
150         gtk_text_buffer_insert_with_tags(textBuffer, iter, buffer, gint(length), tag, NULL);
151         return length;
152     }
153 };
154
155 std::size_t Sys_Print(int level, const char *buf, std::size_t length)
156 {
157     bool contains_newline = std::find(buf, buf + length, '\n') != buf + length;
158
159     if (level == SYS_ERR) {
160         Sys_LogFile(true);
161     }
162
163     if (g_hLogFile != 0) {
164         fwrite(buf, 1, length, g_hLogFile);
165         if (contains_newline) {
166             fflush(g_hLogFile);
167         }
168     }
169
170     if (level != SYS_NOCON) {
171         if (g_console) {
172             auto buffer = gtk_text_view_get_buffer(g_console);
173
174             GtkTextIter iter;
175             gtk_text_buffer_get_end_iter(buffer, &iter);
176
177             static auto end = gtk_text_buffer_create_mark(buffer, "end", &iter, FALSE);
178
179             const GdkColor yellow = {0, 0xb0ff, 0xb0ff, 0x0000};
180             const GdkColor red = {0, 0xffff, 0x0000, 0x0000};
181
182             static auto error_tag = gtk_text_buffer_create_tag(buffer, "red_foreground", "foreground-gdk", &red, NULL);
183             static auto warning_tag = gtk_text_buffer_create_tag(buffer, "yellow_foreground", "foreground-gdk", &yellow,
184                                                                  NULL);
185             static auto standard_tag = gtk_text_buffer_create_tag(buffer, "black_foreground", NULL);
186             GtkTextTag *tag;
187             switch (level) {
188                 case SYS_WRN:
189                     tag = warning_tag;
190                     break;
191                 case SYS_ERR:
192                     tag = error_tag;
193                     break;
194                 case SYS_STD:
195                 case SYS_VRB:
196                 default:
197                     tag = standard_tag;
198                     break;
199             }
200
201
202             {
203                 GtkTextBufferOutputStream textBuffer(buffer, &iter, tag);
204                 if (!globalCharacterSet().isUTF8()) {
205                     BufferedTextOutputStream<GtkTextBufferOutputStream> buffered(textBuffer);
206                     buffered << StringRange(buf, buf + length);
207                 } else {
208                     textBuffer << StringRange(buf, buf + length);
209                 }
210             }
211
212             // update console widget immediatly if we're doing something time-consuming
213             if (contains_newline) {
214                 gtk_text_view_scroll_mark_onscreen(g_console, end);
215
216                 if (!ScreenUpdates_Enabled() && gtk_widget_get_realized(g_console)) {
217                     ScreenUpdates_process();
218                 }
219             }
220         }
221     }
222     return length;
223 }
224
225
226 class SysPrintOutputStream : public TextOutputStream {
227 public:
228     std::size_t write(const char *buffer, std::size_t length)
229     {
230         return Sys_Print(SYS_STD, buffer, length);
231     }
232 };
233
234 class SysPrintErrorStream : public TextOutputStream {
235 public:
236     std::size_t write(const char *buffer, std::size_t length)
237     {
238         return Sys_Print(SYS_ERR, buffer, length);
239     }
240 };
241
242 SysPrintOutputStream g_outputStream;
243
244 TextOutputStream &getSysPrintOutputStream()
245 {
246     return g_outputStream;
247 }
248
249 SysPrintErrorStream g_errorStream;
250
251 TextOutputStream &getSysPrintErrorStream()
252 {
253     return g_errorStream;
254 }