delegate port-connections to low priority thread.
[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 <gtk/gtk.h>
28 #include <gtkmm/accelkey.h>
29
30 #include "pbd/stateful.h"
31 #include "pbd/signals.h"
32
33 #include "gtkmm2ext/visibility.h"
34
35 namespace Gtk {
36         class Window;
37 }
38
39 namespace Gtkmm2ext {
40
41 class Bindings;
42 class ActionMap;
43
44 class LIBGTKMM2EXT_API Keyboard : public sigc::trackable, PBD::Stateful
45 {
46   public:
47         Keyboard ();
48         ~Keyboard ();
49
50         XMLNode& get_state (void);
51         int set_state (const XMLNode&, int version);
52
53         virtual void setup_keybindings () = 0;
54
55         typedef std::vector<uint32_t> State;
56         typedef uint32_t ModifierMask;
57
58         static uint32_t PrimaryModifier;
59         static uint32_t SecondaryModifier;
60         static uint32_t TertiaryModifier;
61         static uint32_t Level4Modifier;
62         static uint32_t CopyModifier;
63         static uint32_t RangeSelectModifier;
64         static uint32_t GainFineScaleModifier;
65         static uint32_t GainExtraFineScaleModifier;
66
67         // Modifiers for scroll wheel
68         static uint32_t ScrollZoomVerticalModifier;
69         static uint32_t ScrollZoomHorizontalModifier;
70         static uint32_t ScrollHorizontalModifier;
71
72         static const char* primary_modifier_name ();
73         static const char* secondary_modifier_name ();
74         static const char* tertiary_modifier_name ();
75         static const char* level4_modifier_name ();
76
77         static void set_primary_modifier (uint32_t newval) {
78                 set_modifier (newval, PrimaryModifier);
79         }
80         static void set_secondary_modifier (uint32_t newval) {
81                 set_modifier (newval, SecondaryModifier);
82         }
83         static void set_tertiary_modifier (uint32_t newval) {
84                 set_modifier (newval, TertiaryModifier);
85         }
86         static void set_level4_modifier (uint32_t newval) {
87                 set_modifier (newval, Level4Modifier);
88         }
89         static void set_copy_modifier (uint32_t newval) {
90                 set_modifier (newval, CopyModifier);
91         }
92         static void set_range_select_modifier (uint32_t newval) {
93                 set_modifier (newval, RangeSelectModifier);
94         }
95
96         bool key_is_down (uint32_t keyval);
97
98         static GdkModifierType RelevantModifierKeyMask;
99
100         static bool no_modifier_keys_pressed(GdkEventButton* ev) {
101                 return (ev->state & RelevantModifierKeyMask) == 0;
102         }
103
104         static bool no_modifier_keys_pressed(GdkEventKey* ev) {
105                 return (ev->state & RelevantModifierKeyMask) == 0;
106         }
107
108         bool leave_window (GdkEventCrossing *ev, Gtk::Window*);
109         bool enter_window (GdkEventCrossing *ev, Gtk::Window*);
110         bool focus_in_window (GdkEventFocus *ev, Gtk::Window*);
111         bool focus_out_window (GdkEventFocus *ev, Gtk::Window*);
112
113         static bool modifier_state_contains (guint state, ModifierMask);
114         static bool modifier_state_equals   (guint state, ModifierMask);
115
116         static bool no_modifiers_active (guint state);
117
118         static void set_snap_modifier (guint);
119         /** @return Modifier mask to temporarily toggle grid setting; with this modifier
120          *  - magnetic or normal grid should become no grid and
121          *  - no grid should become normal grid
122          */
123         static ModifierMask snap_modifier () { return ModifierMask (snap_mod); }
124
125         static void set_snap_delta_modifier (guint);
126         /** @return Modifier mask to temporarily toggle between relative and absolute grid setting.
127          *  Absolute grid is for aligning objects with the grid lines.
128          *  Relative grid is for maintaining an initial position relative to the grid lines.
129          *  With this modifier:
130          *  - magnetic or normal grid should snap relative to an initial grid offset
131          *  - no grid should snap relative to the grid.
132          */
133         static ModifierMask snap_delta_modifier () { return ModifierMask (snap_delta_mod); }
134
135         static guint edit_button() { return edit_but; }
136         static void set_edit_button (guint);
137         static guint edit_modifier() { return edit_mod; }
138         static void set_edit_modifier(guint);
139
140         static guint delete_button() { return delete_but; }
141         static void set_delete_button(guint);
142         static guint delete_modifier() { return delete_mod; }
143         static void set_delete_modifier(guint);
144
145         static guint insert_note_button() { return insert_note_but; }
146         static void set_insert_note_button (guint);
147         static guint insert_note_modifier() { return insert_note_mod; }
148         static void set_insert_note_modifier(guint);
149
150         static bool is_edit_event (GdkEventButton*);
151         static bool is_delete_event (GdkEventButton*);
152         static bool is_insert_note_event (GdkEventButton*);
153         static bool is_context_menu_event (GdkEventButton*);
154         static bool is_button2_event (GdkEventButton*);
155
156         static Keyboard& the_keyboard() { return *_the_keyboard; }
157
158         static bool some_magic_widget_has_focus ();
159         static void magic_widget_grab_focus ();
160         static void magic_widget_drop_focus ();
161         static Gtk::Window* get_current_window () { return current_window; };
162
163         static void close_current_dialog ();
164
165         static void keybindings_changed ();
166         static void save_keybindings ();
167         static void set_can_save_keybindings (bool yn);
168         static std::string current_binding_name () { return _current_binding_name; }
169         static std::map<std::string,std::string> binding_files;
170
171         static bool catch_user_event_for_pre_dialog_focus (GdkEvent* ev, Gtk::Window* w);
172
173         static bool load_keybindings (std::string const& path);
174         static void save_keybindings (std::string const& path);
175
176         static std::string binding_filename_suffix;
177
178         int reset_bindings ();
179
180         sigc::signal0<void> ZoomVerticalModifierReleased;
181
182   protected:
183         static Keyboard* _the_keyboard;
184
185         guint           snooper_id;
186         State           state;
187
188         static guint     edit_but;
189         static guint     edit_mod;
190         static guint     delete_but;
191         static guint     delete_mod;
192         static guint     insert_note_but;
193         static guint     insert_note_mod;
194         static guint     snap_mod;
195         static guint     snap_delta_mod;
196         static guint     button2_modifiers;
197         static Gtk::Window* current_window;
198         static std::string user_keybindings_path;
199         static bool can_save_keybindings;
200         static bool bindings_changed_after_save_became_legal;
201         static std::string _current_binding_name;
202
203         typedef std::pair<std::string,std::string> two_strings;
204
205         static gint _snooper (GtkWidget*, GdkEventKey*, gpointer);
206         gint snooper (GtkWidget*, GdkEventKey*);
207
208         static void set_modifier (uint32_t newval, uint32_t& variable);
209
210         static bool _some_magic_widget_has_focus;
211
212         static Gtk::Window* pre_dialog_active_window;
213
214         static int read_keybindings (std::string const& path);
215         static int store_keybindings (std::string const& path);
216 };
217
218 } /* namespace */
219
220 #endif /* __libgtkmm2ext_keyboard_h__ */