Alter snap modifier so that it turns the grid on when it's off as well as vice-versa.
[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 class Keyboard : public sigc::trackable, PBD::Stateful
37 {
38   public:
39         Keyboard ();
40         ~Keyboard ();
41
42         XMLNode& get_state (void);
43         int set_state (const XMLNode&);
44
45         typedef std::vector<uint32_t> State;
46         typedef uint32_t ModifierMask;
47
48         static uint32_t PrimaryModifier;
49         static uint32_t SecondaryModifier;
50         static uint32_t TertiaryModifier;
51         static uint32_t Level4Modifier;
52         static uint32_t CopyModifier;
53         static uint32_t RangeSelectModifier;
54
55         static void set_primary_modifier (uint32_t newval) {
56                 set_modifier (newval, PrimaryModifier);
57         }
58         static void set_secondary_modifier (uint32_t newval) {
59                 set_modifier (newval, SecondaryModifier);
60         }
61         static void set_tertiary_modifier (uint32_t newval) {
62                 set_modifier (newval, TertiaryModifier);
63         }
64         static void set_level4_modifier (uint32_t newval) {
65                 set_modifier (newval, Level4Modifier);
66         }
67         static void set_copy_modifier (uint32_t newval) {
68                 set_modifier (newval, CopyModifier);
69         }
70         static void set_range_select_modifier (uint32_t newval) {
71                 set_modifier (newval, RangeSelectModifier);
72         }
73
74         bool key_is_down (uint32_t keyval);
75
76         static GdkModifierType RelevantModifierKeyMask;
77
78         static bool no_modifier_keys_pressed(GdkEventButton* ev) {
79                 return (ev->state & RelevantModifierKeyMask) == 0;
80         }
81
82         bool leave_window (GdkEventCrossing *ev, Gtk::Window*);
83         bool enter_window (GdkEventCrossing *ev, Gtk::Window*);
84
85         static bool modifier_state_contains (guint state, ModifierMask);
86         static bool modifier_state_equals   (guint state, ModifierMask);
87
88         static Selection::Operation selection_type (guint state);
89
90         static bool no_modifiers_active (guint state);
91
92         static void set_snap_modifier (guint);
93
94         /** @return Modifier mask to temporarily toggle grid setting; with this modifier
95          *  - magnetic or normal grid should become no grid and
96          *  - no grid should become normal grid
97          */
98         static ModifierMask snap_modifier () { return ModifierMask (snap_mod); }
99
100         static guint edit_button() { return edit_but; }
101         static void set_edit_button (guint);
102         static guint edit_modifier() { return edit_mod; }
103         static void set_edit_modifier(guint);
104
105         static guint delete_button() { return delete_but; }
106         static void set_delete_button(guint);
107         static guint delete_modifier() { return delete_mod; }
108         static void set_delete_modifier(guint);
109
110         static bool is_edit_event (GdkEventButton*);
111         static bool is_delete_event (GdkEventButton*);
112         static bool is_context_menu_event (GdkEventButton*);
113         static bool is_button2_event (GdkEventButton*);
114
115         static Keyboard& the_keyboard() { return *_the_keyboard; }
116
117         static bool some_magic_widget_has_focus ();
118         static void magic_widget_grab_focus ();
119         static void magic_widget_drop_focus ();
120
121         static void setup_keybindings ();
122         static void keybindings_changed ();
123         static void save_keybindings ();
124         static bool load_keybindings (std::string path);
125         static void set_can_save_keybindings (bool yn);
126         static std::string current_binding_name () { return _current_binding_name; }
127         static std::map<std::string,std::string> binding_files;
128
129         struct AccelKeyLess {
130             bool operator() (const Gtk::AccelKey a, const Gtk::AccelKey b) const {
131                     if (a.get_key() != b.get_key()) {
132                             return a.get_key() < b.get_key();
133                     } else {
134                             return a.get_mod() < b.get_mod();
135                     }
136             }
137         };
138
139   private:
140         static Keyboard* _the_keyboard;
141
142         guint           snooper_id;
143         State           state;
144
145         static guint     edit_but;
146         static guint     edit_mod;
147         static guint     delete_but;
148         static guint     delete_mod;
149         static guint     snap_mod;
150         static guint     button2_modifiers;
151         static Gtk::Window* current_window;
152         static std::string user_keybindings_path;
153         static bool can_save_keybindings;
154         static bool bindings_changed_after_save_became_legal;
155         static std::string _current_binding_name;
156
157         typedef std::pair<std::string,std::string> two_strings;
158
159         static std::map<Gtk::AccelKey,two_strings,AccelKeyLess> release_keys;
160
161         static gint _snooper (GtkWidget*, GdkEventKey*, gpointer);
162         gint snooper (GtkWidget*, GdkEventKey*);
163
164         static void set_modifier (uint32_t newval, uint32_t& variable);
165
166         static bool _some_magic_widget_has_focus;
167 };
168
169 #endif /* __ardour_keyboard_h__ */