Split route group list out of Editor.
[ardour.git] / gtk2_ardour / editor_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 "editor_group_tabs.h"
22 #include "editor.h"
23 #include "route_time_axis.h"
24 #include "utils.h"
25 #include "editor_route_groups.h"
26
27 using namespace std;
28 using namespace ARDOUR;
29
30 EditorGroupTabs::EditorGroupTabs (Editor* e)
31         : GroupTabs (e)
32 {
33         
34 }
35
36 list<GroupTabs::Tab>
37 EditorGroupTabs::compute_tabs () const
38 {
39         list<Tab> tabs;
40
41         Tab tab;
42         tab.from = 0;
43         tab.group = 0;
44
45         int32_t y = 0;
46         for (Editor::TrackViewList::iterator i = _editor->track_views.begin(); i != _editor->track_views.end(); ++i) {
47
48                 if ((*i)->marked_for_display() == false) {
49                         continue;
50                 }
51                 
52                 RouteGroup* g = (*i)->route_group ();
53
54                 if (g != tab.group) {
55                         if (tab.group) {
56                                 tab.to = y;
57                                 tab.last_ui_size = (*i)->effective_height ();
58                                 tabs.push_back (tab);
59                         }
60
61                         tab.from = y;
62                         tab.group = g;
63                         tab.colour = (*i)->color ();
64                         tab.first_ui_size = (*i)->effective_height ();
65                 }
66
67                 y += (*i)->effective_height ();
68         }
69
70         if (tab.group) {
71                 tab.to = y;
72                 tabs.push_back (tab);
73         }
74
75         return tabs;
76 }
77
78 void
79 EditorGroupTabs::draw_tab (cairo_t* cr, Tab const & tab) const
80 {
81         double const arc_radius = _width;
82
83         if (tab.group->is_active()) {
84                 cairo_set_source_rgba (cr, tab.colour.get_red_p (), tab.colour.get_green_p (), tab.colour.get_blue_p (), 1);
85         } else {
86                 cairo_set_source_rgba (cr, 1, 1, 1, 0.2);
87         }
88         
89         cairo_move_to (cr, 0, tab.from + arc_radius);
90         cairo_arc (cr, _width, tab.from + arc_radius, arc_radius, M_PI, 3 * M_PI / 2);
91         cairo_line_to (cr, _width, tab.to);
92         cairo_arc (cr, _width, tab.to - arc_radius, arc_radius, M_PI / 2, M_PI);
93         cairo_line_to (cr, 0, tab.from + arc_radius);
94         cairo_fill (cr);
95
96         pair<string, double> const f = fit_to_pixels (cr, tab.group->name(), tab.to - tab.from - arc_radius * 2);
97
98         cairo_text_extents_t ext;
99         cairo_text_extents (cr, tab.group->name().c_str(), &ext);
100
101         cairo_set_source_rgb (cr, 1, 1, 1);
102         cairo_move_to (cr, _width - ext.height / 2, tab.from + (f.second + tab.to - tab.from) / 2);
103         cairo_save (cr);
104         cairo_rotate (cr, - M_PI / 2);
105         cairo_show_text (cr, f.first.c_str());
106         cairo_restore (cr);
107 }
108
109 double
110 EditorGroupTabs::primary_coordinate (double, double y) const
111 {
112         return y;
113 }
114
115 void
116 EditorGroupTabs::reflect_tabs (list<Tab> const & tabs)
117 {
118         list<Tab>::const_iterator j = tabs.begin ();
119         
120         int32_t y = 0;
121         for (Editor::TrackViewList::iterator i = _editor->track_views.begin(); i != _editor->track_views.end(); ++i) {
122
123                 if ((*i)->marked_for_display() == false) {
124                         continue;
125                 }
126
127                 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (*i);
128                 if (rtv) {
129
130                         if (j == tabs.end()) {
131                                 
132                                 /* already run out of tabs, so no edit group */
133                                 rtv->route()->set_route_group (0, this);
134                                 
135                         } else {
136                                 
137                                 if (y >= j->to) {
138                                         /* this tab finishes before this track starts, so onto the next tab */
139                                         ++j;
140                                 }
141
142                                 double const h = y + (*i)->effective_height() / 2;
143
144                                 if (j->from < h && j->to > h) {
145                                         rtv->route()->set_route_group (j->group, this);
146                                 } else {
147                                         rtv->route()->set_route_group (0, this);
148                                 }
149                                 
150                         }
151                 }
152
153                 y += (*i)->effective_height ();
154         }
155 }
156
157
158 Gtk::Menu*
159 EditorGroupTabs::get_menu (RouteGroup *g)
160 {
161         return _editor->_route_groups->menu (g);
162 }