]> icculus.org git repositories - divverent/netradiant.git/blob - radiant/commands.cpp
fix the last commit
[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
32 typedef std::pair<Accelerator, bool> ShortcutValue; // accelerator, isRegistered
33 typedef std::map<CopiedString, ShortcutValue> Shortcuts;
34
35 void Shortcuts_foreach(Shortcuts& shortcuts, CommandVisitor& visitor)
36 {
37   for(Shortcuts::iterator i = shortcuts.begin(); i != shortcuts.end(); ++i)
38   {
39     visitor.visit((*i).first.c_str(), (*i).second.first);
40   }
41 }
42
43 Shortcuts g_shortcuts;
44
45 const Accelerator& GlobalShortcuts_insert(const char* name, const Accelerator& accelerator)
46 {
47   return (*g_shortcuts.insert(Shortcuts::value_type(name, ShortcutValue(accelerator, false))).first).second.first;
48 }
49
50 void GlobalShortcuts_foreach(CommandVisitor& visitor)
51 {
52   Shortcuts_foreach(g_shortcuts, visitor);
53 }
54
55 void GlobalShortcuts_register(const char* name)
56 {
57   Shortcuts::iterator i = g_shortcuts.find(name);
58   if(i != g_shortcuts.end())
59   {
60     (*i).second.second = true;
61   }
62 }
63
64 void GlobalShortcuts_reportUnregistered()
65 {
66   for(Shortcuts::iterator i = g_shortcuts.begin(); i != g_shortcuts.end(); ++i)
67   {
68     if((*i).second.first.key != 0 && !(*i).second.second)
69     {
70       globalOutputStream() << "shortcut not registered: " << (*i).first.c_str() << "\n";
71     }
72   }
73 }
74
75 typedef std::map<CopiedString, Command> Commands;
76
77 Commands g_commands;
78
79 void GlobalCommands_insert(const char* name, const Callback& callback, const Accelerator& accelerator)
80 {
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));
83 }
84
85 const Command& GlobalCommands_find(const char* command)
86 {
87   Commands::iterator i = g_commands.find(command);
88   ASSERT_MESSAGE(i != g_commands.end(), "failed to lookup command " << makeQuoted(command));
89   return (*i).second;
90 }
91
92 typedef std::map<CopiedString, Toggle> Toggles;
93
94
95 Toggles g_toggles;
96
97 void GlobalToggles_insert(const char* name, const Callback& callback, const BoolExportCallback& exportCallback, const Accelerator& accelerator)
98 {
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));
101 }
102 const Toggle& GlobalToggles_find(const char* name)
103 {
104   Toggles::iterator i = g_toggles.find(name);
105   ASSERT_MESSAGE(i != g_toggles.end(), "failed to lookup toggle " << makeQuoted(name));
106   return (*i).second;
107 }
108
109 typedef std::map<CopiedString, KeyEvent> KeyEvents;
110
111
112 KeyEvents g_keyEvents;
113
114 void GlobalKeyEvents_insert(const char* name, const Accelerator& accelerator, const Callback& keyDown, const Callback& keyUp)
115 {
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));
118 }
119 const KeyEvent& GlobalKeyEvents_find(const char* name)
120 {
121   KeyEvents::iterator i = g_keyEvents.find(name);
122   ASSERT_MESSAGE(i != g_keyEvents.end(), "failed to lookup keyEvent " << makeQuoted(name));
123   return (*i).second;
124 }
125
126
127
128
129 #include <cctype>
130
131 #include <gtk/gtkbox.h>
132 #include <gtk/gtkliststore.h>
133 #include <gtk/gtktreemodel.h>
134 #include <gtk/gtktreeview.h>
135 #include <gtk/gtkcellrenderertext.h>
136
137 #include "gtkutil/dialog.h"
138 #include "mainframe.h"
139
140 #include "stream/textfilestream.h"
141 #include "stream/stringstream.h"
142
143
144 struct command_list_dialog_t : public ModalDialog
145 {
146   command_list_dialog_t()
147     : m_close_button(*this, eIDCANCEL)
148   {
149   }
150   ModalDialogButton m_close_button;
151 };
152
153 void DoCommandListDlg()
154 {
155   command_list_dialog_t dialog;
156
157   GtkWindow* window = create_modal_dialog_window(MainFrame_getWindow(), "Mapped Commands", dialog, -1, 400);
158
159   GtkAccelGroup* accel = gtk_accel_group_new();
160   gtk_window_add_accel_group(window, accel);
161
162   GtkHBox* hbox = create_dialog_hbox(4, 4);
163   gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(hbox));
164
165   {
166     GtkScrolledWindow* scr = create_scrolled_window(GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
167     gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(scr), TRUE, TRUE, 0);
168
169     {
170       GtkListStore* store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_STRING);
171
172       GtkWidget* view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
173
174       {
175         GtkCellRenderer* renderer = gtk_cell_renderer_text_new();
176         GtkTreeViewColumn* column = gtk_tree_view_column_new_with_attributes("Command", renderer, "text", 0, 0);
177         gtk_tree_view_append_column(GTK_TREE_VIEW(view), column);
178       }
179
180       {
181         GtkCellRenderer* renderer = gtk_cell_renderer_text_new();
182         GtkTreeViewColumn* column = gtk_tree_view_column_new_with_attributes("Key", renderer, "text", 1, 0);
183         gtk_tree_view_append_column(GTK_TREE_VIEW(view), column);
184       }
185
186       gtk_widget_show(view);
187       gtk_container_add(GTK_CONTAINER (scr), view);
188
189       {
190         // Initialize dialog
191         StringOutputStream path(256);
192         path << SettingsPath_get() << "commandlist.txt";
193         globalOutputStream() << "Writing the command list to " << path.c_str() << "\n";
194         class BuildCommandList : public CommandVisitor
195         {
196           TextFileOutputStream m_commandList;
197           GtkListStore* m_store;
198         public:
199           BuildCommandList(const char* filename, GtkListStore* store) : m_commandList(filename), m_store(store)
200           {
201           }
202           void visit(const char* name, Accelerator& accelerator)
203           {
204             StringOutputStream modifiers;
205             modifiers << accelerator;
206
207             {
208               GtkTreeIter iter;
209               gtk_list_store_append(m_store, &iter);
210               gtk_list_store_set(m_store, &iter, 0, name, 1, modifiers.c_str(), -1);
211             }
212  
213             if(!m_commandList.failed())
214             {
215               m_commandList << makeLeftJustified(name, 25) << " " << modifiers.c_str() << '\n';
216             }
217           }
218         } visitor(path.c_str(), store);
219
220         GlobalShortcuts_foreach(visitor);
221       }
222     
223       g_object_unref(G_OBJECT(store));
224     }
225   }
226
227   GtkVBox* vbox = create_dialog_vbox(4);
228   gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(vbox), FALSE, FALSE, 0);
229   {
230     GtkButton* button = create_modal_dialog_button("Close", dialog.m_close_button);
231     gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(button), FALSE, FALSE, 0);
232     widget_make_default(GTK_WIDGET(button));
233     gtk_widget_grab_focus(GTK_WIDGET(button));
234     gtk_widget_add_accelerator(GTK_WIDGET(button), "clicked", accel, GDK_Return, (GdkModifierType)0, (GtkAccelFlags)0);
235     gtk_widget_add_accelerator(GTK_WIDGET(button), "clicked", accel, GDK_Escape, (GdkModifierType)0, (GtkAccelFlags)0);
236   }
237
238   modal_dialog_show(window, dialog);
239   gtk_widget_destroy(GTK_WIDGET(window));
240 }
241
242 #include "profile/profile.h"
243
244 const char* const COMMANDS_VERSION = "1.0";
245
246 void SaveCommandMap(const char* path)
247 {
248   StringOutputStream strINI(256);
249   strINI << path << "shortcuts.ini";
250
251   TextFileOutputStream file(strINI.c_str());
252   if(!file.failed())
253   {
254     file << "[Version]\n";
255     file << "number=" << COMMANDS_VERSION << "\n";
256     file << "\n";
257     file << "[Commands]\n";
258     class WriteCommandMap : public CommandVisitor
259     {
260       TextFileOutputStream& m_file;
261     public:
262       WriteCommandMap(TextFileOutputStream& file) : m_file(file)
263       {
264       }
265       void visit(const char* name, Accelerator& accelerator)
266       {
267         m_file << name << "=";
268
269         const char* key = global_keys_find(accelerator.key);
270
271         if(!string_empty(key))
272         {
273           m_file << key;
274         }
275         else if(accelerator.key != 0)
276         {
277           m_file << gdk_keyval_name(accelerator.key);
278         }
279
280         if(accelerator.modifiers & GDK_MOD1_MASK)
281         {
282           m_file << "+Alt";
283         }
284         if(accelerator.modifiers & GDK_CONTROL_MASK)
285         {
286           m_file << "+Ctrl";
287         }
288         if(accelerator.modifiers & GDK_SHIFT_MASK)
289         {
290           m_file << "+Shift";
291         }
292
293         m_file << "\n";
294       }
295     } visitor(file);
296     GlobalShortcuts_foreach(visitor);
297   }
298 }
299
300 const char* stringrange_find(const char* first, const char* last, char c)
301 {
302   const char* p = strchr(first, '+');
303   if(p == 0)
304   {
305     return last;
306   }
307   return p;
308 }
309
310 class ReadCommandMap : public CommandVisitor
311 {
312   const char* m_filename;
313   std::size_t m_count;
314 public:
315   ReadCommandMap(const char* filename) : m_filename(filename), m_count(0)
316   {
317   }
318   void visit(const char* name, Accelerator& accelerator)
319   {
320     char value[1024];
321     if (read_var(m_filename, "Commands", name, value ))
322     {
323       if(string_empty(value))
324       {
325         accelerator.key = 0;
326         accelerator.modifiers = (GdkModifierType)0;
327         return;
328       }
329       int modifiers = 0;
330       const char* last = value + string_length(value);
331       const char* keyEnd = stringrange_find(value, last, '+');
332
333       for(const char* modifier = keyEnd; modifier != last;)
334       {
335         const char* next = stringrange_find(modifier + 1, last, '+');
336         if(next - modifier == 4
337           && string_equal_nocase_n(modifier, "+alt", 4))
338         {
339           modifiers |= GDK_MOD1_MASK;
340         }
341         else if(next - modifier == 5
342           && string_equal_nocase_n(modifier, "+ctrl", 5) != 0)
343         {
344           modifiers |= GDK_CONTROL_MASK;
345         }
346         else if(next - modifier == 6
347           && string_equal_nocase_n(modifier, "+shift", 6) != 0)
348         {
349           modifiers |= GDK_SHIFT_MASK;
350         }
351         else
352         {
353           globalOutputStream() << "WARNING: failed to parse user command " << makeQuoted(value) << ": unknown modifier " << makeQuoted(StringRange(modifier, next)) << "\n";
354         }
355         modifier = next;
356       }
357       accelerator.modifiers = (GdkModifierType)modifiers;
358
359
360       // strBuff has been cleaned of it's modifiers .. switch between a regular key and a virtual one
361       // based on length
362       if(keyEnd - value == 1 && std::isalpha(value[0])) // most often case.. deal with first
363       {
364         accelerator.key = std::toupper(value[0]);
365         ++m_count;
366       }
367       else // special key
368       {
369         CopiedString keyName(StringRange(value, keyEnd));
370         accelerator.key = global_keys_find(keyName.c_str());
371         if(accelerator.key != 0)
372         {
373           ++m_count;
374         }
375         else
376         {
377           globalOutputStream() << "WARNING: failed to parse user command " << makeQuoted(value) << ": unknown key " << makeQuoted(keyName.c_str()) << "\n";
378         }
379       }
380     }
381   }
382   std::size_t count() const
383   {
384     return m_count;
385   }
386 };
387     
388 void LoadCommandMap(const char* path)
389 {
390   StringOutputStream strINI(256);
391   strINI << path << "shortcuts.ini";
392
393   FILE* f = fopen (strINI.c_str(), "r");
394   if (f != 0)
395   {
396     fclose(f);
397     globalOutputStream() << "loading custom shortcuts list from " << makeQuoted(strINI.c_str()) << "\n";
398
399     Version version = version_parse(COMMANDS_VERSION);
400     Version dataVersion = { 0, 0 };
401
402     {
403       char value[1024];
404       if(read_var(strINI.c_str(), "Version", "number", value))
405       {
406         dataVersion = version_parse(value);
407       }
408     }
409
410     if(version_compatible(version, dataVersion))
411     {
412       globalOutputStream() << "commands import: data version " << dataVersion << " is compatible with code version " << version << "\n";
413       ReadCommandMap visitor(strINI.c_str());
414       GlobalShortcuts_foreach(visitor);
415       globalOutputStream() << "parsed " << Unsigned(visitor.count()) << " custom shortcuts\n";
416     }
417     else
418     {
419       globalOutputStream() << "commands import: data version " << dataVersion << " is not compatible with code version " << version << "\n";
420     }
421   }
422   else
423   {
424     globalOutputStream() << "failed to load custom shortcuts from " << makeQuoted(strINI.c_str()) << "\n";
425   }
426 }