1 #ifndef __libgtkmm2ext_bindings_h__
2 #define __libgtkmm2ext_bindings_h__
10 #include <gdk/gdkkeysyms.h>
11 #include <gtkmm/action.h>
12 #include <gtkmm/radioaction.h>
13 #include <gtkmm/toggleaction.h>
15 #include "pbd/signals.h"
17 #include "gtkmm2ext/visibility.h"
24 class LIBGTKMM2EXT_API KeyboardKey
28 _val = GDK_VoidSymbol;
31 KeyboardKey (uint32_t state, uint32_t keycode);
33 static KeyboardKey null_key() { return KeyboardKey (0, 0); }
35 uint32_t state() const { return _val >> 32; }
36 uint32_t key() const { return _val & 0xffff; }
38 bool operator<(const KeyboardKey& other) const {
39 return _val < other._val;
42 bool operator==(const KeyboardKey& other) const {
43 return _val == other._val;
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&);
51 std::string display_label() const;
57 class LIBGTKMM2EXT_API MouseButton {
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; }
67 bool operator<(const MouseButton& other) const {
68 return _val < other._val;
71 bool operator==(const MouseButton& other) const {
72 return _val == other._val;
75 std::string name() const;
76 static bool make_button (const std::string&, MouseButton&);
82 class LIBGTKMM2EXT_API Bindings;
84 class LIBGTKMM2EXT_API ActionMap {
86 ActionMap (std::string const& name);
89 std::string name() const { return _name; }
91 Glib::RefPtr<Gtk::ActionGroup> create_action_group (const std::string& group_name);
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,
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);
108 Glib::RefPtr<Gtk::Action> find_action (const std::string& name);
109 Glib::RefPtr<Gtk::Action> find_action (char const * group_name, char const * action_name);
111 void set_bindings (Bindings*);
112 Bindings* bindings() const { return _bindings; }
114 typedef std::vector<Glib::RefPtr<Gtk::Action> > Actions;
115 void get_actions (Actions&);
117 static std::list<ActionMap*> action_maps;
119 /* used by control surface protocols and other UIs */
120 static void get_all_actions (std::vector<std::string>& paths,
121 std::vector<std::string>& labels,
122 std::vector<std::string>& tooltips,
123 std::vector<std::string>& keys,
124 std::vector<Glib::RefPtr<Gtk::Action> >& actions);
129 /* hash for faster lookup of actions by name */
131 typedef std::map<std::string, Glib::RefPtr<Gtk::Action> > _ActionMap;
134 /* initialized to null; set after a Bindings object has ::associated()
135 * itself with this action map.
142 class LIBGTKMM2EXT_API ActionMapOwner {
144 Gtkmm2ext::ActionMap myactions;
146 ActionMapOwner (std::string const & map_name) : myactions (map_name) {}
147 Glib::RefPtr<Gtk::Action> find_action (const std::string& name) { return myactions.find_action (name); }
148 Glib::RefPtr<Gtk::Action> find_action (char const * group_name, char const * action_name) { return myactions.find_action (group_name, action_name); }
151 class LIBGTKMM2EXT_API StaticActionMapOwner {
153 virtual Gtkmm2ext::ActionMap& my_actions() const = 0;
155 virtual ~StaticActionMapOwner() {}
156 Glib::RefPtr<Gtk::Action> find_action (const std::string& name) { return my_actions().find_action (name); }
157 Glib::RefPtr<Gtk::Action> find_action (char const * group_name, char const * action_name) { return my_actions().find_action (group_name, action_name); }
160 class LIBGTKMM2EXT_API Bindings {
168 ActionInfo (std::string const& name) : action_name (name) {}
169 ActionInfo (std::string const& name, std::string const& grp) : action_name (name), group_name (grp) {}
171 std::string action_name;
172 std::string group_name; /* may be empty */
173 Glib::RefPtr<Gtk::Action> action;
175 typedef std::map<KeyboardKey,ActionInfo> KeybindingMap;
177 Bindings (std::string const& name);
180 std::string const& name() const { return _name; }
186 bool empty_keys () const;
187 bool empty_mouse () const;
189 bool add (KeyboardKey, Operation, std::string const&, XMLProperty const*, bool can_save = false);
190 bool replace (KeyboardKey, Operation, std::string const& action_name, bool can_save = true);
191 bool remove (Operation, std::string const& action_name, bool can_save = false);
193 bool activate (KeyboardKey, Operation);
195 void add (MouseButton, Operation, std::string const&, XMLProperty const*);
196 void remove (MouseButton, Operation);
197 bool activate (MouseButton, Operation);
199 bool is_bound (KeyboardKey const&, Operation) const;
200 std::string bound_name (KeyboardKey const&, Operation) const;
201 bool is_registered (Operation op, std::string const& action_name) const;
203 KeyboardKey get_binding_for_action (Glib::RefPtr<Gtk::Action>, Operation& op);
205 bool load (XMLNode const& node);
206 void load_operation (XMLNode const& node);
207 void save (XMLNode& root);
208 void save_as_html (std::ostream&, bool) const;
210 /* GTK has the following position a Gtk::Action:
212 * accel_path: <Actions>/GroupName/ActionName
215 * We want proper namespacing and we're not interested in
216 * the silly <Actions> "extra" namespace. So in Ardour:
218 * accel_path: <Actions>/GroupName/ActionName
219 * name: GroupName/ActionName
221 * This (static) method returns the "ardour" name for the action.
223 static std::string ardour_action_name (Glib::RefPtr<Gtk::Action>);
225 void set_action_map (ActionMap&);
227 /* used for editing bindings */
228 void get_all_actions (std::vector<std::string>& paths,
229 std::vector<std::string>& labels,
230 std::vector<std::string>& tooltips,
231 std::vector<std::string>& keys,
232 std::vector<Glib::RefPtr<Gtk::Action> >& actions);
234 /* all bindings currently in existence, as grouped into Bindings */
235 static void reset_bindings () { bindings.clear (); }
236 static std::list<Bindings*> bindings;
237 static Bindings* get_bindings (std::string const& name, ActionMap&);
238 static void associate_all ();
239 static void save_all_bindings_as_html (std::ostream&);
241 static PBD::Signal1<void,Bindings*> BindingsChanged;
245 ActionMap* _action_map;
246 KeybindingMap press_bindings;
247 KeybindingMap release_bindings;
249 typedef std::map<MouseButton,ActionInfo> MouseButtonBindingMap;
250 MouseButtonBindingMap button_press_bindings;
251 MouseButtonBindingMap button_release_bindings;
253 void push_to_gtk (KeyboardKey, Glib::RefPtr<Gtk::Action>);
255 KeybindingMap& get_keymap (Operation op);
256 const KeybindingMap& get_keymap (Operation op) const;
257 MouseButtonBindingMap& get_mousemap (Operation op);
262 std::ostream& operator<<(std::ostream& out, Gtkmm2ext::KeyboardKey const & k);
264 #endif /* __libgtkmm2ext_bindings_h__ */