Update canvas/UI lib GPL boilerplate and (C) from git log
[ardour.git] / libs / gtkmm2ext / action_model.cc
1 /*
2  * Copyright (C) 2019 Johannes Mueller <github@johannes-mueller.org>
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 along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include <vector>
20
21 #include  <gtkmm/combobox.h>
22 #include <gtkmm/liststore.h>
23
24 #include "pbd/i18n.h"
25 #include "pbd/strsplit.h"
26
27 #include "gtkmm2ext/actions.h"
28 #include "gtkmm2ext/action_model.h"
29
30 using namespace std;
31 using namespace Gtk;
32
33 namespace ActionManager {
34
35 const ActionModel&
36 ActionModel::instance ()
37 {
38         static ActionModel am;
39         return am;
40 }
41
42 ActionModel::ActionModel ()
43 {
44         _model = TreeStore::create (_columns);
45         _model->clear ();
46
47         typedef std::map<string,TreeIter> NodeMap;
48         NodeMap nodes;
49         NodeMap::iterator r;
50
51         TreeIter rowp;
52         TreeModel::Row parent;
53
54         rowp = _model->append ();
55         parent = *(rowp);
56         parent[_columns.name] = _("Disabled");
57
58         vector<string> paths;
59         vector<string> labels;
60         vector<string> tooltips;
61         vector<string> keys;
62         vector<Glib::RefPtr<Gtk::Action> > actions;
63
64         get_all_actions (paths, labels, tooltips, keys, actions);
65
66         vector<string>::iterator k;
67         vector<string>::iterator p;
68         vector<string>::iterator t;
69         vector<string>::iterator l;
70
71         for (l = labels.begin(), k = keys.begin(), p = paths.begin(), t = tooltips.begin(); l != labels.end(); ++k, ++p, ++t, ++l) {
72
73                 TreeModel::Row row;
74                 vector<string> parts;
75                 parts.clear ();
76                 split (*p, parts, '/');
77
78                 if (parts.empty()) {
79                         continue;
80                 }
81
82                 //kinda kludgy way to avoid displaying menu items as mappable
83                 if ( parts[0] == _("Main_menu") )
84                         continue;
85                 if ( parts[0] == _("JACK") )
86                         continue;
87                 if ( parts[0] == _("redirectmenu") )
88                         continue;
89                 if ( parts[0] == _("Editor_menus") )
90                         continue;
91                 if ( parts[0] == _("RegionList") )
92                         continue;
93                 if ( parts[0] == _("ProcessorMenu") )
94                         continue;
95
96                 if ((r = nodes.find (parts[0])) == nodes.end()) {
97                         /* top level is missing */
98
99                         TreeIter rowp;
100                         TreeModel::Row parent;
101                         rowp = _model->append();
102                         nodes[parts[0]] = rowp;
103                         parent = *(rowp);
104                         parent[_columns.name] = parts[0];
105
106                         row = *(_model->append (parent.children()));
107                 } else {
108                         row = *(_model->append ((*r->second)->children()));
109                 }
110
111                 /* add this action */
112
113                 if (l->empty ()) {
114                         row[_columns.name] = *t;
115                 } else {
116                         row[_columns.name] = *l;
117                 }
118
119                 row[_columns.path] = *p;
120         }
121 }
122
123 bool
124 ActionModel::find_action_in_model (const TreeModel::iterator& iter, std::string const & action_path, TreeModel::iterator* found) const
125 {
126         TreeModel::Row row = *iter;
127         string path = row[_columns.path];
128
129         if (path == action_path) {
130                 *found = iter;
131                 return true;
132         }
133
134         return false;
135 }
136
137 void
138 ActionModel::build_action_combo (ComboBox& cb, string const& current_action) const
139 {
140         cb.set_model (_model);
141         cb.pack_start (_columns.name);
142
143         if (current_action.empty()) {
144                 cb.set_active (0); /* "disabled" */
145                 return;
146         }
147
148         TreeModel::iterator iter = _model->children().end();
149
150         _model->foreach_iter (sigc::bind (sigc::mem_fun (*this, &ActionModel::find_action_in_model), current_action, &iter));
151
152         if (iter != _model->children().end()) {
153                 cb.set_active (iter);
154         } else {
155                 cb.set_active (0);
156         }
157 }
158
159 void
160 ActionModel::build_custom_action_combo (ComboBox& cb, const vector<pair<string,string> >& actions, const string& current_action) const
161 {
162         Glib::RefPtr<Gtk::ListStore> model (Gtk::ListStore::create (_columns));
163         TreeIter rowp;
164         TreeModel::Row row;
165         int active_row = -1;
166         int n;
167         vector<pair<string,string> >::const_iterator i;
168
169         rowp = model->append();
170         row = *(rowp);
171         row[_columns.name] = _("Disabled");
172         row[_columns.path] = string();
173
174         if (current_action.empty()) {
175                 active_row = 0;
176         }
177
178         for (i = actions.begin(), n = 0; i != actions.end(); ++i, ++n) {
179                 rowp = model->append();
180                 row = *(rowp);
181                 row[_columns.name] = i->first;
182                 row[_columns.path] = i->second;
183                 if (current_action == i->second) {
184                         active_row = n+1;
185                 }
186         }
187
188         cb.set_model (model);
189         cb.pack_start (_columns.name);
190
191         if (active_row >= 0) {
192                 cb.set_active (active_row);
193         }
194 }
195
196 }