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