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