Make level meter colors user definable. Base, Mid, Top, and Clip colors are defined...
[ardour.git] / gtk2_ardour / keyeditor.cc
1 #include <map>
2
3 #include <ardour/profile.h>
4
5 #include <gtkmm/stock.h>
6 #include <gtkmm/accelkey.h>
7 #include <gtkmm/accelmap.h>
8 #include <gtkmm/uimanager.h>
9
10 #include <pbd/strsplit.h>
11 #include <pbd/replace_all.h>
12
13 #include "actions.h"
14 #include "keyboard.h"
15 #include "keyeditor.h"
16
17 #include "i18n.h"
18
19 using namespace std;
20 using namespace Gtk;
21 using namespace Gdk;
22 using namespace PBD;
23
24 KeyEditor::KeyEditor ()
25         : ArdourDialog (_("Keybinding Editor"), false)
26 {
27         can_bind = false;
28         last_state = 0;
29
30         model = TreeStore::create(columns);
31
32         view.set_model (model);
33         view.append_column (_("Action"), columns.action);
34         view.append_column (_("Binding"), columns.binding);
35         view.set_headers_visible (true);
36         view.get_selection()->set_mode (SELECTION_SINGLE);
37         view.set_reorderable (false);
38         view.set_size_request (500,300);
39         view.set_enable_search (false);
40         view.set_rules_hint (true);
41         view.set_name (X_("KeyEditorTree"));
42         
43         view.get_selection()->signal_changed().connect (mem_fun (*this, &KeyEditor::action_selected));
44         
45         scroller.add (view);
46         scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
47
48         get_vbox()->pack_start (scroller);
49         get_vbox()->set_border_width (12);
50
51         scroller.show ();
52         view.show ();
53 }
54
55 void
56 KeyEditor::on_show ()
57 {
58         populate ();
59         view.get_selection()->unselect_all ();
60         ArdourDialog::on_show ();
61 }
62
63 void
64 KeyEditor::on_unmap ()
65 {
66         ArdourDialog::on_unmap ();
67 }
68
69 void
70 KeyEditor::action_selected ()
71 {
72 }
73
74 bool
75 KeyEditor::on_key_press_event (GdkEventKey* ev)
76 {
77         can_bind = true;
78         last_state = ev->state;
79         return false;
80 }
81
82 bool
83 KeyEditor::on_key_release_event (GdkEventKey* ev)
84 {
85         if (ARDOUR::Profile->get_sae() || !can_bind || ev->state != last_state) {
86                 return false;
87         }
88
89         TreeModel::iterator i = view.get_selection()->get_selected();
90
91         if (i != model->children().end()) {
92                 string path = (*i)[columns.path];
93                 
94                 if (!(*i)[columns.bindable]) {
95                         goto out;
96                 } 
97
98                 bool result = AccelMap::change_entry (path,
99                                                       ev->keyval,
100                                                       (ModifierType) ev->state,
101                                                       true);
102
103                 if (result) {
104                         bool known;
105                         AccelKey key;
106
107                         known = ActionManager::lookup_entry (path, key);
108                         
109                         if (known) {
110                                 (*i)[columns.binding] = ActionManager::ui_manager->get_accel_group()->name (key.get_key(), Gdk::ModifierType (key.get_mod()));
111                         } else {
112                                 (*i)[columns.binding] = string();
113                         }
114                 }
115
116                 
117         }
118
119   out:
120         can_bind = false;
121         return true;
122 }
123
124 void
125 KeyEditor::populate ()
126 {
127         vector<string> paths;
128         vector<string> labels;
129         vector<string> keys;
130         vector<AccelKey> bindings;
131         typedef std::map<string,TreeIter> NodeMap;
132         NodeMap nodes;
133         NodeMap::iterator r;
134         
135         ActionManager::get_all_actions (labels, paths, keys, bindings);
136         
137         vector<string>::iterator k;
138         vector<string>::iterator p;
139         vector<string>::iterator l;
140
141         model->clear ();
142
143         for (l = labels.begin(), k = keys.begin(), p = paths.begin(); l != labels.end(); ++k, ++p, ++l) {
144
145                 TreeModel::Row row;
146                 vector<string> parts;
147                 
148                 parts.clear ();
149
150                 split (*p, parts, '/');
151                 
152                 if (parts.empty()) {
153                         continue;
154                 }
155
156                 if ((r = nodes.find (parts[1])) == nodes.end()) {
157
158                         /* top level is missing */
159
160                         TreeIter rowp;
161                         TreeModel::Row parent;
162                         rowp = model->append();
163                         nodes[parts[1]] = rowp;
164                         parent = *(rowp);
165                         parent[columns.action] = parts[1];
166                         parent[columns.bindable] = false;
167
168                         row = *(model->append (parent.children()));
169
170                 } else {
171                         
172                         row = *(model->append ((*r->second)->children()));
173
174                 }
175                 
176                 /* add this action */
177
178                 row[columns.action] = (*l);
179                 row[columns.path] = (*p);
180                 row[columns.bindable] = true;
181                 
182                 if (*k == ActionManager::unbound_string) {
183                         row[columns.binding] = string();
184                 } else {
185
186 #ifdef GTKOSX
187                         string label = (*k);
188                         replace_all (label, "<Mod5>", _("Command-"));
189                         replace_all (label, "<Alt>", _("Option-"));
190                         replace_all (label, "<Shift>", _("Shift-"));
191                         row[columns.binding] = label;
192 #else           
193                         row[columns.binding] = (*k);
194 #endif
195                 }
196         }
197 }