]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/mru.cpp
Re-align patch texture dialog
[xonotic/netradiant.git] / radiant / mru.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 "mru.h"
23
24 #include <string.h>
25 #include <stdio.h>
26 #include <gtk/gtklabel.h>
27
28 #include "os/file.h"
29 #include "generic/callback.h"
30 #include "stream/stringstream.h"
31 #include "convert.h"
32
33 #include "gtkutil/menu.h"
34 #include "map.h"
35 #include "qe3.h"
36
37 #define MRU_MAX 4
38 namespace {
39 GtkMenuItem *MRU_items[MRU_MAX];
40 std::size_t MRU_used;
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" };
45 }
46
47 inline const char* MRU_GetText( std::size_t index ){
48         return MRU_filenames[index].c_str();
49 }
50
51 class EscapedMnemonic
52 {
53 StringBuffer m_buffer;
54 public:
55 EscapedMnemonic( std::size_t capacity ) : m_buffer( capacity ){
56         m_buffer.push_back( '_' );
57 }
58 const char* c_str() const {
59         return m_buffer.c_str();
60 }
61 void push_back( char c ){ // not escaped
62         m_buffer.push_back( c );
63 }
64 std::size_t write( const char* buffer, std::size_t length ){
65         for ( const char* end = buffer + length; buffer != end; ++buffer )
66         {
67                 if ( *buffer == '_' ) {
68                         m_buffer.push_back( '_' );
69                 }
70
71                 m_buffer.push_back( *buffer );
72         }
73         return length;
74 }
75 };
76
77 template<typename T>
78 inline EscapedMnemonic& operator<<( EscapedMnemonic& ostream, const T& t ){
79         return ostream_write( ostream, t );
80 }
81
82
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() );
87 }
88
89 void MRU_SetText( std::size_t index, const char *filename ){
90         MRU_filenames[index] = filename;
91         MRU_updateWidget( index, filename );
92 }
93
94 void MRU_AddFile( const char *str ){
95         std::size_t i;
96         const char* text;
97
98         // check if file is already in our list
99         for ( i = 0; i < MRU_used; i++ )
100         {
101                 text = MRU_GetText( i );
102
103                 if ( strcmp( text, str ) == 0 ) {
104                         // reorder menu
105                         for (; i > 0; i-- )
106                                 MRU_SetText( i, MRU_GetText( i - 1 ) );
107
108                         MRU_SetText( 0, str );
109
110                         return;
111                 }
112         }
113
114         if ( MRU_used < MRU_MAX ) {
115                 MRU_used++;
116         }
117
118         // move items down
119         for ( i = MRU_used - 1; i > 0; i-- )
120                 MRU_SetText( i, MRU_GetText( i - 1 ) );
121
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] ) );
125 }
126
127 void MRU_Init(){
128         if ( MRU_used > MRU_MAX ) {
129                 MRU_used = MRU_MAX;
130         }
131 }
132
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] ) );
140                 }
141         }
142 }
143
144 void MRU_Activate( std::size_t index ){
145         char text[1024];
146         strcpy( text, MRU_GetText( index ) );
147
148         if ( file_readable( text ) ) { //\todo Test 'map load succeeds' instead of 'file is readable'.
149                 MRU_AddFile( text );
150                 Map_RegionOff();
151                 Map_Free();
152                 Map_LoadFile( text );
153         }
154         else
155         {
156                 MRU_used--;
157
158                 for ( std::size_t i = index; i < MRU_used; i++ )
159                         MRU_SetText( i, MRU_GetText( i + 1 ) );
160
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 );
164                 }
165                 else
166                 {
167                         gtk_widget_hide( GTK_WIDGET( MRU_items[MRU_used] ) );
168                 }
169         }
170 }
171
172
173 class LoadMRU
174 {
175 std::size_t m_number;
176 public:
177 LoadMRU( std::size_t number )
178         : m_number( number ){
179 }
180 void load(){
181         if ( ConfirmModified( "Open Map" ) ) {
182                 MRU_Activate( m_number - 1 );
183         }
184 }
185 };
186
187 typedef MemberCaller<LoadMRU, &LoadMRU::load> LoadMRUCaller;
188
189 LoadMRU g_load_mru1( 1 );
190 LoadMRU g_load_mru2( 2 );
191 LoadMRU g_load_mru3( 3 );
192 LoadMRU g_load_mru4( 4 );
193
194 void MRU_constructMenu( GtkMenu* menu ){
195         {
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 );
199         }
200         {
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 );
204         }
205         {
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 );
209         }
210         {
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 );
214         }
215 }
216
217 #include "preferencesystem.h"
218 #include "stringio.h"
219
220 void MRU_Construct(){
221         GlobalPreferenceSystem().registerPreference( "Count", SizeImportStringCaller( MRU_used ), SizeExportStringCaller( MRU_used ) );
222
223         for ( std::size_t i = 0; i != MRU_MAX; ++i )
224         {
225                 GlobalPreferenceSystem().registerPreference( MRU_keys[i], CopiedStringImportStringCaller( MRU_filenames[i] ), CopiedStringExportStringCaller( MRU_filenames[i] ) );
226         }
227
228         MRU_Init();
229 }
230 void MRU_Destroy(){
231 }