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 <gtk/gtktextbuffer.h>
26 #include <gtk/gtktextview.h>
27 #include <gtk/gtkmenuitem.h>
28 #include <gtk/gtkscrolledwindow.h>
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"
41 #include "mainframe.h"
43 // handle to the console log file
49 bool g_Console_enableLogging = false;
51 // called whenever we need to open/close/check the console log file
52 void Sys_LogFile( bool enable ){
53 if ( enable && !g_hLogFile ) {
54 // settings say we should be logging and we don't have a log file .. so create it
55 if ( !SettingsPath_get()[0] ) {
56 return; // cannot open a log file yet
58 // open a file to log the console (if user prefs say so)
59 // the file handle is g_hLogFile
60 // the log file is erased
61 StringOutputStream name( 256 );
62 name << SettingsPath_get() << "radiant.log";
63 g_hLogFile = fopen( name.c_str(), "w" );
64 if ( g_hLogFile != 0 ) {
65 globalOutputStream() << "Started logging to " << name.c_str() << "\n";
68 globalOutputStream() << "Today is: " << ctime( &localtime )
69 << "This is NetRadiant '" RADIANT_VERSION "' compiled " __DATE__ "\n" RADIANT_ABOUTMSG "\n";
72 gtk_MessageBox( 0, "Failed to create log file, check write permissions in Radiant directory.\n",
73 "Console logging", eMB_OK, eMB_ICONERROR );
76 else if ( !enable && g_hLogFile != 0 ) {
77 // settings say we should not be logging but still we have an active logfile .. close it
80 globalOutputStream() << "Closing log file at " << ctime( &localtime ) << "\n";
86 GtkWidget* g_console = 0;
89 GtkTextBuffer* buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW( g_console ) );
90 gtk_text_buffer_set_text( buffer, "", -1 );
93 void console_populate_popup( GtkTextView* textview, GtkMenu* menu, gpointer user_data ){
94 menu_separator( menu );
96 GtkWidget* item = gtk_menu_item_new_with_label( "Clear" );
97 g_signal_connect( G_OBJECT( item ), "activate", G_CALLBACK( console_clear ), 0 );
98 gtk_widget_show( item );
99 container_add_widget( GTK_CONTAINER( menu ), item );
102 gboolean destroy_set_null( GtkWindow* widget, GtkWidget** p ){
107 WidgetFocusPrinter g_consoleWidgetFocusPrinter( "console" );
109 GtkWidget* Console_constructWindow( GtkWindow* toplevel ){
110 GtkWidget* scr = gtk_scrolled_window_new( 0, 0 );
111 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( scr ), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
112 gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW( scr ), GTK_SHADOW_IN );
113 gtk_widget_show( scr );
116 GtkWidget* text = gtk_text_view_new();
117 gtk_widget_set_size_request( text, 0, -1 ); // allow shrinking
118 gtk_text_view_set_wrap_mode( GTK_TEXT_VIEW( text ), GTK_WRAP_WORD );
119 gtk_text_view_set_editable( GTK_TEXT_VIEW( text ), FALSE );
120 gtk_container_add( GTK_CONTAINER( scr ), text );
121 gtk_widget_show( text );
124 //globalExtendedASCIICharacterSet().print();
126 widget_connect_escape_clear_focus_widget( g_console );
128 //g_consoleWidgetFocusPrinter.connect(g_console);
130 g_signal_connect( G_OBJECT( g_console ), "populate-popup", G_CALLBACK( console_populate_popup ), 0 );
131 g_signal_connect( G_OBJECT( g_console ), "destroy", G_CALLBACK( destroy_set_null ), &g_console );
134 gtk_container_set_focus_chain( GTK_CONTAINER( scr ), NULL );
139 //#pragma GCC push_options
140 //#pragma GCC optimize ("O0")
142 class GtkTextBufferOutputStream : public TextOutputStream
144 GtkTextBuffer* textBuffer;
148 GtkTextBufferOutputStream( GtkTextBuffer* textBuffer, GtkTextIter* iter, GtkTextTag* tag ) : textBuffer( textBuffer ), iter( iter ), tag( tag ){
150 std::size_t __attribute__((optimize("O0"))) write( const char* buffer, std::size_t length ){
151 gtk_text_buffer_insert_with_tags( textBuffer, iter, buffer, gint( length ), tag, 0 );
156 //#pragma GCC pop_options
158 std::size_t Sys_Print( int level, const char* buf, std::size_t length ){
159 bool contains_newline = std::find( buf, buf + length, '\n' ) != buf + length;
161 if ( level == SYS_ERR ) {
165 if ( g_hLogFile != 0 ) {
166 fwrite( buf, 1, length, g_hLogFile );
167 if ( contains_newline ) {
168 fflush( g_hLogFile );
172 if ( level != SYS_NOCON ) {
173 if ( g_console != 0 ) {
174 GtkTextBuffer* buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW( g_console ) );
177 gtk_text_buffer_get_end_iter( buffer, &iter );
179 static GtkTextMark* end = gtk_text_buffer_create_mark( buffer, "end", &iter, FALSE );
181 const GdkColor yellow = { 0, 0xb0ff, 0xb0ff, 0x0000 };
182 const GdkColor red = { 0, 0xffff, 0x0000, 0x0000 };
184 static GtkTextTag* error_tag = gtk_text_buffer_create_tag( buffer, "red_foreground", "foreground-gdk", &red, 0 );
185 static GtkTextTag* warning_tag = gtk_text_buffer_create_tag( buffer, "yellow_foreground", "foreground-gdk", &yellow, 0 );
186 static GtkTextTag* standard_tag = gtk_text_buffer_create_tag( buffer, "black_foreground", 0 );
205 GtkTextBufferOutputStream textBuffer( buffer, &iter, tag );
206 if ( !globalCharacterSet().isUTF8() ) {
207 BufferedTextOutputStream<GtkTextBufferOutputStream> buffered( textBuffer );
208 buffered << StringRange( buf, buf + length );
212 textBuffer << StringRange( buf, buf + length );
216 // update console widget immediatly if we're doing something time-consuming
217 if ( contains_newline ) {
218 gtk_text_view_scroll_mark_onscreen( GTK_TEXT_VIEW( g_console ), end );
220 if ( !ScreenUpdates_Enabled() && GTK_WIDGET_REALIZED( g_console ) ) {
221 ScreenUpdates_process();
230 class SysPrintOutputStream : public TextOutputStream
233 std::size_t write( const char* buffer, std::size_t length ){
234 return Sys_Print( SYS_STD, buffer, length );
238 class SysPrintErrorStream : public TextOutputStream
241 std::size_t write( const char* buffer, std::size_t length ){
242 return Sys_Print( SYS_ERR, buffer, length );
246 SysPrintOutputStream g_outputStream;
248 TextOutputStream& getSysPrintOutputStream(){
249 return g_outputStream;
252 SysPrintErrorStream g_errorStream;
254 TextOutputStream& getSysPrintErrorStream(){
255 return g_errorStream;