Ardour  9.0-pre0-350-gf17a656217
bindings.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010-2019 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2017 Robin Gareus <robin@gareus.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef __libgtkmm2ext_bindings_h__
21 #define __libgtkmm2ext_bindings_h__
22 
23 #include <map>
24 #include <vector>
25 #include <list>
26 
27 #include <stdint.h>
28 
29 #include <gdk/gdkkeysyms.h>
30 #include <gtkmm/action.h>
31 #include <gtkmm/radioaction.h>
32 #include <gtkmm/toggleaction.h>
33 
34 #include "pbd/signals.h"
35 
36 #include "gtkmm2ext/visibility.h"
37 
38 class XMLNode;
39 class XMLProperty;
40 
41 namespace Gtkmm2ext {
42 
44 {
45  public:
47  _val = GDK_VoidSymbol;
48  }
49 
50  KeyboardKey (uint32_t state, uint32_t keycode);
51 
52  static KeyboardKey null_key() { return KeyboardKey (0, 0); }
53 
54  uint32_t state() const { return _val >> 32; }
55  uint32_t key() const { return _val & 0xffffffff; }
56 
57  bool operator<(const KeyboardKey& other) const {
58  return _val < other._val;
59  }
60 
61  bool operator==(const KeyboardKey& other) const {
62  return _val == other._val;
63  }
64 
65  std::string name() const;
66  std::string native_name() const;
67  std::string native_short_name() const;
68  static bool make_key (const std::string&, KeyboardKey&);
69 
70  std::string display_label() const;
71 
72  private:
73  uint64_t _val;
74 };
75 
77  public:
79  _val = ~0ULL;
80  }
81 
82  MouseButton (uint32_t state, uint32_t button_number);
83  uint32_t state() const { return _val >> 32; }
84  uint32_t button() const { return _val & 0xffff; }
85 
86  bool operator<(const MouseButton& other) const {
87  return _val < other._val;
88  }
89 
90  bool operator==(const MouseButton& other) const {
91  return _val == other._val;
92  }
93 
94  std::string name() const;
95  static bool make_button (const std::string&, MouseButton&);
96 
97  private:
98  uint64_t _val;
99 };
100 
102 
104  public:
105  enum Operation {
107  Release
108  };
109 
110  struct ActionInfo {
111  ActionInfo (std::string const& name) : action_name (name) {}
112  ActionInfo (std::string const& name, std::string const& grp) : action_name (name), group_name (grp) {}
113 
114  std::string action_name;
115  std::string group_name; /* may be empty */
116  mutable Glib::RefPtr<Gtk::Action> action;
117  };
118  typedef std::map<KeyboardKey,ActionInfo> KeybindingMap;
119 
120  Bindings (std::string const& name);
122 
123  std::string const& name() const { return _name; }
124 
125  void reassociate ();
126  void associate ();
127  void dissociate ();
128 
129  bool empty() const;
130  bool empty_keys () const;
131  bool empty_mouse () const;
132 
133  bool add (KeyboardKey, Operation, std::string const&, XMLProperty const*, bool can_save = false);
134  bool replace (KeyboardKey, Operation, std::string const& action_name, bool can_save = true);
135  bool remove (Operation, std::string const& action_name, bool can_save = false);
136 
138 
139  void add (MouseButton, Operation, std::string const&, XMLProperty const*);
142 
143  bool is_bound (KeyboardKey const&, Operation, std::string* path = 0) const;
144  std::string bound_name (KeyboardKey const&, Operation) const;
145  bool is_registered (Operation op, std::string const& action_name) const;
146 
147  KeyboardKey get_binding_for_action (Glib::RefPtr<Gtk::Action>, Operation& op);
148 
149  bool load (XMLNode const& node);
150  void load_operation (XMLNode const& node);
151  void save (XMLNode& root);
152  void save_as_html (std::ostream&, bool) const;
153 
154  /* used for editing bindings */
155  void get_all_actions (std::vector<std::string>& paths,
156  std::vector<std::string>& labels,
157  std::vector<std::string>& tooltips,
158  std::vector<std::string>& keys,
159  std::vector<Glib::RefPtr<Gtk::Action> >& actions);
160 
161  /* all bindings currently in existence, as grouped into Bindings */
162  static void reset_bindings () { bindings.clear (); }
163  static std::list<Bindings*> bindings;
164  static Bindings* get_bindings (std::string const & name);
165  static void associate_all ();
166  static void save_all_bindings_as_html (std::ostream&);
167 
169 
175  }
176  }
177  };
178 
179  private:
180  std::string _name;
183 
184  typedef std::map<MouseButton,ActionInfo> MouseButtonBindingMap;
187 
188  void push_to_gtk (KeyboardKey, Glib::RefPtr<Gtk::Action>);
189 
191  const KeybindingMap& get_keymap (Operation op) const;
193 
194  /* GTK has the following position a Gtk::Action:
195  *
196  * accel_path: <Actions>/GroupName/ActionName
197  * name: ActionName
198  *
199  * We want proper namespacing and we're not interested in
200  * the silly <Actions> "extra" namespace. So in Ardour:
201  *
202  * accel_path: <Actions>/GroupName/ActionName
203  * name: GroupName/ActionName
204  *
205  * This (static) method returns the "ardour" name for the action.
206  */
207  static std::string ardour_action_name (Glib::RefPtr<Gtk::Action>);
208 
209  static int _drag_active;
210  friend struct DragsBlockBindings;
211 };
212 
213 } // namespace
214 
215 std::ostream& operator<<(std::ostream& out, Gtkmm2ext::KeyboardKey const & k);
216 
217 #endif /* __libgtkmm2ext_bindings_h__ */
std::ostream & operator<<(std::ostream &out, Gtkmm2ext::KeyboardKey const &k)
MouseButtonBindingMap button_release_bindings
Definition: bindings.h:186
void add(MouseButton, Operation, std::string const &, XMLProperty const *)
static std::string ardour_action_name(Glib::RefPtr< Gtk::Action >)
MouseButtonBindingMap button_press_bindings
Definition: bindings.h:185
bool activate(KeyboardKey, Operation)
const KeybindingMap & get_keymap(Operation op) const
void save(XMLNode &root)
KeyboardKey get_binding_for_action(Glib::RefPtr< Gtk::Action >, Operation &op)
void save_as_html(std::ostream &, bool) const
static std::list< Bindings * > bindings
Definition: bindings.h:163
bool activate(MouseButton, Operation)
KeybindingMap release_bindings
Definition: bindings.h:182
void push_to_gtk(KeyboardKey, Glib::RefPtr< Gtk::Action >)
bool load(XMLNode const &node)
void load_operation(XMLNode const &node)
static Bindings * get_bindings(std::string const &name)
void get_all_actions(std::vector< std::string > &paths, std::vector< std::string > &labels, std::vector< std::string > &tooltips, std::vector< std::string > &keys, std::vector< Glib::RefPtr< Gtk::Action > > &actions)
void remove(MouseButton, Operation)
bool empty() const
std::string bound_name(KeyboardKey const &, Operation) const
static void associate_all()
Bindings(std::string const &name)
std::map< MouseButton, ActionInfo > MouseButtonBindingMap
Definition: bindings.h:184
bool add(KeyboardKey, Operation, std::string const &, XMLProperty const *, bool can_save=false)
static PBD::Signal< void(Bindings *)> BindingsChanged
Definition: bindings.h:168
std::string const & name() const
Definition: bindings.h:123
static void reset_bindings()
Definition: bindings.h:162
static void save_all_bindings_as_html(std::ostream &)
MouseButtonBindingMap & get_mousemap(Operation op)
bool replace(KeyboardKey, Operation, std::string const &action_name, bool can_save=true)
bool empty_mouse() const
KeybindingMap press_bindings
Definition: bindings.h:181
bool is_registered(Operation op, std::string const &action_name) const
bool remove(Operation, std::string const &action_name, bool can_save=false)
bool empty_keys() const
std::string _name
Definition: bindings.h:180
bool is_bound(KeyboardKey const &, Operation, std::string *path=0) const
KeybindingMap & get_keymap(Operation op)
static int _drag_active
Definition: bindings.h:209
std::map< KeyboardKey, ActionInfo > KeybindingMap
Definition: bindings.h:118
bool operator<(const KeyboardKey &other) const
Definition: bindings.h:57
std::string display_label() const
uint32_t state() const
Definition: bindings.h:54
static KeyboardKey null_key()
Definition: bindings.h:52
std::string native_name() const
std::string native_short_name() const
bool operator==(const KeyboardKey &other) const
Definition: bindings.h:61
std::string name() const
uint32_t key() const
Definition: bindings.h:55
KeyboardKey(uint32_t state, uint32_t keycode)
static bool make_key(const std::string &, KeyboardKey &)
MouseButton(uint32_t state, uint32_t button_number)
bool operator==(const MouseButton &other) const
Definition: bindings.h:90
bool operator<(const MouseButton &other) const
Definition: bindings.h:86
static bool make_button(const std::string &, MouseButton &)
uint32_t button() const
Definition: bindings.h:84
std::string name() const
uint32_t state() const
Definition: bindings.h:83
Definition: xml++.h:114
#define GDK_VoidSymbol
GtkImageIconNameData name
Definition: gtkimage.h:6
#define LIBGTKMM2EXT_API
ActionInfo(std::string const &name, std::string const &grp)
Definition: bindings.h:112
Glib::RefPtr< Gtk::Action > action
Definition: bindings.h:116
ActionInfo(std::string const &name)
Definition: bindings.h:111