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