Fix missing return.
[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         return false;
76 }
77
78 bool
79 KeyEditor::on_key_release_event (GdkEventKey* ev)
80 {
81         if (!can_bind || ev->state != last_state) {
82                 return false;
83         }
84
85         TreeModel::iterator i = view.get_selection()->get_selected();
86
87         if (i != model->children().end()) {
88                 string path = (*i)[columns.path];
89                 
90                 if (!(*i)[columns.bindable]) {
91                         goto out;
92                 } 
93
94                 bool result = AccelMap::change_entry (path,
95                                                       ev->keyval,
96                                                       (ModifierType) ev->state,
97                                                       true);
98
99                 if (result) {
100                         bool known;
101                         AccelKey key;
102
103                         known = ActionManager::lookup_entry (path, key);
104                         
105                         if (known) {
106                                 (*i)[columns.binding] = ActionManager::ui_manager->get_accel_group()->name (key.get_key(), Gdk::ModifierType (key.get_mod()));
107                         } else {
108                                 (*i)[columns.binding] = string();
109                         }
110                 }
111
112                 
113         }
114
115   out:
116         can_bind = false;
117         return true;
118 }
119
120 void
121 KeyEditor::populate ()
122 {
123         vector<string> paths;
124         vector<string> labels;
125         vector<string> keys;
126         vector<AccelKey> bindings;
127         typedef std::map<string,TreeIter> NodeMap;
128         NodeMap nodes;
129         NodeMap::iterator r;
130         
131         ActionManager::get_all_actions (labels, paths, keys, bindings);
132         
133         vector<string>::iterator k;
134         vector<string>::iterator p;
135         vector<string>::iterator l;
136
137         model->clear ();
138
139         for (l = labels.begin(), k = keys.begin(), p = paths.begin(); l != labels.end(); ++k, ++p, ++l) {
140                 
141                 TreeModel::Row row;
142                 vector<string> parts;
143                 
144                 parts.clear ();
145
146                 split (*p, parts, '/');
147                 
148                 if (parts.empty()) {
149                         continue;
150                 }
151
152                 if ((r = nodes.find (parts[1])) == nodes.end()) {
153
154                         /* top level is missing */
155
156                         TreeIter rowp;
157                         TreeModel::Row parent;
158                         rowp = model->append();
159                         nodes[parts[1]] = rowp;
160                         parent = *(rowp);
161                         parent[columns.action] = parts[1];
162                         parent[columns.bindable] = false;
163
164                         row = *(model->append (parent.children()));
165
166                 } else {
167                         
168                         row = *(model->append ((*r->second)->children()));
169
170                 }
171                 
172                 /* add this action */
173
174                 row[columns.action] = (*l);
175                 row[columns.path] = (*p);
176                 row[columns.bindable] = true;
177                 
178                 if (*k == ActionManager::unbound_string) {
179                         row[columns.binding] = string();
180                 } else {
181                         row[columns.binding] = (*k);
182                 }
183         }
184 }