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