Optimize automation-event process splitting
[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/gui_thread.h"
24 #include "gtkmm2ext/utils.h"
25 #include "gtkmm2ext/actions.h"
26
27 #include "ardour/location.h"
28 #include "ardour/profile.h"
29 #include "ardour/session.h"
30
31 #include "audio_clock.h"
32 #include "automation_line.h"
33 #include "control_point.h"
34 #include "editor.h"
35 #include "region_view.h"
36 #include "time_info_box.h"
37
38 #include "pbd/i18n.h"
39
40 using namespace Gtk;
41 using namespace ARDOUR;
42 using std::min;
43 using std::max;
44
45 TimeInfoBox::TimeInfoBox (std::string state_node_name, bool with_punch)
46         : table (3, 3)
47         , punch_start (0)
48         , punch_end (0)
49         , syncing_selection (false)
50         , syncing_punch (false)
51         , with_punch_clock (with_punch)
52 {
53         set_name (X_("TimeInfoBox"));
54
55         selection_start = new AudioClock (
56                         string_compose ("%1-selection-start", state_node_name),
57                         false, "selection", false, false, false, false);
58         selection_end = new AudioClock (
59                         string_compose ("%1-selection-end", state_node_name),
60                         false, "selection", false, false, false, false);
61         selection_length = new AudioClock (
62                         string_compose ("%1-selection-length", state_node_name),
63                         false, "selection", false, false, true, false);
64
65         selection_title.set_text (_("Selection"));
66
67         set_homogeneous (false);
68         set_spacing (0);
69         set_border_width (2);
70
71         pack_start (table, false, false);
72
73         table.set_homogeneous (false);
74         table.set_spacings (0);
75         table.set_border_width (2);
76         table.set_col_spacings (2);
77
78         Gtk::Label* l;
79
80         selection_title.set_name ("TimeInfoSelectionTitle");
81         if (with_punch_clock) {
82                 table.attach (selection_title, 1, 2, 0, 1);
83         }
84         l = manage (new Label);
85         l->set_text (_("Start"));
86         l->set_alignment (1.0, 0.5);
87         l->set_name (X_("TimeInfoSelectionLabel"));
88         table.attach (*l, 0, 1, 1, 2, FILL);
89         table.attach (*selection_start, 1, 2, 1, 2);
90
91         l = manage (new Label);
92         l->set_text (_("End"));
93         l->set_alignment (1.0, 0.5);
94         l->set_name (X_("TimeInfoSelectionLabel"));
95         table.attach (*l, 0, 1, 2, 3, FILL);
96         table.attach (*selection_end, 1, 2, 2, 3);
97
98         l = manage (new Label);
99         l->set_text (_("Length"));
100         l->set_alignment (1.0, 0.5);
101         l->set_name (X_("TimeInfoSelectionLabel"));
102         table.attach (*l, 0, 1, 3, 4, FILL);
103         table.attach (*selection_length, 1, 2, 3, 4);
104
105         if (with_punch_clock) {
106                 punch_start = new AudioClock (
107                                 string_compose ("%1-punch-start", state_node_name),
108                                 false, "punch", false, false, false, false);
109                 punch_end = new AudioClock (
110                                 string_compose ("%1-punch-end", state_node_name),
111                                 false, "punch", false, false, false, false);
112                 punch_title.set_text (_("Punch"));
113
114                 punch_title.set_name ("TimeInfoSelectionTitle");
115                 table.attach (punch_title, 2, 3, 0, 1);
116                 table.attach (*punch_start, 2, 3, 1, 2);
117                 table.attach (*punch_end, 2, 3, 2, 3);
118         }
119
120         show_all ();
121
122         selection_start->mode_changed.connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::sync_selection_mode), selection_start));
123         selection_end->mode_changed.connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::sync_selection_mode), selection_end));
124         selection_length->mode_changed.connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::sync_selection_mode), selection_length));
125
126         selection_start->signal_button_release_event().connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::clock_button_release_event), selection_start), true);
127         selection_end->signal_button_release_event().connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::clock_button_release_event), selection_end), true);
128
129         if (with_punch_clock) {
130                 punch_start->mode_changed.connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::sync_punch_mode), punch_start));
131                 punch_end->mode_changed.connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::sync_punch_mode), punch_end));
132
133                 punch_start->signal_button_release_event().connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::clock_button_release_event), punch_start), true);
134                 punch_end->signal_button_release_event().connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::clock_button_release_event), punch_end), true);
135         }
136
137         Editor::instance().get_selection().TimeChanged.connect (sigc::mem_fun (*this, &TimeInfoBox::selection_changed));
138         Editor::instance().get_selection().RegionsChanged.connect (sigc::mem_fun (*this, &TimeInfoBox::selection_changed));
139
140         Editor::instance().MouseModeChanged.connect (editor_connections, invalidator(*this), boost::bind (&TimeInfoBox::track_mouse_mode, this), gui_context());
141 }
142
143 TimeInfoBox::~TimeInfoBox ()
144 {
145         delete selection_length;
146         delete selection_end;
147         delete selection_start;
148
149         delete punch_start;
150         delete punch_end;
151 }
152
153 void
154 TimeInfoBox::track_mouse_mode ()
155 {
156         selection_changed ();
157 }
158
159 bool
160 TimeInfoBox::clock_button_release_event (GdkEventButton* ev, AudioClock* src)
161 {
162         if (!_session) {
163                 return false;
164         }
165
166         if (ev->button == 1) {
167                 if (!src->off()) {
168                         _session->request_locate (src->current_time ());
169                 }
170                 return true;
171         }
172
173         return false;
174 }
175
176 void
177 TimeInfoBox::sync_selection_mode (AudioClock* src)
178 {
179         if (!syncing_selection) {
180                 syncing_selection = true;
181                 selection_start->set_mode (src->mode());
182                 selection_end->set_mode (src->mode());
183                 selection_length->set_mode (src->mode());
184                 syncing_selection = false;
185         }
186 }
187
188 void
189 TimeInfoBox::sync_punch_mode (AudioClock* src)
190 {
191         if (!with_punch_clock) {
192                 return;
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         if (!with_punch_clock) {
213                 return;
214         }
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::region_selection_changed ()
235 {
236         samplepos_t s, e;
237         Selection& selection (Editor::instance().get_selection());
238         s = selection.regions.start();
239         e = selection.regions.end_sample();
240         selection_start->set_off (false);
241         selection_end->set_off (false);
242         selection_length->set_off (false);
243         selection_start->set (s);
244         selection_end->set (e);
245         selection_length->set (e, false, s);
246 }
247
248 void
249 TimeInfoBox::selection_changed ()
250 {
251         samplepos_t s, e;
252         Selection& selection (Editor::instance().get_selection());
253
254         region_property_connections.drop_connections();
255
256         switch (Editor::instance().current_mouse_mode()) {
257
258         case Editing::MouseContent:
259                 /* displaying MIDI note selection is tricky */
260                 selection_start->set_off (true);
261                 selection_end->set_off (true);
262                 selection_length->set_off (true);
263                 break;
264
265         case Editing::MouseObject:
266                 if (selection.regions.empty()) {
267                         if (selection.points.empty()) {
268                                 Glib::RefPtr<Action> act = ActionManager::get_action ("MouseMode", "set-mouse-mode-object-range");
269                                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (act);
270
271                                 if (tact && tact->get_active() && !selection.time.empty()) {
272                                         /* show selected range */
273                                         selection_start->set_off (false);
274                                         selection_end->set_off (false);
275                                         selection_length->set_off (false);
276                                         selection_start->set (selection.time.start());
277                                         selection_end->set (selection.time.end_sample());
278                                         selection_length->set (selection.time.end_sample(), false, selection.time.start());
279                                 } else {
280                                         selection_start->set_off (true);
281                                         selection_end->set_off (true);
282                                         selection_length->set_off (true);
283                                 }
284                         } else {
285                                 s = max_samplepos;
286                                 e = 0;
287                                 for (PointSelection::iterator i = selection.points.begin(); i != selection.points.end(); ++i) {
288                                         samplepos_t const p = (*i)->line().session_position ((*i)->model ());
289                                         s = min (s, p);
290                                         e = max (e, p);
291                                 }
292                                 selection_start->set_off (false);
293                                 selection_end->set_off (false);
294                                 selection_length->set_off (false);
295                                 selection_start->set (s);
296                                 selection_end->set (e);
297                                 selection_length->set (e, false, s);
298                         }
299                 } else {
300                         /* this is more efficient than tracking changes per region in large selections */
301                         std::set<boost::shared_ptr<ARDOUR::Playlist> > playlists;
302                         for (RegionSelection::iterator s = selection.regions.begin(); s != selection.regions.end(); ++s) {
303                                 boost::shared_ptr<Playlist> pl = (*s)->region()->playlist();
304                                 if (pl) {
305                                         playlists.insert (pl);
306                                 }
307                         }
308                         for (std::set<boost::shared_ptr<ARDOUR::Playlist> >::iterator ps = playlists.begin(); ps != playlists.end(); ++ps) {
309                                 (*ps)->ContentsChanged.connect (region_property_connections, invalidator (*this),
310                                                                 boost::bind (&TimeInfoBox::region_selection_changed, this), gui_context());
311                         }
312                         region_selection_changed ();
313                 }
314                 break;
315
316         case Editing::MouseRange:
317                 if (selection.time.empty()) {
318                         Glib::RefPtr<Action> act = ActionManager::get_action ("MouseMode", "set-mouse-mode-object-range");
319                         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (act);
320
321                         if (tact && tact->get_active() &&  !selection.regions.empty()) {
322                                 /* show selected regions */
323                                 s = selection.regions.start();
324                                 e = selection.regions.end_sample();
325                                 selection_start->set_off (false);
326                                 selection_end->set_off (false);
327                                 selection_length->set_off (false);
328                                 selection_start->set (s);
329                                 selection_end->set (e);
330                                 selection_length->set (e, false, s);
331                         } else {
332                                 selection_start->set_off (true);
333                                 selection_end->set_off (true);
334                                 selection_length->set_off (true);
335                         }
336                 } else {
337                         selection_start->set_off (false);
338                         selection_end->set_off (false);
339                         selection_length->set_off (false);
340                         selection_start->set (selection.time.start());
341                         selection_end->set (selection.time.end_sample());
342                         selection_length->set (selection.time.end_sample(), false, selection.time.start());
343                 }
344                 break;
345
346         default:
347                 selection_start->set_off (true);
348                 selection_end->set_off (true);
349                 selection_length->set_off (true);
350                 break;
351         }
352 }
353
354 void
355 TimeInfoBox::punch_location_changed (Location* loc)
356 {
357         if (loc && with_punch_clock) {
358                 watch_punch (loc);
359         }
360 }
361
362 void
363 TimeInfoBox::watch_punch (Location* punch)
364 {
365         assert (with_punch_clock);
366         punch_connections.drop_connections ();
367
368         punch->start_changed.connect (punch_connections, MISSING_INVALIDATOR, boost::bind (&TimeInfoBox::punch_changed, this, _1), gui_context());
369         punch->end_changed.connect (punch_connections, MISSING_INVALIDATOR, boost::bind (&TimeInfoBox::punch_changed, this, _1), gui_context());
370
371         punch_changed (punch);
372 }
373
374 void
375 TimeInfoBox::punch_changed (Location* loc)
376 {
377         assert (with_punch_clock);
378         if (!loc) {
379                 punch_start->set_off (true);
380                 punch_end->set_off (true);
381                 return;
382         }
383
384         punch_start->set_off (false);
385         punch_end->set_off (false);
386
387         punch_start->set (loc->start());
388         punch_end->set (loc->end());
389 }