merge fix for tempo branch
[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 #include "gtkmm2ext/visibility.h"
34
35 namespace Gtk {
36         class Window;
37 }
38
39 namespace Gtkmm2ext {
40
41 class LIBGTKMM2EXT_API Keyboard : public sigc::trackable, PBD::Stateful
42 {
43   public:
44         Keyboard ();
45         ~Keyboard ();
46
47         XMLNode& get_state (void);
48         int set_state (const XMLNode&, int version);
49
50         virtual void setup_keybindings () = 0;
51
52         typedef std::vector<uint32_t> State;
53         typedef uint32_t ModifierMask;
54
55         static uint32_t PrimaryModifier;
56         static uint32_t SecondaryModifier;
57         static uint32_t TertiaryModifier;
58         static uint32_t Level4Modifier;
59         static uint32_t CopyModifier;
60         static uint32_t RangeSelectModifier;
61         static uint32_t GainFineScaleModifier;
62         static uint32_t GainExtraFineScaleModifier;
63
64         // Modifiers for scroll wheel
65         static uint32_t ScrollZoomVerticalModifier;
66         static uint32_t ScrollZoomHorizontalModifier;
67         static uint32_t ScrollHorizontalModifier;
68
69         static const char* primary_modifier_name ();
70         static const char* secondary_modifier_name ();
71         static const char* tertiary_modifier_name ();
72         static const char* level4_modifier_name ();
73         static const char* copy_modifier_name ();
74         static const char* rangeselect_modifier_name ();
75
76         static void set_primary_modifier (uint32_t newval) {
77                 set_modifier (newval, PrimaryModifier);
78         }
79         static void set_secondary_modifier (uint32_t newval) {
80                 set_modifier (newval, SecondaryModifier);
81         }
82         static void set_tertiary_modifier (uint32_t newval) {
83                 set_modifier (newval, TertiaryModifier);
84         }
85         static void set_level4_modifier (uint32_t newval) {
86                 set_modifier (newval, Level4Modifier);
87         }
88         static void set_copy_modifier (uint32_t newval) {
89                 set_modifier (newval, CopyModifier);
90         }
91         static void set_range_select_modifier (uint32_t newval) {
92                 set_modifier (newval, RangeSelectModifier);
93         }
94
95         bool key_is_down (uint32_t keyval);
96
97         static GdkModifierType RelevantModifierKeyMask;
98
99         static bool no_modifier_keys_pressed(GdkEventButton* ev) {
100                 return (ev->state & RelevantModifierKeyMask) == 0;
101         }
102
103         static bool no_modifier_keys_pressed(GdkEventKey* ev) {
104                 return (ev->state & RelevantModifierKeyMask) == 0;
105         }
106
107         bool leave_window (GdkEventCrossing *ev, Gtk::Window*);
108         bool enter_window (GdkEventCrossing *ev, Gtk::Window*);
109         bool focus_in_window (GdkEventFocus *ev, Gtk::Window*);
110         bool focus_out_window (GdkEventFocus *ev, Gtk::Window*);
111
112         static bool modifier_state_contains (guint state, ModifierMask);
113         static bool modifier_state_equals   (guint state, ModifierMask);
114
115         static bool no_modifiers_active (guint state);
116
117         static void set_snap_modifier (guint);
118
119         /** @return Modifier mask to temporarily toggle grid setting; with this modifier
120          *  - magnetic or normal grid should become no grid and
121          *  - no grid should become normal grid
122          */
123         static ModifierMask snap_modifier () { return ModifierMask (snap_mod); }
124
125         static guint edit_button() { return edit_but; }
126         static void set_edit_button (guint);
127         static guint edit_modifier() { return edit_mod; }
128         static void set_edit_modifier(guint);
129
130         static guint delete_button() { return delete_but; }
131         static void set_delete_button(guint);
132         static guint delete_modifier() { return delete_mod; }
133         static void set_delete_modifier(guint);
134
135         static guint insert_note_button() { return insert_note_but; }
136         static void set_insert_note_button (guint);
137         static guint insert_note_modifier() { return insert_note_mod; }
138         static void set_insert_note_modifier(guint);
139         
140         static bool is_edit_event (GdkEventButton*);
141         static bool is_delete_event (GdkEventButton*);
142         static bool is_insert_note_event (GdkEventButton*);
143         static bool is_context_menu_event (GdkEventButton*);
144         static bool is_button2_event (GdkEventButton*);
145
146         static Keyboard& the_keyboard() { return *_the_keyboard; }
147
148         static bool some_magic_widget_has_focus ();
149         static void magic_widget_grab_focus ();
150         static void magic_widget_drop_focus ();
151         static Gtk::Window* get_current_window () { return current_window; };
152
153         static void close_current_dialog ();
154
155         static void keybindings_changed ();
156         static void save_keybindings ();
157         static bool load_keybindings (std::string path);
158         static void set_can_save_keybindings (bool yn);
159         static std::string current_binding_name () { return _current_binding_name; }
160         static std::map<std::string,std::string> binding_files;
161
162         int reset_bindings ();
163
164         struct AccelKeyLess {
165             bool operator() (const Gtk::AccelKey a, const Gtk::AccelKey b) const {
166                     if (a.get_key() != b.get_key()) {
167                             return a.get_key() < b.get_key();
168                     } else {
169                             return a.get_mod() < b.get_mod();
170                     }
171             }
172         };
173
174         sigc::signal0<void> ZoomVerticalModifierReleased;
175
176   protected:
177         static Keyboard* _the_keyboard;
178
179         guint           snooper_id;
180         State           state;
181
182         static guint     edit_but;
183         static guint     edit_mod;
184         static guint     delete_but;
185         static guint     delete_mod;
186         static guint     insert_note_but;
187         static guint     insert_note_mod;
188         static guint     snap_mod;
189         static guint     button2_modifiers;
190         static Gtk::Window* current_window;
191         static std::string user_keybindings_path;
192         static bool can_save_keybindings;
193         static bool bindings_changed_after_save_became_legal;
194         static std::string _current_binding_name;
195
196         typedef std::pair<std::string,std::string> two_strings;
197
198         static std::map<Gtk::AccelKey,two_strings,AccelKeyLess> release_keys;
199
200         static gint _snooper (GtkWidget*, GdkEventKey*, gpointer);
201         gint snooper (GtkWidget*, GdkEventKey*);
202
203         static void set_modifier (uint32_t newval, uint32_t& variable);
204
205         static bool _some_magic_widget_has_focus;
206 };
207
208 } /* namespace */
209
210 #endif /* __libgtkmm2ext_keyboard_h__ */