ported bobtoolz
[xonotic/netradiant.git] / libs / gtkutil / accelerator.cpp
index 6a63fddcb4711c44eab4d93dc6fd883e00393dde..778da6f433931e019c13633b181ff1d2b49011a1 100644 (file)
@@ -30,30 +30,145 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
 #include "generic/callback.h"
 #include "generic/bitfield.h"
+#include "string/string.h"
 
 #include "pointer.h"
 #include "closure.h"
 
+#include <gdk/gdkkeysyms.h>
 
 
+
+struct SKeyInfo
+{
+  const char* m_strName;
+  unsigned int m_nVKKey;
+};
+
+SKeyInfo g_Keys[] =
+{
+  {"Space", GDK_space},
+  {"Backspace", GDK_BackSpace},
+  {"Escape", GDK_Escape},
+  {"End", GDK_End},
+  {"Insert", GDK_Insert},
+  {"Delete", GDK_Delete},
+  {"PageUp", GDK_Prior},
+  {"PageDown", GDK_Next},
+  {"Up", GDK_Up},
+  {"Down", GDK_Down},
+  {"Left", GDK_Left},
+  {"Right", GDK_Right},
+  {"F1", GDK_F1},
+  {"F2", GDK_F2},
+  {"F3", GDK_F3},
+  {"F4", GDK_F4},
+  {"F5", GDK_F5},
+  {"F6", GDK_F6},
+  {"F7", GDK_F7},
+  {"F8", GDK_F8},
+  {"F9", GDK_F9},
+  {"F10", GDK_F10},
+  {"F11", GDK_F11},
+  {"F12", GDK_F12},
+  {"Tab", GDK_Tab},
+  {"Return", GDK_Return},                           
+  {"Comma", GDK_comma},
+  {"Period", GDK_period},
+  {"Plus", GDK_KP_Add},
+  {"Multiply", GDK_multiply},
+  {"Minus", GDK_KP_Subtract},
+  {"NumPad0", GDK_KP_0},
+  {"NumPad1", GDK_KP_1},
+  {"NumPad2", GDK_KP_2},
+  {"NumPad3", GDK_KP_3},
+  {"NumPad4", GDK_KP_4},
+  {"NumPad5", GDK_KP_5},
+  {"NumPad6", GDK_KP_6},
+  {"NumPad7", GDK_KP_7},
+  {"NumPad8", GDK_KP_8},
+  {"NumPad9", GDK_KP_9},
+  {"[", 219},
+  {"]", 221},
+  {"\\", 220},
+  {"Home", GDK_Home}
+};
+
+int g_nKeyCount = sizeof(g_Keys) / sizeof(SKeyInfo);
+
+const char* global_keys_find(unsigned int key)
+{
+  for(int i = 0; i < g_nKeyCount; ++i)
+  {
+    if(g_Keys[i].m_nVKKey == key)
+    {
+      return g_Keys[i].m_strName;
+    }
+  }
+  return "";
+}
+
+unsigned int global_keys_find(const char* name)
+{
+  for(int i = 0; i < g_nKeyCount; ++i)
+  {
+    if(string_equal_nocase(g_Keys[i].m_strName, name))
+    {
+      return g_Keys[i].m_nVKKey;
+    }
+  }
+  return 0;
+}
+
+void accelerator_write(const Accelerator& accelerator, TextOutputStream& ostream)
+{
+  if(accelerator.modifiers & GDK_SHIFT_MASK)
+  {
+    ostream << "Shift + ";
+  }
+  if(accelerator.modifiers & GDK_MOD1_MASK)
+  {
+    ostream << "Alt + ";
+  }
+  if(accelerator.modifiers & GDK_CONTROL_MASK)
+  {
+    ostream << "Control + ";
+  }
+
+  const char* keyName = global_keys_find(accelerator.key);
+  if(!string_empty(keyName))
+  {
+    ostream << keyName;
+  }
+  else
+  {
+    ostream << static_cast<char>(accelerator.key);
+  }
+}
+
 typedef std::map<Accelerator, Callback> AcceleratorMap;
 
