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
25 #include "debugging/debugging.h"
29 #include "string/string.h"
30 #include "versionlib.h"
31 #include "gtkutil/messagebox.h"
34 typedef std::pair<Accelerator, int> ShortcutValue; // accelerator, isRegistered
35 typedef std::map<CopiedString, ShortcutValue> Shortcuts;
37 void Shortcuts_foreach(Shortcuts &shortcuts, CommandVisitor &visitor)
39 for (Shortcuts::iterator i = shortcuts.begin(); i != shortcuts.end(); ++i) {
40 visitor.visit((*i).first.c_str(), (*i).second.first);
44 Shortcuts g_shortcuts;
46 const Accelerator &GlobalShortcuts_insert(const char *name, const Accelerator &accelerator)
48 return (*g_shortcuts.insert(Shortcuts::value_type(name, ShortcutValue(accelerator, false))).first).second.first;
51 void GlobalShortcuts_foreach(CommandVisitor &visitor)
53 Shortcuts_foreach(g_shortcuts, visitor);
56 void GlobalShortcuts_register(const char *name, int type)
58 Shortcuts::iterator i = g_shortcuts.find(name);
59 if (i != g_shortcuts.end()) {
60 (*i).second.second = type;
64 void GlobalShortcuts_reportUnregistered()
66 for (Shortcuts::iterator i = g_shortcuts.begin(); i != g_shortcuts.end(); ++i) {
67 if ((*i).second.first.key != 0 && !(*i).second.second) {
68 globalOutputStream() << "shortcut not registered: " << (*i).first.c_str() << "\n";
73 typedef std::map<CopiedString, Command> Commands;
77 void GlobalCommands_insert(const char *name, const Callback<void()> &callback, const Accelerator &accelerator)
79 bool added = g_commands.insert(
80 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<void()> &callback,
97 const Callback<void(const Callback<void(bool)> &)> &exportCallback,
98 const Accelerator &accelerator)
100 bool added = g_toggles.insert(Toggles::value_type(name, Toggle(callback, GlobalShortcuts_insert(name, accelerator),
101 exportCallback))).second;
102 ASSERT_MESSAGE(added, "toggle already registered: " << makeQuoted(name));
105 const Toggle &GlobalToggles_find(const char *name)
107 Toggles::iterator i = g_toggles.find(name);
108 ASSERT_MESSAGE(i != g_toggles.end(), "failed to lookup toggle " << makeQuoted(name));
112 typedef std::map<CopiedString, KeyEvent> KeyEvents;
115 KeyEvents g_keyEvents;
117 void GlobalKeyEvents_insert(const char *name, const Accelerator &accelerator, const Callback<void()> &keyDown,
118 const Callback<void()> &keyUp)
120 bool added = g_keyEvents.insert(
121 KeyEvents::value_type(name, KeyEvent(GlobalShortcuts_insert(name, accelerator), keyDown, keyUp))).second;
122 ASSERT_MESSAGE(added, "command already registered: " << makeQuoted(name));
125 const KeyEvent &GlobalKeyEvents_find(const char *name)
127 KeyEvents::iterator i = g_keyEvents.find(name);
128 ASSERT_MESSAGE(i != g_keyEvents.end(), "failed to lookup keyEvent " << makeQuoted(name));
133 void disconnect_accelerator(const char *name)
135 Shortcuts::iterator i = g_shortcuts.find(name);
136 if (i != g_shortcuts.end()) {
137 switch ((*i).second.second) {
140 command_disconnect_accelerator(name);
144 toggle_remove_accelerator(name);
150 void connect_accelerator(const char *name)
152 Shortcuts::iterator i = g_shortcuts.find(name);
153 if (i != g_shortcuts.end()) {
154 switch ((*i).second.second) {
157 command_connect_accelerator(name);
161 toggle_add_accelerator(name);
168 #include <uilib/uilib.h>
169 #include <gdk/gdkkeysyms.h>
171 #include "gtkutil/dialog.h"
172 #include "mainframe.h"
174 #include "stream/textfilestream.h"
175 #include "stream/stringstream.h"
178 struct command_list_dialog_t : public ModalDialog {
179 command_list_dialog_t()
180 : m_close_button(*this, eIDCANCEL), m_list(ui::null), m_command_iter(), m_model(ui::null),
181 m_waiting_for_key(false)
185 ModalDialogButton m_close_button;
188 GtkTreeIter m_command_iter;
189 ui::TreeModel m_model;
190 bool m_waiting_for_key;
193 void accelerator_clear_button_clicked(ui::Button btn, gpointer dialogptr)
195 command_list_dialog_t &dialog = *(command_list_dialog_t *) dialogptr;
197 if (dialog.m_waiting_for_key) {
198 // just unhighlight, user wanted to cancel
199 dialog.m_waiting_for_key = false;
200 gtk_list_store_set(ui::ListStore::from(dialog.m_model), &dialog.m_command_iter, 2, false, -1);
201 gtk_widget_set_sensitive(dialog.m_list, true);
202 dialog.m_model = ui::TreeModel(ui::null);
206 auto sel = gtk_tree_view_get_selection(dialog.m_list);
209 if (!gtk_tree_selection_get_selected(sel, &model, &iter)) {
214 memset(&val, 0, sizeof(val));
215 gtk_tree_model_get_value(model, &iter, 0, &val);
216 const char *commandName = g_value_get_string(&val);;
218 // clear the ACTUAL accelerator too!
219 disconnect_accelerator(commandName);
221 Shortcuts::iterator thisShortcutIterator = g_shortcuts.find(commandName);
222 if (thisShortcutIterator == g_shortcuts.end()) {
225 thisShortcutIterator->second.first = accelerator_null();
227 gtk_list_store_set(ui::ListStore::from(model), &iter, 1, "", -1);
232 void accelerator_edit_button_clicked(ui::Button btn, gpointer dialogptr)
234 command_list_dialog_t &dialog = *(command_list_dialog_t *) dialogptr;
236 // 1. find selected row
237 auto sel = gtk_tree_view_get_selection(dialog.m_list);
240 if (!gtk_tree_selection_get_selected(sel, &model, &iter)) {
243 dialog.m_command_iter = iter;
244 dialog.m_model = ui::TreeModel::from(model);
246 // 2. disallow changing the row
247 //gtk_widget_set_sensitive(dialog.m_list, false);
249 // 3. highlight the row
250 gtk_list_store_set(ui::ListStore::from(model), &iter, 2, true, -1);
252 // 4. grab keyboard focus
253 dialog.m_waiting_for_key = true;
256 bool accelerator_window_key_press(ui::Window widget, GdkEventKey *event, gpointer dialogptr)
258 command_list_dialog_t &dialog = *(command_list_dialog_t *) dialogptr;
260 if (!dialog.m_waiting_for_key) {
265 if ( event->is_modifier ) {
269 switch (event->keyval) {
270 case GDK_KEY_Shift_L:
271 case GDK_KEY_Shift_R:
272 case GDK_KEY_Control_L:
273 case GDK_KEY_Control_R:
274 case GDK_KEY_Caps_Lock:
275 case GDK_KEY_Shift_Lock:
280 case GDK_KEY_Super_L:
281 case GDK_KEY_Super_R:
282 case GDK_KEY_Hyper_L:
283 case GDK_KEY_Hyper_R:
288 dialog.m_waiting_for_key = false;
290 // 7. find the name of the accelerator
292 memset(&val, 0, sizeof(val));
293 gtk_tree_model_get_value(dialog.m_model, &dialog.m_command_iter, 0, &val);
294 const char *commandName = g_value_get_string(&val);;
295 Shortcuts::iterator thisShortcutIterator = g_shortcuts.find(commandName);
296 if (thisShortcutIterator == g_shortcuts.end()) {
297 gtk_list_store_set(ui::ListStore::from(dialog.m_model), &dialog.m_command_iter, 2, false, -1);
298 gtk_widget_set_sensitive(dialog.m_list, true);
302 // 8. build an Accelerator
303 Accelerator newAccel(event->keyval, (GdkModifierType) event->state);
305 // 8. verify the key is still free, show a dialog to ask what to do if not
306 class VerifyAcceleratorNotTaken : public CommandVisitor {
307 const char *commandName;
308 const Accelerator &newAccel;
314 VerifyAcceleratorNotTaken(const char *name, const Accelerator &accelerator, ui::Widget w, ui::TreeModel m)
315 : commandName(name), newAccel(accelerator), widget(w), model(m), allow(true)
319 void visit(const char *name, Accelerator &accelerator)
321 if (!strcmp(name, commandName)) {
327 if (accelerator.key == 0) {
330 if (accelerator == newAccel) {
331 StringOutputStream msg;
332 msg << "The command " << name << " is already assigned to the key " << accelerator << ".\n\n"
333 << "Do you want to unassign " << name << " first?";
334 auto r = ui::alert(widget.window(), msg.c_str(), "Key already used", ui::alert_type::YESNOCANCEL);
335 if (r == ui::alert_response::YES) {
336 // clear the ACTUAL accelerator too!
337 disconnect_accelerator(name);
338 // delete the modifier
339 accelerator = accelerator_null();
340 // empty the cell of the key binds dialog
342 if (gtk_tree_model_get_iter_first(model, &i)) {
345 memset(&val, 0, sizeof(val));
346 gtk_tree_model_get_value(model, &i, 0, &val);
347 const char *thisName = g_value_get_string(&val);;
348 if (!strcmp(thisName, name)) {
349 gtk_list_store_set(ui::ListStore::from(model), &i, 1, "", -1);
352 if (!gtk_tree_model_iter_next(model, &i)) {
357 } else if (r == ui::alert_response::CANCEL) {
363 } verify_visitor(commandName, newAccel, widget, dialog.m_model);
364 GlobalShortcuts_foreach(verify_visitor);
366 gtk_list_store_set(ui::ListStore::from(dialog.m_model), &dialog.m_command_iter, 2, false, -1);
367 gtk_widget_set_sensitive(dialog.m_list, true);
369 if (verify_visitor.allow) {
370 // clear the ACTUAL accelerator first
371 disconnect_accelerator(commandName);
373 thisShortcutIterator->second.first = newAccel;
375 // write into the cell
376 StringOutputStream modifiers;
377 modifiers << newAccel;
378 gtk_list_store_set(ui::ListStore::from(dialog.m_model), &dialog.m_command_iter, 1, modifiers.c_str(), -1);
380 // set the ACTUAL accelerator too!
381 connect_accelerator(commandName);
386 dialog.m_model = ui::TreeModel(ui::null);
394 if(!model) {g_error("Unable to get model from cell renderer");}
395 gtk_tree_model_get_iter_from_string(model, &row, path_string);
397 gtk_tree_model_get_value(model, &row, 0, &val);
398 const char *name = g_value_get_string(&val);
399 Shortcuts::iterator i = g_shortcuts.find(name);
400 if(i != g_shortcuts.end())
402 accelerator_parse(i->second.first, new_text);
403 StringOutputStream modifiers;
404 modifiers << i->second.first;
405 gtk_list_store_set(ui::ListStore::from(model), &row, 1, modifiers.c_str(), -1);
410 void DoCommandListDlg()
412 command_list_dialog_t dialog;
414 ui::Window window = MainFrame_getWindow().create_modal_dialog_window("Mapped Commands", dialog, -1, 400);
415 window.on_key_press([](ui::Widget widget, GdkEventKey *event, gpointer dialogptr) {
416 return accelerator_window_key_press(ui::Window::from(widget), event, dialogptr);
419 auto accel = ui::AccelGroup(ui::New);
420 window.add_accel_group(accel);
422 auto hbox = create_dialog_hbox(4, 4);
426 auto scr = create_scrolled_window(ui::Policy::NEVER, ui::Policy::AUTOMATIC);
427 hbox.pack_start(scr, TRUE, TRUE, 0);
430 auto store = ui::ListStore::from(
431 gtk_list_store_new(4, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_INT));
433 auto view = ui::TreeView(ui::TreeModel::from(store._handle));
434 dialog.m_list = view;
436 gtk_tree_view_set_enable_search(view, false); // annoying
439 auto renderer = ui::CellRendererText(ui::New);
440 auto column = ui::TreeViewColumn("Command", renderer, {{"text", 0},
443 gtk_tree_view_append_column(view, column);
447 auto renderer = ui::CellRendererText(ui::New);
448 auto column = ui::TreeViewColumn("Key", renderer, {{"text", 1},
451 gtk_tree_view_append_column(view, column);
459 StringOutputStream path(256);
460 path << SettingsPath_get() << "commandlist.txt";
461 globalOutputStream() << "Writing the command list to " << path.c_str() << "\n";
462 class BuildCommandList : public CommandVisitor {
463 TextFileOutputStream m_commandList;
464 ui::ListStore m_store;
466 BuildCommandList(const char *filename, ui::ListStore store) : m_commandList(filename),
471 void visit(const char *name, Accelerator &accelerator)
473 StringOutputStream modifiers;
474 modifiers << accelerator;
476 m_store.append(0, name, 1, modifiers.c_str(), 2, false, 3, 800);
478 if (!m_commandList.failed()) {
479 int l = strlen(name);
480 m_commandList << name;
482 m_commandList << ' ';
484 m_commandList << modifiers.c_str() << '\n';
487 } visitor(path.c_str(), store);
489 GlobalShortcuts_foreach(visitor);
496 auto vbox = create_dialog_vbox(4);
497 hbox.pack_start(vbox, TRUE, TRUE, 0);
499 auto editbutton = create_dialog_button("Edit", (GCallback) accelerator_edit_button_clicked, &dialog);
500 vbox.pack_start(editbutton, FALSE, FALSE, 0);
502 auto clearbutton = create_dialog_button("Clear", (GCallback) accelerator_clear_button_clicked, &dialog);
503 vbox.pack_start(clearbutton, FALSE, FALSE, 0);
505 ui::Widget spacer = ui::Image(ui::New);
507 vbox.pack_start(spacer, TRUE, TRUE, 0);
509 auto button = create_modal_dialog_button("Close", dialog.m_close_button);
510 vbox.pack_start(button, FALSE, FALSE, 0);
511 widget_make_default(button);
512 gtk_widget_grab_default(button);
513 gtk_widget_add_accelerator(button, "clicked", accel, GDK_KEY_Return, (GdkModifierType) 0, (GtkAccelFlags) 0);
514 gtk_widget_add_accelerator(button, "clicked", accel, GDK_KEY_Escape, (GdkModifierType) 0, (GtkAccelFlags) 0);
517 modal_dialog_show(window, dialog);
521 #include "profile/profile.h"
523 const char *const COMMANDS_VERSION = "1.0-gtk-accelnames";
525 void SaveCommandMap(const char *path)
527 StringOutputStream strINI(256);
528 strINI << path << "shortcuts.ini";
530 TextFileOutputStream file(strINI.c_str());
531 if (!file.failed()) {
532 file << "[Version]\n";
533 file << "number=" << COMMANDS_VERSION << "\n";
535 file << "[Commands]\n";
536 class WriteCommandMap : public CommandVisitor {
537 TextFileOutputStream &m_file;
539 WriteCommandMap(TextFileOutputStream &file) : m_file(file)
543 void visit(const char *name, Accelerator &accelerator)
545 m_file << name << "=";
547 const char *key = gtk_accelerator_name(accelerator.key, accelerator.modifiers);
552 GlobalShortcuts_foreach(visitor);
556 const char *stringrange_find(const char *first, const char *last, char c)
558 const char *p = strchr(first, '+');
565 class ReadCommandMap : public CommandVisitor {
566 const char *m_filename;
569 ReadCommandMap(const char *filename) : m_filename(filename), m_count(0)
573 void visit(const char *name, Accelerator &accelerator)
576 if (read_var(m_filename, "Commands", name, value)) {
577 if (string_empty(value)) {
579 accelerator.modifiers = (GdkModifierType) 0;
583 gtk_accelerator_parse(value, &accelerator.key, &accelerator.modifiers);
584 accelerator = accelerator; // fix modifiers
586 if (accelerator.key != 0) {
589 globalOutputStream() << "WARNING: failed to parse user command " << makeQuoted(name) << ": unknown key "
590 << makeQuoted(value) << "\n";
595 std::size_t count() const
601 void LoadCommandMap(const char *path)
603 StringOutputStream strINI(256);
604 strINI << path << "shortcuts.ini";
606 FILE *f = fopen(strINI.c_str(), "r");
609 globalOutputStream() << "loading custom shortcuts list from " << makeQuoted(strINI.c_str()) << "\n";
611 Version version = version_parse(COMMANDS_VERSION);
612 Version dataVersion = {0, 0};
616 if (read_var(strINI.c_str(), "Version", "number", value)) {
617 dataVersion = version_parse(value);
621 if (version_compatible(version, dataVersion)) {
622 globalOutputStream() << "commands import: data version " << dataVersion
623 << " is compatible with code version " << version << "\n";
624 ReadCommandMap visitor(strINI.c_str());
625 GlobalShortcuts_foreach(visitor);
626 globalOutputStream() << "parsed " << Unsigned(visitor.count()) << " custom shortcuts\n";
628 globalOutputStream() << "commands import: data version " << dataVersion
629 << " is not compatible with code version " << version << "\n";
632 globalOutputStream() << "failed to load custom shortcuts from " << makeQuoted(strINI.c_str()) << "\n";