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
29 #include "generic/callback.h"
32 #include "gtkutil/menu.h"
36 const int MRU_MAX = 4;
38 GtkMenuItem *MRU_items[MRU_MAX];
40 typedef CopiedString MRU_filename_t;
41 MRU_filename_t MRU_filenames[MRU_MAX];
42 typedef const char* MRU_key_t;
43 MRU_key_t MRU_keys[MRU_MAX] = { "File0", "File1", "File2", "File3" };
46 inline const char* MRU_GetText( std::size_t index ){
47 return MRU_filenames[index].c_str();
56 EscapedMnemonic() : m_buffer(){
57 m_buffer.push_back( '_' );
59 const char* c_str() const {
60 return m_buffer.c_str();
62 void push_back( char c ){ // not escaped
63 m_buffer.push_back( c );
65 std::size_t write( const char* buffer, std::size_t length ){
66 for ( const char* end = buffer + length; buffer != end; ++buffer )
68 if ( *buffer == '_' ) {
69 m_buffer.push_back( '_' );
72 m_buffer.push_back( *buffer );
79 inline EscapedMnemonic& operator<<( EscapedMnemonic& ostream, const T& t ){
80 return ostream_write( ostream, t );
84 void MRU_updateWidget( std::size_t index, const char *filename ){
85 EscapedMnemonic mnemonic;
86 mnemonic << Unsigned( index + 1 ) << "- " << filename;
87 gtk_label_set_text_with_mnemonic( GTK_LABEL( gtk_bin_get_child( GTK_BIN( MRU_items[index] ) ) ), mnemonic.c_str() );
90 void MRU_SetText( std::size_t index, const char *filename ){
91 MRU_filenames[index] = filename;
92 MRU_updateWidget( index, filename );
95 void MRU_AddFile( const char *str ){
99 // check if file is already in our list
100 for ( i = 0; i < MRU_used; i++ )
102 text = MRU_GetText( i );
104 if ( strcmp( text, str ) == 0 ) {
107 MRU_SetText( i, MRU_GetText( i - 1 ) );
109 MRU_SetText( 0, str );
115 if ( MRU_used < MRU_MAX ) {
120 for ( i = MRU_used - 1; i > 0; i-- )
121 MRU_SetText( i, MRU_GetText( i - 1 ) );
123 MRU_SetText( 0, str );
124 gtk_widget_set_sensitive( ui::MenuItem::from(MRU_items[0]) , TRUE );
125 ui::MenuItem::from(MRU_items[MRU_used - 1] ).show();
129 if ( MRU_used > MRU_MAX ) {
134 void MRU_AddWidget( ui::MenuItem widget, std::size_t pos ){
135 if ( pos < MRU_MAX ) {
136 MRU_items[pos] = widget;
137 if ( pos < MRU_used ) {
138 MRU_updateWidget( pos, MRU_GetText( pos ) );
139 gtk_widget_set_sensitive( ui::MenuItem::from(MRU_items[0]) , TRUE );
140 ui::MenuItem::from(MRU_items[pos]).show();
145 void MRU_Activate( std::size_t index ){
147 strcpy( text, MRU_GetText( index ) );
149 if ( file_readable( text ) ) { //\todo Test 'map load succeeds' instead of 'file is readable'.
153 Map_LoadFile( text );
159 for ( std::size_t i = index; i < MRU_used; i++ )
160 MRU_SetText( i, MRU_GetText( i + 1 ) );
162 if ( MRU_used == 0 ) {
163 auto label = ui::Label::from(gtk_bin_get_child(GTK_BIN(MRU_items[0] )) );
164 label.text("Recent Files");
165 gtk_widget_set_sensitive( ui::MenuItem::from(MRU_items[0]), FALSE );
169 ui::MenuItem::from(MRU_items[MRU_used]).hide();
178 std::size_t m_number;
181 LoadMRU( std::size_t number )
182 : m_number( number ){
185 if ( ConfirmModified( "Open Map" ) ) {
186 MRU_Activate( m_number - 1 );
191 typedef MemberCaller<LoadMRU, void(), &LoadMRU::load> LoadMRUCaller;
193 LoadMRU g_load_mru1( 1 );
194 LoadMRU g_load_mru2( 2 );
195 LoadMRU g_load_mru3( 3 );
196 LoadMRU g_load_mru4( 4 );
198 void MRU_constructMenu( ui::Menu menu ){
200 auto item = create_menu_item_with_mnemonic( menu, "_1", LoadMRUCaller( g_load_mru1 ) );
201 gtk_widget_set_sensitive( item , FALSE );
202 MRU_AddWidget( item, 0 );
205 auto item = create_menu_item_with_mnemonic( menu, "_2", LoadMRUCaller( g_load_mru2 ) );
207 MRU_AddWidget( item, 1 );
210 auto item = create_menu_item_with_mnemonic( menu, "_3", LoadMRUCaller( g_load_mru3 ) );
212 MRU_AddWidget( item, 2 );
215 auto item = create_menu_item_with_mnemonic( menu, "_4", LoadMRUCaller( g_load_mru4 ) );
217 MRU_AddWidget( item, 3 );
221 #include "preferencesystem.h"
222 #include "stringio.h"
224 void MRU_Construct(){
225 GlobalPreferenceSystem().registerPreference( "Count", make_property_string( MRU_used ) );
227 for ( std::size_t i = 0; i != MRU_MAX; ++i )
229 GlobalPreferenceSystem().registerPreference( MRU_keys[i], make_property_string( MRU_filenames[i] ) );