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