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)
49 return MRU_filenames[index].c_str();
54 StringBuffer m_buffer;
56 EscapedMnemonic(std::size_t capacity) : m_buffer(capacity)
58 m_buffer.push_back('_');
60 const char* c_str() const
62 return m_buffer.c_str();
64 void push_back(char c) // not escaped
66 m_buffer.push_back(c);
68 std::size_t write(const char* buffer, std::size_t length)
70 for(const char* end = buffer + length; buffer != end; ++buffer)
74 m_buffer.push_back('_');
77 m_buffer.push_back(*buffer);
84 inline EscapedMnemonic& operator<<(EscapedMnemonic& ostream, const T& t)
86 return ostream_write(ostream, t);
90 void MRU_updateWidget(std::size_t index, const char *filename)
92 EscapedMnemonic mnemonic(64);
93 mnemonic << Unsigned(index + 1) << "- " << ConvertLocaleToUTF8(filename);
94 gtk_label_set_text_with_mnemonic(GTK_LABEL(gtk_bin_get_child(GTK_BIN(MRU_items[index]))), mnemonic.c_str());
97 void MRU_SetText(std::size_t index, const char *filename)
99 MRU_filenames[index] = filename;
100 MRU_updateWidget(index, filename);
103 void MRU_AddFile (const char *str)
108 // check if file is already in our list
109 for (i = 0; i < MRU_used; i++)
111 text = MRU_GetText (i);
113 if (strcmp (text, str) == 0)
117 MRU_SetText(i, MRU_GetText (i-1));
125 if (MRU_used < MRU_MAX)
129 for (i = MRU_used-1; i > 0; i--)
130 MRU_SetText (i, MRU_GetText (i-1));
132 MRU_SetText (0, str);
133 gtk_widget_set_sensitive(GTK_WIDGET(MRU_items[0]), TRUE);
134 gtk_widget_show(GTK_WIDGET(MRU_items[MRU_used-1]));
139 if(MRU_used > MRU_MAX)
143 void MRU_AddWidget(GtkMenuItem *widget, std::size_t pos)
147 MRU_items[pos] = widget;
150 MRU_updateWidget(pos, MRU_GetText(pos));
151 gtk_widget_set_sensitive(GTK_WIDGET(MRU_items[0]), TRUE);
152 gtk_widget_show(GTK_WIDGET(MRU_items[pos]));
157 void MRU_Activate (std::size_t index)
160 strcpy(text, MRU_GetText(index));
162 if (file_readable(text)) //\todo Test 'map load succeeds' instead of 'file is readable'.
173 for (std::size_t i = index; i < MRU_used; i++)
174 MRU_SetText (i, MRU_GetText (i+1));
178 gtk_label_set_text(GTK_LABEL(GTK_BIN(MRU_items[0])->child), "Recent Files");
179 gtk_widget_set_sensitive(GTK_WIDGET(MRU_items[0]), FALSE);
183 gtk_widget_hide(GTK_WIDGET(MRU_items[MRU_used]));
191 std::size_t m_number;
193 LoadMRU(std::size_t number)
199 if (ConfirmModified("Open Map"))
201 MRU_Activate(m_number - 1);
206 typedef MemberCaller<LoadMRU, &LoadMRU::load> LoadMRUCaller;
208 LoadMRU g_load_mru1(1);
209 LoadMRU g_load_mru2(2);
210 LoadMRU g_load_mru3(3);
211 LoadMRU g_load_mru4(4);
213 void MRU_constructMenu(GtkMenu* menu)
216 GtkMenuItem* item = create_menu_item_with_mnemonic(menu, "_1", LoadMRUCaller(g_load_mru1));
217 gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
218 MRU_AddWidget(item, 0);
221 GtkMenuItem* item = create_menu_item_with_mnemonic(menu, "_2", LoadMRUCaller(g_load_mru2));
222 gtk_widget_hide(GTK_WIDGET(item));
223 MRU_AddWidget(item, 1);
226 GtkMenuItem* item = create_menu_item_with_mnemonic(menu, "_3", LoadMRUCaller(g_load_mru3));
227 gtk_widget_hide(GTK_WIDGET(item));
228 MRU_AddWidget(item, 2);
231 GtkMenuItem* item = create_menu_item_with_mnemonic(menu, "_4", LoadMRUCaller(g_load_mru4));
232 gtk_widget_hide(GTK_WIDGET(item));
233 MRU_AddWidget(item, 3);
237 #include "preferencesystem.h"
238 #include "stringio.h"
242 GlobalPreferenceSystem().registerPreference("Count", SizeImportStringCaller(MRU_used), SizeExportStringCaller(MRU_used));
244 for(std::size_t i = 0; i != MRU_MAX; ++i)
246 GlobalPreferenceSystem().registerPreference(MRU_keys[i], CopiedStringImportStringCaller(MRU_filenames[i]), CopiedStringExportStringCaller(MRU_filenames[i]));