the Properties & 64bit region commit
[ardour.git] / gtk2_ardour / mixer_group_tabs.cc
1 /*
2     Copyright (C) 2009 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 */
19
20 #include "ardour/route_group.h"
21 #include "ardour/session.h"
22 #include "mixer_group_tabs.h"
23 #include "mixer_strip.h"
24 #include "mixer_ui.h"
25 #include "utils.h"
26 #include "i18n.h"
27 #include "route_group_dialog.h"
28
29 using namespace std;
30 using namespace Gtk;
31 using namespace ARDOUR;
32 using namespace PBD;
33
34 MixerGroupTabs::MixerGroupTabs (Mixer_UI* m)
35         : GroupTabs (0),
36           _mixer (m),
37           _menu (0)
38 {
39
40 }
41
42
43 list<GroupTabs::Tab>
44 MixerGroupTabs::compute_tabs () const
45 {
46         list<Tab> tabs;
47
48         Tab tab;
49         tab.from = 0;
50         tab.group = 0;
51
52         int32_t x = 0;
53         TreeModel::Children rows = _mixer->track_model->children ();
54         for (TreeModel::Children::iterator i = rows.begin(); i != rows.end(); ++i) {
55
56                 MixerStrip* s = (*i)[_mixer->track_columns.strip];
57
58                 if (s->route()->is_master() || s->route()->is_control() || !s->marked_for_display()) {
59                         continue;
60                 }
61
62                 RouteGroup* g = s->route_group ();
63
64                 if (g != tab.group) {
65                         if (tab.group) {
66                                 tab.to = x;
67                                 tabs.push_back (tab);
68                         }
69
70                         tab.from = x;
71                         tab.group = g;
72                         tab.colour = s->color ();
73                 }
74
75                 x += s->get_width ();
76         }
77
78         if (tab.group) {
79                 tab.to = x;
80                 tabs.push_back (tab);
81         }
82
83         return tabs;
84 }
85
86 void
87 MixerGroupTabs::draw_tab (cairo_t* cr, Tab const & tab) const
88 {
89         double const arc_radius = _height;
90
91         if (tab.group && tab.group->is_active()) {
92                 cairo_set_source_rgba (cr, tab.colour.get_red_p (), tab.colour.get_green_p (), tab.colour.get_blue_p (), 1);
93         } else {
94                 cairo_set_source_rgba (cr, 1, 1, 1, 0.2);
95         }
96
97         cairo_arc (cr, tab.from + arc_radius, _height, arc_radius, M_PI, 3 * M_PI / 2);
98         cairo_line_to (cr, tab.to - arc_radius, 0);
99         cairo_arc (cr, tab.to - arc_radius, _height, arc_radius, 3 * M_PI / 2, 2 * M_PI);
100         cairo_line_to (cr, tab.from, _height);
101         cairo_fill (cr);
102
103         if (tab.group) {
104                 pair<string, double> const f = fit_to_pixels (cr, tab.group->name(), tab.to - tab.from - arc_radius * 2);
105                 
106                 cairo_text_extents_t ext;
107                 cairo_text_extents (cr, tab.group->name().c_str(), &ext);
108                 
109                 cairo_set_source_rgb (cr, 1, 1, 1);
110                 cairo_move_to (cr, tab.from + (tab.to - tab.from - f.second) / 2, _height - ext.height / 2);
111                 cairo_save (cr);
112                 cairo_show_text (cr, f.first.c_str());
113                 cairo_restore (cr);
114         }
115 }
116
117 double
118 MixerGroupTabs::primary_coordinate (double x, double) const
119 {
120         return x;
121 }
122
123 RouteList
124 MixerGroupTabs::routes_for_tab (Tab const * t) const
125 {
126         RouteList routes;
127         int32_t x = 0;
128
129         TreeModel::Children rows = _mixer->track_model->children ();
130         for (TreeModel::Children::iterator i = rows.begin(); i != rows.end(); ++i) {
131
132                 MixerStrip* s = (*i)[_mixer->track_columns.strip];
133
134                 if (s->route()->is_master() || s->route()->is_control() || !s->marked_for_display()) {
135                         continue;
136                 }
137
138                 if (x >= t->to) {
139                         /* tab finishes before this track starts */
140                         break;
141                 }
142
143                 double const h = x + s->get_width() / 2;
144
145                 if (t->from < h && t->to > h) {
146                         routes.push_back (s->route ());
147                 }
148
149                 x += s->get_width ();
150         }
151
152         return routes;
153 }
154
155 Gtk::Menu*
156 MixerGroupTabs::get_menu (RouteGroup* g)
157 {
158         if (g == 0) {
159                 return 0;
160         }
161
162         using namespace Menu_Helpers;
163
164         delete _menu;
165         _menu = new Menu;
166
167         MenuList& items = _menu->items ();
168         items.push_back (MenuElem (_("Edit..."), sigc::bind (sigc::mem_fun (*this, &MixerGroupTabs::edit_group), g)));
169         items.push_back (MenuElem (_("Subgroup"), sigc::bind (sigc::mem_fun (*this, &MixerGroupTabs::make_subgroup), g)));
170         items.push_back (SeparatorElem());
171         items.push_back (MenuElem (_("Remove"), sigc::bind (sigc::mem_fun (*this, &MixerGroupTabs::remove_group), g)));
172
173         return _menu;
174 }
175
176 void
177 MixerGroupTabs::edit_group (RouteGroup* g)
178 {
179         RouteGroupDialog d (g, Gtk::Stock::APPLY);
180         d.do_run ();
181 }
182
183 void
184 MixerGroupTabs::remove_group (RouteGroup *g)
185 {
186         _session->remove_route_group (*g);
187 }
188
189 void
190 MixerGroupTabs::make_subgroup (RouteGroup* g)
191 {
192         g->make_subgroup ();
193 }
194
195 void
196 MixerGroupTabs::destroy_subgroup (RouteGroup* g)
197 {
198         g->destroy_subgroup ();
199 }
200
201 ARDOUR::RouteGroup *
202 MixerGroupTabs::new_route_group () const
203 {
204         PropertyList plist;
205
206         plist.add (Properties::active, true);
207         plist.add (Properties::mute, true);
208         plist.add (Properties::solo, true);
209         plist.add (Properties::gain, true);
210         plist.add (Properties::recenable, true);
211
212         RouteGroup* g = new RouteGroup (*_session, "");
213         g->set_properties (plist);
214
215         RouteGroupDialog d (g, Gtk::Stock::NEW);
216         int const r = d.do_run ();
217
218         if (r != Gtk::RESPONSE_OK) {
219                 delete g;
220                 return 0;
221         }
222         
223         _session->add_route_group (g);
224         return g;
225 }