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"
31 #include "gtkutil/messagebox.h"
32 #include <gtk/gtktreeselection.h>
33 #include <gtk/gtkbutton.h>
36 typedef std::pair<Accelerator, int> ShortcutValue; // accelerator, isRegistered
37 typedef std::map<CopiedString, ShortcutValue> Shortcuts;
39 void Shortcuts_foreach(Shortcuts& shortcuts, CommandVisitor& visitor)
41 for(Shortcuts::iterator i = shortcuts.begin(); i != shortcuts.end(); ++i)
43 visitor.visit((*i).first.c_str(), (*i).second.first);
47 Shortcuts g_shortcuts;
49 const Accelerator& GlobalShortcuts_insert(const char* name, const Accelerator& accelerator)
51 return (*g_shortcuts.insert(Shortcuts::value_type(name, ShortcutValue(accelerator, false))).first).second.first;
54 void GlobalShortcuts_foreach(CommandVisitor& visitor)
56 Shortcuts_foreach(g_shortcuts, visitor);
59 void GlobalShortcuts_register(const char* name, int type)
61 Shortcuts::iterator i = g_shortcuts.find(name);
62 if(i != g_shortcuts.end())
64 (*i).second.second = type;
68 void GlobalShortcuts_reportUnregistered()
70 for(Shortcuts::iterator i = g_shortcuts.begin(); i != g_shortcuts.end(); ++i)
72 if((*i).second.first.key != 0 && !(*i).second.second)
74 globalOutputStream() << "shortcut not registered: " << (*i).first.c_str() << "\n";
79 typedef std::map<CopiedString, Command> Commands;
83 void GlobalCommands_insert(const char* name, const Callback& callback, const Accelerator& accelerator)
85 bool added = g_commands.insert(Commands::value_type(name, Command(callback, GlobalShortcuts_insert(name, accelerator)))).second;
86 ASSERT_MESSAGE(added, "command already registered: " << makeQuoted(name));
89 const Command& GlobalCommands_find(const char* command)
91 Commands::iterator i = g_commands.find(command);
92 ASSERT_MESSAGE(i != g_commands.end(), "failed to lookup command " << makeQuoted(command));
96 typedef std::map<CopiedString, Toggle> Toggles;
101 void GlobalToggles_insert(const char* name, const Callback& callback, const BoolExportCallback& exportCallback, const Accelerator& accelerator)
103 bool added = g_toggles.insert(Toggles::value_type(name, Toggle(callback, GlobalShortcuts_insert(name, accelerator), exportCallback))).second;
104 ASSERT_MESSAGE(added, "toggle already registered: " << makeQuoted(name));
106 const Toggle& GlobalToggles_find(const char* name)
108 Toggles::iterator i = g_toggles.find(name);
109 ASSERT_MESSAGE(i != g_toggles.end(), "failed to lookup toggle " << makeQuoted(name));
113 typedef std::map<CopiedString, KeyEvent> KeyEvents;
116 KeyEvents g_keyEvents;
118 void GlobalKeyEvents_insert(const char* name, const Accelerator& accelerator, const Callback& keyDown, const Callback& keyUp)
120 bool added = g_keyEvents.insert(KeyEvents::value_type(name, KeyEvent(GlobalShortcuts_insert(name, accelerator), keyDown, keyUp))).second;
121 ASSERT_MESSAGE(added, "command already registered: " << makeQuoted(name));
123 const KeyEvent& GlobalKeyEvents_find(const char* name)
125 KeyEvents::iterator i = g_keyEvents.find(name);
126 ASSERT_MESSAGE(i != g_keyEvents.end(), "failed to lookup keyEvent " << makeQuoted(name));
131 void disconnect_accelerator(const char *name)
133 Shortcuts::iterator i = g_shortcuts.find(name);
134 if(i != g_shortcuts.end())
136 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())
155 switch((*i).second.second)
159 command_connect_accelerator(name);
163 toggle_add_accelerator(name);
172 #include <gtk/gtkbox.h>
173 #include <gtk/gtkliststore.h>
174 #include <gtk/gtktreemodel.h>
175 #include <gtk/gtktreeview.h>
176 #include <gtk/gtkcellrenderertext.h>
178 #include "gtkutil/dialog.h"
179 #include "mainframe.h"
181 #include "stream/textfilestream.h"
182 #include "stream/stringstream.h"
185 struct command_list_dialog_t : public ModalDialog
187 command_list_dialog_t()
188 : m_close_button(*this, eIDCANCEL), m_list(NULL), m_command_iter(), m_model(NULL), m_waiting_for_key(false)
191 ModalDialogButton m_close_button;
194 GtkTreeIter m_command_iter;
195 GtkTreeModel *m_model;
196 bool m_waiting_for_key;
199 void accelerator_clear_button_clicked(GtkButton *btn, gpointer dialogptr)
201 command_list_dialog_t &dialog = * (command_list_dialog_t *) dialogptr;
203 if(dialog.m_waiting_for_key)
205 // just unhighlight, user wanted to cancel
206 dialog.m_waiting_for_key = false;
207 gtk_list_store_set(GTK_LIST_STORE(dialog.m_model), &dialog.m_command_iter, 2, false, -1);
208 gtk_widget_set_sensitive(GTK_WIDGET(dialog.m_list), true);
209 dialog.m_model = NULL;
213 GtkTreeSelection *sel = gtk_tree_view_get_selection(dialog.m_list);
216 if(!gtk_tree_selection_get_selected(sel, &model, &iter))
220 memset(&val, 0, sizeof(val));
221 gtk_tree_model_get_value(GTK_TREE_MODEL(model), &iter, 0, &val);
222 const char *commandName = g_value_get_string(&val);;
224 // clear the ACTUAL accelerator too!
225 disconnect_accelerator(commandName);
227 Shortcuts::iterator thisShortcutIterator = g_shortcuts.find(commandName);
228 if(thisShortcutIterator == g_shortcuts.end())
230 thisShortcutIterator->second.first = accelerator_null();
232 gtk_list_store_set(GTK_LIST_STORE(model), &iter, 1, "", -1);
237 void accelerator_edit_button_clicked(GtkButton *btn, gpointer dialogptr)
239 command_list_dialog_t &dialog = * (command_list_dialog_t *) dialogptr;
241 // 1. find selected row
242 GtkTreeSelection *sel = gtk_tree_view_get_selection(dialog.m_list);
245 if(!gtk_tree_selection_get_selected(sel, &model, &iter))
247 dialog.m_command_iter = iter;
248 dialog.m_model = model;
250 // 2. disallow changing the row
251 //gtk_widget_set_sensitive(GTK_WIDGET(dialog.m_list), false);
253 // 3. highlight the row
254 gtk_list_store_set(GTK_LIST_STORE(model), &iter, 2, true, -1);
256 // 4. grab keyboard focus
257 dialog.m_waiting_for_key = true;
260 gboolean accelerator_window_key_press(GtkWidget *widget, GdkEventKey *event, gpointer dialogptr)
262 command_list_dialog_t &dialog = * (command_list_dialog_t *) dialogptr;
264 if(!dialog.m_waiting_for_key)
268 if(event->is_modifier)
271 switch(event->keyval)
291 dialog.m_waiting_for_key = false;
293 // 7. find the name of the accelerator
295 memset(&val, 0, sizeof(val));
296 gtk_tree_model_get_value(GTK_TREE_MODEL(dialog.m_model), &dialog.m_command_iter, 0, &val);
297 const char *commandName = g_value_get_string(&val);;
298 Shortcuts::iterator thisShortcutIterator = g_shortcuts.find(commandName);
299 if(thisShortcutIterator == g_shortcuts.end())
301 gtk_list_store_set(GTK_LIST_STORE(dialog.m_model), &dialog.m_command_iter, 2, false, -1);
302 gtk_widget_set_sensitive(GTK_WIDGET(dialog.m_list), true);
306 // 8. build an Accelerator
307 Accelerator newAccel(event->keyval, (GdkModifierType) event->state);
309 // 8. verify the key is still free, show a dialog to ask what to do if not
310 class VerifyAcceleratorNotTaken : public CommandVisitor
312 const char *commandName;
313 const Accelerator &newAccel;
318 VerifyAcceleratorNotTaken(const char *name, const Accelerator &accelerator, GtkWidget *w, GtkTreeModel *m) : commandName(name), newAccel(accelerator), widget(w), model(m), allow(true)
321 void visit(const char* name, Accelerator& accelerator)
323 if(!strcmp(name, commandName))
327 if(accelerator.key == 0)
329 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 EMessageBoxReturn r = gtk_MessageBox (widget, msg.c_str(), "Key already used", eMB_YESNOCANCEL);
337 // clear the ACTUAL accelerator too!
338 disconnect_accelerator(name);
339 // delete the modifier
340 accelerator = accelerator_null();
341 // empty the cell of the key binds dialog
343 if(gtk_tree_model_get_iter_first(GTK_TREE_MODEL(model), &i))
348 memset(&val, 0, sizeof(val));
349 gtk_tree_model_get_value(GTK_TREE_MODEL(model), &i, 0, &val);
350 const char *thisName = g_value_get_string(&val);;
351 if(!strcmp(thisName, name))
352 gtk_list_store_set(GTK_LIST_STORE(model), &i, 1, "", -1);
354 if(!gtk_tree_model_iter_next(GTK_TREE_MODEL(model), &i))
359 else if(r == eIDCANCEL)
366 } verify_visitor(commandName, newAccel, widget, dialog.m_model);
367 GlobalShortcuts_foreach(verify_visitor);
369 gtk_list_store_set(GTK_LIST_STORE(dialog.m_model), &dialog.m_command_iter, 2, false, -1);
370 gtk_widget_set_sensitive(GTK_WIDGET(dialog.m_list), true);
372 if(verify_visitor.allow)
374 // clear the ACTUAL accelerator first
375 disconnect_accelerator(commandName);
377 thisShortcutIterator->second.first = newAccel;
379 // write into the cell
380 StringOutputStream modifiers;
381 modifiers << newAccel;
382 gtk_list_store_set(GTK_LIST_STORE(dialog.m_model), &dialog.m_command_iter, 1, modifiers.c_str(), -1);
384 // set the ACTUAL accelerator too!
385 connect_accelerator(commandName);
390 dialog.m_model = NULL;
398 if(!model) {g_error("Unable to get model from cell renderer");}
399 gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(model), &row, path_string);
401 gtk_tree_model_get_value(GTK_TREE_MODEL(model), &row, 0, &val);
402 const char *name = g_value_get_string(&val);
403 Shortcuts::iterator i = g_shortcuts.find(name);
404 if(i != g_shortcuts.end())
406 accelerator_parse(i->second.first, new_text);
407 StringOutputStream modifiers;
408 modifiers << i->second.first;
409 gtk_list_store_set(GTK_LIST_STORE(model), &row, 1, modifiers.c_str(), -1);
414 void DoCommandListDlg()
416 command_list_dialog_t dialog;
418 GtkWindow* window = create_modal_dialog_window(MainFrame_getWindow(), "Mapped Commands", dialog, -1, 400);
419 g_signal_connect(G_OBJECT(window), "key-press-event", (GCallback) accelerator_window_key_press, &dialog);
421 GtkAccelGroup* accel = gtk_accel_group_new();
422 gtk_window_add_accel_group(window, accel);
424 GtkHBox* hbox = create_dialog_hbox(4, 4);
425 gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(hbox));
428 GtkScrolledWindow* scr = create_scrolled_window(GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
429 gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(scr), TRUE, TRUE, 0);
432 GtkListStore* store = gtk_list_store_new(4, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_INT);
434 GtkWidget* view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
435 dialog.m_list = GTK_TREE_VIEW(view);
437 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(view), false); // annoying
440 GtkCellRenderer* renderer = gtk_cell_renderer_text_new();
441 GtkTreeViewColumn* column = gtk_tree_view_column_new_with_attributes("Command", renderer, "text", 0, "weight-set", 2, "weight", 3, NULL);
442 gtk_tree_view_append_column(GTK_TREE_VIEW(view), column);
446 GtkCellRenderer* renderer = gtk_cell_renderer_text_new();
447 GtkTreeViewColumn* column = gtk_tree_view_column_new_with_attributes("Key", renderer, "text", 1, "weight-set", 2, "weight", 3, NULL);
448 gtk_tree_view_append_column(GTK_TREE_VIEW(view), column);
451 gtk_widget_show(view);
452 gtk_container_add(GTK_CONTAINER (scr), view);
456 StringOutputStream path(256);
457 path << SettingsPath_get() << "commandlist.txt";
458 globalOutputStream() << "Writing the command list to " << path.c_str() << "\n";
459 class BuildCommandList : public CommandVisitor
461 TextFileOutputStream m_commandList;
462 GtkListStore* m_store;
464 BuildCommandList(const char* filename, GtkListStore* store) : m_commandList(filename), m_store(store)
467 void visit(const char* name, Accelerator& accelerator)
469 StringOutputStream modifiers;
470 modifiers << accelerator;
474 gtk_list_store_append(m_store, &iter);
475 gtk_list_store_set(m_store, &iter, 0, name, 1, modifiers.c_str(), 2, false, 3, 800, -1);
478 if(!m_commandList.failed())
480 int l = strlen(name);
481 m_commandList << name;
483 m_commandList << ' ';
484 m_commandList << modifiers.c_str() << '\n';
487 } visitor(path.c_str(), store);
489 GlobalShortcuts_foreach(visitor);
492 g_object_unref(G_OBJECT(store));
496 GtkVBox* vbox = create_dialog_vbox(4);
497 gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(vbox), TRUE, TRUE, 0);
499 GtkButton* editbutton = create_dialog_button("Edit", (GCallback) accelerator_edit_button_clicked, &dialog);
500 gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(editbutton), FALSE, FALSE, 0);
502 GtkButton* clearbutton = create_dialog_button("Clear", (GCallback) accelerator_clear_button_clicked, &dialog);
503 gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(clearbutton), FALSE, FALSE, 0);
505 GtkWidget *spacer = gtk_image_new();
506 gtk_widget_show(spacer);
507 gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(spacer), TRUE, TRUE, 0);
509 GtkButton* button = create_modal_dialog_button("Close", dialog.m_close_button);
510 gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(button), FALSE, FALSE, 0);
511 widget_make_default(GTK_WIDGET(button));
512 gtk_widget_grab_default(GTK_WIDGET(button));
513 gtk_widget_add_accelerator(GTK_WIDGET(button), "clicked", accel, GDK_Return, (GdkModifierType)0, (GtkAccelFlags)0);
514 gtk_widget_add_accelerator(GTK_WIDGET(button), "clicked", accel, GDK_Escape, (GdkModifierType)0, (GtkAccelFlags)0);
517 modal_dialog_show(window, dialog);
518 gtk_widget_destroy(GTK_WIDGET(window));
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());
533 file << "[Version]\n";
534 file << "number=" << COMMANDS_VERSION << "\n";
536 file << "[Commands]\n";
537 class WriteCommandMap : public CommandVisitor
539 TextFileOutputStream& m_file;
541 WriteCommandMap(TextFileOutputStream& file) : m_file(file)
544 void visit(const char* name, Accelerator& accelerator)
546 m_file << name << "=";
548 const char* key = gtk_accelerator_name(accelerator.key, accelerator.modifiers);
553 GlobalShortcuts_foreach(visitor);
557 const char* stringrange_find(const char* first, const char* last, char c)
559 const char* p = strchr(first, '+');
567 class ReadCommandMap : public CommandVisitor
569 const char* m_filename;
572 ReadCommandMap(const char* filename) : m_filename(filename), m_count(0)
575 void visit(const char* name, Accelerator& accelerator)
578 if (read_var(m_filename, "Commands", name, value ))
580 if(string_empty(value))
583 accelerator.modifiers = (GdkModifierType)0;
587 gtk_accelerator_parse(value, &accelerator.key, &accelerator.modifiers);
588 accelerator = accelerator; // fix modifiers
590 if(accelerator.key != 0)
596 globalOutputStream() << "WARNING: failed to parse user command " << makeQuoted(name) << ": unknown key " << makeQuoted(value) << "\n";
600 std::size_t count() const
606 void LoadCommandMap(const char* path)
608 StringOutputStream strINI(256);
609 strINI << path << "shortcuts.ini";
611 FILE* f = fopen (strINI.c_str(), "r");
615 globalOutputStream() << "loading custom shortcuts list from " << makeQuoted(strINI.c_str()) << "\n";
617 Version version = version_parse(COMMANDS_VERSION);
618 Version dataVersion = { 0, 0 };
622 if(read_var(strINI.c_str(), "Version", "number", value))
624 dataVersion = version_parse(value);
628 if(version_compatible(version, dataVersion))
630 globalOutputStream() << "commands import: data version " << dataVersion << " is compatible with code version " << version << "\n";
631 ReadCommandMap visitor(strINI.c_str());
632 GlobalShortcuts_foreach(visitor);
633 globalOutputStream() << "parsed " << Unsigned(visitor.count()) << " custom shortcuts\n";
637 globalOutputStream() << "commands import: data version " << dataVersion << " is not compatible with code version " << version << "\n";
642 globalOutputStream() << "failed to load custom shortcuts from " << makeQuoted(strINI.c_str()) << "\n";