8df76fef258ca5c18c592a01efa1603e4d50a163
[ardour.git] / gtk2_ardour / editor_edit_groups.cc
1 /*
2     Copyright (C) 2000 Paul Davis 
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
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18     $Id$
19 */
20
21 #include <cstdlib>
22 #include <cmath>
23
24 #include <gtkmm2ext/stop_signal.h>
25 #include <gtkmm2ext/gtk_ui.h>
26 #include <ardour/route_group.h>
27
28 #include "editor.h"
29 #include "keyboard.h"
30 #include "marker.h"
31 #include "time_axis_view.h"
32 #include "prompter.h"
33 #include "gui_thread.h"
34
35 #include <ardour/route.h>
36
37 #include "i18n.h"
38
39 using namespace sigc;
40 using namespace ARDOUR;
41 using namespace PBD;
42 using namespace Gtk;
43
44 void
45 Editor::build_edit_group_list_menu ()
46 {
47         using namespace Gtk::Menu_Helpers;
48
49         edit_group_list_menu = new Menu;
50         edit_group_list_menu->set_name ("ArdourContextMenu");
51         MenuList& items = edit_group_list_menu->items();
52
53         items.push_back (MenuElem (_("Activate All"), mem_fun(*this, &Editor::activate_all_edit_groups)));
54         items.push_back (MenuElem (_("Disable All"), mem_fun(*this, &Editor::disable_all_edit_groups)));
55         items.push_back (SeparatorElem());
56         items.push_back (MenuElem (_("Add group"), mem_fun(*this, &Editor::new_edit_group)));
57         
58 }
59
60 void
61 Editor::activate_all_edit_groups ()
62 {
63         Gtk::TreeModel::Children children = group_model->children();
64         for(Gtk::TreeModel::Children::iterator iter = children.begin(); iter != children.end(); ++iter) {
65                 (*iter)[group_columns.is_active] = true;
66         }
67 }
68
69 void
70 Editor::disable_all_edit_groups ()
71 {
72         Gtk::TreeModel::Children children = group_model->children();
73         for(Gtk::TreeModel::Children::iterator iter = children.begin(); iter != children.end(); ++iter) {
74                 (*iter)[group_columns.is_active] = false;
75         }
76 }
77
78 void
79 Editor::new_edit_group ()
80 {
81         session->add_edit_group ("");
82 }
83
84 void
85 Editor::remove_selected_edit_group ()
86 {
87         Glib::RefPtr<TreeSelection> selection = edit_group_display.get_selection();
88         TreeView::Selection::ListHandle_Path rows = selection->get_selected_rows ();
89
90         if (rows.empty()) {
91                 return;
92         }
93
94         TreeView::Selection::ListHandle_Path::iterator i = rows.begin();
95         TreeIter iter;
96         
97         /* selection mode is single, so rows.begin() is it */
98
99         if ((iter = group_model->get_iter (*i))) {
100
101                 RouteGroup* rg = (*iter)[group_columns.routegroup];
102
103                 if (rg) {
104                         session->remove_edit_group (*rg);
105                 }
106         }
107 }
108
109 void
110 Editor::edit_group_list_button_clicked ()
111 {
112         new_edit_group ();
113 }
114
115 gint
116 Editor::edit_group_list_button_press_event (GdkEventButton* ev)
117 {
118         if (Keyboard::is_context_menu_event (ev)) {
119                 if (edit_group_list_menu == 0) {
120                         build_edit_group_list_menu ();
121                 }
122                 edit_group_list_menu->popup (1, 0);
123                 return true;
124         }
125
126
127         RouteGroup* group;
128         TreeIter iter;
129         TreeModel::Path path;
130         TreeViewColumn* column;
131         int cellx;
132         int celly;
133
134         if (!edit_group_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
135                 return false;
136         }
137
138         switch (GPOINTER_TO_UINT (column->get_data (X_("colnum")))) {
139         case 0:
140                 if (Keyboard::is_edit_event (ev)) {
141                         if ((iter = group_model->get_iter (path))) {
142                                 if ((group = (*iter)[group_columns.routegroup]) != 0) {
143                                         // edit_route_group (group);
144                                         return true;
145                                 }
146                         }
147                         
148                 } 
149                 break;
150
151         case 1:
152                 if ((iter = group_model->get_iter (path))) {
153                         bool active = (*iter)[group_columns.is_active];
154                         (*iter)[group_columns.is_active] = !active;
155                         return true;
156                 }
157                 break;
158                 
159         case 2:
160                 if ((iter = group_model->get_iter (path))) {
161                         bool visible = (*iter)[group_columns.is_visible];
162                         (*iter)[group_columns.is_visible] = !visible;
163                         return true;
164                 }
165                 break;
166
167         default:
168                 break;
169         }
170         
171         return false;
172  }
173
174 void 
175 Editor::edit_group_row_change (const Gtk::TreeModel::Path& path,const Gtk::TreeModel::iterator& iter)
176 {
177         RouteGroup* group;
178
179         if (in_edit_group_row_change) {
180                 return;
181         }
182
183         if ((group = (*iter)[group_columns.routegroup]) == 0) {
184                 return;
185         }
186
187         if ((*iter)[group_columns.is_visible]) {
188                 for (TrackViewList::iterator j = track_views.begin(); j != track_views.end(); ++j) {
189                         if ((*j)->edit_group() == group) {
190                                 show_track_in_display (**j);
191                         }
192                 }
193         } else {
194                 for (TrackViewList::iterator j = track_views.begin(); j != track_views.end(); ++j) {
195                         if ((*j)->edit_group() == group) {
196                                 hide_track_in_display (**j);
197                         }
198                 }
199         }
200
201         bool active = (*iter)[group_columns.is_active];
202         group->set_active (active, this);
203
204
205         string name = (*iter)[group_columns.text];
206
207         if (name != group->name()) {
208                 group->set_name (name);
209         }
210 }
211
212 void
213 Editor::add_edit_group (RouteGroup* group)
214 {
215         ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::add_edit_group), group));
216         bool focus = false;
217
218         TreeModel::Row row = *(group_model->append());
219         row[group_columns.is_active] = group->is_active();
220         row[group_columns.is_visible] = !group->is_hidden();
221
222         in_edit_group_row_change = true;
223
224         row[group_columns.routegroup] = group;
225
226         if (!group->name().empty()) {
227                 row[group_columns.text] = group->name();
228         } else {
229                 row[group_columns.text] = _("unnamed");
230                 focus = true;
231         }
232
233         group->FlagsChanged.connect (bind (mem_fun(*this, &Editor::group_flags_changed), group));
234
235         if (focus) {
236                 TreeViewColumn* col = edit_group_display.get_column (0);
237                 CellRendererText* name_cell = dynamic_cast<CellRendererText*>(edit_group_display.get_column_cell_renderer (0));
238                 edit_group_display.set_cursor (group_model->get_path (row), *col, *name_cell, true);
239         }
240
241         in_edit_group_row_change = false;
242 }
243
244 void
245 Editor::edit_groups_changed ()
246 {
247         ENSURE_GUI_THREAD (mem_fun (*this, &Editor::edit_groups_changed));
248
249         /* just rebuild the while thing */
250
251         group_model->clear ();
252
253         {
254                 TreeModel::Row row;
255                 row = *(group_model->append());
256                 row[group_columns.is_active] = false;
257                 row[group_columns.is_visible] = true;
258                 row[group_columns.text] = (_("-all-"));
259                 row[group_columns.routegroup] = 0;
260         }
261
262         session->foreach_edit_group (mem_fun (*this, &Editor::add_edit_group));
263 }
264
265 void
266 Editor::group_flags_changed (void* src, RouteGroup* group)
267 {
268         ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::group_flags_changed), src, group));
269
270         in_edit_group_row_change = true;
271
272         Gtk::TreeModel::Children children = group_model->children();
273         for(Gtk::TreeModel::Children::iterator iter = children.begin(); iter != children.end(); ++iter) {
274                 if (group == (*iter)[group_columns.routegroup]) {
275                         (*iter)[group_columns.is_active] = group->is_active();
276                         (*iter)[group_columns.is_visible] = !group->is_hidden();
277                         (*iter)[group_columns.text] = group->name();
278                 }
279         }
280
281         in_edit_group_row_change = false;
282 }
283
284 void
285 Editor::edit_group_name_edit (const Glib::ustring& path, const Glib::ustring& new_text)
286 {
287         RouteGroup* group;
288         TreeIter iter;
289         
290         if ((iter = group_model->get_iter (path))) {
291         
292                 if ((group = (*iter)[group_columns.routegroup]) == 0) {
293                         return;
294                 }
295                 
296                 if (new_text != group->name()) {
297                         group->set_name (new_text);
298                 }
299         }
300 }