Remove unneeded header include
[ardour.git] / gtk2_ardour / keyeditor.cc
1 /*
2     Copyright (C) 2002 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 #ifdef WAF_BUILD
21 #include "gtk2ardour-config.h"
22 #endif
23
24 #include <map>
25
26 #include <gtkmm/stock.h>
27 #include <gtkmm/label.h>
28 #include <gtkmm/accelkey.h>
29 #include <gtkmm/accelmap.h>
30 #include <gtkmm/uimanager.h>
31
32 #include "gtkmm2ext/utils.h"
33
34 #include "pbd/strsplit.h"
35
36 #include "ardour/profile.h"
37
38 #include "actions.h"
39 #include "keyboard.h"
40 #include "keyeditor.h"
41 #include "utils.h"
42
43 #include "i18n.h"
44
45 using namespace std;
46 using namespace Gtk;
47 using namespace Gdk;
48 using namespace PBD;
49
50 using Gtkmm2ext::Keyboard;
51
52 KeyEditor::KeyEditor ()
53         : ArdourWindow (_("Key Bindings"))
54         , unbind_button (_("Remove shortcut"))
55         , unbind_box (BUTTONBOX_END)
56
57 {
58         can_bind = false;
59         last_state = 0;
60
61         model = TreeStore::create(columns);
62
63         view.set_model (model);
64         view.append_column (_("Action"), columns.action);
65         view.append_column (_("Shortcut"), columns.binding);
66         view.set_headers_visible (true);
67         view.get_selection()->set_mode (SELECTION_SINGLE);
68         view.set_reorderable (false);
69         view.set_size_request (500,300);
70         view.set_enable_search (false);
71         view.set_rules_hint (true);
72         view.set_name (X_("KeyEditorTree"));
73
74         view.get_selection()->signal_changed().connect (sigc::mem_fun (*this, &KeyEditor::action_selected));
75
76         scroller.add (view);
77         scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
78
79         add (scroller);
80
81         if (!ARDOUR::Profile->get_sae()) {
82
83                 Label* hint = manage (new Label (_("Select an action, then press the key(s) to (re)set its shortcut")));
84                 hint->show ();
85                 unbind_box.set_spacing (6);
86                 unbind_box.pack_start (*hint, false, true);
87                 unbind_box.pack_start (unbind_button, false, false);
88                 unbind_button.signal_clicked().connect (sigc::mem_fun (*this, &KeyEditor::unbind));
89
90                 add (unbind_box);
91                 unbind_box.show ();
92                 unbind_button.show ();
93
94         }
95
96         view.show ();
97         scroller.show ();
98
99         unbind_button.set_sensitive (false);
100 }
101
102 void
103 KeyEditor::unbind ()
104 {
105         TreeModel::iterator i = view.get_selection()->get_selected();
106
107         unbind_button.set_sensitive (false);
108
109         if (i != model->children().end()) {
110                 string path = (*i)[columns.path];
111
112                 if (!(*i)[columns.bindable]) {
113                         return;
114                 }
115
116                 bool result = AccelMap::change_entry (path,
117                                                       0,
118                                                       (ModifierType) 0,
119                                                       true);
120                 if (result) {
121                         (*i)[columns.binding] = string ();
122                 }
123         }
124 }
125
126 void
127 KeyEditor::on_show ()
128 {
129         populate ();
130         view.get_selection()->unselect_all ();
131         ArdourWindow::on_show ();
132 }
133
134 void
135 KeyEditor::on_unmap ()
136 {
137         ArdourWindow::on_unmap ();
138 }
139
140 void
141 KeyEditor::action_selected ()
142 {
143         if (view.get_selection()->count_selected_rows() == 0) {
144                 return;
145         }
146
147         TreeModel::iterator i = view.get_selection()->get_selected();
148
149         unbind_button.set_sensitive (false);
150
151         if (i != model->children().end()) {
152
153                 string path = (*i)[columns.path];
154
155                 if (!(*i)[columns.bindable]) {
156                         return;
157                 }
158
159                 string binding = (*i)[columns.binding];
160
161                 if (!binding.empty()) {
162                         unbind_button.set_sensitive (true);
163                 }
164         }
165 }
166
167 bool
168 KeyEditor::on_key_press_event (GdkEventKey* ev)
169 {
170         can_bind = true;
171         last_state = ev->state;
172         return false;
173 }
174
175 bool
176 KeyEditor::on_key_release_event (GdkEventKey* ev)
177 {
178         if (ARDOUR::Profile->get_sae() || !can_bind || ev->state != last_state) {
179                 return false;
180         }
181
182         TreeModel::iterator i = view.get_selection()->get_selected();
183
184         if (i != model->children().end()) {
185                 string path = (*i)[columns.path];
186
187                 if (!(*i)[columns.bindable]) {
188                         goto out;
189                 }
190
191                 Gtkmm2ext::possibly_translate_keyval_to_make_legal_accelerator (ev->keyval);
192
193
194                 bool result = AccelMap::change_entry (path,
195                                                       ev->keyval,
196                                                       ModifierType (Keyboard::RelevantModifierKeyMask & ev->state),
197                                                       true);
198
199                 if (result) {
200                         AccelKey key;
201                         (*i)[columns.binding] = ActionManager::get_key_representation (path, key);
202                 }
203         }
204
205   out:
206         can_bind = false;
207         return true;
208 }
209
210 void
211 KeyEditor::populate ()
212 {
213         vector<string> paths;
214         vector<string> labels;
215         vector<string> tooltips;
216         vector<string> keys;
217         vector<AccelKey> bindings;
218         typedef std::map<string,TreeIter> NodeMap;
219         NodeMap nodes;
220         NodeMap::iterator r;
221
222         ActionManager::get_all_actions (labels, paths, tooltips, keys, bindings);
223
224         vector<string>::iterator k;
225         vector<string>::iterator p;
226         vector<string>::iterator t;
227         vector<string>::iterator l;
228
229         model->clear ();
230
231         for (l = labels.begin(), k = keys.begin(), p = paths.begin(), t = tooltips.begin(); l != labels.end(); ++k, ++p, ++t, ++l) {
232
233                 TreeModel::Row row;
234                 vector<string> parts;
235
236                 parts.clear ();
237
238                 split (*p, parts, '/');
239
240                 if (parts.empty()) {
241                         continue;
242                 }
243
244                 //kinda kludgy way to avoid displaying menu items as mappable
245                 if ( parts[1] == _("Main_menu") )
246                         continue;
247                 if ( parts[1] == _("JACK") )
248                         continue;
249                 if ( parts[1] == _("redirectmenu") )
250                         continue;
251                 if ( parts[1] == _("Editor_menus") )
252                         continue;
253                 if ( parts[1] == _("RegionList") )
254                         continue;
255                 if ( parts[1] == _("ProcessorMenu") )
256                         continue;
257
258                 if ((r = nodes.find (parts[1])) == nodes.end()) {
259
260                         /* top level is missing */
261
262                         TreeIter rowp;
263                         TreeModel::Row parent;
264                         rowp = model->append();
265                         nodes[parts[1]] = rowp;
266                         parent = *(rowp);
267                         parent[columns.action] = parts[1];
268                         parent[columns.bindable] = false;
269
270                         row = *(model->append (parent.children()));
271
272                 } else {
273
274                         row = *(model->append ((*r->second)->children()));
275
276                 }
277
278                 /* add this action */
279
280                 if (l->empty ()) {
281                         row[columns.action] = *t;
282                 } else {
283                         row[columns.action] = *l;
284                 }
285                 row[columns.path] = (*p);
286                 row[columns.bindable] = true;
287
288                 if (*k == ActionManager::unbound_string) {
289                         row[columns.binding] = string();
290                 } else {
291                         row[columns.binding] = (*k);
292                 }
293         }
294 }