-void accelerator_map_insert(AcceleratorMap& acceleratorMap, Accelerator accelerator, const Callback& callback)
+bool accelerator_map_insert(AcceleratorMap& acceleratorMap, Accelerator accelerator, const Callback& callback)
 {
   if(accelerator.key != 0)
   {
-    ASSERT_MESSAGE(acceleratorMap.find(accelerator) == acceleratorMap.end(), "failed to add accelerator");
-    acceleratorMap.insert(AcceleratorMap::value_type(accelerator, callback));
+    return acceleratorMap.insert(AcceleratorMap::value_type(accelerator, callback)).second;
   }
+  return true;
 }
 
-void accelerator_map_erase(AcceleratorMap& acceleratorMap, Accelerator accelerator)
+bool accelerator_map_erase(AcceleratorMap& acceleratorMap, Accelerator accelerator)
 {
   if(accelerator.key != 0)
   {
-    ASSERT_MESSAGE(acceleratorMap.find(accelerator) != acceleratorMap.end(), "failed to remove accelerator");
-    acceleratorMap.erase(accelerator);
+    AcceleratorMap::iterator i = acceleratorMap.find(accelerator);
+    if(i == acceleratorMap.end())
+    {
+      return false;
+    }
+    acceleratorMap.erase(i);
   }
+  return true;
 }
 
 Accelerator accelerator_for_event_key(guint keyval, guint state)
@@ -381,29 +496,47 @@ void GlobalPressedKeys_disconnect(GtkWindow* window)
 
 void special_accelerators_add(Accelerator accelerator, const Callback& callback)
 {
-  accelerator_map_insert(g_special_accelerators, accelerator, callback);
+  if(!accelerator_map_insert(g_special_accelerators, accelerator, callback))
+  {
+    globalErrorStream() << "special_accelerators_add: already exists: " << makeQuoted(accelerator);
+  }
 }
 void special_accelerators_remove(Accelerator accelerator)
 {
-  accelerator_map_erase(g_special_accelerators, accelerator);
+  if(!accelerator_map_erase(g_special_accelerators, accelerator))
+  {
+    globalErrorStream() << "special_accelerators_remove: not found: " << makeQuoted(accelerator);
+  }
 }
 
 void keydown_accelerators_add(Accelerator accelerator, const Callback& callback)
 {
-  accelerator_map_insert(g_keydown_accelerators, accelerator, callback);
+  if(!accelerator_map_insert(g_keydown_accelerators, accelerator, callback))
+  {
+    globalErrorStream() << "keydown_accelerators_add: already exists: " << makeQuoted(accelerator);
+  }
 }
 void keydown_accelerators_remove(Accelerator accelerator)
 {
-  accelerator_map_erase(g_keydown_accelerators, accelerator);
+  if(!accelerator_map_erase(g_keydown_accelerators, accelerator))
+  {
+    globalErrorStream() << "keydown_accelerators_remove: not found: " << makeQuoted(accelerator);
+  }
 }
 
 void keyup_accelerators_add(Accelerator accelerator, const Callback& callback)
 {
-  accelerator_map_insert(g_keyup_accelerators, accelerator, callback);
+  if(!accelerator_map_insert(g_keyup_accelerators, accelerator, callback))
+  {
+    globalErrorStream() << "keyup_accelerators_add: already exists: " << makeQuoted(accelerator);
+  }
 }
 void keyup_accelerators_remove(Accelerator accelerator)
 {
-  accelerator_map_erase(g_keyup_accelerators, accelerator);
+  if(!accelerator_map_erase(g_keyup_accelerators, accelerator))
+  {
+    globalErrorStream() << "keyup_accelerators_remove: not found: " << makeQuoted(accelerator);
+  }
 }