switch to using boost::signals2 instead of sigc++, at least for libardour. not finish...
[ardour.git] / gtk2_ardour / 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 <gtkmm/stock.h>
21 #include "ardour/session.h"
22 #include "ardour/route_group.h"
23 #include "ardour/route.h"
24 #include "route_group_dialog.h"
25 #include "group_tabs.h"
26 #include "keyboard.h"
27 #include "i18n.h"
28
29 using namespace std;
30 using namespace Gtk;
31 using namespace ARDOUR;
32 using Gtkmm2ext::Keyboard;
33
34 GroupTabs::GroupTabs (Editor* e)
35         : EditorComponent (e),
36           _dragging (0),
37           _dragging_new_tab (0)
38 {
39
40 }
41
42 void
43 GroupTabs::set_session (Session* s)
44 {
45         EditorComponent::set_session (s);
46
47         if (_session) {
48                 _session_connections.add_connection (_session->RouteGroupChanged.connect (boost::bind (&GroupTabs::set_dirty, this)));
49         }
50 }
51
52
53 /** Handle a size request.
54  *  @param req GTK requisition
55  */
56 void
57 GroupTabs::on_size_request (Gtk::Requisition *req)
58 {
59         /* Use a dummy, small width and the actual height that we want */
60         req->width = 16;
61         req->height = 16;
62 }
63
64 bool
65 GroupTabs::on_button_press_event (GdkEventButton* ev)
66 {
67         using namespace Menu_Helpers;
68
69         double const p = primary_coordinate (ev->x, ev->y);
70
71         list<Tab>::iterator prev;
72         list<Tab>::iterator next;
73         Tab* t = click_to_tab (p, &prev, &next);
74
75         _drag_min = prev != _tabs.end() ? prev->to : 0;
76         _drag_max = next != _tabs.end() ? next->from : extent ();
77
78         if (ev->button == 1) {
79
80                 if (t == 0) {
81                         Tab n;
82                         n.from = n.to = p;
83                         _dragging_new_tab = true;
84
85                         if (next == _tabs.end()) {
86                                 _tabs.push_back (n);
87                                 t = &_tabs.back ();
88                         } else {
89                                 list<Tab>::iterator j = _tabs.insert (next, n);
90                                 t = &(*j);
91                         }
92                         
93                 } else {
94                         _dragging_new_tab = false;
95                 }
96
97                 _dragging = t;
98                 _drag_moved = false;
99                 _drag_first = p;
100
101                 double const h = (t->from + t->to) / 2;
102                 if (p < h) {
103                         _drag_moving = t->from;
104                         _drag_fixed = t->to;
105                         _drag_offset = p - t->from;
106                 } else {
107                         _drag_moving = t->to;
108                         _drag_fixed = t->from;
109                         _drag_offset = p - t->to;
110                 }
111
112         } else if (ev->button == 3) {
113
114                 RouteGroup* g = t ? t->group : 0;
115                 Menu* m = get_menu (g);
116                 if (m) {
117                         m->popup (ev->button, ev->time);
118                 }
119
120         }
121
122         return true;
123 }
124
125
126 bool
127 GroupTabs::on_motion_notify_event (GdkEventMotion* ev)
128 {
129         if (_dragging == 0) {
130                 return false;
131         }
132
133         double const p = primary_coordinate (ev->x, ev->y);
134
135         if (p != _drag_first) {
136                 _drag_moved = true;
137         }
138
139         _drag_moving = p - _drag_offset;
140
141         _dragging->from = min (_drag_moving, _drag_fixed);
142         _dragging->to = max (_drag_moving, _drag_fixed);
143
144         _dragging->from = max (_dragging->from, _drag_min);
145         _dragging->to = min (_dragging->to, _drag_max);
146
147         set_dirty ();
148         queue_draw ();
149
150         return true;
151 }
152
153
154 bool
155 GroupTabs::on_button_release_event (GdkEventButton* ev)
156 {
157         if (_dragging == 0) {
158                 return false;
159         }
160
161         if (!_drag_moved) {
162
163                 if (_dragging->group) {
164                         
165                         if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
166                                 
167                                 /* edit */
168                                 RouteGroupDialog d (_dragging->group, Gtk::Stock::APPLY);
169                                 d.do_run ();
170                                 
171                         } else {
172                                 
173                                 /* toggle active state */
174                                 _dragging->group->set_active (!_dragging->group->is_active (), this);
175                                 
176                         }
177                 }
178
179         } else {
180                 /* finish drag */
181                 RouteList routes = routes_for_tab (_dragging);
182
183                 if (!routes.empty()) {
184                         if (_dragging_new_tab) {
185                                 RouteGroup* g = new_route_group ();
186                                 if (g) {
187                                         for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
188                                                 g->add (*i);
189                                         }
190                                 }
191                         } else {
192                                 boost::shared_ptr<RouteList> r = _session->get_routes ();
193                                 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
194
195                                         if (find (routes.begin(), routes.end(), *i) == routes.end()) {
196                                                 /* this route is not on the list of those that should be in _dragging's group */
197                                                 if ((*i)->route_group() == _dragging->group) {
198                                                         _dragging->group->remove (*i);
199                                                 }
200                                         } else {
201                                                 _dragging->group->add (*i);
202                                         }
203                                 }
204                         }
205                 }
206
207                 set_dirty ();
208                 queue_draw ();
209         }
210
211         _dragging = 0;
212
213         return true;
214 }
215
216 void
217 GroupTabs::render (cairo_t* cr)
218 {
219         if (_dragging == 0) {
220                 _tabs = compute_tabs ();
221         }
222
223         /* background */
224
225         cairo_set_source_rgb (cr, 0, 0, 0);
226         cairo_rectangle (cr, 0, 0, _width, _height);
227         cairo_fill (cr);
228
229         /* tabs */
230
231         for (list<Tab>::const_iterator i = _tabs.begin(); i != _tabs.end(); ++i) {
232                 draw_tab (cr, *i);
233         }
234 }
235
236
237 /** Convert a click position to a tab.
238  *  @param c Click position.
239  *  @param prev Filled in with the previous tab to the click, or 0.
240  *  @param next Filled in with the next tab after the click, or 0.
241  *  @return Tab under the click, or 0.
242  */
243
244 GroupTabs::Tab *
245 GroupTabs::click_to_tab (double c, list<Tab>::iterator* prev, list<Tab>::iterator* next)
246 {
247         *prev = *next = _tabs.end ();
248         Tab* under = 0;
249
250         list<Tab>::iterator i = _tabs.begin ();
251         while (i != _tabs.end()) {
252
253                 if (i->from > c) {
254                         break;
255                 }
256
257                 if (i->to < c) {
258                         *prev = i;
259                         ++i;
260                         continue;
261                 }
262
263                 if (i->from <= c && c < i->to) {
264                         under = &(*i);
265                 }
266
267                 ++i;
268         }
269
270         if (i != _tabs.end()) {
271                 *next = i;
272
273                 if (under) {
274                         *next++;
275                 }
276         }
277
278         return under;
279 }
280