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