new toys :P
[divverent/netradiant.git] / libs / gtkutil / accelerator.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 "accelerator.h"
23
24 #include "debugging/debugging.h"
25
26 #include <map>
27 #include <set>
28 #include <gtk/gtkwindow.h>
29 #include <gtk/gtkaccelgroup.h>
30
31 #include "generic/callback.h"
32 #include "generic/bitfield.h"
33 #include "string/string.h"
34
35 #include "pointer.h"
36 #include "closure.h"
37
38 #include <gdk/gdkkeysyms.h>
39
40
41
42 const char* global_keys_find(unsigned int key)
43 {
44   const char *s;
45   if(key == 0)
46     return "";
47   s = gdk_keyval_name(key);
48   if(!s)
49         return "";
50   return s;
51 }
52
53 unsigned int global_keys_find(const char* name)
54 {
55   guint k;
56   if(!name || !*name)
57     return 0;
58   k = gdk_keyval_from_name(name);
59   if(k == GDK_VoidSymbol)
60         return 0;
61   return k;
62 }
63
64 void accelerator_write(const Accelerator& accelerator, TextOutputStream& ostream)
65 {
66   if(accelerator.modifiers & GDK_SHIFT_MASK)
67   {
68     ostream << "Shift + ";
69   }
70   if(accelerator.modifiers & GDK_MOD1_MASK)
71   {
72     ostream << "Alt + ";
73   }
74   if(accelerator.modifiers & GDK_CONTROL_MASK)
75   {
76     ostream << "Control + ";
77   }
78
79   const char* keyName = global_keys_find(accelerator.key);
80   if(!string_empty(keyName))
81   {
82     ostream << keyName;
83   }
84   else
85   {
86     ostream << static_cast<char>(accelerator.key);
87   }
88 }
89
90 typedef std::map<Accelerator, Callback> AcceleratorMap;
91 typedef std::set<Accelerator> AcceleratorSet;
92
93 bool accelerator_map_insert(AcceleratorMap& acceleratorMap, Accelerator accelerator, const Callback& callback)
94 {
95   if(accelerator.key != 0)
96   {
97     return acceleratorMap.insert(AcceleratorMap::value_type(accelerator, callback)).second;
98   }
99   return true;
100 }
101
102 bool accelerator_map_erase(AcceleratorMap& acceleratorMap, Accelerator accelerator)
103 {
104   if(accelerator.key != 0)
105   {
106     AcceleratorMap::iterator i = acceleratorMap.find(accelerator);
107     if(i == acceleratorMap.end())
108     {
109       return false;
110     }
111     acceleratorMap.erase(i);
112   }
113   return true;
114 }
115
116 Accelerator accelerator_for_event_key(guint keyval, guint state)
117 {
118   keyval = gdk_keyval_to_upper(keyval);
119   if(keyval == GDK_ISO_Left_Tab)
120     keyval = GDK_Tab;
121   return Accelerator(keyval, (GdkModifierType)(state & gtk_accelerator_get_default_mod_mask()));
122 }
123
124 bool AcceleratorMap_activate(const AcceleratorMap& acceleratorMap, const Accelerator& accelerator)
125 {
126   AcceleratorMap::const_iterator i = acceleratorMap.find(accelerator);
127   if(i != acceleratorMap.end())
128   {
129     (*i).second();
130     return true;
131   }
132
133   return false;
134 }
135
136 static gboolean accelerator_key_event(GtkWindow* window, GdkEventKey* event, AcceleratorMap* acceleratorMap)
137 {
138   return AcceleratorMap_activate(*acceleratorMap, accelerator_for_event_key(event->keyval, event->state));
139 }
140
141
142 AcceleratorMap g_special_accelerators;
143
144
145 namespace MouseButton
146 {
147   enum 
148   {
149     Left = 1 << 0,
150     Right = 1 << 1,
151     Middle = 1 << 2,
152   };
153 }
154
155 typedef unsigned int ButtonMask;
156
157 void print_buttons(ButtonMask mask)
158 {
159   globalOutputStream() << "button state: ";
160   if((mask & MouseButton::Left) != 0)
161   {
162     globalOutputStream() << "Left ";
163   }
164   if((mask & MouseButton::Right) != 0)
165   {
166     globalOutputStream() << "Right ";
167   }
168   if((mask & MouseButton::Middle) != 0)
169   {
170     globalOutputStream() << "Middle ";
171   }
172   globalOutputStream() << "\n";
173 }
174
175 ButtonMask ButtonMask_for_event_button(guint button)
176 {
177   switch(button)
178   {
179   case 1:
180     return MouseButton::Left;
181   case 2:
182     return MouseButton::Middle;
183   case 3:
184     return MouseButton::Right;
185   }
186   return 0;
187 }
188
189 bool window_has_accel(GtkWindow* toplevel)
190 {
191   return g_slist_length(gtk_accel_groups_from_object(G_OBJECT(toplevel))) != 0;
192 }
193
194 namespace
195 {
196   bool g_accel_enabled = true;
197 }
198
199 bool global_accel_enabled()
200 {
201   return g_accel_enabled;
202 }
203
204
205 GClosure* accel_group_add_accelerator(GtkAccelGroup* group, Accelerator accelerator, const Callback& callback);
206 void accel_group_remove_accelerator(GtkAccelGroup* group, Accelerator accelerator);
207
208 AcceleratorMap g_queuedAcceleratorsAdd;
209 AcceleratorSet g_queuedAcceleratorsRemove;
210
211 void globalQueuedAccelerators_add(Accelerator accelerator, const Callback& callback)
212 {
213   if(!g_queuedAcceleratorsAdd.insert(AcceleratorMap::value_type(accelerator, callback)).second)
214   {
215     globalErrorStream() << "globalQueuedAccelerators_add: accelerator already queued: " << accelerator << "\n";
216   }
217 }
218
219 void globalQueuedAccelerators_remove(Accelerator accelerator)
220 {
221   if(g_queuedAcceleratorsAdd.erase(accelerator) == 0)
222   {
223     if(!g_queuedAcceleratorsRemove.insert(accelerator).second)
224     {
225       globalErrorStream() << "globalQueuedAccelerators_remove: accelerator already queued: " << accelerator << "\n";
226     }
227   }
228 }
229
230 void globalQueuedAccelerators_commit()
231 {
232   for(AcceleratorSet::const_iterator i = g_queuedAcceleratorsRemove.begin(); i != g_queuedAcceleratorsRemove.end(); ++i)
233   {
234     //globalOutputStream() << "removing: " << (*i).first << "\n";
235     accel_group_remove_accelerator(global_accel, *i);
236   }
237   g_queuedAcceleratorsRemove.clear();
238   for(AcceleratorMap::const_iterator i = g_queuedAcceleratorsAdd.begin(); i != g_queuedAcceleratorsAdd.end(); ++i)
239   {
240     //globalOutputStream() << "adding: " << (*i).first << "\n";
241     accel_group_add_accelerator(global_accel, (*i).first, (*i).second);
242   }
243   g_queuedAcceleratorsAdd.clear();
244 }
245
246 void accel_group_test(GtkWindow* toplevel, GtkAccelGroup* accel)
247 {
248   guint n_entries;
249   gtk_accel_group_query(accel, '4', (GdkModifierType)0, &n_entries);
250   globalOutputStream() << "grid4: " << n_entries << "\n";
251   globalOutputStream() << "toplevel accelgroups: " << g_slist_length(gtk_accel_groups_from_object(G_OBJECT(toplevel))) << "\n";
252 }
253
254 typedef std::set<GtkWindow*> WindowSet;
255 WindowSet g_accel_windows;
256
257 bool Buttons_press(ButtonMask& buttons, guint button, guint state)
258 {
259   if(buttons == 0 && bitfield_enable(buttons, ButtonMask_for_event_button(button)) != 0)
260   {
261     ASSERT_MESSAGE(g_accel_enabled, "Buttons_press: accelerators not enabled");
262     g_accel_enabled = false;
263     for(WindowSet::iterator i = g_accel_windows.begin(); i != g_accel_windows.end(); ++i)
264     {
265       GtkWindow* toplevel = *i;
266       ASSERT_MESSAGE(window_has_accel(toplevel), "ERROR");
267       ASSERT_MESSAGE(GTK_WIDGET_TOPLEVEL(toplevel), "disabling accel for non-toplevel window");
268       gtk_window_remove_accel_group(toplevel,  global_accel);
269 #if 0
270       globalOutputStream() << reinterpret_cast<unsigned int>(toplevel) << ": disabled global accelerators\n";
271 #endif
272 #if 0
273       accel_group_test(toplevel, global_accel);
274 #endif
275     }
276   }
277   buttons = bitfield_enable(buttons, ButtonMask_for_event_button(button));
278 #if 0
279   globalOutputStream() << "Buttons_press: ";
280   print_buttons(buttons);
281 #endif
282   return false;
283 }
284
285 bool Buttons_release(ButtonMask& buttons, guint button, guint state)
286 {
287   if(buttons != 0 && bitfield_disable(buttons, ButtonMask_for_event_button(button)) == 0)
288   {
289     ASSERT_MESSAGE(!g_accel_enabled, "Buttons_release: accelerators are enabled");
290     g_accel_enabled = true;
291     for(WindowSet::iterator i = g_accel_windows.begin(); i != g_accel_windows.end(); ++i)
292     {
293       GtkWindow* toplevel = *i;
294       ASSERT_MESSAGE(!window_has_accel(toplevel), "ERROR");
295       ASSERT_MESSAGE(GTK_WIDGET_TOPLEVEL(toplevel), "enabling accel for non-toplevel window");
296       gtk_window_add_accel_group(toplevel, global_accel);
297 #if 0
298       globalOutputStream() << reinterpret_cast<unsigned int>(toplevel) << ": enabled global accelerators\n";
299 #endif
300 #if 0
301       accel_group_test(toplevel, global_accel);
302 #endif
303     }
304     globalQueuedAccelerators_commit();
305   }
306   buttons = bitfield_disable(buttons, ButtonMask_for_event_button(button));
307 #if 0
308   globalOutputStream() << "Buttons_release: ";
309   print_buttons(buttons);
310 #endif
311   return false;
312 }
313
314 bool Buttons_releaseAll(ButtonMask& buttons)
315 {
316   Buttons_release(buttons, MouseButton::Left | MouseButton::Middle | MouseButton::Right, 0);
317   return false;
318 }
319
320 struct PressedButtons
321 {
322   ButtonMask buttons;
323
324   PressedButtons() : buttons(0)
325   {
326   }
327 };
328
329 gboolean PressedButtons_button_press(GtkWidget* widget, GdkEventButton* event, PressedButtons* pressed)
330 {
331   if(event->type == GDK_BUTTON_PRESS)
332   {
333     return Buttons_press(pressed->buttons, event->button, event->state);
334   }
335   return FALSE;
336 }
337
338 gboolean PressedButtons_button_release(GtkWidget* widget, GdkEventButton* event, PressedButtons* pressed)
339 {
340   if(event->type == GDK_BUTTON_RELEASE)
341   {
342     return Buttons_release(pressed->buttons, event->button, event->state);
343   }
344   return FALSE;
345 }
346
347 gboolean PressedButtons_focus_out(GtkWidget* widget, GdkEventFocus* event, PressedButtons* pressed)
348 {
349   Buttons_releaseAll(pressed->buttons);
350   return FALSE;
351 }
352
353 void PressedButtons_connect(PressedButtons& pressedButtons, GtkWidget* widget)
354 {
355   g_signal_connect(G_OBJECT(widget), "button_press_event", G_CALLBACK(PressedButtons_button_press), &pressedButtons);
356   g_signal_connect(G_OBJECT(widget), "button_release_event", G_CALLBACK(PressedButtons_button_release), &pressedButtons);
357   g_signal_connect(G_OBJECT(widget), "focus_out_event", G_CALLBACK(PressedButtons_focus_out), &pressedButtons);
358 }
359
360 PressedButtons g_pressedButtons;
361
362
363 #include <set>
364
365 struct PressedKeys
366 {
367   typedef std::set<guint> Keys;
368   Keys keys;
369   std::size_t refcount;
370
371   PressedKeys() : refcount(0)
372   {
373   }
374 };
375
376 AcceleratorMap g_keydown_accelerators;
377 AcceleratorMap g_keyup_accelerators;
378
379 bool Keys_press(PressedKeys::Keys& keys, guint keyval)
380 {
381   if(keys.insert(keyval).second)
382   {
383     return AcceleratorMap_activate(g_keydown_accelerators, accelerator_for_event_key(keyval, 0));
384   }
385   return g_keydown_accelerators.find(accelerator_for_event_key(keyval, 0)) != g_keydown_accelerators.end();
386 }
387
388 bool Keys_release(PressedKeys::Keys& keys, guint keyval)
389 {
390   if(keys.erase(keyval) != 0)
391   {
392     return AcceleratorMap_activate(g_keyup_accelerators, accelerator_for_event_key(keyval, 0));
393   }
394   return g_keyup_accelerators.find(accelerator_for_event_key(keyval, 0)) != g_keyup_accelerators.end();
395 }
396
397 void Keys_releaseAll(PressedKeys::Keys& keys, guint state)
398 {
399   for(PressedKeys::Keys::iterator i = keys.begin(); i != keys.end(); ++i)
400   {
401     AcceleratorMap_activate(g_keyup_accelerators, accelerator_for_event_key(*i, state));
402   }
403   keys.clear();
404 }
405
406 gboolean PressedKeys_key_press(GtkWidget* widget, GdkEventKey* event, PressedKeys* pressedKeys)
407 {
408   //globalOutputStream() << "pressed: " << event->keyval << "\n";
409   return event->state == 0 && Keys_press(pressedKeys->keys, event->keyval);
410 }
411
412 gboolean PressedKeys_key_release(GtkWidget* widget, GdkEventKey* event, PressedKeys* pressedKeys)
413 {
414   //globalOutputStream() << "released: " << event->keyval << "\n";
415   return Keys_release(pressedKeys->keys, event->keyval);
416 }
417
418 gboolean PressedKeys_focus_in(GtkWidget* widget, GdkEventFocus* event, PressedKeys* pressedKeys)
419 {
420   ++pressedKeys->refcount;
421   return FALSE;
422 }
423
424 gboolean PressedKeys_focus_out(GtkWidget* widget, GdkEventFocus* event, PressedKeys* pressedKeys)
425 {
426   if(--pressedKeys->refcount == 0)
427   {
428     Keys_releaseAll(pressedKeys->keys, 0);
429   }
430   return FALSE;
431 }
432
433 PressedKeys g_pressedKeys;
434
435 void GlobalPressedKeys_releaseAll()
436 {
437   Keys_releaseAll(g_pressedKeys.keys, 0);
438 }
439
440 void GlobalPressedKeys_connect(GtkWindow* window)
441 {
442   unsigned int key_press_handler = g_signal_connect(G_OBJECT(window), "key_press_event", G_CALLBACK(PressedKeys_key_press), &g_pressedKeys);
443   unsigned int key_release_handler = g_signal_connect(G_OBJECT(window), "key_release_event", G_CALLBACK(PressedKeys_key_release), &g_pressedKeys);
444   g_object_set_data(G_OBJECT(window), "key_press_handler", gint_to_pointer(key_press_handler));
445   g_object_set_data(G_OBJECT(window), "key_release_handler", gint_to_pointer(key_release_handler));
446   unsigned int focus_in_handler = g_signal_connect(G_OBJECT(window), "focus_in_event", G_CALLBACK(PressedKeys_focus_in), &g_pressedKeys);
447   unsigned int focus_out_handler = g_signal_connect(G_OBJECT(window), "focus_out_event", G_CALLBACK(PressedKeys_focus_out), &g_pressedKeys);
448   g_object_set_data(G_OBJECT(window), "focus_in_handler", gint_to_pointer(focus_in_handler));
449   g_object_set_data(G_OBJECT(window), "focus_out_handler", gint_to_pointer(focus_out_handler));
450 }
451
452 void GlobalPressedKeys_disconnect(GtkWindow* window)
453 {
454   g_signal_handler_disconnect(G_OBJECT(window), gpointer_to_int(g_object_get_data(G_OBJECT(window), "key_press_handler")));
455   g_signal_handler_disconnect(G_OBJECT(window), gpointer_to_int(g_object_get_data(G_OBJECT(window), "key_release_handler")));
456   g_signal_handler_disconnect(G_OBJECT(window), gpointer_to_int(g_object_get_data(G_OBJECT(window), "focus_in_handler")));
457   g_signal_handler_disconnect(G_OBJECT(window), gpointer_to_int(g_object_get_data(G_OBJECT(window), "focus_out_handler")));
458 }
459
460
461
462 void special_accelerators_add(Accelerator accelerator, const Callback& callback)
463 {
464   //globalOutputStream() << "special_accelerators_add: " << makeQuoted(accelerator) << "\n";
465   if(!accelerator_map_insert(g_special_accelerators, accelerator, callback))
466   {
467     globalErrorStream() << "special_accelerators_add: already exists: " << makeQuoted(accelerator) << "\n";
468   }
469 }
470 void special_accelerators_remove(Accelerator accelerator)
471 {
472   //globalOutputStream() << "special_accelerators_remove: " << makeQuoted(accelerator) << "\n";
473   if(!accelerator_map_erase(g_special_accelerators, accelerator))
474   {
475     globalErrorStream() << "special_accelerators_remove: not found: " << makeQuoted(accelerator) << "\n";
476   }
477 }
478
479 void keydown_accelerators_add(Accelerator accelerator, const Callback& callback)
480 {
481   //globalOutputStream() << "keydown_accelerators_add: " << makeQuoted(accelerator) << "\n";
482   if(!accelerator_map_insert(g_keydown_accelerators, accelerator, callback))
483   {
484     globalErrorStream() << "keydown_accelerators_add: already exists: " << makeQuoted(accelerator) << "\n";
485   }
486 }
487 void keydown_accelerators_remove(Accelerator accelerator)
488 {
489   //globalOutputStream() << "keydown_accelerators_remove: " << makeQuoted(accelerator) << "\n";
490   if(!accelerator_map_erase(g_keydown_accelerators, accelerator))
491   {
492     globalErrorStream() << "keydown_accelerators_remove: not found: " << makeQuoted(accelerator) << "\n";
493   }
494 }
495
496 void keyup_accelerators_add(Accelerator accelerator, const Callback& callback)
497 {
498   //globalOutputStream() << "keyup_accelerators_add: " << makeQuoted(accelerator) << "\n";
499   if(!accelerator_map_insert(g_keyup_accelerators, accelerator, callback))
500   {
501     globalErrorStream() << "keyup_accelerators_add: already exists: " << makeQuoted(accelerator) << "\n";
502   }
503 }
504 void keyup_accelerators_remove(Accelerator accelerator)
505 {
506   //globalOutputStream() << "keyup_accelerators_remove: " << makeQuoted(accelerator) << "\n";
507   if(!accelerator_map_erase(g_keyup_accelerators, accelerator))
508   {
509     globalErrorStream() << "keyup_accelerators_remove: not found: " << makeQuoted(accelerator) << "\n";
510   }
511 }
512
513
514 gboolean accel_closure_callback(GtkAccelGroup* group, GtkWidget* widget, guint key, GdkModifierType modifiers, gpointer data)
515 {
516   (*reinterpret_cast<Callback*>(data))();
517   return TRUE;
518 }
519
520 GClosure* accel_group_add_accelerator(GtkAccelGroup* group, Accelerator accelerator, const Callback& callback)
521 {
522   if(accelerator.key != 0 && gtk_accelerator_valid(accelerator.key, accelerator.modifiers))
523   {
524     //globalOutputStream() << "global_accel_connect: " << makeQuoted(accelerator) << "\n";
525     GClosure* closure = create_cclosure(G_CALLBACK(accel_closure_callback), callback);
526     gtk_accel_group_connect(group, accelerator.key, accelerator.modifiers, GTK_ACCEL_VISIBLE, closure);
527     return closure;
528   }
529   else
530   {
531     special_accelerators_add(accelerator, callback);
532     return 0;
533   }
534 }
535
536 void accel_group_remove_accelerator(GtkAccelGroup* group, Accelerator accelerator)
537 {
538   if(accelerator.key != 0 && gtk_accelerator_valid(accelerator.key, accelerator.modifiers))
539   {
540     //globalOutputStream() << "global_accel_disconnect: " << makeQuoted(accelerator) << "\n";
541     gtk_accel_group_disconnect_key(group, accelerator.key, accelerator.modifiers);
542   }
543   else
544   {
545     special_accelerators_remove(accelerator);
546   }
547 }
548
549 GtkAccelGroup* global_accel = 0;
550
551 void global_accel_init()
552 {
553   global_accel = gtk_accel_group_new();
554 }
555
556 void global_accel_destroy()
557 {
558   g_object_unref(global_accel);
559 }
560
561 GClosure* global_accel_group_add_accelerator(Accelerator accelerator, const Callback& callback)
562 {
563   if(!global_accel_enabled())
564   {
565     // workaround: cannot add to GtkAccelGroup while it is disabled
566     //globalOutputStream() << "queued for add: " << accelerator << "\n";
567     globalQueuedAccelerators_add(accelerator, callback);
568     return 0;
569   }
570   return accel_group_add_accelerator(global_accel, accelerator, callback);
571 }
572 void global_accel_group_remove_accelerator(Accelerator accelerator)
573 {
574   if(!global_accel_enabled())
575   {
576     //globalOutputStream() << "queued for remove: " << accelerator << "\n";
577     globalQueuedAccelerators_remove(accelerator);
578     return;
579   }
580   accel_group_remove_accelerator(global_accel, accelerator);
581 }
582
583 /// \brief Propagates key events to the focus-widget, overriding global accelerators.
584 static gboolean override_global_accelerators(GtkWindow* window, GdkEventKey* event, gpointer data)
585 {
586   gboolean b = gtk_window_propagate_key_event(window, event);
587   return b;
588 }
589
590 void global_accel_connect_window(GtkWindow* window)
591 {
592 #if 1
593   unsigned int override_handler = g_signal_connect(G_OBJECT(window), "key_press_event", G_CALLBACK(override_global_accelerators), 0);
594   g_object_set_data(G_OBJECT(window), "override_handler", gint_to_pointer(override_handler));
595
596   unsigned int special_key_press_handler = g_signal_connect(G_OBJECT(window), "key_press_event", G_CALLBACK(accelerator_key_event), &g_special_accelerators);
597   g_object_set_data(G_OBJECT(window), "special_key_press_handler", gint_to_pointer(special_key_press_handler));
598
599   GlobalPressedKeys_connect(window);
600 #else
601   unsigned int key_press_handler = g_signal_connect(G_OBJECT(window), "key_press_event", G_CALLBACK(accelerator_key_event), &g_keydown_accelerators);
602   unsigned int key_release_handler = g_signal_connect(G_OBJECT(window), "key_release_event", G_CALLBACK(accelerator_key_event), &g_keyup_accelerators);
603   g_object_set_data(G_OBJECT(window), "key_press_handler", gint_to_pointer(key_press_handler));
604   g_object_set_data(G_OBJECT(window), "key_release_handler", gint_to_pointer(key_release_handler));
605 #endif
606   g_accel_windows.insert(window);
607   gtk_window_add_accel_group(window, global_accel);
608 }
609 void global_accel_disconnect_window(GtkWindow* window)
610 {
611 #if 1
612   GlobalPressedKeys_disconnect(window);
613
614   g_signal_handler_disconnect(G_OBJECT(window), gpointer_to_int(g_object_get_data(G_OBJECT(window), "override_handler")));
615   g_signal_handler_disconnect(G_OBJECT(window), gpointer_to_int(g_object_get_data(G_OBJECT(window), "special_key_press_handler")));
616 #else
617   g_signal_handler_disconnect(G_OBJECT(window), gpointer_to_int(g_object_get_data(G_OBJECT(window), "key_press_handler")));
618   g_signal_handler_disconnect(G_OBJECT(window), gpointer_to_int(g_object_get_data(G_OBJECT(window), "key_release_handler")));
619 #endif
620   gtk_window_remove_accel_group(window, global_accel);
621   std::size_t count = g_accel_windows.erase(window);
622   ASSERT_MESSAGE(count == 1, "failed to remove accel group\n");
623 }
624
625
626 GClosure* global_accel_group_find(Accelerator accelerator)
627 {
628   guint numEntries = 0;
629   GtkAccelGroupEntry* entry = gtk_accel_group_query(global_accel, accelerator.key, accelerator.modifiers, &numEntries);
630   if(numEntries != 0)
631   {
632     if(numEntries != 1)
633     {
634       char* name = gtk_accelerator_name(accelerator.key, accelerator.modifiers);
635       globalErrorStream() << "accelerator already in-use: " << name << "\n";
636       g_free(name);
637     }
638     return entry->closure;
639   }
640   return 0;
641 }
642
643 void global_accel_group_connect(const Accelerator& accelerator, const Callback& callback)
644 {
645   if(accelerator.key != 0)
646   {
647     global_accel_group_add_accelerator(accelerator, callback);
648   }
649 }
650
651 void global_accel_group_disconnect(const Accelerator& accelerator, const Callback& callback)
652 {
653   if(accelerator.key != 0)
654   {
655     global_accel_group_remove_accelerator(accelerator);
656   }
657 }
658
659