]> git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/console.cpp
Wrap GtkScrolledWindow
[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 #include <uilib/uilib.h>
30
31 #include "gtkutil/accelerator.h"
32 #include "gtkutil/messagebox.h"
33 #include "gtkutil/container.h"
34 #include "gtkutil/menu.h"
35 #include "gtkutil/nonmodal.h"
36 #include "stream/stringstream.h"
37 #include "convert.h"
38
39 #include "version.h"
40 #include "aboutmsg.h"
41 #include "gtkmisc.h"
42 #include "mainframe.h"
43
44 // handle to the console log file
45 namespace
46 {
47 FILE* g_hLogFile;
48 }
49
50 bool g_Console_enableLogging = false;
51
52 // called whenever we need to open/close/check the console log file
53 void Sys_LogFile( bool enable ){
54         if ( enable && !g_hLogFile ) {
55                 // settings say we should be logging and we don't have a log file .. so create it
56                 if ( !SettingsPath_get()[0] ) {
57                         return; // cannot open a log file yet
58                 }
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                         globalOutputStream() << "Started logging to " << name.c_str() << "\n";
67                         time_t localtime;
68                         time( &localtime );
69                         globalOutputStream() << "Today is: " << ctime( &localtime )
70                                                                  << "This is NetRadiant '" << radiant::version()
71                                                                  << "' compiled " __DATE__ "\n"
72                                                                  << radiant::about_msg() << "\n";
73                 }
74                 else{
75                         ui::root.alert( "Failed to create log file, check write permissions in Radiant directory.\n",
76                                                         "Console logging", ui::alert_type::OK, ui::alert_icon::ERROR );
77                 }
78         }
79         else if ( !enable && g_hLogFile != 0 ) {
80                 // settings say we should not be logging but still we have an active logfile .. close it
81                 time_t localtime;
82                 time( &localtime );
83                 globalOutputStream() << "Closing log file at " << ctime( &localtime ) << "\n";
84                 fclose( g_hLogFile );
85                 g_hLogFile = 0;
86         }
87 }
88
89 ui::Widget g_console;
90
91 void console_clear(){
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         menu_separator( menu );
98
99         ui::Widget item(gtk_menu_item_new_with_label( "Clear" ));
100         g_signal_connect( G_OBJECT( item ), "activate", G_CALLBACK( console_clear ), 0 );
101         gtk_widget_show( item );
102         container_add_widget( GTK_CONTAINER( menu ), item );
103 }
104
105 gboolean destroy_set_null( ui::Window widget, ui::Widget* p ){
106         *p = ui::Widget();
107         return FALSE;
108 }
109
110 WidgetFocusPrinter g_consoleWidgetFocusPrinter( "console" );
111
112 ui::Widget Console_constructWindow( ui::Window toplevel ){
113         ui::Widget scr = ui::ScrolledWindow();
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                 ui::Widget text = ui::Widget(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_consoleWidgetFocusPrinter.connect(g_console);
132
133                 g_signal_connect( G_OBJECT( g_console ), "populate-popup", G_CALLBACK( console_populate_popup ), 0 );
134                 g_signal_connect( G_OBJECT( g_console ), "destroy", G_CALLBACK( destroy_set_null ), &g_console );
135         }
136
137         gtk_container_set_focus_chain( GTK_CONTAINER( scr ), NULL );
138
139         return scr;
140 }
141
142 class GtkTextBufferOutputStream : public TextOutputStream
143 {
144 GtkTextBuffer* textBuffer;
145 GtkTextIter* iter;
146 GtkTextTag* tag;
147 public:
148 GtkTextBufferOutputStream( GtkTextBuffer* textBuffer, GtkTextIter* iter, GtkTextTag* tag ) : textBuffer( textBuffer ), iter( iter ), tag( tag ){
149 }
150 std::size_t write( const char* buffer, std::size_t length ){
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         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                         GtkTextBuffer* buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW( g_console ) );
173
174                         GtkTextIter iter;
175                         gtk_text_buffer_get_end_iter( buffer, &iter );
176
177                         static GtkTextMark* 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 GtkTextTag* error_tag = gtk_text_buffer_create_tag( buffer, "red_foreground", "foreground-gdk", &red, 0 );
183                         static GtkTextTag* warning_tag = gtk_text_buffer_create_tag( buffer, "yellow_foreground", "foreground-gdk", &yellow, 0 );
184                         static GtkTextTag* standard_tag = gtk_text_buffer_create_tag( buffer, "black_foreground", 0 );
185                         GtkTextTag* tag;
186                         switch ( level )
187                         {
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                                 }
208                                 else
209                                 {
210                                         textBuffer << StringRange( buf, buf + length );
211                                 }
212                         }
213
214                         // update console widget immediatly if we're doing something time-consuming
215                         if ( contains_newline ) {
216                                 gtk_text_view_scroll_mark_onscreen( GTK_TEXT_VIEW( g_console ), end );
217
218                                 if ( !ScreenUpdates_Enabled() && GTK_WIDGET_REALIZED( g_console ) ) {
219                                         ScreenUpdates_process();
220                                 }
221                         }
222                 }
223         }
224         return length;
225 }
226
227
228 class SysPrintOutputStream : public TextOutputStream
229 {
230 public:
231 std::size_t write( const char* buffer, std::size_t length ){
232         return Sys_Print( SYS_STD, buffer, length );
233 }
234 };
235
236 class SysPrintErrorStream : public TextOutputStream
237 {
238 public:
239 std::size_t write( const char* buffer, std::size_t length ){
240         return Sys_Print( SYS_ERR, buffer, length );
241 }
242 };
243
244 SysPrintOutputStream g_outputStream;
245
246 TextOutputStream& getSysPrintOutputStream(){
247         return g_outputStream;
248 }
249
250 SysPrintErrorStream g_errorStream;
251
252 TextOutputStream& getSysPrintErrorStream(){
253         return g_errorStream;
254 }