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