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
26 #include <gtk/gtklabel.h>
29 #include "generic/callback.h"
30 #include "stream/stringstream.h"
33 #include "gtkutil/menu.h"
39 GtkMenuItem *MRU_items[MRU_MAX];
41 typedef CopiedString MRU_filename_t;
42 MRU_filename_t MRU_filenames[MRU_MAX];
43 typedef const char* MRU_key_t;
44 MRU_key_t MRU_keys[MRU_MAX] = { "File0", "File1", "File2", "File3" };
47 inline const char* MRU_GetText( std::size_t index ){
48 return MRU_filenames[index].c_str();
53 StringBuffer m_buffer;
55 EscapedMnemonic( std::size_t capacity ) : m_buffer( capacity ){
56 m_buffer.push_back( '_' );
58 const char* c_str() const {
59 return m_buffer.c_str();
61 void push_back( char c ){ // not escaped
62 m_buffer.push_back( c );
64 std::size_t write( const char* buffer, std::size_t length ){
65 for ( const char* end = buffer + length; buffer != end; ++buffer )
67 if ( *buffer == '_' ) {
68 m_buffer.push_back( '_' );
71 m_buffer.push_back( *buffer );
78 inline EscapedMnemonic& operator<<( EscapedMnemonic& ostream, const T& t ){
79 return ostream_write( ostream, t );
83 void MRU_updateWidget( std::size_t index, const char *filename ){
84 EscapedMnemonic mnemonic( 64 );
85 mnemonic << Unsigned( index + 1 ) << "- " << filename;
86 gtk_label_set_text_with_mnemonic( GTK_LABEL( gtk_bin_get_child( GTK_BIN( MRU_items[index] ) ) ), mnemonic.c_str() );
89 void MRU_SetText( std::size_t index, const char *filename ){
90 MRU_filenames[index] = filename;
91 MRU_updateWidget( index, filename );
94 void MRU_AddFile( const char *str ){
98 // check if file is already in our list
99 for ( i = 0; i < MRU_used; i++ )
101 text = MRU_GetText( i );
103 if ( strcmp( text, str ) == 0 ) {
106 MRU_SetText( i, MRU_GetText( i - 1 ) );
108 MRU_SetText( 0, str );
114 if ( MRU_used < MRU_MAX ) {
119 for ( i = MRU_used - 1; i > 0; i-- )
120 MRU_SetText( i, MRU_GetText( i - 1 ) );
122 MRU_SetText( 0, str );
123 gtk_widget_set_sensitive( GTK_WIDGET( MRU_items[0] ), TRUE );
124 gtk_widget_show( GTK_WIDGET( MRU_items[MRU_used - 1] ) );
128 if ( MRU_used > MRU_MAX ) {
133 void MRU_AddWidget( GtkMenuItem *widget, std::size_t pos ){
134 if ( pos < MRU_MAX ) {
135 MRU_items[pos] = widget;
136 if ( pos < MRU_used ) {
137 MRU_updateWidget( pos, MRU_GetText( pos ) );
138 gtk_widget_set_sensitive( GTK_WIDGET( MRU_items[0] ), TRUE );
139 gtk_widget_show( GTK_WIDGET( MRU_items[pos] ) );
144 void MRU_Activate( std::size_t index ){
146 strcpy( text, MRU_GetText( index ) );
148 if ( file_readable( text ) ) { //\todo Test 'map load succeeds' instead of 'file is readable'.
152 Map_LoadFile( text );
158 for ( std::size_t i = index; i < MRU_used; i++ )
159 MRU_SetText( i, MRU_GetText( i + 1 ) );
161 if ( MRU_used == 0 ) {
162 gtk_label_set_text( GTK_LABEL( GTK_BIN( MRU_items[0] )->child ), "Recent Files" );
163 gtk_widget_set_sensitive( GTK_WIDGET( MRU_items[0] ), FALSE );
167 gtk_widget_hide( GTK_WIDGET( MRU_items[MRU_used] ) );
175 std::size_t m_number;
177 LoadMRU( std::size_t number )
178 : m_number( number ){
181 if ( ConfirmModified( "Open Map" ) ) {
182 MRU_Activate( m_number - 1 );
187 typedef MemberCaller<LoadMRU, &LoadMRU::load> LoadMRUCaller;
189 LoadMRU g_load_mru1( 1 );
190 LoadMRU g_load_mru2( 2 );
191 LoadMRU g_load_mru3( 3 );
192 LoadMRU g_load_mru4( 4 );
194 void MRU_constructMenu( GtkMenu* menu ){
196 GtkMenuItem* item = create_menu_item_with_mnemonic( menu, "_1", LoadMRUCaller( g_load_mru1 ) );
197 gtk_widget_set_sensitive( GTK_WIDGET( item ), FALSE );
198 MRU_AddWidget( item, 0 );
201 GtkMenuItem* item = create_menu_item_with_mnemonic( menu, "_2", LoadMRUCaller( g_load_mru2 ) );
202 gtk_widget_hide( GTK_WIDGET( item ) );
203 MRU_AddWidget( item, 1 );
206 GtkMenuItem* item = create_menu_item_with_mnemonic( menu, "_3", LoadMRUCaller( g_load_mru3 ) );
207 gtk_widget_hide( GTK_WIDGET( item ) );
208 MRU_AddWidget( item, 2 );
211 GtkMenuItem* item = create_menu_item_with_mnemonic( menu, "_4", LoadMRUCaller( g_load_mru4 ) );
212 gtk_widget_hide( GTK_WIDGET( item ) );
213 MRU_AddWidget( item, 3 );
217 #include "preferencesystem.h"
218 #include "stringio.h"
220 void MRU_Construct(){
221 GlobalPreferenceSystem().registerPreference( "Count", SizeImportStringCaller( MRU_used ), SizeExportStringCaller( MRU_used ) );
223 for ( std::size_t i = 0; i != MRU_MAX; ++i )
225 GlobalPreferenceSystem().registerPreference( MRU_keys[i], CopiedStringImportStringCaller( MRU_filenames[i] ), CopiedStringExportStringCaller( MRU_filenames[i] ) );