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