merge exportvis branch into cairocanvas, to reduce the number of "floating" branches.
[ardour.git] / libs / gtkmm2ext / gtkmm2ext / bindings.h
1 #ifndef __libgtkmm2ext_bindings_h__
2 #define __libgtkmm2ext_bindings_h__
3
4 #include <map>
5 #include <stdint.h>
6 #include <gdk/gdkkeysyms.h>
7 #include <gtkmm/action.h>
8 #include <gtkmm/action.h>
9 #include <gtkmm/radioaction.h>
10 #include <gtkmm/toggleaction.h>
11
12 #include "gtkmm2ext/visibility.h"
13
14 class XMLNode;
15
16 namespace Gtkmm2ext {
17
18 class LIBGTKMM2EXT_API KeyboardKey
19 {
20   public:
21         KeyboardKey () {
22                 _val = GDK_VoidSymbol;
23         }
24         
25         KeyboardKey (uint32_t state, uint32_t keycode);
26         
27         uint32_t state() const { return _val >> 32; }
28         uint32_t key() const { return _val & 0xffff; }
29         
30         bool operator<(const KeyboardKey& other) const {
31                 return _val < other._val;
32         }
33
34         bool operator==(const KeyboardKey& other) const {
35                 return _val == other._val;
36         }
37
38         std::string name() const;
39         static bool make_key (const std::string&, KeyboardKey&);
40
41   private:
42         uint64_t _val;
43 };
44
45 class LIBGTKMM2EXT_API MouseButton {
46   public:
47         MouseButton () {
48                 _val = ~0ULL;
49         }
50
51         MouseButton (uint32_t state, uint32_t button_number);
52         uint32_t state() const { return _val >> 32; }
53         uint32_t button() const { return _val & 0xffff; }
54
55         bool operator<(const MouseButton& other) const {
56                 return _val < other._val;
57         }
58
59         bool operator==(const MouseButton& other) const {
60                 return _val == other._val;
61         }
62
63         std::string name() const;
64         static bool make_button (const std::string&, MouseButton&);
65         static void set_ignored_state (int mask) {
66                 _ignored_state = mask;
67         }
68
69   private:
70         uint64_t _val;
71         static uint32_t _ignored_state;
72 };
73
74 class LIBGTKMM2EXT_API ActionMap {
75   public:
76         ActionMap() {}
77         ~ActionMap() {}
78
79         Glib::RefPtr<Gtk::Action> register_action (const char* path,
80                                                    const char* name, const char* label, sigc::slot<void> sl);
81         Glib::RefPtr<Gtk::Action> register_radio_action (const char* path, Gtk::RadioAction::Group&,
82                                                          const char* name, const char* label, 
83                                                          sigc::slot<void,GtkAction*> sl,
84                                                          int value);
85         Glib::RefPtr<Gtk::Action> register_toggle_action (const char*path,
86                                                           const char* name, const char* label, sigc::slot<void> sl);
87
88         Glib::RefPtr<Gtk::Action> find_action (const std::string& name);
89
90   private:
91         typedef std::map<std::string, Glib::RefPtr<Gtk::Action> > _ActionMap;
92         _ActionMap actions;
93 };        
94
95 class LIBGTKMM2EXT_API Bindings {
96   public:
97         enum Operation { 
98                 Press,
99                 Release
100         };
101         
102         Bindings();
103         ~Bindings ();
104
105         void add (KeyboardKey, Operation, Glib::RefPtr<Gtk::Action>);
106         void remove (KeyboardKey, Operation);
107         bool activate (KeyboardKey, Operation);
108
109         void add (MouseButton, Operation, Glib::RefPtr<Gtk::Action>);
110         void remove (MouseButton, Operation);
111         bool activate (MouseButton, Operation);
112
113         bool load (const std::string& path);
114         void load (const XMLNode& node);
115         bool save (const std::string& path);
116         void save (XMLNode& root);
117         
118         void set_action_map (ActionMap&);
119
120         static void set_ignored_state (int mask) {
121                 _ignored_state = mask;
122         }
123         static uint32_t ignored_state() { return _ignored_state; }
124
125   private:
126         typedef std::map<KeyboardKey,Glib::RefPtr<Gtk::Action> > KeybindingMap;
127
128         KeybindingMap press_bindings;
129         KeybindingMap release_bindings;
130
131         typedef std::map<MouseButton,Glib::RefPtr<Gtk::Action> > MouseButtonBindingMap;
132         MouseButtonBindingMap button_press_bindings;
133         MouseButtonBindingMap button_release_bindings;
134
135         ActionMap* action_map;
136         static uint32_t _ignored_state;
137 };
138
139 } // namespace
140
141 #endif /* __libgtkmm2ext_bindings_h__ */