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