more ardour-button-ization; fix transparency of selection rects; use "correct" cursor...
[ardour.git] / gtk2_ardour / time_info_box.cc
1 /*
2     Copyright (C) 2011 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 <algorithm>
21 #include "pbd/compose.h"
22
23 #include "gtkmm2ext/cairocell.h"
24 #include "gtkmm2ext/gui_thread.h"
25 #include "gtkmm2ext/utils.h"
26 #include "gtkmm2ext/stateful_button.h"
27 #include "gtkmm2ext/actions.h"
28
29 #include "ardour/location.h"
30 #include "ardour/session.h"
31
32 #include "time_info_box.h"
33 #include "audio_clock.h"
34 #include "editor.h"
35
36 #include "i18n.h"
37
38 using namespace Gtk;
39 using namespace ARDOUR;
40 using std::min;
41 using std::max;
42
43 TimeInfoBox::TimeInfoBox ()
44         : left (2, 4)
45         , right (2, 4)
46         , syncing_selection (false)
47         , syncing_punch (false)
48 {
49         selection_start = new AudioClock ("selection-start", false, "SelectionClockDisplay", false, false, false, false);
50         selection_end = new AudioClock ("selection-end", false, "SelectionClockDisplay", false, false, false, false);
51         selection_length = new AudioClock ("selection-length", false, "SelectionClockDisplay", false, false, true, false);
52
53         punch_start = new AudioClock ("punch-start", false, "PunchClockDisplay", false, false, false, false);
54         punch_end = new AudioClock ("punch-end", false, "PunchClockDisplay", false, false, false, false);
55
56         CairoEditableText& ss (selection_start->main_display());
57         ss.set_corner_radius (0);
58
59         CairoEditableText& se (selection_end->main_display());
60         se.set_corner_radius (0);
61
62         CairoEditableText& sl (selection_length->main_display());
63         sl.set_corner_radius (0);
64
65         CairoEditableText& ps (punch_start->main_display());
66         ps.set_corner_radius (0);
67
68         CairoEditableText& pe (punch_end->main_display());
69         pe.set_corner_radius (0);
70
71         selection_title.set_text (_("Selection"));
72         punch_title.set_text (_("Punch"));
73
74         set_homogeneous (false);
75         set_spacing (6);
76         set_border_width (2);
77
78         pack_start (left, true, true);
79         pack_start (right, true, true);
80
81         left.set_homogeneous (false);
82         left.set_spacings (0);
83         left.set_border_width (2);
84         left.set_col_spacings (2);
85
86         right.set_homogeneous (false);
87         right.set_spacings (0);
88         right.set_border_width (2);
89         right.set_col_spacings (2);
90
91
92         Gtk::Label* l;
93
94         selection_title.set_name ("TimeInfoSelectionTitle");
95         left.attach (selection_title, 0, 2, 0, 1);
96         l = manage (new Label);
97         l->set_text (_("Start"));
98         l->set_alignment (1.0, 0.5);
99         l->set_name (X_("TimeInfoSelectionLabel"));
100         left.attach (*l, 0, 1, 1, 2, FILL);
101         left.attach (*selection_start, 1, 2, 1, 2);
102
103         l = manage (new Label);
104         l->set_text (_("End"));
105         l->set_alignment (1.0, 0.5);
106         l->set_name (X_("TimeInfoSelectionLabel"));
107         left.attach (*l, 0, 1, 2, 3, FILL);
108         left.attach (*selection_end, 1, 2, 2, 3);
109
110         l = manage (new Label);
111         l->set_text (_("Length"));
112         l->set_alignment (1.0, 0.5);
113         l->set_name (X_("TimeInfoSelectionLabel"));
114         left.attach (*l, 0, 1, 3, 4, FILL);
115         left.attach (*selection_length, 1, 2, 3, 4);
116
117         punch_in_button.set_name ("punch button");
118         punch_out_button.set_name ("punch button");
119         punch_in_button.set_text (_("In"));
120         punch_out_button.set_text (_("Out"));
121
122         Glib::RefPtr<Action> act = ActionManager::get_action ("Transport", "TogglePunchIn");
123         punch_in_button.set_related_action (act);
124         act = ActionManager::get_action ("Transport", "TogglePunchOut");
125         punch_out_button.set_related_action (act);
126
127         Gtkmm2ext::UI::instance()->set_tip (punch_in_button, _("Start recording at auto-punch start"));
128         Gtkmm2ext::UI::instance()->set_tip (punch_out_button, _("Stop recording at auto-punch end"));
129
130         punch_title.set_name ("TimeInfoSelectionTitle");
131         right.attach (punch_title, 2, 4, 0, 1);
132         right.attach (punch_in_button, 2, 3, 1, 2, FILL, SHRINK);
133         right.attach (*punch_start, 3, 4, 1, 2);
134         right.attach (punch_out_button, 2, 3, 2, 3, FILL, SHRINK);
135         right.attach (*punch_end, 3, 4, 2, 3);
136
137         show_all ();
138
139         selection_start->mode_changed.connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::sync_selection_mode), selection_start));
140         selection_end->mode_changed.connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::sync_selection_mode), selection_end));
141         selection_length->mode_changed.connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::sync_selection_mode), selection_length));
142
143         punch_start->mode_changed.connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::sync_punch_mode), punch_start));
144         punch_end->mode_changed.connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::sync_punch_mode), punch_end));
145
146         selection_start->signal_button_release_event().connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::clock_button_release_event), selection_start), true);
147         selection_end->signal_button_release_event().connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::clock_button_release_event), selection_end), true);
148
149         punch_start->signal_button_release_event().connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::clock_button_release_event), punch_start), true);
150         punch_end->signal_button_release_event().connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::clock_button_release_event), punch_end), true);
151
152         Editor::instance().get_selection().TimeChanged.connect (sigc::mem_fun (*this, &TimeInfoBox::selection_changed));
153         Editor::instance().get_selection().RegionsChanged.connect (sigc::mem_fun (*this, &TimeInfoBox::selection_changed));
154
155         Editor::instance().MouseModeChanged.connect (editor_connections, invalidator(*this), ui_bind (&TimeInfoBox::track_mouse_mode, this), gui_context());
156 }
157
158 TimeInfoBox::~TimeInfoBox ()
159 {
160         delete selection_length;
161         delete selection_end;
162         delete selection_start;
163         
164         delete punch_start;
165         delete punch_end;
166 }
167
168 void
169 TimeInfoBox::track_mouse_mode ()
170 {
171         selection_changed ();
172 }
173
174 bool
175 TimeInfoBox::clock_button_release_event (GdkEventButton* ev, AudioClock* src)
176 {
177         if (!_session) {
178                 return false;
179         }
180
181         if (ev->button == 1) {
182                 _session->request_locate (src->current_time ());
183                 return true;
184         }
185
186         return false;
187 }
188
189 void
190 TimeInfoBox::sync_selection_mode (AudioClock* src)
191 {
192         if (!syncing_selection) {
193                 syncing_selection = true;
194                 selection_start->set_mode (src->mode());
195                 selection_end->set_mode (src->mode());
196                 selection_length->set_mode (src->mode());
197                 syncing_selection = false;
198         }
199 }
200
201 void
202 TimeInfoBox::sync_punch_mode (AudioClock* src)
203 {
204         if (!syncing_punch) {
205                 syncing_punch = true;
206                 punch_start->set_mode (src->mode());
207                 punch_end->set_mode (src->mode());
208                 syncing_punch = false;
209         }
210 }
211         
212
213 void
214 TimeInfoBox::set_session (Session* s)
215 {
216         SessionHandlePtr::set_session (s);
217
218         selection_start->set_session (s);
219         selection_end->set_session (s);
220         selection_length->set_session (s);
221
222         punch_start->set_session (s);
223         punch_end->set_session (s);
224
225         if (s) {
226                 Location* punch = s->locations()->auto_punch_location ();
227                 
228                 if (punch) {
229                         watch_punch (punch);
230                 }
231                 
232                 punch_changed (punch);
233
234                 _session->auto_punch_location_changed.connect (_session_connections, MISSING_INVALIDATOR, 
235                                                                boost::bind (&TimeInfoBox::punch_location_changed, this, _1), gui_context());
236         }
237 }
238
239 void
240 TimeInfoBox::selection_changed ()
241 {
242         framepos_t s, e;
243         Selection& selection (Editor::instance().get_selection());
244
245         switch (Editor::instance().current_mouse_mode()) {
246
247         case Editing::MouseObject:
248                 if (Editor::instance().internal_editing()) {
249                         /* displaying MIDI note selection is tricky */
250                         
251                         selection_start->set_off (true);
252                         selection_end->set_off (true);
253                         selection_length->set_off (true);
254
255                 } else {
256                         if (selection.regions.empty()) {
257                                 if (selection.points.empty()) {
258                                         selection_start->set_off (true);
259                                         selection_end->set_off (true);
260                                         selection_length->set_off (true);
261                                 } else {
262                                         s = max_framepos;
263                                         e = 0;
264                                         for (PointSelection::iterator i = selection.points.begin(); i != selection.points.end(); ++i) {
265                                                 s = min (s, (framepos_t) i->start);
266                                                 e = max (e, (framepos_t) i->end);
267                                         }
268                                         selection_start->set_off (false);
269                                         selection_end->set_off (false);
270                                         selection_length->set_off (false);
271                                         selection_start->set (s);
272                                         selection_end->set (e);
273                                         selection_length->set (e - s + 1);
274                                 }
275                         } else {
276                                 s = selection.regions.start();
277                                 e = selection.regions.end_frame();
278                                 selection_start->set_off (false);
279                                 selection_end->set_off (false);
280                                 selection_length->set_off (false);
281                                 selection_start->set (s);
282                                 selection_end->set (e);
283                                 selection_length->set (e - s + 1);
284                         }
285                 }
286                 break;
287
288         case Editing::MouseRange:
289                 if (selection.time.empty()) {
290                         selection_start->set_off (true);
291                         selection_end->set_off (true);
292                         selection_length->set_off (true);
293                 } else {
294                         selection_start->set_off (false);
295                         selection_end->set_off (false);
296                         selection_length->set_off (false);
297                         selection_start->set (selection.time.start());
298                         selection_end->set (selection.time.end_frame());
299                         selection_length->set (selection.time.length());
300                 }
301                 break;
302
303         default:
304                 selection_start->set_off (true);
305                 selection_end->set_off (true);
306                 selection_length->set_off (true);       
307                 break;
308         }
309 }
310
311 void
312 TimeInfoBox::punch_location_changed (Location* loc)
313 {
314         if (loc) {
315                 watch_punch (loc);
316         } 
317 }
318
319 void
320 TimeInfoBox::watch_punch (Location* punch)
321 {
322         punch_connections.drop_connections ();
323
324         punch->start_changed.connect (punch_connections, MISSING_INVALIDATOR, boost::bind (&TimeInfoBox::punch_changed, this, _1), gui_context());
325         punch->end_changed.connect (punch_connections, MISSING_INVALIDATOR, boost::bind (&TimeInfoBox::punch_changed, this, _1), gui_context());
326
327         punch_changed (punch);
328 }
329
330 void
331 TimeInfoBox::punch_changed (Location* loc)
332 {
333         if (!loc) {
334                 punch_start->set_off (true);
335                 punch_end->set_off (true);
336                 return;
337         }
338
339         punch_start->set_off (false);
340         punch_end->set_off (false);
341
342         punch_start->set (loc->start());
343         punch_end->set (loc->end());
344 }       
345
346 bool
347 TimeInfoBox::on_expose_event (GdkEventExpose* ev)
348 {
349         {
350                 int x, y;
351                 Gtk::Widget* window_parent;
352                 Glib::RefPtr<Gdk::Window> win = Gtkmm2ext::window_to_draw_on (*this, &window_parent);
353
354                 if (win) {
355                 
356                         Cairo::RefPtr<Cairo::Context> context = win->create_cairo_context();
357
358 #if 0                   
359                         translate_coordinates (*window_parent, ev->area.x, ev->area.y, x, y);
360                         context->rectangle (x, y, ev->area.width, ev->area.height);
361                         context->clip ();
362 #endif
363                         translate_coordinates (*window_parent, 0, 0, x, y);
364                         context->set_source_rgba (0.149, 0.149, 0.149, 1.0);
365                         Gtkmm2ext::rounded_rectangle (context, x, y, get_allocation().get_width(), get_allocation().get_height(), 9);
366                         context->fill ();
367                 }
368         }
369
370         HBox::on_expose_event (ev);
371
372         return false;
373 }