5878c3d3acac0fed345e653d1b62dbc572047751
[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 #include "mixer_group_tabs.h"
26 #include "mixer_strip.h"
27 #include "mixer_ui.h"
28 #include "utils.h"
29 #include "i18n.h"
30 #include "route_group_dialog.h"
31
32 using namespace std;
33 using namespace Gtk;
34 using namespace ARDOUR;
35 using namespace PBD;
36
37 MixerGroupTabs::MixerGroupTabs (Mixer_UI* m)
38         : _mixer (m)
39 {
40
41 }
42
43
44 list<GroupTabs::Tab>
45 MixerGroupTabs::compute_tabs () const
46 {
47         list<Tab> tabs;
48
49         Tab tab;
50         tab.from = 0;
51         tab.group = 0;
52
53         int32_t x = 0;
54         TreeModel::Children rows = _mixer->track_model->children ();
55         for (TreeModel::Children::iterator i = rows.begin(); i != rows.end(); ++i) {
56
57                 MixerStrip* s = (*i)[_mixer->track_columns.strip];
58
59                 if (s->route()->is_master() || s->route()->is_monitor() || !s->marked_for_display()) {
60                         continue;
61                 }
62
63                 RouteGroup* g = s->route_group ();
64
65                 if (g != tab.group) {
66                         if (tab.group) {
67                                 tab.to = x;
68                                 tabs.push_back (tab);
69                         }
70
71                         tab.from = x;
72                         tab.group = g;
73
74                         if (g) {
75                                 tab.color = group_color (g);
76                         }
77                 }
78
79                 x += s->get_width ();
80         }
81
82         if (tab.group) {
83                 tab.to = x;
84                 tabs.push_back (tab);
85         }
86
87         return tabs;
88 }
89
90 void
91 MixerGroupTabs::draw_tab (cairo_t* cr, Tab const & tab) const
92 {
93         double const arc_radius = get_height();
94
95         if (tab.group && tab.group->is_active()) {
96                 cairo_set_source_rgba (cr, tab.color.get_red_p (), tab.color.get_green_p (), tab.color.get_blue_p (), 1);
97         } else {
98                 cairo_set_source_rgba (cr, 1, 1, 1, 0.2);
99         }
100
101         cairo_arc (cr, tab.from + arc_radius, get_height(), arc_radius, M_PI, 3 * M_PI / 2);
102         cairo_line_to (cr, tab.to - arc_radius, 0);
103         cairo_arc (cr, tab.to - arc_radius, get_height(), arc_radius, 3 * M_PI / 2, 2 * M_PI);
104         cairo_line_to (cr, tab.from, get_height());
105         cairo_fill (cr);
106
107         if (tab.group) {
108                 pair<string, double> const f = Gtkmm2ext::fit_to_pixels (cr, tab.group->name(), tab.to - tab.from - arc_radius * 2);
109
110                 cairo_text_extents_t ext;
111                 cairo_text_extents (cr, tab.group->name().c_str(), &ext);
112
113                 cairo_set_source_rgb (cr, 1, 1, 1);
114                 cairo_move_to (cr, tab.from + (tab.to - tab.from - f.second) / 2, get_height() - ext.height / 2);
115                 cairo_save (cr);
116                 cairo_show_text (cr, f.first.c_str());
117                 cairo_restore (cr);
118         }
119 }
120
121 double
122 MixerGroupTabs::primary_coordinate (double x, double) const
123 {
124         return x;
125 }
126
127 RouteList
128 MixerGroupTabs::routes_for_tab (Tab const * t) const
129 {
130         RouteList routes;
131         int32_t x = 0;
132
133         TreeModel::Children rows = _mixer->track_model->children ();
134         for (TreeModel::Children::iterator i = rows.begin(); i != rows.end(); ++i) {
135
136                 MixerStrip* s = (*i)[_mixer->track_columns.strip];
137
138                 if (s->route()->is_master() || s->route()->is_monitor() || !s->marked_for_display()) {
139                         continue;
140                 }
141
142                 if (x >= t->to) {
143                         /* tab finishes before this track starts */
144                         break;
145                 }
146
147                 double const h = x + s->get_width() / 2;
148
149                 if (t->from < h && t->to > h) {
150                         routes.push_back (s->route ());
151                 }
152
153                 x += s->get_width ();
154         }
155
156         return routes;
157 }
158
159 PropertyList
160 MixerGroupTabs::default_properties () const
161 {
162         PropertyList plist;
163
164         plist.add (Properties::active, true);
165         plist.add (Properties::mute, true);
166         plist.add (Properties::solo, true);
167         plist.add (Properties::gain, true);
168         plist.add (Properties::recenable, true);
169
170         return plist;
171 }
172
173 RouteList
174 MixerGroupTabs::selected_routes () const
175 {
176         RouteList rl;
177         BOOST_FOREACH (RouteUI* r, _mixer->selection().routes) {
178                 boost::shared_ptr<Route> rp = r->route();
179                 if (rp) {
180                         rl.push_back (rp);
181                 }
182         }
183         return rl;
184 }
185
186 void
187 MixerGroupTabs::sync_order_keys ()
188 {
189         _mixer->sync_order_keys_from_treeview ();
190 }