]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/console.cpp
ported PrtView plugin
[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 <gtk/gtktextbuffer.h>
26 #include <gtk/gtktextview.h>
27 #include <gtk/gtkmenuitem.h>
28 #include <gtk/gtkscrolledwindow.h>
29
30 #include "gtkutil/accelerator.h"
31 #include "gtkutil/messagebox.h"
32 #include "gtkutil/container.h"
33 #include "gtkutil/menu.h"
34 #include "gtkutil/nonmodal.h"
35 #include "stream/stringstream.h"
36 #include "convert.h"
37
38 #include "version.h"
39 #include "aboutmsg.h"
40 #include "gtkmisc.h"
41 #include "mainframe.h"
42
43 // handle to the console log file
44 namespace
45 {
46   FILE* g_hLogFile;
47 }
48
49 bool g_Console_enableLogging = false;
50
51 // called whenever we need to open/close/check the console log file
52 void Sys_LogFile(bool enable)
53 {
54   if (enable && !g_hLogFile)
55   {
56     // settings say we should be logging and we don't have a log file .. so create it
57     // open a file to log the console (if user prefs say so)
58     // the file handle is g_hLogFile
59     // the log file is erased
60     StringOutputStream name(256);
61     name << SettingsPath_get() << "radiant.log";
62     g_hLogFile = fopen( name.c_str(), "w" );
63     if (g_hLogFile != 0)
64     {
65       globalOutputStream() << "Started logging to " << name.c_str() << "\n";
66       time_t localtime;
67       time(&localtime);
68       globalOutputStream() << "Today is: " << ctime(&localtime)
69         << "This is GtkRadiant '" RADIANT_VERSION "' compiled " __DATE__ "\n" RADIANT_ABOUTMSG "\n";
70     }
71     else
72       gtk_MessageBox (0, "Failed to create log file, check write permissions in Radiant directory.\n",
73           "Console logging", eMB_OK, eMB_ICONERROR );
74   }
75   else if (!enable && g_hLogFile != 0)
76   {
77     // settings say we should not be logging but still we have an active logfile .. close it
78     time_t localtime;
79     time(&localtime);
80     globalOutputStream() << "Closing log file at " << ctime(&localtime) << "\n";
81     fclose( g_hLogFile );
82     g_hLogFile = 0;
83   }
84 }
85
86 GtkWidget* g_console = 0;
87
88 void console_clear()
89 {
90   GtkTextBuffer* buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(g_console));
91   gtk_text_buffer_set_text(buffer, "", -1);
92 }
93
94 void console_populate_popup(GtkTextView* textview, GtkMenu* menu, gpointer user_data)
95 {
96   menu_separator(menu);
97
98   GtkWidget* item = gtk_menu_item_new_with_label ("Clear");
99   g_signal_connect(G_OBJECT (item), "activate", G_CALLBACK(console_clear), 0);
100   gtk_widget_show (item);
101   container_add_widget(GTK_CONTAINER(menu), item);
102 }
103
104 gboolean destroy_set_null(GtkWindow* widget, GtkWidget** p)
105 {
106   *p = 0;
107   return FALSE;
108 }
109
110
111 GtkWidget* Console_constructWindow(GtkWindow* toplevel)
112 {
113   GtkWidget* scr = gtk_scrolled_window_new (0, 0);
114   gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scr), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
115   gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scr), GTK_SHADOW_IN);
116   gtk_widget_show(scr);
117
118   {
119     GtkWidget* text = gtk_text_view_new();
120     gtk_widget_set_size_request(text, 0, -1); // allow shrinking
121     gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD);
122     gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
123     gtk_container_add(GTK_CONTAINER (scr), text);
124     gtk_widget_show(text);
125     g_console = text;
126
127     //globalExtendedASCIICharacterSet().print();
128
129     widget_connect_escape_clear_focus_widget(g_console);
130
131     g_signal_connect(G_OBJECT(g_console), "populate-popup", G_CALLBACK(console_populate_popup), 0);
132     g_signal_connect(G_OBJECT(g_console), "destroy", G_CALLBACK(destroy_set_null), &g_console);
133   }
134
135   gtk_container_set_focus_chain(GTK_CONTAINER(scr), NULL);
136
137   return scr;
138 }
139
140 class GtkTextBufferOutputStream : public TextOutputStream
141 {
142   GtkTextBuffer* textBuffer;
143   GtkTextIter* iter;
144   GtkTextTag* tag;
145 public:
146   GtkTextBufferOutputStream(GtkTextBuffer* textBuffer, GtkTextIter* iter, GtkTextTag* tag) : textBuffer(textBuffer), iter(iter), tag(tag)
147   {
148   }
149   std::size_t write(const char* buffer, std::size_t length)
150   {
151     gtk_text_buffer_insert_with_tags(textBuffer, iter, buffer, gint(length), tag, 0);
152     return length;
153   }
154 };
155
156 std::size_t Sys_Print(int level, const char* buf, std::size_t length)
157 {
158   bool contains_newline = strchr(buf, '\n') != 0;
159
160   if(level == SYS_ERR)
161   {
162     Sys_LogFile(true);
163   }
164
165   if (g_hLogFile != 0)
166   {
167     fwrite(buf, 1, length, g_hLogFile);
168     if(contains_newline)
169     {
170       fflush(g_hLogFile);
171     }
172   }
173
174   if (level != SYS_NOCON)
175   {
176     if (g_console != 0)
177     {
178       GtkTextBuffer* buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(g_console));
179       
180       GtkTextIter iter;
181       gtk_text_buffer_get_end_iter(buffer, &iter);
182
183       static GtkTextMark* end = gtk_text_buffer_create_mark(buffer, "end", &iter, FALSE);
184
185       const GdkColor yellow = { 0, 0xb0ff, 0xb0ff, 0x0000 };
186       const GdkColor red = { 0, 0xffff, 0x0000, 0x0000 };
187       const GdkColor black = { 0, 0x0000, 0x0000, 0x0000 };
188
189       static GtkTextTag* error_tag = gtk_text_buffer_create_tag (buffer, "red_foreground", "foreground-gdk", &red, 0);
190       static GtkTextTag* warning_tag = gtk_text_buffer_create_tag (buffer, "yellow_foreground", "foreground-gdk", &yellow, 0);
191       static GtkTextTag* standard_tag = gtk_text_buffer_create_tag (buffer, "black_foreground", "foreground-gdk", &black, 0);
192       GtkTextTag* tag;
193       switch (level)
194       {
195       case SYS_WRN:
196         tag = warning_tag;
197         break;
198       case SYS_ERR:
199         tag = error_tag;
200         break;
201       case SYS_STD:
202       case SYS_VRB:
203       default:
204         tag = standard_tag;
205         break;
206       }
207
208
209       {
210         GtkTextBufferOutputStream textBuffer(buffer, &iter, tag);
211         if(!globalCharacterSet().isUTF8())
212         {
213           BufferedTextOutputStream<GtkTextBufferOutputStream> buffered(textBuffer);
214           buffered << ConvertLocaleToUTF8(StringRange(buf, buf + length));
215         }
216         else
217         {
218           textBuffer << StringRange(buf, buf + length);
219         }
220       }
221
222       // update console widget immediatly if we're doing something time-consuming
223       if(contains_newline)
224       {
225         gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(g_console), end);
226
227         if(!ScreenUpdates_Enabled() && GTK_WIDGET_REALIZED(g_console))
228         {
229           ScreenUpdates_process();
230         }
231       }
232     }
233   }
234   return length;
235 }
236
237
238 class SysPrintOutputStream : public TextOutputStream
239 {
240 public:
241   std::size_t write(const char* buffer, std::size_t length)
242   {
243     return Sys_Print(SYS_STD, buffer, length);
244   }
245 };
246
247 class SysPrintErrorStream : public TextOutputStream
248 {
249 public:
250   std::size_t write(const char* buffer, std::size_t length)
251   {
252     return Sys_Print(SYS_ERR, buffer, length);
253   }
254 };
255
256 SysPrintOutputStream g_outputStream;
257
258 TextOutputStream& getSysPrintOutputStream()
259 {
260   return g_outputStream;
261 }
262
263 SysPrintErrorStream g_errorStream;
264
265 TextOutputStream& getSysPrintErrorStream()
266 {
267   return g_errorStream;
268 }
269
270