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