]> icculus.org git repositories - divverent/netradiant.git/blob - radiant/commands.cpp
new args -meta and -patchmeta to -convert with .map file argument
[divverent/netradiant.git] / radiant / commands.cpp
1 /*
2 Copyright (C) 2001-2006, William Joseph.
3 All Rights Reserved.
4
5 This file is part of GtkRadiant.
6
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.
11
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.
16
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
20 */
21
22 #include "commands.h"
23
24 #include "debugging/debugging.h"
25 #include "warnings.h"
26
27 #include <map>
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>
34 #include "gtkmisc.h"
35
36 typedef std::pair<Accelerator, int> ShortcutValue; // accelerator, isRegistered
37 typedef std::map<CopiedString, ShortcutValue> Shortcuts;
38
39 void Shortcuts_foreach(Shortcuts& shortcuts, CommandVisitor& visitor)
40 {
41   for(Shortcuts::iterator i = shortcuts.begin(); i != shortcuts.end(); ++i)
42   {
43     visitor.visit((*i).first.c_str(), (*i).second.first);
44   }
45 }
46
47 Shortcuts g_shortcuts;
48
49 const Accelerator& GlobalShortcuts_insert(const char* name, const Accelerator& accelerator)
50 {
51   return (*g_shortcuts.insert(Shortcuts::value_type(name, ShortcutValue(accelerator, false))).first).second.first;
52 }
53
54 void GlobalShortcuts_foreach(CommandVisitor& visitor)
55 {
56   Shortcuts_foreach(g_shortcuts, visitor);
57 }
58
59 void GlobalShortcuts_register(const char* name, int type)
60 {
61   Shortcuts::iterator i = g_shortcuts.find(name);
62   if(i != g_shortcuts.end())
63   {
64     (*i).second.second = type;
65   }
66 }
67
68 void GlobalShortcuts_reportUnregistered()
69 {
70   for(Shortcuts::iterator i = g_shortcuts.begin(); i != g_shortcuts.end(); ++i)
71   {
72     if((*i).second.first.key != 0 && !(*i).second.second)
73     {
74       globalOutputStream() << "shortcut not registered: " << (*i).first.c_str() << "\n";
75     }
76   }
77 }
78
79 typedef std::map<CopiedString, Command> Commands;
80
81 Commands g_commands;
82
83 void GlobalCommands_insert(const char* name, const Callback& callback, const Accelerator& accelerator)
84 {
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));
87 }
88
89 const Command& GlobalCommands_find(const char* command)
90 {
91   Commands::iterator i = g_commands.find(command);
92   ASSERT_MESSAGE(i != g_commands.end(), "failed to lookup command " << makeQuoted(command));
93   return (*i).second;
94 }
95
96 typedef std::map<CopiedString, Toggle> Toggles;
97
98
99 Toggles g_toggles;
100
101 void GlobalToggles_insert(const char* name, const Callback& callback, const BoolExportCallback& exportCallback, const Accelerator& accelerator)
102 {
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));
105 }
106 const Toggle& GlobalToggles_find(const char* name)
107 {
108   Toggles::iterator i = g_toggles.find(name);
109   ASSERT_MESSAGE(i != g_toggles.end(), "failed to lookup toggle " << makeQuoted(name));
110   return (*i).second;
111 }
112
113 typedef std::map<CopiedString, KeyEvent> KeyEvents;
114
115
116 KeyEvents g_keyEvents;
117
118 void GlobalKeyEvents_insert(const char* name, const Accelerator& accelerator, const Callback& keyDown, const Callback& keyUp)
119 {
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));
122 }
123 const KeyEvent& GlobalKeyEvents_find(const char* name)
124 {
125   KeyEvents::iterator i = g_keyEvents.find(name);
126   ASSERT_MESSAGE(i != g_keyEvents.end(), "failed to lookup keyEvent " << makeQuoted(name));
127   return (*i).second;
128 }
129
130
131 void disconnect_accelerator(const char *name)
132 {
133         Shortcuts::iterator i = g_shortcuts.find(name);
134         if(i != g_shortcuts.end())
135         {
136                 switch((*i).second.second)
137                 {
138                         case 1:
139                                 // command
140                                 command_disconnect_accelerator(name);
141                                 break;
142                         case 2:
143                                 // toggle
144                                 toggle_remove_accelerator(name);
145                                 break;
146                 }
147         }
148 }
149
150 void connect_accelerator(const char *name)
151 {
152         Shortcuts::iterator i = g_shortcuts.find(name);
153         if(i != g_shortcuts.end())
154         {
155                 switch((*i).second.second)
156                 {
157                         case 1:
158                                 // command
159                                 command_connect_accelerator(name);
160                                 break;
161                         case 2:
162                                 // toggle
163                                 toggle_add_accelerator(name);
164                                 break;
165                 }
166         }
167 }
168
169
170 #include <cctype>
171
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>
177
178 #include "gtkutil/dialog.h"
179 #include "mainframe.h"
180
181 #include "stream/textfilestream.h"
182 #include "stream/stringstream.h"
183
184
185 struct command_list_dialog_t : public ModalDialog
186 {
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)
189   {
190   }
191   ModalDialogButton m_close_button;
192
193   GtkTreeView *m_list;
194   GtkTreeIter m_command_iter;
195   GtkTreeModel *m_model;
196   bool m_waiting_for_key;
197 };
198
199 void accelerator_clear_button_clicked(GtkButton *btn, gpointer dialogptr)
200 {
201         command_list_dialog_t &dialog = * (command_list_dialog_t *) dialogptr;
202
203         if(dialog.m_waiting_for_key)
204         {
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;
210                 return;
211         }
212
213         GtkTreeSelection *sel = gtk_tree_view_get_selection(dialog.m_list);
214         GtkTreeModel *model;
215         GtkTreeIter iter;
216         if(!gtk_tree_selection_get_selected(sel, &model, &iter))
217                 return;
218
219         GValue val;
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);;
223
224         // clear the ACTUAL accelerator too!
225         disconnect_accelerator(commandName);
226
227         Shortcuts::iterator thisShortcutIterator = g_shortcuts.find(commandName);
228         if(thisShortcutIterator == g_shortcuts.end())
229                 return;
230         thisShortcutIterator->second.first = accelerator_null();
231
232         gtk_list_store_set(GTK_LIST_STORE(model), &iter, 1, "", -1);
233
234         g_value_unset(&val);
235 }
236
237 void accelerator_edit_button_clicked(GtkButton *btn, gpointer dialogptr)
238 {
239         command_list_dialog_t &dialog = * (command_list_dialog_t *) dialogptr;
240
241         // 1. find selected row
242         GtkTreeSelection *sel = gtk_tree_view_get_selection(dialog.m_list);
243         GtkTreeModel *model;
244         GtkTreeIter iter;
245         if(!gtk_tree_selection_get_selected(sel, &model, &iter))
246                 return;
247         dialog.m_command_iter = iter;
248         dialog.m_model = model;
249         
250         // 2. disallow changing the row
251         //gtk_widget_set_sensitive(GTK_WIDGET(dialog.m_list), false);
252
253         // 3. highlight the row
254         gtk_list_store_set(GTK_LIST_STORE(model), &iter, 2, true, -1);
255
256         // 4. grab keyboard focus
257         dialog.m_waiting_for_key = true;
258 }
259
260 gboolean accelerator_window_key_press(GtkWidget *widget, GdkEventKey *event, gpointer dialogptr)
261 {
262         command_list_dialog_t &dialog = * (command_list_dialog_t *) dialogptr;
263
264         if(!dialog.m_waiting_for_key)
265                 return false;
266
267 #if 0
268         if(event->is_modifier)
269                 return false;
270 #else
271         switch(event->keyval)
272         {
273                 case GDK_Shift_L:
274                 case GDK_Shift_R:
275                 case GDK_Control_L:
276                 case GDK_Control_R:
277                 case GDK_Caps_Lock:
278                 case GDK_Shift_Lock:
279                 case GDK_Meta_L:
280                 case GDK_Meta_R:
281                 case GDK_Alt_L:
282                 case GDK_Alt_R:
283                 case GDK_Super_L:
284                 case GDK_Super_R:
285                 case GDK_Hyper_L:
286                 case GDK_Hyper_R:
287                         return false;
288         }
289 #endif
290
291         dialog.m_waiting_for_key = false;
292
293         // 7. find the name of the accelerator
294         GValue val;
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())
300         {
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);
303                 return true;
304         }
305
306         // 8. build an Accelerator
307         Accelerator newAccel(event->keyval, (GdkModifierType) event->state);
308
309         // 8. verify the key is still free, show a dialog to ask what to do if not
310         class VerifyAcceleratorNotTaken : public CommandVisitor
311         {
312                 const char *commandName;
313                 const Accelerator &newAccel;
314                 GtkWidget *widget;
315                 GtkTreeModel *model;
316                 public:
317                 bool allow;
318                 VerifyAcceleratorNotTaken(const char *name, const Accelerator &accelerator, GtkWidget *w, GtkTreeModel *m) : commandName(name), newAccel(accelerator), widget(w), model(m), allow(true)
319                 {
320                 }
321                 void visit(const char* name, Accelerator& accelerator)
322                 {
323                         if(!strcmp(name, commandName))
324                                 return;
325                         if(!allow)
326                                 return;
327                         if(accelerator.key == 0)
328                                 return;
329                         if(accelerator == newAccel)
330                         {
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);
335                                 if(r == eIDYES)
336                                 {
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
342                                         GtkTreeIter i;
343                                         if(gtk_tree_model_get_iter_first(GTK_TREE_MODEL(model), &i))
344                                         {
345                                                 for(;;)
346                                                 {
347                                                         GValue val;
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);
353                                                         g_value_unset(&val);
354                                                         if(!gtk_tree_model_iter_next(GTK_TREE_MODEL(model), &i))
355                                                                 break;
356                                                 }
357                                         }
358                                 }
359                                 else if(r == eIDCANCEL)
360                                 {
361                                         // aborted
362                                         allow = false;
363                                 }
364                         }
365                 }
366         } verify_visitor(commandName, newAccel, widget, dialog.m_model);
367         GlobalShortcuts_foreach(verify_visitor);
368
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);
371
372         if(verify_visitor.allow)
373         {
374                 // clear the ACTUAL accelerator first
375                 disconnect_accelerator(commandName);
376
377                 thisShortcutIterator->second.first = newAccel;
378
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);
383
384                 // set the ACTUAL accelerator too!
385                 connect_accelerator(commandName);
386         }
387
388         g_value_unset(&val);
389
390         dialog.m_model = NULL;
391
392         return true;
393 }
394
395 /*
396         GtkTreeIter row;
397         GValue val;
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);
400
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())
405         {
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);
410         }
411 };
412 */
413
414 void DoCommandListDlg()
415 {
416   command_list_dialog_t dialog;
417
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);
420
421   GtkAccelGroup* accel = gtk_accel_group_new();
422   gtk_window_add_accel_group(window, accel);
423
424   GtkHBox* hbox = create_dialog_hbox(4, 4);
425   gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(hbox));
426
427   {
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);
430
431     {
432       GtkListStore* store = gtk_list_store_new(4, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_INT);
433
434       GtkWidget* view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
435           dialog.m_list = GTK_TREE_VIEW(view);
436
437           gtk_tree_view_set_enable_search(GTK_TREE_VIEW(view), false); // annoying
438
439       {
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);
443       }
444
445       {
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);
449       }
450
451       gtk_widget_show(view);
452       gtk_container_add(GTK_CONTAINER (scr), view);
453
454       {
455         // Initialize dialog
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
460         {
461           TextFileOutputStream m_commandList;
462           GtkListStore* m_store;
463         public:
464           BuildCommandList(const char* filename, GtkListStore* store) : m_commandList(filename), m_store(store)
465           {
466           }
467           void visit(const char* name, Accelerator& accelerator)
468           {
469             StringOutputStream modifiers;
470             modifiers << accelerator;
471
472             {
473               GtkTreeIter iter;
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);
476             }
477  
478             if(!m_commandList.failed())
479             {
480               int l = strlen(name);
481               m_commandList << name;
482               while(l++ < 25)
483                                   m_commandList << ' ';
484               m_commandList << modifiers.c_str() << '\n';
485             }
486           }
487         } visitor(path.c_str(), store);
488
489         GlobalShortcuts_foreach(visitor);
490       }
491     
492       g_object_unref(G_OBJECT(store));
493     }
494   }
495
496   GtkVBox* vbox = create_dialog_vbox(4);
497   gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(vbox), TRUE, TRUE, 0);
498   {
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);
501
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);
504
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);
508
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);
515   }
516
517   modal_dialog_show(window, dialog);
518   gtk_widget_destroy(GTK_WIDGET(window));
519 }
520
521 #include "profile/profile.h"
522
523 const char* const COMMANDS_VERSION = "1.0-gtk-accelnames";
524
525 void SaveCommandMap(const char* path)
526 {
527   StringOutputStream strINI(256);
528   strINI << path << "shortcuts.ini";
529
530   TextFileOutputStream file(strINI.c_str());
531   if(!file.failed())
532   {
533     file << "[Version]\n";
534     file << "number=" << COMMANDS_VERSION << "\n";
535     file << "\n";
536     file << "[Commands]\n";
537     class WriteCommandMap : public CommandVisitor
538     {
539       TextFileOutputStream& m_file;
540     public:
541       WriteCommandMap(TextFileOutputStream& file) : m_file(file)
542       {
543       }
544       void visit(const char* name, Accelerator& accelerator)
545       {
546         m_file << name << "=";
547
548         const char* key = gtk_accelerator_name(accelerator.key, accelerator.modifiers);
549                 m_file << key;
550         m_file << "\n";
551       }
552     } visitor(file);
553     GlobalShortcuts_foreach(visitor);
554   }
555 }
556
557 const char* stringrange_find(const char* first, const char* last, char c)
558 {
559   const char* p = strchr(first, '+');
560   if(p == 0)
561   {
562     return last;
563   }
564   return p;
565 }
566
567 class ReadCommandMap : public CommandVisitor
568 {
569   const char* m_filename;
570   std::size_t m_count;
571 public:
572   ReadCommandMap(const char* filename) : m_filename(filename), m_count(0)
573   {
574   }
575   void visit(const char* name, Accelerator& accelerator)
576   {
577     char value[1024];
578     if (read_var(m_filename, "Commands", name, value ))
579     {
580       if(string_empty(value))
581       {
582         accelerator.key = 0;
583         accelerator.modifiers = (GdkModifierType)0;
584         return;
585       }
586
587           gtk_accelerator_parse(value, &accelerator.key, &accelerator.modifiers);
588           accelerator = accelerator; // fix modifiers
589
590           if(accelerator.key != 0)
591           {
592                   ++m_count;
593           }
594           else
595           {
596                   globalOutputStream() << "WARNING: failed to parse user command " << makeQuoted(name) << ": unknown key " << makeQuoted(value) << "\n";
597           }
598     }
599   }
600   std::size_t count() const
601   {
602     return m_count;
603   }
604 };
605     
606 void LoadCommandMap(const char* path)
607 {
608   StringOutputStream strINI(256);
609   strINI << path << "shortcuts.ini";
610
611   FILE* f = fopen (strINI.c_str(), "r");
612   if (f != 0)
613   {
614     fclose(f);
615     globalOutputStream() << "loading custom shortcuts list from " << makeQuoted(strINI.c_str()) << "\n";
616
617     Version version = version_parse(COMMANDS_VERSION);
618     Version dataVersion = { 0, 0 };
619
620     {
621       char value[1024];
622       if(read_var(strINI.c_str(), "Version", "number", value))
623       {
624         dataVersion = version_parse(value);
625       }
626     }
627
628     if(version_compatible(version, dataVersion))
629     {
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";
634     }
635     else
636     {
637       globalOutputStream() << "commands import: data version " << dataVersion << " is not compatible with code version " << version << "\n";
638     }
639   }
640   else
641   {
642     globalOutputStream() << "failed to load custom shortcuts from " << makeQuoted(strINI.c_str()) << "\n";
643   }
644 }