2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
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.
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.
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
25 #include <uilib/uilib.h>
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"
39 #include "mainframe.h"
41 // handle to the console log file
47 bool g_Console_enableLogging = false;
49 // called whenever we need to open/close/check the console log file
50 void Sys_LogFile( bool enable ){
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
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";
66 globalOutputStream() << "Today is: " << ctime( &localtime )
67 << "This is NetRadiant '" RADIANT_VERSION "' compiled " __DATE__ "\n" RADIANT_ABOUTMSG "\n";
70 ui::root.alert( "Failed to create log file, check write permissions in Radiant directory.\n",
71 "Console logging", ui::alert_type::OK, ui::alert_icon::Error );
74 else if ( !enable && g_hLogFile != 0 ) {
75 // settings say we should not be logging but still we have an active logfile .. close it
78 globalOutputStream() << "Closing log file at " << ctime( &localtime ) << "\n";
87 GtkTextBuffer* buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW( g_console ) );
88 gtk_text_buffer_set_text( buffer, "", -1 );
91 void console_populate_popup( GtkTextView* textview, ui::Menu menu, gpointer user_data ){
92 menu_separator( menu );
94 ui::Widget item(ui::MenuItem( "Clear" ));
95 item.connect( "activate", G_CALLBACK( console_clear ), 0 );
100 gboolean destroy_set_null( ui::Window widget, ui::Widget* p ){
105 WidgetFocusPrinter g_consoleWidgetFocusPrinter( "console" );
107 ui::Widget Console_constructWindow( ui::Window toplevel ){
108 auto scr = ui::ScrolledWindow();
109 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( scr ), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
110 gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW( scr ), GTK_SHADOW_IN );
114 ui::Widget text = ui::TextView();
115 gtk_widget_set_size_request( text, 0, -1 ); // allow shrinking
116 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( text ), GTK_WRAP_WORD );
117 gtk_text_view_set_editable( GTK_TEXT_VIEW( text ), FALSE );
122 //globalExtendedASCIICharacterSet().print();
124 widget_connect_escape_clear_focus_widget( g_console );
126 //g_consoleWidgetFocusPrinter.connect(g_console);
128 g_console.connect( "populate-popup", G_CALLBACK( console_populate_popup ), 0 );
129 g_console.connect( "destroy", G_CALLBACK( destroy_set_null ), &g_console );
132 gtk_container_set_focus_chain( GTK_CONTAINER( scr ), NULL );
137 class GtkTextBufferOutputStream : public TextOutputStream
139 GtkTextBuffer* textBuffer;
143 GtkTextBufferOutputStream( GtkTextBuffer* textBuffer, GtkTextIter* iter, GtkTextTag* tag ) : textBuffer( textBuffer ), iter( iter ), tag( tag ){
145 std::size_t write( const char* buffer, std::size_t length ){
146 gtk_text_buffer_insert_with_tags( textBuffer, iter, buffer, gint( length ), tag, 0 );
151 std::size_t Sys_Print( int level, const char* buf, std::size_t length ){
152 bool contains_newline = std::find( buf, buf + length, '\n' ) != buf + length;
154 if ( level == SYS_ERR ) {
158 if ( g_hLogFile != 0 ) {
159 fwrite( buf, 1, length, g_hLogFile );
160 if ( contains_newline ) {
161 fflush( g_hLogFile );
165 if ( level != SYS_NOCON ) {
166 if ( g_console != 0 ) {
167 GtkTextBuffer* buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW( g_console ) );
170 gtk_text_buffer_get_end_iter( buffer, &iter );
172 static GtkTextMark* end = gtk_text_buffer_create_mark( buffer, "end", &iter, FALSE );
174 const GdkColor yellow = { 0, 0xb0ff, 0xb0ff, 0x0000 };
175 const GdkColor red = { 0, 0xffff, 0x0000, 0x0000 };
177 static GtkTextTag* error_tag = gtk_text_buffer_create_tag( buffer, "red_foreground", "foreground-gdk", &red, 0 );
178 static GtkTextTag* warning_tag = gtk_text_buffer_create_tag( buffer, "yellow_foreground", "foreground-gdk", &yellow, 0 );
179 static GtkTextTag* standard_tag = gtk_text_buffer_create_tag( buffer, "black_foreground", 0 );
198 GtkTextBufferOutputStream textBuffer( buffer, &iter, tag );
199 if ( !globalCharacterSet().isUTF8() ) {
200 BufferedTextOutputStream<GtkTextBufferOutputStream> buffered( textBuffer );
201 buffered << StringRange( buf, buf + length );
205 textBuffer << StringRange( buf, buf + length );
209 // update console widget immediatly if we're doing something time-consuming
210 if ( contains_newline ) {
211 gtk_text_view_scroll_mark_onscreen( GTK_TEXT_VIEW( g_console ), end );
213 if ( !ScreenUpdates_Enabled() && gtk_widget_get_realized( g_console ) ) {
214 ScreenUpdates_process();
223 class SysPrintOutputStream : public TextOutputStream
226 std::size_t write( const char* buffer, std::size_t length ){
227 return Sys_Print( SYS_STD, buffer, length );
231 class SysPrintErrorStream : public TextOutputStream
234 std::size_t write( const char* buffer, std::size_t length ){
235 return Sys_Print( SYS_ERR, buffer, length );
239 SysPrintOutputStream g_outputStream;
241 TextOutputStream& getSysPrintOutputStream(){
242 return g_outputStream;
245 SysPrintErrorStream g_errorStream;
247 TextOutputStream& getSysPrintErrorStream(){
248 return g_errorStream;