backport/minimize difference to mixbus branch
[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 <boost/foreach.hpp>
21
22 #include "gtkmm2ext/utils.h"
23
24 #include "ardour/route_group.h"
25
26 #include "canvas/colors.h"
27
28 #include "mixer_group_tabs.h"
29 #include "mixer_strip.h"
30 #include "mixer_ui.h"
31 #include "rgb_macros.h"
32 #include "route_group_dialog.h"
33 #include "ui_config.h"
34 #include "utils.h"
35
36 #include "pbd/i18n.h"
37
38 using namespace std;
39 using namespace Gtk;
40 using namespace ARDOUR;
41 using namespace ARDOUR_UI_UTILS;
42 using namespace PBD;
43
44 MixerGroupTabs::MixerGroupTabs (Mixer_UI* m)
45         : _mixer (m)
46 {
47
48 }
49
50
51 list<GroupTabs::Tab>
52 MixerGroupTabs::compute_tabs () const
53 {
54         list<Tab> tabs;
55
56         Tab tab;
57         tab.from = 0;
58         tab.group = 0;
59
60         int32_t x = 0;
61         TreeModel::Children rows = _mixer->track_model->children ();
62         for (TreeModel::Children::iterator i = rows.begin(); i != rows.end(); ++i) {
63
64                 AxisView* av = (*i)[_mixer->stripable_columns.strip];
65                 MixerStrip* s = dynamic_cast<MixerStrip*> (av);
66
67                 if (!s) {
68                         continue;
69                 }
70
71                 if (s->route()->is_master() || s->route()->is_monitor() || !s->marked_for_display()) {
72                         continue;
73                 }
74 #ifdef MIXBUS
75                 if (s->route()->mixbus()) {
76                         continue;
77                 }
78 #endif
79
80                 RouteGroup* g = s->route_group ();
81
82                 if (g != tab.group) {
83                         if (tab.group) {
84                                 tab.to = x;
85                                 tabs.push_back (tab);
86                         }
87
88                         tab.from = x;
89                         tab.group = g;
90
91                         if (g) {
92                                 tab.color = group_color (g);
93                         }
94                 }
95
96                 int ww = 0, wh = 0;
97                 s->get_size_request (ww, wh); // widget may not be realized, get_width() is invalid.
98                 x += ww;
99         }
100
101         if (tab.group) {
102                 tab.to = x;
103                 tabs.push_back (tab);
104         }
105
106         return tabs;
107 }
108
109 void
110 MixerGroupTabs::draw_tab (cairo_t* cr, Tab const & tab) const
111 {
112         double const arc_radius = get_height();
113         double r, g, b, a;
114
115         if (tab.group && tab.group->is_active()) {
116                 ArdourCanvas::color_to_rgba (tab.color, r, g, b, a);
117         } else {
118                 ArdourCanvas::color_to_rgba (UIConfiguration::instance().color ("inactive group tab"), r, g, b, a);
119         }
120
121         a = 1.0;
122
123         cairo_set_source_rgba (cr, r, g, b, a);
124         cairo_arc (cr, tab.from + arc_radius, get_height(), arc_radius, M_PI, 3 * M_PI / 2);
125         cairo_line_to (cr, tab.to - arc_radius, 0);
126         cairo_arc (cr, tab.to - arc_radius, get_height(), arc_radius, 3 * M_PI / 2, 2 * M_PI);
127         cairo_line_to (cr, tab.from, get_height());
128         cairo_fill (cr);
129
130         if (tab.group) {
131                 pair<string, double> const f = Gtkmm2ext::fit_to_pixels (cr, tab.group->name(), tab.to - tab.from - arc_radius * 2);
132
133                 cairo_text_extents_t ext;
134                 cairo_text_extents (cr, tab.group->name().c_str(), &ext);
135
136                 ArdourCanvas::Color c = ArdourCanvas::contrasting_text_color (ArdourCanvas::rgba_to_color (r, g, b, a));
137                 ArdourCanvas::color_to_rgba (c, r, g, b, a);
138
139                 cairo_set_source_rgb (cr, r, g, b);
140                 cairo_move_to (cr, tab.from + (tab.to - tab.from - f.second) / 2, get_height() - ext.height / 2);
141                 cairo_save (cr);
142                 cairo_show_text (cr, f.first.c_str());
143                 cairo_restore (cr);
144         }
145 }
146
147 double
148 MixerGroupTabs::primary_coordinate (double x, double) const
149 {
150         return x;
151 }
152
153 RouteList
154 MixerGroupTabs::routes_for_tab (Tab const * t) const
155 {
156         RouteList routes;
157         int32_t x = 0;
158
159         TreeModel::Children rows = _mixer->track_model->children ();
160         for (TreeModel::Children::iterator i = rows.begin(); i != rows.end(); ++i) {
161
162                 AxisView* av = (*i)[_mixer->stripable_columns.strip];
163                 MixerStrip* s = dynamic_cast<MixerStrip*> (av);
164
165                 if (!s) {
166                         continue;
167                 }
168
169                 if (s->route()->is_master() || s->route()->is_monitor() || !s->marked_for_display()) {
170                         continue;
171                 }
172
173                 if (x >= t->to) {
174                         /* tab finishes before this track starts */
175                         break;
176                 }
177
178                 double const h = x + s->get_width() / 2;
179
180                 if (t->from < h && t->to > h) {
181                         routes.push_back (s->route ());
182                 }
183
184                 x += s->get_width ();
185         }
186
187         return routes;
188 }
189
190 RouteList
191 MixerGroupTabs::selected_routes () const
192 {
193         RouteList rl;
194         BOOST_FOREACH (AxisView* r, _mixer->selection().axes) {
195                 boost::shared_ptr<Route> rp = boost::dynamic_pointer_cast<Route> (r->stripable());
196                 if (rp) {
197                         rl.push_back (rp);
198                 }
199         }
200         return rl;
201 }
202