2 Copyright (C) 2001-2006, William Joseph.
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
24 #include "debugging/debugging.h"
28 #include "string/string.h"
29 #include "versionlib.h"
30 #include "gtkutil/accelerator.h"
32 typedef std::pair<Accelerator, bool> ShortcutValue; // accelerator, isRegistered
33 typedef std::map<CopiedString, ShortcutValue> Shortcuts;
35 void Shortcuts_foreach(Shortcuts& shortcuts, CommandVisitor& visitor)
37 for(Shortcuts::iterator i = shortcuts.begin(); i != shortcuts.end(); ++i)
39 visitor.visit((*i).first.c_str(), (*i).second.first);
43 Shortcuts g_shortcuts;
45 const Accelerator& GlobalShortcuts_insert(const char* name, const Accelerator& accelerator)
47 return (*g_shortcuts.insert(Shortcuts::value_type(name, ShortcutValue(accelerator, false))).first).second.first;
50 void GlobalShortcuts_foreach(CommandVisitor& visitor)
52 Shortcuts_foreach(g_shortcuts, visitor);
55 void GlobalShortcuts_register(const char* name)
57 Shortcuts::iterator i = g_shortcuts.find(name);
58 if(i != g_shortcuts.end())
60 (*i).second.second = true;
64 void GlobalShortcuts_reportUnregistered()
66 for(Shortcuts::iterator i = g_shortcuts.begin(); i != g_shortcuts.end(); ++i)
68 if((*i).second.first.key != 0 && !(*i).second.second)
70 globalOutputStream() << "shortcut not registered: " << (*i).first.c_str() << "\n";
75 typedef std::map<CopiedString, Command> Commands;
79 void GlobalCommands_insert(const char* name, const Callback& callback, const Accelerator& accelerator)
81 bool added = g_commands.insert(Commands::value_type(name, Command(callback, GlobalShortcuts_insert(name, accelerator)))).second;
82 ASSERT_MESSAGE(added, "command already registered: " << makeQuoted(name));
85 const Command& GlobalCommands_find(const char* command)
87 Commands::iterator i = g_commands.find(command);
88 ASSERT_MESSAGE(i != g_commands.end(), "failed to lookup command " << makeQuoted(command));
92 typedef std::map<CopiedString, Toggle> Toggles;
97 void GlobalToggles_insert(const char* name, const Callback& callback, const BoolExportCallback& exportCallback, const Accelerator& accelerator)
99 bool added = g_toggles.insert(Toggles::value_type(name, Toggle(callback, GlobalShortcuts_insert(name, accelerator), exportCallback))).second;
100 ASSERT_MESSAGE(added, "toggle already registered: " << makeQuoted(name));
102 const Toggle& GlobalToggles_find(const char* name)
104 Toggles::iterator i = g_toggles.find(name);
105 ASSERT_MESSAGE(i != g_toggles.end(), "failed to lookup toggle " << makeQuoted(name));
109 typedef std::map<CopiedString, KeyEvent> KeyEvents;
112 KeyEvents g_keyEvents;
114 void GlobalKeyEvents_insert(const char* name, const Accelerator& accelerator, const Callback& keyDown, const Callback& keyUp)
116 bool added = g_keyEvents.insert(KeyEvents::value_type(name, KeyEvent(GlobalShortcuts_insert(name, accelerator), keyDown, keyUp))).second;
117 ASSERT_MESSAGE(added, "command already registered: " << makeQuoted(name));
119 const KeyEvent& GlobalKeyEvents_find(const char* name)
121 KeyEvents::iterator i = g_keyEvents.find(name);
122 ASSERT_MESSAGE(i != g_keyEvents.end(), "failed to lookup keyEvent " << makeQuoted(name));
132 #define __toascii(c) ((c) & 0x7f)
135 inline char ascii_for_keyval(int keyval)
137 return __toascii(keyval);
142 #include <gtk/gtkbox.h>
143 #include <gtk/gtkliststore.h>
144 #include <gtk/gtktreemodel.h>
145 #include <gtk/gtktreeview.h>
146 #include <gtk/gtkcellrenderertext.h>
148 #include "gtkutil/dialog.h"
149 #include "mainframe.h"
151 #include "stream/textfilestream.h"
152 #include "stream/stringstream.h"
155 struct command_list_dialog_t : public ModalDialog
157 command_list_dialog_t()
158 : m_close_button(*this, eIDCANCEL)
161 ModalDialogButton m_close_button;
164 void DoCommandListDlg()
166 command_list_dialog_t dialog;
168 GtkWindow* window = create_modal_dialog_window(MainFrame_getWindow(), "Mapped Commands", dialog, -1, 400);
170 GtkAccelGroup* accel = gtk_accel_group_new();
171 gtk_window_add_accel_group(window, accel);
173 GtkHBox* hbox = create_dialog_hbox(4, 4);
174 gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(hbox));
177 GtkScrolledWindow* scr = create_scrolled_window(GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
178 gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(scr), TRUE, TRUE, 0);
181 GtkListStore* store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_STRING);
183 GtkWidget* view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
186 GtkCellRenderer* renderer = gtk_cell_renderer_text_new();
187 GtkTreeViewColumn* column = gtk_tree_view_column_new_with_attributes("Command", renderer, "text", 0, 0);
188 gtk_tree_view_append_column(GTK_TREE_VIEW(view), column);
192 GtkCellRenderer* renderer = gtk_cell_renderer_text_new();
193 GtkTreeViewColumn* column = gtk_tree_view_column_new_with_attributes("Key", renderer, "text", 1, 0);
194 gtk_tree_view_append_column(GTK_TREE_VIEW(view), column);
197 gtk_widget_show(view);
198 gtk_container_add(GTK_CONTAINER (scr), view);
202 StringOutputStream path(256);
203 path << SettingsPath_get() << "commandlist.txt";
204 globalOutputStream() << "Writing the command list to " << path.c_str() << "\n";
205 class BuildCommandList : public CommandVisitor
207 TextFileOutputStream m_commandList;
208 GtkListStore* m_store;
210 BuildCommandList(const char* filename, GtkListStore* store) : m_commandList(filename), m_store(store)
213 void visit(const char* name, Accelerator& accelerator)
215 StringOutputStream modifiers;
216 modifiers << accelerator;
220 gtk_list_store_append(m_store, &iter);
221 gtk_list_store_set(m_store, &iter, 0, name, 1, modifiers.c_str(), -1);
224 if(!m_commandList.failed())
226 m_commandList << makeLeftJustified(name, 25) << " " << modifiers.c_str() << '\n';
229 } visitor(path.c_str(), store);
231 GlobalShortcuts_foreach(visitor);
234 g_object_unref(G_OBJECT(store));
238 GtkVBox* vbox = create_dialog_vbox(4);
239 gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(vbox), FALSE, FALSE, 0);
241 GtkButton* button = create_modal_dialog_button("Close", dialog.m_close_button);
242 gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(button), FALSE, FALSE, 0);
243 widget_make_default(GTK_WIDGET(button));
244 gtk_widget_grab_focus(GTK_WIDGET(button));
245 gtk_widget_add_accelerator(GTK_WIDGET(button), "clicked", accel, GDK_Return, (GdkModifierType)0, (GtkAccelFlags)0);
246 gtk_widget_add_accelerator(GTK_WIDGET(button), "clicked", accel, GDK_Escape, (GdkModifierType)0, (GtkAccelFlags)0);
249 modal_dialog_show(window, dialog);
250 gtk_widget_destroy(GTK_WIDGET(window));
253 #include "profile/profile.h"
255 const char* const COMMANDS_VERSION = "1.0";
257 void SaveCommandMap(const char* path)
259 StringOutputStream strINI(256);
260 strINI << path << "shortcuts.ini";
262 TextFileOutputStream file(strINI.c_str());
265 file << "[Version]\n";
266 file << "number=" << COMMANDS_VERSION << "\n";
268 file << "[Commands]\n";
269 class WriteCommandMap : public CommandVisitor
271 TextFileOutputStream& m_file;
273 WriteCommandMap(TextFileOutputStream& file) : m_file(file)
276 void visit(const char* name, Accelerator& accelerator)
278 m_file << name << "=";
280 const char* key = global_keys_find(accelerator.key);
281 if(!string_empty(key))
285 else if(accelerator.key != 0)
287 m_file << ascii_for_keyval(accelerator.key);
290 if(accelerator.modifiers & GDK_MOD1_MASK)
294 if(accelerator.modifiers & GDK_CONTROL_MASK)
298 if(accelerator.modifiers & GDK_SHIFT_MASK)
306 GlobalShortcuts_foreach(visitor);
310 const char* stringrange_find(const char* first, const char* last, char c)
312 const char* p = strchr(first, '+');
320 class ReadCommandMap : public CommandVisitor
322 const char* m_filename;
325 ReadCommandMap(const char* filename) : m_filename(filename), m_count(0)
328 void visit(const char* name, Accelerator& accelerator)
331 if (read_var(m_filename, "Commands", name, value ))
333 if(string_empty(value))
336 accelerator.modifiers = (GdkModifierType)0;
340 const char* last = value + string_length(value);
341 const char* keyEnd = stringrange_find(value, last, '+');
342 for(const char* modifier = keyEnd; modifier != last;)
344 const char* next = stringrange_find(modifier + 1, last, '+');
345 if(next - modifier == 4
346 && string_equal_nocase_n(modifier, "+alt", 4))
348 modifiers |= GDK_MOD1_MASK;
350 else if(next - modifier == 5
351 && string_equal_nocase_n(modifier, "+ctrl", 5) != 0)
353 modifiers |= GDK_CONTROL_MASK;
355 else if(next - modifier == 6
356 && string_equal_nocase_n(modifier, "+shift", 6) != 0)
358 modifiers |= GDK_SHIFT_MASK;
362 globalOutputStream() << "WARNING: failed to parse user command " << makeQuoted(value) << ": unknown modifier " << makeQuoted(StringRange(modifier, next)) << "\n";
366 accelerator.modifiers = (GdkModifierType)modifiers;
369 // strBuff has been cleaned of it's modifiers .. switch between a regular key and a virtual one
371 if(keyEnd - value == 1) // most often case.. deal with first
373 accelerator.key = std::toupper(value[0]);
378 CopiedString keyName(StringRange(value, keyEnd));
379 accelerator.key = global_keys_find(keyName.c_str());
380 if(accelerator.key != 0)
386 globalOutputStream() << "WARNING: failed to parse user command " << makeQuoted(value) << ": unknown key " << makeQuoted(keyName.c_str()) << "\n";
391 std::size_t count() const
397 void LoadCommandMap(const char* path)
399 StringOutputStream strINI(256);
400 strINI << path << "shortcuts.ini";
402 FILE* f = fopen (strINI.c_str(), "r");
406 globalOutputStream() << "loading custom shortcuts list from " << makeQuoted(strINI.c_str()) << "\n";
408 Version version = version_parse(COMMANDS_VERSION);
409 Version dataVersion = { 0, 0 };
413 if(read_var(strINI.c_str(), "Version", "number", value))
415 dataVersion = version_parse(value);
419 if(version_compatible(version, dataVersion))
421 globalOutputStream() << "commands import: data version " << dataVersion << " is compatible with code version " << version << "\n";
422 ReadCommandMap visitor(strINI.c_str());
423 GlobalShortcuts_foreach(visitor);
424 globalOutputStream() << "parsed " << Unsigned(visitor.count()) << " custom shortcuts\n";
428 globalOutputStream() << "commands import: data version " << dataVersion << " is not compatible with code version " << version << "\n";
433 globalOutputStream() << "failed to load custom shortcuts from " << makeQuoted(strINI.c_str()) << "\n";