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