Fix restoration of key bindings assigned to Windows+key, bug #7037
[ardour.git] / libs / gtkmm2ext / gtkmm2ext / bindings.h
1 #ifndef __libgtkmm2ext_bindings_h__
2 #define __libgtkmm2ext_bindings_h__
3
4 #include <map>
5 #include <vector>
6 #include <list>
7
8 #include <stdint.h>
9
10 #include <gdk/gdkkeysyms.h>
11 #include <gtkmm/action.h>
12 #include <gtkmm/radioaction.h>
13 #include <gtkmm/toggleaction.h>
14
15 #include "pbd/signals.h"
16
17 #include "gtkmm2ext/visibility.h"
18
19 class XMLNode;
20 class XMLProperty;
21
22 namespace Gtkmm2ext {
23
24 class LIBGTKMM2EXT_API KeyboardKey
25 {
26   public:
27         KeyboardKey () {
28                 _val = GDK_VoidSymbol;
29         }
30
31         KeyboardKey (uint32_t state, uint32_t keycode);
32
33         static KeyboardKey null_key() { return KeyboardKey (0, 0); }
34
35         uint32_t state() const { return _val >> 32; }
36         uint32_t key() const { return _val & 0xffff; }
37
38         bool operator<(const KeyboardKey& other) const {
39                 return _val < other._val;
40         }
41
42         bool operator==(const KeyboardKey& other) const {
43                 return _val == other._val;
44         }
45
46         std::string name() const;
47         std::string native_name() const;
48         std::string native_short_name() const;
49         static bool make_key (const std::string&, KeyboardKey&);
50
51         std::string display_label() const;
52
53   private:
54         uint64_t _val;
55 };
56
57 class LIBGTKMM2EXT_API MouseButton {
58   public:
59         MouseButton () {
60                 _val = ~0ULL;
61         }
62
63         MouseButton (uint32_t state, uint32_t button_number);
64         uint32_t state() const { return _val >> 32; }
65         uint32_t button() const { return _val & 0xffff; }
66
67         bool operator<(const MouseButton& other) const {
68                 return _val < other._val;
69         }
70
71         bool operator==(const MouseButton& other) const {
72                 return _val == other._val;
73         }
74
75         std::string name() const;
76         static bool make_button (const std::string&, MouseButton&);
77
78   private:
79         uint64_t _val;
80 };
81
82 class LIBGTKMM2EXT_API Bindings;
83
84 class LIBGTKMM2EXT_API ActionMap {
85   public:
86         ActionMap (std::string const& name);
87         ~ActionMap();
88
89         std::string name() const { return _name; }
90
91         Glib::RefPtr<Gtk::ActionGroup> create_action_group (const std::string& group_name);
92
93         Glib::RefPtr<Gtk::Action> register_action (Glib::RefPtr<Gtk::ActionGroup> group, const char* name, const char* label);
94         Glib::RefPtr<Gtk::Action> register_action (Glib::RefPtr<Gtk::ActionGroup> group,
95                                                    const char* name, const char* label, sigc::slot<void> sl);
96         Glib::RefPtr<Gtk::Action> register_radio_action (Glib::RefPtr<Gtk::ActionGroup> group,
97                                                          Gtk::RadioAction::Group&,
98                                                          const char* name, const char* label,
99                                                          sigc::slot<void,GtkAction*> sl,
100                                                          int value);
101         Glib::RefPtr<Gtk::Action> register_radio_action (Glib::RefPtr<Gtk::ActionGroup> group,
102                                                          Gtk::RadioAction::Group&,
103                                                          const char* name, const char* label,
104                                                          sigc::slot<void> sl);
105         Glib::RefPtr<Gtk::Action> register_toggle_action (Glib::RefPtr<Gtk::ActionGroup> group,
106                                                           const char* name, const char* label, sigc::slot<void> sl);
107
108         Glib::RefPtr<Gtk::Action> find_action (const std::string& name);
109
110         void set_bindings (Bindings*);
111         Bindings* bindings() const { return _bindings; }
112
113         typedef std::vector<Glib::RefPtr<Gtk::Action> > Actions;
114         void get_actions (Actions&);
115
116         static std::list<ActionMap*> action_maps;
117
118         /* used by control surface protocols and other UIs */
119         static void get_all_actions (std::vector<std::string>& paths,
120                                      std::vector<std::string>& labels,
121                                      std::vector<std::string>& tooltips,
122                                      std::vector<std::string>& keys,
123                                      std::vector<Glib::RefPtr<Gtk::Action> >& actions);
124
125   private:
126         std::string _name;
127
128         /* hash for faster lookup of actions by name */
129
130         typedef std::map<std::string, Glib::RefPtr<Gtk::Action> > _ActionMap;
131         _ActionMap _actions;
132
133         /* initialized to null; set after a Bindings object has ::associated()
134          * itself with this action map.
135          */
136
137         Bindings* _bindings;
138
139 };
140
141 class LIBGTKMM2EXT_API Bindings {
142   public:
143         enum Operation {
144                 Press,
145                 Release
146         };
147
148         struct ActionInfo {
149                 ActionInfo (std::string const& name) : action_name (name) {}
150                 ActionInfo (std::string const& name, std::string const& grp) : action_name (name), group_name (grp) {}
151
152                 std::string action_name;
153                 std::string group_name; /* may be empty */
154                 Glib::RefPtr<Gtk::Action> action;
155         };
156         typedef std::map<KeyboardKey,ActionInfo> KeybindingMap;
157
158         Bindings (std::string const& name);
159         ~Bindings ();
160
161         std::string const& name() const { return _name; }
162
163         void associate ();
164         void dissociate ();
165
166         bool empty() const;
167         bool empty_keys () const;
168         bool empty_mouse () const;
169
170         bool add (KeyboardKey, Operation, std::string const&, XMLProperty const*, bool can_save = false);
171         bool replace (KeyboardKey, Operation, std::string const& action_name, bool can_save = true);
172         bool remove (Operation, std::string const& action_name, bool can_save = false);
173
174         bool activate (KeyboardKey, Operation);
175
176         void add (MouseButton, Operation, std::string const&, XMLProperty const*);
177         void remove (MouseButton, Operation);
178         bool activate (MouseButton, Operation);
179
180         bool is_bound (KeyboardKey const&, Operation) const;
181         bool is_registered (Operation op, std::string const& action_name) const;
182
183         KeyboardKey get_binding_for_action (Glib::RefPtr<Gtk::Action>, Operation& op);
184
185         bool load (XMLNode const& node);
186         void load_operation (XMLNode const& node);
187         void save (XMLNode& root);
188         void save_as_html (std::ostream&) const;
189
190         /* GTK has the following position a Gtk::Action:
191          *
192          *  accel_path: <Actions>/GroupName/ActionName
193          *  name: ActionName
194          *
195          * We want proper namespacing and we're not interested in
196          * the silly <Actions> "extra" namespace. So in Ardour:
197          *
198          * accel_path: <Actions>/GroupName/ActionName
199          * name: GroupName/ActionName
200          *
201          * This (static) method returns the "ardour" name for the action.
202          */
203         static std::string ardour_action_name (Glib::RefPtr<Gtk::Action>);
204
205         void set_action_map (ActionMap&);
206
207         /* used for editing bindings */
208         void get_all_actions (std::vector<std::string>& paths,
209                               std::vector<std::string>& labels,
210                               std::vector<std::string>& tooltips,
211                               std::vector<std::string>& keys,
212                               std::vector<Glib::RefPtr<Gtk::Action> >& actions);
213
214         /* all bindings currently in existence, as grouped into Bindings */
215         static std::list<Bindings*> bindings;
216         static Bindings* get_bindings (std::string const& name, ActionMap&);
217         static void associate_all ();
218         static void save_all_bindings_as_html (std::ostream&);
219
220         static PBD::Signal1<void,Bindings*> BindingsChanged;
221
222   private:
223         std::string  _name;
224         ActionMap*   _action_map;
225         KeybindingMap press_bindings;
226         KeybindingMap release_bindings;
227
228         typedef std::map<MouseButton,ActionInfo> MouseButtonBindingMap;
229         MouseButtonBindingMap button_press_bindings;
230         MouseButtonBindingMap button_release_bindings;
231
232         void push_to_gtk (KeyboardKey, Glib::RefPtr<Gtk::Action>);
233
234         KeybindingMap& get_keymap (Operation op);
235         const KeybindingMap& get_keymap (Operation op) const;
236         MouseButtonBindingMap& get_mousemap (Operation op);
237 };
238
239 } // namespace
240
241 std::ostream& operator<<(std::ostream& out, Gtkmm2ext::KeyboardKey const & k);
242
243 #endif /* __libgtkmm2ext_bindings_h__ */