]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/commands.cpp
fixed compile error
[xonotic/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
31 typedef std::pair<Accelerator, bool> ShortcutValue; // accelerator, isRegistered
32 typedef std::map<CopiedString, ShortcutValue> Shortcuts;
33
34 void Shortcuts_foreach(Shortcuts& shortcuts, CommandVisitor& visitor)
35 {
36   for(Shortcuts::iterator i = shortcuts.begin(); i != shortcuts.end(); ++i)
37   {
38     visitor.visit((*i).first.c_str(), (*i).second.first);
39   }
40 }
41
42 Shortcuts g_shortcuts;
43
44 const Accelerator& GlobalShortcuts_insert(const char* name, const Accelerator& accelerator)
45 {
46   return (*g_shortcuts.insert(Shortcuts::value_type(name, ShortcutValue(accelerator, false))).first).second.first;
47 }
48
49 void GlobalShortcuts_foreach(CommandVisitor& visitor)
50 {
51   Shortcuts_foreach(g_shortcuts, visitor);
52 }
53
54 void GlobalShortcuts_register(const char* name)
55 {
56   Shortcuts::iterator i = g_shortcuts.find(name);
57   if(i != g_shortcuts.end())
58   {
59     (*i).second.second = true;
60   }
61 }
62
63 void GlobalShortcuts_reportUnregistered()
64 {
65   for(Shortcuts::iterator i = g_shortcuts.begin(); i != g_shortcuts.end(); ++i)
66   {
67     if((*i).second.first.key != 0 && !(*i).second.second)
68     {
69       globalOutputStream() << "shortcut not registered: " << (*i).first.c_str() << "\n";
70     }
71   }
72 }
73
74 typedef std::map<CopiedString, Command> Commands;
75
76 Commands g_commands;
77
78 void GlobalCommands_insert(const char* name, const Callback& callback, const Accelerator& accelerator)
79 {
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));
82 }
83
84 const Command& GlobalCommands_find(const char* command)
85 {
86   Commands::iterator i = g_commands.find(command);
87   ASSERT_MESSAGE(i != g_commands.end(), "failed to lookup command " << makeQuoted(command));
88   return (*i).second;
89 }
90
91 typedef std::map<CopiedString, Toggle> Toggles;
92
93
94 Toggles g_toggles;
95
96 void GlobalToggles_insert(const char* name, const Callback& callback, const BoolExportCallback& exportCallback, const Accelerator& accelerator)
97 {
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));
100 }
101 const Toggle& GlobalToggles_find(const char* name)
102 {
103   Toggles::iterator i = g_toggles.find(name);
104   ASSERT_MESSAGE(i != g_toggles.end(), "failed to lookup toggle " << makeQuoted(name));
105   return (*i).second;
106 }
107
108 typedef std::map<CopiedString, KeyEvent> KeyEvents;
109
110
111 KeyEvents g_keyEvents;
112
113 void GlobalKeyEvents_insert(const char* name, const Accelerator& accelerator, const Callback& keyDown, const Callback& keyUp)
114 {
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));
117 }
118 const KeyEvent& GlobalKeyEvents_find(const char* name)
119 {
120   KeyEvents::iterator i = g_keyEvents.find(name);
121   ASSERT_MESSAGE(i != g_keyEvents.end(), "failed to lookup keyEvent " << makeQuoted(name));
122   return (*i).second;
123 }
124
125
126
127
128 #include <gdk/gdkkeysyms.h>
129 #include <ctype.h>
130
131 #ifdef __APPLE__
132 #define __toascii(c)    ((c) & 0x7f)
133 #endif
134
135 inline char ascii_for_keyval(int keyval)
136 {
137   return __toascii(keyval);
138 }
139
140
141
142 struct SKeyInfo
143 {
144   const char* m_strName;
145   unsigned int m_nVKKey;
146 };
147
148 SKeyInfo g_Keys[] =
149 {
150   {"Space", GDK_space},
151   {"Backspace", GDK_BackSpace},
152   {"Escape", GDK_Escape},
153   {"End", GDK_End},
154   {"Insert", GDK_Insert},
155   {"Delete", GDK_Delete},
156   {"PageUp", GDK_Prior},
157   {"PageDown", GDK_Next},
158   {"Up", GDK_Up},
159   {"Down", GDK_Down},
160   {"Left", GDK_Left},
161   {"Right", GDK_Right},
162   {"F1", GDK_F1},
163   {"F2", GDK_F2},
164   {"F3", GDK_F3},
165   {"F4", GDK_F4},
166   {"F5", GDK_F5},
167   {"F6", GDK_F6},
168   {"F7", GDK_F7},
169   {"F8", GDK_F8},
170   {"F9", GDK_F9},
171   {"F10", GDK_F10},
172   {"F11", GDK_F11},
173   {"F12", GDK_F12},
174   {"Tab", GDK_Tab},
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},
191   {"[", 219},
192   {"]", 221},
193   {"\\", 220},
194   {"Home", GDK_Home}
195 };
196
197 int g_nKeyCount = sizeof(g_Keys) / sizeof(SKeyInfo);
198
199 const char* global_keys_find(unsigned int key)
200 {
201   for(int i = 0; i < g_nKeyCount; ++i)
202   {
203     if(g_Keys[i].m_nVKKey == key)
204     {
205       return g_Keys[i].m_strName;
206     }
207   }
208   return "";
209 }
210
211 unsigned int global_keys_find(const char* name)
212 {
213   for(int i = 0; i < g_nKeyCount; ++i)
214   {
215     if(string_equal_nocase(g_Keys[i].m_strName, name))
216     {
217       return g_Keys[i].m_nVKKey;
218     }
219   }
220   return 0;
221 }
222
223
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>
229
230 #include "gtkutil/dialog.h"
231 #include "mainframe.h"
232
233 #include "stream/textfilestream.h"
234 #include "stream/stringstream.h"
235
236
237 struct command_list_dialog_t : public ModalDialog
238 {
239   command_list_dialog_t()
240     : m_close_button(*this, eIDCANCEL)
241   {
242   }
243   ModalDialogButton m_close_button;
244 };
245
246 template<typename TextOutputStreamType>
247 TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const Accelerator& accelerator)
248 {
249   if(accelerator.modifiers & GDK_SHIFT_MASK)
250   {
251     ostream << "Shift + ";
252   }
253   if(accelerator.modifiers & GDK_MOD1_MASK)
254   {
255     ostream << "Alt + ";
256   }
257   if(accelerator.modifiers & GDK_CONTROL_MASK)
258   {
259     ostream << "Control + ";
260   }
261
262   const char* keyName = global_keys_find(accelerator.key);
263   if(!string_empty(keyName))
264   {
265     ostream << keyName;
266   }
267   else
268   {
269     ostream << static_cast<char>(accelerator.key);
270   }
271
272   return ostream;
273 }
274
275 void DoCommandListDlg()
276 {
277   command_list_dialog_t dialog;
278
279   GtkWindow* window = create_modal_dialog_window(MainFrame_getWindow(), "Mapped Commands", dialog, -1, 400);
280
281   GtkAccelGroup* accel = gtk_accel_group_new();
282   gtk_window_add_accel_group(window, accel);
283
284   GtkHBox* hbox = create_dialog_hbox(4, 4);
285   gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(hbox));
286
287   {
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);
290
291     {
292       GtkListStore* store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_STRING);
293
294       GtkWidget* view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
295
296       {
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);
300       }
301
302       {
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);
306       }
307
308       gtk_widget_show(view);
309       gtk_container_add(GTK_CONTAINER (scr), view);
310
311       {
312         // Initialize dialog
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
317         {
318           TextFileOutputStream m_commandList;
319           GtkListStore* m_store;
320         public:
321           BuildCommandList(const char* filename, GtkListStore* store) : m_commandList(filename), m_store(store)
322           {
323           }
324           void visit(const char* name, Accelerator& accelerator)
325           {
326             StringOutputStream modifiers;
327             modifiers << accelerator;
328
329             {
330               GtkTreeIter iter;
331               gtk_list_store_append(m_store, &iter);
332               gtk_list_store_set(m_store, &iter, 0, name, 1, modifiers.c_str(), -1);
333             }
334  
335             if(!m_commandList.failed())
336             {
337               m_commandList << makeLeftJustified(name, 25) << " " << modifiers.c_str() << '\n';
338             }
339           }
340         } visitor(path.c_str(), store);
341
342         GlobalShortcuts_foreach(visitor);
343       }
344     
345       g_object_unref(G_OBJECT(store));
346     }
347   }
348
349   GtkVBox* vbox = create_dialog_vbox(4);
350   gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(vbox), FALSE, FALSE, 0);
351   {
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);
358   }
359
360   modal_dialog_show(window, dialog);
361   gtk_widget_destroy(GTK_WIDGET(window));
362 }
363
364 #include "profile/profile.h"
365
366 const char* const COMMANDS_VERSION = "1.0";
367
368 void SaveCommandMap(const char* path)
369 {
370   StringOutputStream strINI(256);
371   strINI << path << "shortcuts.ini";
372
373   TextFileOutputStream file(strINI.c_str());
374   if(!file.failed())
375   {
376     file << "[Version]\n";
377     file << "number=" << COMMANDS_VERSION << "\n";
378     file << "\n";
379     file << "[Commands]\n";
380     class WriteCommandMap : public CommandVisitor
381     {
382       TextFileOutputStream& m_file;
383     public:
384       WriteCommandMap(TextFileOutputStream& file) : m_file(file)
385       {
386       }
387       void visit(const char* name, Accelerator& accelerator)
388       {
389         m_file << name << "=";
390
391         const char* key = global_keys_find(accelerator.key);
392         if(!string_empty(key))
393         {
394           m_file << key;
395         }
396         else if(accelerator.key != 0)
397         {
398           m_file << ascii_for_keyval(accelerator.key);
399         }
400
401         if(accelerator.modifiers & GDK_MOD1_MASK)
402         {
403           m_file << "+Alt";
404         }
405         if(accelerator.modifiers & GDK_CONTROL_MASK)
406         {
407           m_file << "+Ctrl";
408         }
409         if(accelerator.modifiers & GDK_SHIFT_MASK)
410         {
411           m_file << "+Shift";
412         }
413
414         m_file << "\n";
415       }
416     } visitor(file);
417     GlobalShortcuts_foreach(visitor);
418   }
419 }
420
421 const char* stringrange_find(const char* first, const char* last, char c)
422 {
423   const char* p = strchr(first, '+');
424   if(p == 0)
425   {
426     return last;
427   }
428   return p;
429 }
430
431 class ReadCommandMap : public CommandVisitor
432 {
433   const char* m_filename;
434   std::size_t m_count;
435 public:
436   ReadCommandMap(const char* filename) : m_filename(filename), m_count(0)
437   {
438   }
439   void visit(const char* name, Accelerator& accelerator)
440   {
441     char value[1024];
442     if (read_var(m_filename, "Commands", name, value ))
443     {
444       if(string_empty(value))
445       {
446         accelerator.key = 0;
447         accelerator.modifiers = (GdkModifierType)0;
448         return;
449       }
450       int modifiers = 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;)
454       {
455         const char* next = stringrange_find(modifier + 1, last, '+');
456         if(next - modifier == 4
457           && string_equal_nocase_n(modifier, "+alt", 4))
458         {
459           modifiers |= GDK_MOD1_MASK;
460         }
461         else if(next - modifier == 5
462           && string_equal_nocase_n(modifier, "+ctrl", 5) != 0)
463         {
464           modifiers |= GDK_CONTROL_MASK;
465         }
466         else if(next - modifier == 6
467           && string_equal_nocase_n(modifier, "+shift", 6) != 0)
468         {
469           modifiers |= GDK_SHIFT_MASK;
470         }
471         else
472         {
473           globalOutputStream() << "WARNING: failed to parse user command " << makeQuoted(value) << ": unknown modifier " << makeQuoted(StringRange(modifier, next)) << "\n";
474         }
475         modifier = next;
476       }
477       accelerator.modifiers = (GdkModifierType)modifiers;
478
479
480       // strBuff has been cleaned of it's modifiers .. switch between a regular key and a virtual one
481       // based on length
482       if(keyEnd - value == 1) // most often case.. deal with first
483       {
484         accelerator.key = std::toupper(value[0]);
485         ++m_count;
486       }
487       else // special key
488       {
489         CopiedString keyName(StringRange(value, keyEnd));
490         accelerator.key = global_keys_find(keyName.c_str());
491         if(accelerator.key != 0)
492         {
493           ++m_count;
494         }
495         else
496         {
497           globalOutputStream() << "WARNING: failed to parse user command " << makeQuoted(value) << ": unknown key " << makeQuoted(keyName.c_str()) << "\n";
498         }
499       }
500     }
501   }
502   std::size_t count() const
503   {
504     return m_count;
505   }
506 };
507     
508 void LoadCommandMap(const char* path)
509 {
510   StringOutputStream strINI(256);
511   strINI << path << "shortcuts.ini";
512
513   FILE* f = fopen (strINI.c_str(), "r");
514   if (f != 0)
515   {
516     fclose(f);
517     globalOutputStream() << "loading custom shortcuts list from " << makeQuoted(strINI.c_str()) << "\n";
518
519     Version version = version_parse(COMMANDS_VERSION);
520     Version dataVersion = { 0, 0 };
521
522     {
523       char value[1024];
524       if(read_var(strINI.c_str(), "Version", "number", value))
525       {
526         dataVersion = version_parse(value);
527       }
528     }
529
530     if(version_compatible(version, dataVersion))
531     {
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";
536     }
537     else
538     {
539       globalOutputStream() << "commands import: data version " << dataVersion << " is not compatible with code version " << version << "\n";
540     }
541   }
542   else
543   {
544     globalOutputStream() << "failed to load custom shortcuts from " << makeQuoted(strINI.c_str()) << "\n";
545   }
546 }