forward port thread/abstract_ui changes from 2.X to 3.0
[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
60         static const char* primary_modifier_name ();
61         static const char* secondary_modifier_name ();
62         static const char* tertiary_modifier_name ();
63         static const char* level4_modifier_name ();
64         static const char* copy_modifier_name ();
65         static const char* rangeselect_modifier_name ();
66
67         static void set_primary_modifier (uint32_t newval) {
68                 set_modifier (newval, PrimaryModifier);
69         }
70         static void set_secondary_modifier (uint32_t newval) {
71                 set_modifier (newval, SecondaryModifier);
72         }
73         static void set_tertiary_modifier (uint32_t newval) {
74                 set_modifier (newval, TertiaryModifier);
75         }
76         static void set_level4_modifier (uint32_t newval) {
77                 set_modifier (newval, Level4Modifier);
78         }
79         static void set_copy_modifier (uint32_t newval) {
80                 set_modifier (newval, CopyModifier);
81         }
82         static void set_range_select_modifier (uint32_t newval) {
83                 set_modifier (newval, RangeSelectModifier);
84         }
85
86         bool key_is_down (uint32_t keyval);
87
88         static GdkModifierType RelevantModifierKeyMask;
89
90         static bool no_modifier_keys_pressed(GdkEventButton* ev) {
91                 return (ev->state & RelevantModifierKeyMask) == 0;
92         }
93
94         bool leave_window (GdkEventCrossing *ev, Gtk::Window*);
95         bool enter_window (GdkEventCrossing *ev, Gtk::Window*);
96
97         static bool modifier_state_contains (guint state, ModifierMask);
98         static bool modifier_state_equals   (guint state, ModifierMask);
99
100         static bool no_modifiers_active (guint state);
101
102         static void set_snap_modifier (guint);
103
104         /** @return Modifier mask to temporarily toggle grid setting; with this modifier
105          *  - magnetic or normal grid should become no grid and
106          *  - no grid should become normal grid
107          */
108         static ModifierMask snap_modifier () { return ModifierMask (snap_mod); }
109
110         static guint edit_button() { return edit_but; }
111         static void set_edit_button (guint);
112         static guint edit_modifier() { return edit_mod; }
113         static void set_edit_modifier(guint);
114
115         static guint delete_button() { return delete_but; }
116         static void set_delete_button(guint);
117         static guint delete_modifier() { return delete_mod; }
118         static void set_delete_modifier(guint);
119
120         static guint insert_note_button() { return insert_note_but; }
121         static void set_insert_note_button (guint);
122         static guint insert_note_modifier() { return insert_note_mod; }
123         static void set_insert_note_modifier(guint);
124         
125         static bool is_edit_event (GdkEventButton*);
126         static bool is_delete_event (GdkEventButton*);
127         static bool is_insert_note_event (GdkEventButton*);
128         static bool is_context_menu_event (GdkEventButton*);
129         static bool is_button2_event (GdkEventButton*);
130
131         static Keyboard& the_keyboard() { return *_the_keyboard; }
132
133         static bool some_magic_widget_has_focus ();
134         static void magic_widget_grab_focus ();
135         static void magic_widget_drop_focus ();
136
137         static void keybindings_changed ();
138         static void save_keybindings ();
139         static bool load_keybindings (std::string path);
140         static void set_can_save_keybindings (bool yn);
141         static std::string current_binding_name () { return _current_binding_name; }
142         static std::map<std::string,std::string> binding_files;
143
144         struct AccelKeyLess {
145             bool operator() (const Gtk::AccelKey a, const Gtk::AccelKey b) const {
146                     if (a.get_key() != b.get_key()) {
147                             return a.get_key() < b.get_key();
148                     } else {
149                             return a.get_mod() < b.get_mod();
150                     }
151             }
152         };
153
154   protected:
155         static Keyboard* _the_keyboard;
156
157         guint           snooper_id;
158         State           state;
159
160         static guint     edit_but;
161         static guint     edit_mod;
162         static guint     delete_but;
163         static guint     delete_mod;
164         static guint     insert_note_but;
165         static guint     insert_note_mod;
166         static guint     snap_mod;
167         static guint     button2_modifiers;
168         static Gtk::Window* current_window;
169         static std::string user_keybindings_path;
170         static bool can_save_keybindings;
171         static bool bindings_changed_after_save_became_legal;
172         static std::string _current_binding_name;
173
174         typedef std::pair<std::string,std::string> two_strings;
175
176         static std::map<Gtk::AccelKey,two_strings,AccelKeyLess> release_keys;
177
178         static gint _snooper (GtkWidget*, GdkEventKey*, gpointer);
179         gint snooper (GtkWidget*, GdkEventKey*);
180
181         static void set_modifier (uint32_t newval, uint32_t& variable);
182
183         static bool _some_magic_widget_has_focus;
184 };
185
186 } /* namespace */
187
188 #endif /* __libgtkmm2ext_keyboard_h__ */