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"
31 typedef std::pair<Accelerator, bool> ShortcutValue; // accelerator, isRegistered
32 typedef std::map<CopiedString, ShortcutValue> Shortcuts;
34 void Shortcuts_foreach(Shortcuts& shortcuts, CommandVisitor& visitor)
36 for(Shortcuts::iterator i = shortcuts.begin(); i != shortcuts.end(); ++i)
38 visitor.visit((*i).first.c_str(), (*i).second.first);
42 Shortcuts g_shortcuts;
44 const Accelerator& GlobalShortcuts_insert(const char* name, const Accelerator& accelerator)
46 return (*g_shortcuts.insert(Shortcuts::value_type(name, ShortcutValue(accelerator, false))).first).second.first;
49 void GlobalShortcuts_foreach(CommandVisitor& visitor)
51 Shortcuts_foreach(g_shortcuts, visitor);
54 void GlobalShortcuts_register(const char* name)
56 Shortcuts::iterator i = g_shortcuts.find(name);
57 if(i != g_shortcuts.end())
59 (*i).second.second = true;
63 void GlobalShortcuts_reportUnregistered()
65 for(Shortcuts::iterator i = g_shortcuts.begin(); i != g_shortcuts.end(); ++i)
67 if((*i).second.first.key != 0 && !(*i).second.second)
69 globalOutputStream() << "shortcut not registered: " << (*i).first.c_str() << "\n";
74 typedef std::map<CopiedString, Command> Commands;
78 void GlobalCommands_insert(const char* name, const Callback& callback, const Accelerator& accelerator)
80 bool added = g_commands.insert(Commands::value_type(name, Command(callback, GlobalShortcuts_insert(name, accelerator)))).second;
81 ASSERT_MESSAGE(added, "command already registered: " << makeQuoted(name));
84 const Command& GlobalCommands_find(const char* command)
86 Commands::iterator i = g_commands.find(command);
87 ASSERT_MESSAGE(i != g_commands.end(), "failed to lookup command " << makeQuoted(command));
91 typedef std::map<CopiedString, Toggle> Toggles;
96 void GlobalToggles_insert(const char* name, const Callback& callback, const BoolExportCallback& exportCallback, const Accelerator& accelerator)
98 bool added = g_toggles.insert(Toggles::value_type(name, Toggle(callback, GlobalShortcuts_insert(name, accelerator), exportCallback))).second;
99 ASSERT_MESSAGE(added, "toggle already registered: " << makeQuoted(name));
101 const Toggle& GlobalToggles_find(const char* name)
103 Toggles::iterator i = g_toggles.find(name);
104 ASSERT_MESSAGE(i != g_toggles.end(), "failed to lookup toggle " << makeQuoted(name));
108 typedef std::map<CopiedString, KeyEvent> KeyEvents;
111 KeyEvents g_keyEvents;
113 void GlobalKeyEvents_insert(const char* name, const Accelerator& accelerator, const Callback& keyDown, const Callback& keyUp)
115 bool added = g_keyEvents.insert(KeyEvents::value_type(name, KeyEvent(GlobalShortcuts_insert(name, accelerator), keyDown, keyUp))).second;
116 ASSERT_MESSAGE(added, "command already registered: " << makeQuoted(name));
118 const KeyEvent& GlobalKeyEvents_find(const char* name)
120 KeyEvents::iterator i = g_keyEvents.find(name);
121 ASSERT_MESSAGE(i != g_keyEvents.end(), "failed to lookup keyEvent " << makeQuoted(name));
128 #include <gdk/gdkkeysyms.h>
132 #define __toascii(c) ((c) & 0x7f)
135 inline char ascii_for_keyval(int keyval)
137 return __toascii(keyval);
144 const char* m_strName;
145 unsigned int m_nVKKey;
150 {"Space", GDK_space},
151 {"Backspace", GDK_BackSpace},
152 {"Escape", GDK_Escape},
154 {"Insert", GDK_Insert},
155 {"Delete", GDK_Delete},
156 {"PageUp", GDK_Prior},
157 {"PageDown", GDK_Next},
161 {"Right", GDK_Right},
175 {"Return", GDK_Return},
176 {"Comma", GDK_comma},
177 {"Period", GDK_period},
178 {"Plus", GDK_KP_Add},
179 {"Multiply", GDK_multiply},
180 {"Minus", GDK_KP_Subtract},
181 {"NumPad0", GDK_KP_0},
182 {"NumPad1", GDK_KP_1},
183 {"NumPad2", GDK_KP_2},
184 {"NumPad3", GDK_KP_3},
185 {"NumPad4", GDK_KP_4},
186 {"NumPad5", GDK_KP_5},
187 {"NumPad6", GDK_KP_6},
188 {"NumPad7", GDK_KP_7},
189 {"NumPad8", GDK_KP_8},
190 {"NumPad9", GDK_KP_9},
197 int g_nKeyCount = sizeof(g_Keys) / sizeof(SKeyInfo);
199 const char* global_keys_find(unsigned int key)
201 for(int i = 0; i < g_nKeyCount; ++i)
203 if(g_Keys[i].m_nVKKey == key)
205 return g_Keys[i].m_strName;
211 unsigned int global_keys_find(const char* name)
213 for(int i = 0; i < g_nKeyCount; ++i)
215 if(string_equal_nocase(g_Keys[i].m_strName, name))
217 return g_Keys[i].m_nVKKey;
224 #include <gtk/gtkbox.h>
225 #include <gtk/gtkliststore.h>
226 #include <gtk/gtktreemodel.h>
227 #include <gtk/gtktreeview.h>
228 #include <gtk/gtkcellrenderertext.h>
230 #include "gtkutil/dialog.h"
231 #include "mainframe.h"
233 #include "stream/textfilestream.h"
234 #include "stream/stringstream.h"
237 struct command_list_dialog_t : public ModalDialog
239 command_list_dialog_t()
240 : m_close_button(*this, eIDCANCEL)
243 ModalDialogButton m_close_button;
246 template<typename TextOutputStreamType>
247 TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const Accelerator& accelerator)
249 if(accelerator.modifiers & GDK_SHIFT_MASK)
251 ostream << "Shift + ";
253 if(accelerator.modifiers & GDK_MOD1_MASK)
257 if(accelerator.modifiers & GDK_CONTROL_MASK)
259 ostream << "Control + ";
262 const char* keyName = global_keys_find(accelerator.key);
263 if(!string_empty(keyName))
269 ostream << static_cast<char>(accelerator.key);
275 void DoCommandListDlg()
277 command_list_dialog_t dialog;
279 GtkWindow* window = create_modal_dialog_window(MainFrame_getWindow(), "Mapped Commands", dialog, -1, 400);
281 GtkAccelGroup* accel = gtk_accel_group_new();
282 gtk_window_add_accel_group(window, accel);
284 GtkHBox* hbox = create_dialog_hbox(4, 4);
285 gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(hbox));
288 GtkScrolledWindow* scr = create_scrolled_window(GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
289 gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(scr), TRUE, TRUE, 0);
292 GtkListStore* store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_STRING);
294 GtkWidget* view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
297 GtkCellRenderer* renderer = gtk_cell_renderer_text_new();
298 GtkTreeViewColumn* column = gtk_tree_view_column_new_with_attributes("Command", renderer, "text", 0, 0);
299 gtk_tree_view_append_column(GTK_TREE_VIEW(view), column);
303 GtkCellRenderer* renderer = gtk_cell_renderer_text_new();
304 GtkTreeViewColumn* column = gtk_tree_view_column_new_with_attributes("Key", renderer, "text", 1, 0);
305 gtk_tree_view_append_column(GTK_TREE_VIEW(view), column);
308 gtk_widget_show(view);
309 gtk_container_add(GTK_CONTAINER (scr), view);
313 StringOutputStream path(256);
314 path << SettingsPath_get() << "commandlist.txt";
315 globalOutputStream() << "Writing the command list to " << path.c_str() << "\n";
316 class BuildCommandList : public CommandVisitor
318 TextFileOutputStream m_commandList;
319 GtkListStore* m_store;
321 BuildCommandList(const char* filename, GtkListStore* store) : m_commandList(filename), m_store(store)
324 void visit(const char* name, Accelerator& accelerator)
326 StringOutputStream modifiers;
327 modifiers << accelerator;
331 gtk_list_store_append(m_store, &iter);
332 gtk_list_store_set(m_store, &iter, 0, name, 1, modifiers.c_str(), -1);
335 if(!m_commandList.failed())
337 m_commandList << makeLeftJustified(name, 25) << " " << modifiers.c_str() << '\n';
340 } visitor(path.c_str(), store);
342 GlobalShortcuts_foreach(visitor);
345 g_object_unref(G_OBJECT(store));
349 GtkVBox* vbox = create_dialog_vbox(4);
350 gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(vbox), FALSE, FALSE, 0);
352 GtkButton* button = create_modal_dialog_button("Close", dialog.m_close_button);
353 gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(button), FALSE, FALSE, 0);
354 widget_make_default(GTK_WIDGET(button));
355 gtk_widget_grab_focus(GTK_WIDGET(button));
356 gtk_widget_add_accelerator(GTK_WIDGET(button), "clicked", accel, GDK_Return, (GdkModifierType)0, (GtkAccelFlags)0);
357 gtk_widget_add_accelerator(GTK_WIDGET(button), "clicked", accel, GDK_Escape, (GdkModifierType)0, (GtkAccelFlags)0);
360 modal_dialog_show(window, dialog);
361 gtk_widget_destroy(GTK_WIDGET(window));
364 #include "profile/profile.h"
366 const char* const COMMANDS_VERSION = "1.0";
368 void SaveCommandMap(const char* path)
370 StringOutputStream strINI(256);
371 strINI << path << "shortcuts.ini";
373 TextFileOutputStream file(strINI.c_str());
376 file << "[Version]\n";
377 file << "number=" << COMMANDS_VERSION << "\n";
379 file << "[Commands]\n";
380 class WriteCommandMap : public CommandVisitor
382 TextFileOutputStream& m_file;
384 WriteCommandMap(TextFileOutputStream& file) : m_file(file)
387 void visit(const char* name, Accelerator& accelerator)
389 m_file << name << "=";
391 const char* key = global_keys_find(accelerator.key);
392 if(!string_empty(key))
396 else if(accelerator.key != 0)
398 m_file << ascii_for_keyval(accelerator.key);
401 if(accelerator.modifiers & GDK_MOD1_MASK)
405 if(accelerator.modifiers & GDK_CONTROL_MASK)
409 if(accelerator.modifiers & GDK_SHIFT_MASK)
417 GlobalShortcuts_foreach(visitor);
421 const char* stringrange_find(const char* first, const char* last, char c)
423 const char* p = strchr(first, '+');
431 class ReadCommandMap : public CommandVisitor
433 const char* m_filename;
436 ReadCommandMap(const char* filename) : m_filename(filename), m_count(0)
439 void visit(const char* name, Accelerator& accelerator)
442 if (read_var(m_filename, "Commands", name, value ))
444 if(string_empty(value))
447 accelerator.modifiers = (GdkModifierType)0;
451 const char* last = value + string_length(value);
452 const char* keyEnd = stringrange_find(value, last, '+');
453 for(const char* modifier = keyEnd; modifier != last;)
455 const char* next = stringrange_find(modifier + 1, last, '+');
456 if(next - modifier == 4
457 && string_equal_nocase_n(modifier, "+alt", 4))
459 modifiers |= GDK_MOD1_MASK;
461 else if(next - modifier == 5
462 && string_equal_nocase_n(modifier, "+ctrl", 5) != 0)
464 modifiers |= GDK_CONTROL_MASK;
466 else if(next - modifier == 6
467 && string_equal_nocase_n(modifier, "+shift", 6) != 0)
469 modifiers |= GDK_SHIFT_MASK;
473 globalOutputStream() << "WARNING: failed to parse user command " << makeQuoted(value) << ": unknown modifier " << makeQuoted(StringRange(modifier, next)) << "\n";
477 accelerator.modifiers = (GdkModifierType)modifiers;
480 // strBuff has been cleaned of it's modifiers .. switch between a regular key and a virtual one
482 if(keyEnd - value == 1) // most often case.. deal with first
484 accelerator.key = std::toupper(value[0]);
489 CopiedString keyName(value, keyEnd);
490 accelerator.key = global_keys_find(keyName.c_str());
491 if(accelerator.key != 0)
497 globalOutputStream() << "WARNING: failed to parse user command " << makeQuoted(value) << ": unknown key " << makeQuoted(keyName.c_str()) << "\n";
502 std::size_t count() const
508 void LoadCommandMap(const char* path)
510 StringOutputStream strINI(256);
511 strINI << path << "shortcuts.ini";
513 FILE* f = fopen (strINI.c_str(), "r");
517 globalOutputStream() << "loading custom shortcuts list from " << makeQuoted(strINI.c_str()) << "\n";
519 Version version = version_parse(COMMANDS_VERSION);
520 Version dataVersion = { 0, 0 };
524 if(read_var(strINI.c_str(), "Version", "number", value))
526 dataVersion = version_parse(value);
530 if(version_compatible(version, dataVersion))
532 globalOutputStream() << "commands import: data version " << dataVersion << " is compatible with code version " << version << "\n";
533 ReadCommandMap visitor(strINI.c_str());
534 GlobalShortcuts_foreach(visitor);
535 globalOutputStream() << "parsed " << Unsigned(visitor.count()) << " custom shortcuts\n";
539 globalOutputStream() << "commands import: data version " << dataVersion << " is not compatible with code version " << version << "\n";
544 globalOutputStream() << "failed to load custom shortcuts from " << makeQuoted(strINI.c_str()) << "\n";