Fix saving Lua Callbacks when un/register succeeds
[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 "gtkmm2ext/utils.h"
21
22 #include "ardour/route_group.h"
23
24 #include "gtkmm2ext/colors.h"
25
26 #include "editor.h"
27 #include "editor_group_tabs.h"
28 #include "editor_route_groups.h"
29 #include "editor_routes.h"
30 #include "rgb_macros.h"
31 #include "route_time_axis.h"
32 #include "ui_config.h"
33 #include "utils.h"
34
35 #include "pbd/i18n.h"
36
37 using namespace std;
38 using namespace ARDOUR;
39 using namespace ARDOUR_UI_UTILS;
40
41 EditorGroupTabs::EditorGroupTabs (Editor* e)
42         : EditorComponent (e)
43 {
44
45 }
46
47 list<GroupTabs::Tab>
48 EditorGroupTabs::compute_tabs () const
49 {
50         list<Tab> tabs;
51
52         Tab tab;
53         tab.from = 0;
54         tab.group = 0;
55
56         int32_t y = 0;
57         for (TrackViewList::iterator i = _editor->track_views.begin(); i != _editor->track_views.end(); ++i) {
58
59                 if ((*i)->marked_for_display() == false) {
60                         continue;
61                 }
62
63                 RouteGroup* g = (*i)->route_group ();
64
65                 if (g != tab.group) {
66                         if (tab.group) {
67                                 tab.to = y;
68                                 tabs.push_back (tab);
69                         }
70
71                         tab.from = y;
72                         tab.group = g;
73                         if (g) {
74                                 tab.color = group_color (g);
75                         }
76                 }
77
78                 y += (*i)->effective_height ();
79         }
80
81         if (tab.group) {
82                 tab.to = y;
83                 tabs.push_back (tab);
84         }
85
86         return tabs;
87 }
88
89 void
90 EditorGroupTabs::draw_tab (cairo_t* cr, Tab const & tab)
91 {
92         double const arc_radius = get_width();
93         double r, g, b, a;
94
95         if (tab.group && tab.group->is_active()) {
96                 Gtkmm2ext::color_to_rgba (tab.color, r, g, b, a);
97         } else {
98                 Gtkmm2ext::color_to_rgba (UIConfiguration::instance().color ("inactive group tab"), r, g, b, a);
99         }
100
101         a = 1.0;
102
103         cairo_set_source_rgba (cr, r, g, b, a);
104         cairo_move_to (cr, 0, tab.from + arc_radius);
105         cairo_arc (cr, get_width(), tab.from + arc_radius, arc_radius, M_PI, 3 * M_PI / 2);
106         cairo_line_to (cr, get_width(), tab.to);
107         cairo_arc (cr, get_width(), tab.to - arc_radius, arc_radius, M_PI / 2, M_PI);
108         cairo_line_to (cr, 0, tab.from + arc_radius);
109         cairo_fill (cr);
110
111         if (tab.group && (tab.to - tab.from) > arc_radius) {
112                 int text_width, text_height;
113
114                 Glib::RefPtr<Pango::Layout> layout;
115                 layout = Pango::Layout::create (get_pango_context ());
116                 layout->set_ellipsize (Pango::ELLIPSIZE_MIDDLE);
117
118                 layout->set_text (tab.group->name ());
119                 layout->set_width ((tab.to - tab.from - arc_radius) * PANGO_SCALE);
120                 layout->get_pixel_size (text_width, text_height);
121
122                 cairo_move_to (cr, (get_width() - text_height) * .5, (text_width + tab.to + tab.from) * .5);
123
124                 Gtkmm2ext::Color c = Gtkmm2ext::contrasting_text_color (Gtkmm2ext::rgba_to_color (r, g, b, a));
125                 Gtkmm2ext::color_to_rgba (c, r, g, b, a);
126                 cairo_set_source_rgb (cr, r, g, b);
127
128                 cairo_save (cr);
129                 cairo_rotate (cr, M_PI * -.5);
130                 pango_cairo_show_layout (cr, layout->gobj ());
131                 cairo_restore (cr);
132         }
133 }
134
135 double
136 EditorGroupTabs::primary_coordinate (double, double y) const
137 {
138         return y;
139 }
140
141 RouteList
142 EditorGroupTabs::routes_for_tab (Tab const * t) const
143 {
144         RouteList routes;
145         int32_t y = 0;
146
147         for (TrackViewList::iterator i = _editor->track_views.begin(); i != _editor->track_views.end(); ++i) {
148
149                 if ((*i)->marked_for_display() == false) {
150                         continue;
151                 }
152
153                 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (*i);
154                 if (rtv) {
155
156                         if (y >= t->to) {
157                                 /* tab finishes before this track starts */
158                                 break;
159                         }
160
161                         double const h = y + (*i)->effective_height() / 2;
162
163                         if (t->from < h && t->to > h) {
164                                 routes.push_back (rtv->route ());
165                         }
166                 }
167
168                 y += (*i)->effective_height ();
169         }
170
171         return routes;
172 }
173
174
175 void
176 EditorGroupTabs::add_menu_items (Gtk::Menu* m, RouteGroup* g)
177 {
178         using namespace Gtk::Menu_Helpers;
179
180         if (g) {
181                 MenuList& items = m->items ();
182                 items.push_back (MenuElem (_("Fit to Window"), sigc::bind (sigc::mem_fun (*_editor, &Editor::fit_route_group), g)));
183         }
184 }
185
186 RouteList
187 EditorGroupTabs::selected_routes () const
188 {
189         RouteList rl;
190
191         for (TrackSelection::iterator i = _editor->get_selection().tracks.begin(); i != _editor->get_selection().tracks.end(); ++i) {
192                 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (*i);
193                 if (rtv) {
194                         rl.push_back (rtv->route());
195                 }
196         }
197
198         return rl;
199 }
200