fix key editor resize behaviour
[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/filesystem_paths.h"
37 #include "ardour/profile.h"
38
39 #include "actions.h"
40 #include "keyboard.h"
41 #include "keyeditor.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         vpacker.set_spacing (6);
80         vpacker.set_border_width (12);
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         reset_button.add (reset_label);
99         reset_label.set_markup (string_compose ("<span size=\"large\" weight=\"bold\">%1</span>", _("Reset Bindings to Defaults")));
100                                 
101         reset_box.pack_start (reset_button, true, false);
102         reset_box.show ();
103         reset_button.show ();
104         reset_label.show ();
105         reset_button.signal_clicked().connect (sigc::mem_fun (*this, &KeyEditor::reset));
106         vpacker.pack_start (reset_box, false, false);
107
108         add (vpacker);
109
110         view.show ();
111         scroller.show ();
112         vpacker.show ();
113
114         unbind_button.set_sensitive (false);
115 }
116
117 void
118 KeyEditor::unbind ()
119 {
120         TreeModel::iterator i = view.get_selection()->get_selected();
121
122         unbind_button.set_sensitive (false);
123
124         if (i != model->children().end()) {
125                 string path = (*i)[columns.path];
126
127                 if (!(*i)[columns.bindable]) {
128                         return;
129                 }
130
131                 bool result = AccelMap::change_entry (path,
132                                                       0,
133                                                       (ModifierType) 0,
134                                                       true);
135                 if (result) {
136                         (*i)[columns.binding] = string ();
137                 }
138         }
139 }
140
141 void
142 KeyEditor::on_show ()
143 {
144         populate ();
145         view.get_selection()->unselect_all ();
146         ArdourWindow::on_show ();
147 }
148
149 void
150 KeyEditor::on_unmap ()
151 {
152         ArdourWindow::on_unmap ();
153 }
154
155 void
156 KeyEditor::action_selected ()
157 {
158         if (view.get_selection()->count_selected_rows() == 0) {
159                 return;
160         }
161
162         TreeModel::iterator i = view.get_selection()->get_selected();
163
164         unbind_button.set_sensitive (false);
165
166         if (i != model->children().end()) {
167
168                 string path = (*i)[columns.path];
169
170                 if (!(*i)[columns.bindable]) {
171                         return;
172                 }
173
174                 string binding = (*i)[columns.binding];
175
176                 if (!binding.empty()) {
177                         unbind_button.set_sensitive (true);
178                 }
179         }
180 }
181
182 bool
183 KeyEditor::on_key_press_event (GdkEventKey* ev)
184 {
185         can_bind = true;
186         last_state = ev->state;
187         return false;
188 }
189
190 bool
191 KeyEditor::on_key_release_event (GdkEventKey* ev)
192 {
193         if (ARDOUR::Profile->get_sae() || !can_bind || ev->state != last_state) {
194                 return false;
195         }
196
197         TreeModel::iterator i = view.get_selection()->get_selected();
198
199         if (i != model->children().end()) {
200                 string path = (*i)[columns.path];
201
202                 if (!(*i)[columns.bindable]) {
203                         goto out;
204                 }
205
206                 Gtkmm2ext::possibly_translate_keyval_to_make_legal_accelerator (ev->keyval);
207
208
209                 bool result = AccelMap::change_entry (path,
210                                                       ev->keyval,
211                                                       ModifierType (Keyboard::RelevantModifierKeyMask & ev->state),
212                                                       true);
213
214                 if (result) {
215                         AccelKey key;
216                         (*i)[columns.binding] = ActionManager::get_key_representation (path, key);
217                 }
218         }
219
220   out:
221         can_bind = false;
222         return true;
223 }
224
225 void
226 KeyEditor::populate ()
227 {
228         vector<string> paths;
229         vector<string> labels;
230         vector<string> tooltips;
231         vector<string> keys;
232         vector<AccelKey> bindings;
233         typedef std::map<string,TreeIter> NodeMap;
234         NodeMap nodes;
235         NodeMap::iterator r;
236
237         ActionManager::get_all_actions (labels, paths, tooltips, keys, bindings);
238
239         vector<string>::iterator k;
240         vector<string>::iterator p;
241         vector<string>::iterator t;
242         vector<string>::iterator l;
243
244         model->clear ();
245
246         for (l = labels.begin(), k = keys.begin(), p = paths.begin(), t = tooltips.begin(); l != labels.end(); ++k, ++p, ++t, ++l) {
247
248                 TreeModel::Row row;
249                 vector<string> parts;
250
251                 parts.clear ();
252
253                 split (*p, parts, '/');
254
255                 if (parts.empty()) {
256                         continue;
257                 }
258
259                 //kinda kludgy way to avoid displaying menu items as mappable
260                 if ( parts[1] == _("Main_menu") )
261                         continue;
262                 if ( parts[1] == _("redirectmenu") )
263                         continue;
264                 if ( parts[1] == _("Editor_menus") )
265                         continue;
266                 if ( parts[1] == _("RegionList") )
267                         continue;
268                 if ( parts[1] == _("ProcessorMenu") )
269                         continue;
270
271                 if ((r = nodes.find (parts[1])) == nodes.end()) {
272
273                         /* top level is missing */
274
275                         TreeIter rowp;
276                         TreeModel::Row parent;
277                         rowp = model->append();
278                         nodes[parts[1]] = rowp;
279                         parent = *(rowp);
280                         parent[columns.action] = parts[1];
281                         parent[columns.bindable] = false;
282
283                         row = *(model->append (parent.children()));
284
285                 } else {
286
287                         row = *(model->append ((*r->second)->children()));
288
289                 }
290
291                 /* add this action */
292
293                 if (l->empty ()) {
294                         row[columns.action] = *t;
295                 } else {
296                         row[columns.action] = *l;
297                 }
298                 row[columns.path] = (*p);
299                 row[columns.bindable] = true;
300
301                 if (*k == ActionManager::unbound_string) {
302                         row[columns.binding] = string();
303                 } else {
304                         row[columns.binding] = (*k);
305                 }
306         }
307 }
308
309 void
310 KeyEditor::reset ()
311 {
312         Keyboard::the_keyboard().reset_bindings ();
313 }