Fix for clicking on tabs.
[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 "time_axis_view.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 void
36 EditorGroupTabs::set_session (Session* s)
37 {
38         s->RouteEditGroupChanged.connect (mem_fun (*this, &EditorGroupTabs::set_dirty));
39 }
40
41
42 /** Handle a size request.
43  *  @param req GTK requisition
44  */
45 void
46 EditorGroupTabs::on_size_request (Gtk::Requisition *req)
47 {
48         /* Use a dummy, small height and the actual width that we want */
49         req->width = 16;
50         req->height = 16;
51 }
52
53
54 void
55 EditorGroupTabs::render (cairo_t* cr)
56 {
57         /* background */
58         
59         cairo_set_source_rgb (cr, 0, 0, 0);
60         cairo_rectangle (cr, 0, 0, _width, _height);
61         cairo_fill (cr);
62
63         int32_t curr_start = 0;
64         RouteGroup* curr_group = 0;
65         Gdk::Color curr_colour;
66
67         int32_t y = 0;
68         for (Editor::TrackViewList::iterator i = _editor->track_views.begin(); i != _editor->track_views.end(); ++i) {
69
70                 if ((*i)->marked_for_display() == false) {
71                         continue;
72                 }
73                 
74                 RouteGroup* g = (*i)->edit_group ();
75
76                 if (g != curr_group) {
77                         if (curr_group) {
78                                 draw_group (cr, curr_start, y, curr_group, curr_colour);
79                         }
80
81                         curr_start = y;
82                         curr_group = g;
83                         curr_colour = (*i)->color ();
84                 }
85
86                 y += (*i)->effective_height ();
87         }
88
89         if (curr_group) {
90                 draw_group (cr, curr_start, y, curr_group, curr_colour);
91         }
92 }
93
94 void
95 EditorGroupTabs::draw_group (cairo_t* cr, int32_t y1, int32_t y2, RouteGroup* g, Gdk::Color const & colour)
96 {
97         double const arc_radius = _width;
98
99         if (g->is_active()) {
100                 cairo_set_source_rgba (cr, colour.get_red_p (), colour.get_green_p (), colour.get_blue_p (), 1);
101         } else {
102                 cairo_set_source_rgba (cr, 1, 1, 1, 0.2);
103         }
104         
105         cairo_move_to (cr, 0, y1 + arc_radius);
106         cairo_arc (cr, _width, y1 + arc_radius, arc_radius, M_PI, 3 * M_PI / 2);
107         cairo_line_to (cr, _width, y2);
108         cairo_arc (cr, _width, y2 - arc_radius, arc_radius, M_PI / 2, M_PI);
109         cairo_line_to (cr, 0, y1 + arc_radius);
110         cairo_fill (cr);
111
112         pair<string, double> const f = fit_to_pixels (cr, g->name(), y2 - y1 - arc_radius * 2);
113
114         cairo_text_extents_t ext;
115         cairo_text_extents (cr, g->name().c_str(), &ext);
116
117         cairo_set_source_rgb (cr, 1, 1, 1);
118         cairo_move_to (cr, _width - ext.height / 2, y1 + (f.second + y2 - y1) / 2);
119         cairo_save (cr);
120         cairo_rotate (cr, - M_PI / 2);
121         cairo_show_text (cr, f.first.c_str());
122         cairo_restore (cr);
123 }
124
125 bool
126 EditorGroupTabs::on_button_press_event (GdkEventButton* ev)
127 {
128         int32_t y = 0;
129         Editor::TrackViewList::iterator i = _editor->track_views.begin();
130         while (y < ev->y && i != _editor->track_views.end()) {
131
132                 if ((*i)->marked_for_display()) {
133                         y += (*i)->effective_height ();
134                 }
135                 
136                 if (y < ev->y) {
137                         ++i;
138                 }
139         }
140
141         if (i == _editor->track_views.end()) {
142                 return false;
143         }
144         
145         RouteGroup* g = (*i)->edit_group ();
146         if (g) {
147                 g->set_active (!g->is_active (), this);
148         }
149
150         return true;
151 }