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