Merge branch 'radiosrb' of https://github.com/adiknoth/ardour
[ardour.git] / libs / gtkmm2ext / gtkmm2ext / keyboard.h
1 /*
2     Copyright (C) 2001 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifndef __libgtkmm2ext_keyboard_h__
21 #define __libgtkmm2ext_keyboard_h__
22
23 #include <map>
24 #include <vector>
25 #include <string>
26
27 #include <sigc++/signal.h>
28 #include <gtk/gtk.h>
29 #include <gtkmm/accelkey.h>
30
31 #include "pbd/stateful.h"
32
33 namespace Gtk {
34         class Window;
35 }
36
37 namespace Gtkmm2ext {
38
39 class Keyboard : public sigc::trackable, PBD::Stateful
40 {
41   public:
42         Keyboard ();
43         ~Keyboard ();
44
45         XMLNode& get_state (void);
46         int set_state (const XMLNode&, int version);
47
48         virtual void setup_keybindings () = 0;
49
50         typedef std::vector<uint32_t> State;
51         typedef uint32_t ModifierMask;
52
53         static uint32_t PrimaryModifier;
54         static uint32_t SecondaryModifier;
55         static uint32_t TertiaryModifier;
56         static uint32_t Level4Modifier;
57         static uint32_t CopyModifier;
58         static uint32_t RangeSelectModifier;
59         static uint32_t GainFineScaleModifier;
60         static uint32_t GainExtraFineScaleModifier;
61
62         // Modifiers for scroll wheel
63         static uint32_t ScrollZoomVerticalModifier;
64         static uint32_t ScrollZoomHorizontalModifier;
65         static uint32_t ScrollHorizontalModifier;
66
67         static const char* primary_modifier_name ();
68         static const char* secondary_modifier_name ();
69         static const char* tertiary_modifier_name ();
70         static const char* level4_modifier_name ();
71         static const char* copy_modifier_name ();
72         static const char* rangeselect_modifier_name ();
73
74         static void set_primary_modifier (uint32_t newval) {
75                 set_modifier (newval, PrimaryModifier);
76         }
77         static void set_secondary_modifier (uint32_t newval) {
78                 set_modifier (newval, SecondaryModifier);
79         }
80         static void set_tertiary_modifier (uint32_t newval) {
81                 set_modifier (newval, TertiaryModifier);
82         }
83         static void set_level4_modifier (uint32_t newval) {
84                 set_modifier (newval, Level4Modifier);
85         }
86         static void set_copy_modifier (uint32_t newval) {
87                 set_modifier (newval, CopyModifier);
88         }
89         static void set_range_select_modifier (uint32_t newval) {
90                 set_modifier (newval, RangeSelectModifier);
91         }
92
93         bool key_is_down (uint32_t keyval);
94
95         static GdkModifierType RelevantModifierKeyMask;
96
97         static bool no_modifier_keys_pressed(GdkEventButton* ev) {
98                 return (ev->state & RelevantModifierKeyMask) == 0;
99         }
100
101         static bool no_modifier_keys_pressed(GdkEventKey* ev) {
102                 return (ev->state & RelevantModifierKeyMask) == 0;
103         }
104
105         bool leave_window (GdkEventCrossing *ev, Gtk::Window*);
106         bool enter_window (GdkEventCrossing *ev, Gtk::Window*);
107
108         static bool modifier_state_contains (guint state, ModifierMask);
109         static bool modifier_state_equals   (guint state, ModifierMask);
110
111         static bool no_modifiers_active (guint state);
112
113         static void set_snap_modifier (guint);
114
115         /** @return Modifier mask to temporarily toggle grid setting; with this modifier
116          *  - magnetic or normal grid should become no grid and
117          *  - no grid should become normal grid
118          */
119         static ModifierMask snap_modifier () { return ModifierMask (snap_mod); }
120
121         static guint edit_button() { return edit_but; }
122         static void set_edit_button (guint);
123         static guint edit_modifier() { return edit_mod; }
124         static void set_edit_modifier(guint);
125
126         static guint delete_button() { return delete_but; }
127         static void set_delete_button(guint);
128         static guint delete_modifier() { return delete_mod; }
129         static void set_delete_modifier(guint);
130
131         static guint insert_note_button() { return insert_note_but; }
132         static void set_insert_note_button (guint);
133         static guint insert_note_modifier() { return insert_note_mod; }
134         static void set_insert_note_modifier(guint);
135         
136         static bool is_edit_event (GdkEventButton*);
137         static bool is_delete_event (GdkEventButton*);
138         static bool is_insert_note_event (GdkEventButton*);
139         static bool is_context_menu_event (GdkEventButton*);
140         static bool is_button2_event (GdkEventButton*);
141
142         static Keyboard& the_keyboard() { return *_the_keyboard; }
143
144         static bool some_magic_widget_has_focus ();
145         static void magic_widget_grab_focus ();
146         static void magic_widget_drop_focus ();
147
148         static void close_current_dialog ();
149
150         static void keybindings_changed ();
151         static void save_keybindings ();
152         static bool load_keybindings (std::string path);
153         static void set_can_save_keybindings (bool yn);
154         static std::string current_binding_name () { return _current_binding_name; }
155         static std::map<std::string,std::string> binding_files;
156
157         struct AccelKeyLess {
158             bool operator() (const Gtk::AccelKey a, const Gtk::AccelKey b) const {
159                     if (a.get_key() != b.get_key()) {
160                             return a.get_key() < b.get_key();
161                     } else {
162                             return a.get_mod() < b.get_mod();
163                     }
164             }
165         };
166
167         sigc::signal0<void> ShiftReleased;
168
169   protected:
170         static Keyboard* _the_keyboard;
171
172         guint           snooper_id;
173         State           state;
174
175         static guint     edit_but;
176         static guint     edit_mod;
177         static guint     delete_but;
178         static guint     delete_mod;
179         static guint     insert_note_but;
180         static guint     insert_note_mod;
181         static guint     snap_mod;
182         static guint     button2_modifiers;
183         static Gtk::Window* current_window;
184         static std::string user_keybindings_path;
185         static bool can_save_keybindings;
186         static bool bindings_changed_after_save_became_legal;
187         static std::string _current_binding_name;
188
189         typedef std::pair<std::string,std::string> two_strings;
190
191         static std::map<Gtk::AccelKey,two_strings,AccelKeyLess> release_keys;
192
193         static gint _snooper (GtkWidget*, GdkEventKey*, gpointer);
194         gint snooper (GtkWidget*, GdkEventKey*);
195
196         static void set_modifier (uint32_t newval, uint32_t& variable);
197
198         static bool _some_magic_widget_has_focus;
199 };
200
201 } /* namespace */
202
203 #endif /* __libgtkmm2ext_keyboard_h__ */