]> git.xonotic.org Git - xonotic/netradiant.git/blob - libs/gtkutil/accelerator.h
reformat code! now the code is only ugly on the *inside*
[xonotic/netradiant.git] / libs / gtkutil / accelerator.h
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 #if !defined( INCLUDED_GTKUTIL_ACCELERATOR_H )
23 #define INCLUDED_GTKUTIL_ACCELERATOR_H
24
25 #include <uilib/uilib.h>
26 #include <gdk/gdk.h>
27
28 #include "generic/callback.h"
29 #include "property.h"
30
31 // ignore numlock
32 #define ALLOWED_MODIFIERS ( ~( GDK_MOD2_MASK | GDK_LOCK_MASK | GDK_MOD3_MASK | GDK_MOD4_MASK | GDK_MOD5_MASK ) )
33
34 struct Accelerator {
35     Accelerator(guint _key)
36             : key(gdk_keyval_to_upper(_key)), modifiers((GdkModifierType) 0)
37     {
38     }
39
40     Accelerator(guint _key, GdkModifierType _modifiers)
41             : key(gdk_keyval_to_upper(_key)), modifiers((GdkModifierType) (_modifiers & ALLOWED_MODIFIERS))
42     {
43     }
44
45     Accelerator(const Accelerator &src)
46             : key(gdk_keyval_to_upper(src.key)), modifiers((GdkModifierType) (src.modifiers & ALLOWED_MODIFIERS))
47     {
48     }
49
50     bool operator<(const Accelerator &other) const
51     {
52         guint k1 = key;
53         guint k2 = other.key;
54         int mod1 = modifiers & ALLOWED_MODIFIERS;
55         int mod2 = other.modifiers & ALLOWED_MODIFIERS;
56         return k1 < k2 || (!(k2 < k1) && mod1 < mod2);
57     }
58
59     bool operator==(const Accelerator &other) const
60     {
61         guint k1 = key;
62         guint k2 = other.key;
63         int mod1 = modifiers & ALLOWED_MODIFIERS;
64         int mod2 = other.modifiers & ALLOWED_MODIFIERS;
65         return k1 == k2 && mod1 == mod2;
66     }
67
68     Accelerator &operator=(const Accelerator &other)
69     {
70         key = other.key;
71         modifiers = (GdkModifierType) (other.modifiers & ALLOWED_MODIFIERS);
72         return *this;
73     }
74
75     guint key;
76     GdkModifierType modifiers;
77 };
78
79 inline Accelerator accelerator_null()
80 {
81     return Accelerator(0, (GdkModifierType) 0);
82 }
83
84 const char *global_keys_find(unsigned int key);
85
86 unsigned int global_keys_find(const char *name);
87
88 class TextOutputStream;
89
90 void accelerator_write(const Accelerator &accelerator, TextOutputStream &ostream);
91
92 template<typename TextOutputStreamType>
93 TextOutputStreamType &ostream_write(TextOutputStreamType &ostream, const Accelerator &accelerator)
94 {
95     accelerator_write(accelerator, ostream);
96     return ostream;
97 }
98
99 void keydown_accelerators_add(Accelerator accelerator, const Callback<void()> &callback);
100
101 void keydown_accelerators_remove(Accelerator accelerator);
102
103 void keyup_accelerators_add(Accelerator accelerator, const Callback<void()> &callback);
104
105 void keyup_accelerators_remove(Accelerator accelerator);
106
107 void global_accel_connect_window(ui::Window window);
108
109 void global_accel_disconnect_window(ui::Window window);
110
111 void GlobalPressedKeys_releaseAll();
112
113 extern ui::AccelGroup global_accel;
114
115 GClosure *global_accel_group_find(Accelerator accelerator);
116
117 void global_accel_group_connect(const Accelerator &accelerator, const Callback<void()> &callback);
118
119 void global_accel_group_disconnect(const Accelerator &accelerator, const Callback<void()> &callback);
120
121
122 class Command {
123 public:
124     Callback<void()> m_callback;
125     const Accelerator &m_accelerator;
126
127     Command(const Callback<void()> &callback, const Accelerator &accelerator) : m_callback(callback),
128                                                                                 m_accelerator(accelerator)
129     {
130     }
131 };
132
133 class Toggle {
134 public:
135     Command m_command;
136     Callback<void(const Callback<void(bool)> &)> m_exportCallback;
137
138     Toggle(const Callback<void()> &callback, const Accelerator &accelerator,
139            const Callback<void(const Callback<void(bool)> &)> &exportCallback) : m_command(callback, accelerator),
140                                                                                  m_exportCallback(exportCallback)
141     {
142     }
143 };
144
145 class KeyEvent {
146 public:
147     const Accelerator &m_accelerator;
148     Callback<void()> m_keyDown;
149     Callback<void()> m_keyUp;
150
151     KeyEvent(const Accelerator &accelerator, const Callback<void()> &keyDown, const Callback<void()> &keyUp)
152             : m_accelerator(accelerator), m_keyDown(keyDown), m_keyUp(keyUp)
153     {
154     }
155 };
156
157
158 struct PressedButtons;
159
160 void PressedButtons_connect(PressedButtons &pressedButtons, ui::Widget widget);
161
162 extern PressedButtons g_pressedButtons;
163
164 #endif