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