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