most of the 2.X->3.0 commit (up to rev 4299) except for gtk2_ardour/editor_canvas...
[ardour.git] / gtk2_ardour / 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 __ardour_keyboard_h__
21 #define __ardour_keyboard_h__
22
23 #include <map>
24 #include <vector>
25 #include <string>
26
27 #include <sigc++/signal.h>
28 #include <gtk/gtk.h>
29 #include <gtkmm/accelkey.h>
30
31 #include <ardour/types.h>
32 #include <pbd/stateful.h>
33
34 #include "selection.h"
35
36 using std::string;
37
38 class Keyboard : public sigc::trackable, PBD::Stateful
39 {
40   public:
41         Keyboard ();
42         ~Keyboard ();
43
44         XMLNode& get_state (void);
45         int set_state (const XMLNode&);
46
47         typedef std::vector<uint32_t> State;
48         typedef uint32_t ModifierMask;
49
50         static uint32_t PrimaryModifier;
51         static uint32_t SecondaryModifier;
52         static uint32_t TertiaryModifier;
53         static uint32_t Level4Modifier;
54         static uint32_t CopyModifier;
55         static uint32_t RangeSelectModifier;
56
57         static void set_primary_modifier (uint32_t newval) {
58                 set_modifier (newval, PrimaryModifier);
59         }
60         static void set_secondary_modifier (uint32_t newval) {
61                 set_modifier (newval, SecondaryModifier);
62         }
63         static void set_tertiary_modifier (uint32_t newval) {
64                 set_modifier (newval, TertiaryModifier);
65         }
66         static void set_level4_modifier (uint32_t newval) {
67                 set_modifier (newval, Level4Modifier);
68         }
69         static void set_copy_modifier (uint32_t newval) {
70                 set_modifier (newval, CopyModifier);
71         }
72         static void set_range_select_modifier (uint32_t newval) {
73                 set_modifier (newval, RangeSelectModifier);
74         }
75
76         bool key_is_down (uint32_t keyval);
77
78         static GdkModifierType RelevantModifierKeyMask;
79
80         static bool no_modifier_keys_pressed(GdkEventButton* ev) {
81                 return (ev->state & RelevantModifierKeyMask) == 0;
82         }
83
84         bool leave_window (GdkEventCrossing *ev, Gtk::Window*);
85         bool enter_window (GdkEventCrossing *ev, Gtk::Window*);
86
87         static bool modifier_state_contains (guint state, ModifierMask);
88         static bool modifier_state_equals   (guint state, ModifierMask);
89
90         static Selection::Operation selection_type (guint state);
91
92         static bool no_modifiers_active (guint state);
93
94         static void set_snap_modifier (guint);
95         static ModifierMask snap_modifier () { return ModifierMask (snap_mod); }
96
97         static guint edit_button() { return edit_but; }
98         static void set_edit_button (guint);
99         static guint edit_modifier() { return edit_mod; }
100         static void set_edit_modifier(guint);
101
102         static guint delete_button() { return delete_but; }
103         static void set_delete_button(guint);
104         static guint delete_modifier() { return delete_mod; }
105         static void set_delete_modifier(guint);
106
107         static bool is_edit_event (GdkEventButton*);
108         static bool is_delete_event (GdkEventButton*);
109         static bool is_context_menu_event (GdkEventButton*);
110         static bool is_button2_event (GdkEventButton*);
111
112         static Keyboard& the_keyboard() { return *_the_keyboard; }
113
114         static bool some_magic_widget_has_focus ();
115         static void magic_widget_grab_focus ();
116         static void magic_widget_drop_focus ();
117
118         static void setup_keybindings ();
119         static void keybindings_changed ();
120         static void save_keybindings ();
121         static bool load_keybindings (std::string path);
122         static void set_can_save_keybindings (bool yn);
123         static std::string current_binding_name () { return _current_binding_name; }
124         static std::map<std::string,std::string> binding_files;
125
126         struct AccelKeyLess {
127             bool operator() (const Gtk::AccelKey a, const Gtk::AccelKey b) const {
128                     if (a.get_key() != b.get_key()) {
129                             return a.get_key() < b.get_key();
130                     } else {
131                             return a.get_mod() < b.get_mod();
132                     }
133             }
134         };
135
136   private:
137         static Keyboard* _the_keyboard;
138
139         guint           snooper_id;
140         State           state;
141
142         static guint     edit_but;
143         static guint     edit_mod;
144         static guint     delete_but;
145         static guint     delete_mod;
146         static guint     snap_mod;
147         static guint     button2_modifiers;
148         static Gtk::Window* current_window;
149         static std::string user_keybindings_path;
150         static bool can_save_keybindings;
151         static bool bindings_changed_after_save_became_legal;
152         static std::string _current_binding_name;
153
154         typedef std::pair<std::string,std::string> two_strings;
155
156         static std::map<Gtk::AccelKey,two_strings,AccelKeyLess> release_keys;
157
158         static gint _snooper (GtkWidget*, GdkEventKey*, gpointer);
159         gint snooper (GtkWidget*, GdkEventKey*);
160
161         static void set_modifier (uint32_t newval, uint32_t& variable);
162
163         static bool _some_magic_widget_has_focus;
164 };
165
166 #endif /* __ardour_keyboard_h__ */