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