Fix problems with dragging route groups so that they are too small.
[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 "ardour/route_group.h"
21 #include "ardour/session.h"
22 #include "mixer_group_tabs.h"
23 #include "mixer_strip.h"
24 #include "mixer_ui.h"
25 #include "utils.h"
26
27 using namespace std;
28 using namespace ARDOUR;
29
30 MixerGroupTabs::MixerGroupTabs (Mixer_UI* m)
31         : _mixer (m)
32 {
33         
34 }
35
36
37 list<GroupTabs::Tab>
38 MixerGroupTabs::compute_tabs () const
39 {
40         list<Tab> tabs;
41         
42         Tab tab;
43         tab.from = 0;
44         tab.group = 0;
45
46         int32_t x = 0;
47         for (list<MixerStrip*>::iterator i = _mixer->strips.begin(); i != _mixer->strips.end(); ++i) {
48
49                 if ((*i)->route()->is_master() || (*i)->route()->is_control() || !(*i)->marked_for_display()) {
50                         continue;
51                 }
52
53                 RouteGroup* g = (*i)->route_group ();
54
55                 if (g != tab.group) {
56                         if (tab.group) {
57                                 tab.to = x;
58                                 tab.last_ui_size = (*i)->get_width ();
59                                 tabs.push_back (tab);
60                         }
61
62                         tab.from = x;
63                         tab.group = g;
64                         tab.colour = (*i)->color ();
65                         tab.first_ui_size = (*i)->get_width ();
66                 }
67
68                 x += (*i)->get_width ();
69         }
70
71         if (tab.group) {
72                 tab.to = x;
73                 tabs.push_back (tab);
74         }
75
76         return tabs;
77 }
78
79 void
80 MixerGroupTabs::draw_tab (cairo_t* cr, Tab const & tab) const
81 {
82         double const arc_radius = _height;
83
84         if (tab.group->is_active()) {
85                 cairo_set_source_rgba (cr, tab.colour.get_red_p (), tab.colour.get_green_p (), tab.colour.get_blue_p (), 1);
86         } else {
87                 cairo_set_source_rgba (cr, 1, 1, 1, 0.2);
88         }
89         
90         cairo_arc (cr, tab.from + arc_radius, _height, arc_radius, M_PI, 3 * M_PI / 2);
91         cairo_line_to (cr, tab.to - arc_radius, 0);
92         cairo_arc (cr, tab.to - arc_radius, _height, arc_radius, 3 * M_PI / 2, 2 * M_PI);
93         cairo_line_to (cr, tab.from, _height);
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, tab.from + (tab.to - tab.from - f.second) / 2, _height - ext.height / 2);
103         cairo_save (cr);
104         cairo_show_text (cr, f.first.c_str());
105         cairo_restore (cr);
106 }
107
108 double
109 MixerGroupTabs::primary_coordinate (double x, double) const
110 {
111         return x;
112 }
113
114 void
115 MixerGroupTabs::reflect_tabs (list<Tab> const & tabs)
116 {
117         list<Tab>::const_iterator j = tabs.begin ();
118         
119         int32_t x = 0;
120         for (list<MixerStrip*>::iterator i = _mixer->strips.begin(); i != _mixer->strips.end(); ++i) {
121
122                 if ((*i)->route()->is_master() || (*i)->route()->is_control() || !(*i)->marked_for_display()) {
123                         continue;
124                 }
125                 
126                 if (j == tabs.end()) {
127                         
128                         /* already run out of tabs, so no edit group */
129                         (*i)->route()->set_route_group (0, this);
130                         
131                 } else {
132                         
133                         if (x >= j->to) {
134                                 /* this tab finishes before this track starts, so onto the next tab */
135                                 ++j;
136                         }
137                         
138                         double const h = x + (*i)->get_width() / 2;
139                         
140                         if (j->from < h && j->to > h) {
141                                 (*i)->route()->set_route_group (j->group, this);
142                         } else {
143                                 (*i)->route()->set_route_group (0, this);
144                         }
145                         
146                 }
147
148                 x += (*i)->get_width ();
149         }
150 }
151