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