Drop references held by any GUI Lua script after execution
[ardour.git] / gtk2_ardour / time_info_box.cc
1 /*
2  * Copyright (C) 2011-2017 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2012-2019 Robin Gareus <robin@gareus.org>
4  * Copyright (C) 2016-2017 Nick Mainsbridge <mainsbridge@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include <algorithm>
22 #include "pbd/compose.h"
23
24 #include "gtkmm2ext/gui_thread.h"
25 #include "gtkmm2ext/utils.h"
26 #include "gtkmm2ext/actions.h"
27
28 #include "ardour/location.h"
29 #include "ardour/profile.h"
30 #include "ardour/session.h"
31
32 #include "audio_clock.h"
33 #include "automation_line.h"
34 #include "control_point.h"
35 #include "editor.h"
36 #include "region_view.h"
37 #include "time_info_box.h"
38
39 #include "pbd/i18n.h"
40
41 using namespace Gtk;
42 using namespace ARDOUR;
43 using std::min;
44 using std::max;
45
46 TimeInfoBox::TimeInfoBox (std::string state_node_name, bool with_punch)
47         : table (3, 3)
48         , punch_start (0)
49         , punch_end (0)
50         , syncing_selection (false)
51         , syncing_punch (false)
52         , with_punch_clock (with_punch)
53 {
54         set_name (X_("TimeInfoBox"));
55
56         selection_start = new AudioClock (
57                         string_compose ("%1-selection-start", state_node_name),
58                         false, "selection", false, false, false, false);
59         selection_end = new AudioClock (
60                         string_compose ("%1-selection-end", state_node_name),
61                         false, "selection", false, false, false, false);
62         selection_length = new AudioClock (
63                         string_compose ("%1-selection-length", state_node_name),
64                         false, "selection", false, false, true, false);
65
66         selection_title.set_text (_("Selection"));
67
68         set_homogeneous (false);
69         set_spacing (0);
70         set_border_width (2);
71
72         pack_start (table, false, false);
73
74         table.set_homogeneous (false);
75         table.set_spacings (0);
76         table.set_border_width (2);
77         table.set_col_spacings (2);
78
79         Gtk::Label* l;
80
81         selection_title.set_name ("TimeInfoSelectionTitle");
82         if (with_punch_clock) {
83                 table.attach (selection_title, 1, 2, 0, 1);
84         }
85         l = manage (new Label);
86         l->set_text (_("Start"));
87         l->set_alignment (1.0, 0.5);
88         l->set_name (X_("TimeInfoSelectionLabel"));
89         table.attach (*l, 0, 1, 1, 2, FILL);
90         table.attach (*selection_start, 1, 2, 1, 2);
91
92         l = manage (new Label);
93         l->set_text (_("End"));
94         l->set_alignment (1.0, 0.5);
95         l->set_name (X_("TimeInfoSelectionLabel"));
96         table.attach (*l, 0, 1, 2, 3, FILL);
97         table.attach (*selection_end, 1, 2, 2, 3);
98
99         l = manage (new Label);
100         l->set_text (_("Length"));
101         l->set_alignment (1.0, 0.5);
102         l->set_name (X_("TimeInfoSelectionLabel"));
103         table.attach (*l, 0, 1, 3, 4, FILL);
104         table.attach (*selection_length, 1, 2, 3, 4);
105
106         if (with_punch_clock) {
107                 punch_start = new AudioClock (
108                                 string_compose ("%1-punch-start", state_node_name),
109                                 false, "punch", false, false, false, false);
110                 punch_end = new AudioClock (
111                                 string_compose ("%1-punch-end", state_node_name),
112                                 false, "punch", false, false, false, false);
113                 punch_title.set_text (_("Punch"));
114
115                 punch_title.set_name ("TimeInfoSelectionTitle");
116                 table.attach (punch_title, 2, 3, 0, 1);
117                 table.attach (*punch_start, 2, 3, 1, 2);
118                 table.attach (*punch_end, 2, 3, 2, 3);
119         }
120
121         show_all ();
122
123         selection_start->mode_changed.connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::sync_selection_mode), selection_start));
124         selection_end->mode_changed.connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::sync_selection_mode), selection_end));
125         selection_length->mode_changed.connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::sync_selection_mode), selection_length));
126
127         selection_start->signal_button_release_event().connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::clock_button_release_event), selection_start), true);
128         selection_end->signal_button_release_event().connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::clock_button_release_event), selection_end), true);
129
130         if (with_punch_clock) {
131                 punch_start->mode_changed.connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::sync_punch_mode), punch_start));
132                 punch_end->mode_changed.connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::sync_punch_mode), punch_end));
133
134                 punch_start->signal_button_release_event().connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::clock_button_release_event), punch_start), true);
135                 punch_end->signal_button_release_event().connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::clock_button_release_event), punch_end), true);
136         }
137
138         Editor::instance().get_selection().TimeChanged.connect (sigc::mem_fun (*this, &TimeInfoBox::selection_changed));
139         Editor::instance().get_selection().RegionsChanged.connect (sigc::mem_fun (*this, &TimeInfoBox::selection_changed));
140
141         Editor::instance().MouseModeChanged.connect (editor_connections, invalidator(*this), boost::bind (&TimeInfoBox::track_mouse_mode, this), gui_context());
142 }
143
144 TimeInfoBox::~TimeInfoBox ()
145 {
146         delete selection_length;
147         delete selection_end;
148         delete selection_start;
149
150         delete punch_start;
151         delete punch_end;
152 }
153
154 void
155 TimeInfoBox::track_mouse_mode ()
156 {
157         selection_changed ();
158 }
159
160 bool
161 TimeInfoBox::clock_button_release_event (GdkEventButton* ev, AudioClock* src)
162 {
163         if (!_session) {
164                 return false;
165         }
166
167         if (ev->button == 1) {
168                 if (!src->off()) {
169                         _session->request_locate (src->current_time ());
170                 }
171                 return true;
172         }
173
174         return false;
175 }
176
177 void
178 TimeInfoBox::sync_selection_mode (AudioClock* src)
179 {
180         if (!syncing_selection) {
181                 syncing_selection = true;
182                 selection_start->set_mode (src->mode());
183                 selection_end->set_mode (src->mode());
184                 selection_length->set_mode (src->mode());
185                 syncing_selection = false;
186         }
187 }
188
189 void
190 TimeInfoBox::sync_punch_mode (AudioClock* src)
191 {
192         if (!with_punch_clock) {
193                 return;
194         }
195         if (!syncing_punch) {
196                 syncing_punch = true;
197                 punch_start->set_mode (src->mode());
198                 punch_end->set_mode (src->mode());
199                 syncing_punch = false;
200         }
201 }
202
203
204 void
205 TimeInfoBox::set_session (Session* s)
206 {
207         SessionHandlePtr::set_session (s);
208
209         selection_start->set_session (s);
210         selection_end->set_session (s);
211         selection_length->set_session (s);
212
213         if (!with_punch_clock) {
214                 return;
215         }
216
217         punch_start->set_session (s);
218         punch_end->set_session (s);
219
220         if (s) {
221                 Location* punch = s->locations()->auto_punch_location ();
222
223                 if (punch) {
224                         watch_punch (punch);
225                 }
226
227                 punch_changed (punch);
228
229                 _session->auto_punch_location_changed.connect (_session_connections, MISSING_INVALIDATOR,
230                                 boost::bind (&TimeInfoBox::punch_location_changed, this, _1), gui_context());
231         }
232 }
233
234 void
235 TimeInfoBox::region_selection_changed ()
236 {
237         samplepos_t s, e;
238         Selection& selection (Editor::instance().get_selection());
239         s = selection.regions.start();
240         e = selection.regions.end_sample();
241         selection_start->set_off (false);
242         selection_end->set_off (false);
243         selection_length->set_off (false);
244         selection_start->set (s);
245         selection_end->set (e);
246         selection_length->set (e, false, s);
247 }
248
249 void
250 TimeInfoBox::selection_changed ()
251 {
252         samplepos_t s, e;
253         Selection& selection (Editor::instance().get_selection());
254
255         region_property_connections.drop_connections();
256
257         switch (Editor::instance().current_mouse_mode()) {
258
259         case Editing::MouseContent:
260                 /* displaying MIDI note selection is tricky */
261                 selection_start->set_off (true);
262                 selection_end->set_off (true);
263                 selection_length->set_off (true);
264                 break;
265
266         case Editing::MouseObject:
267                 if (selection.regions.empty()) {
268                         if (selection.points.empty()) {
269                                 Glib::RefPtr<ToggleAction> tact = ActionManager::get_toggle_action ("MouseMode", "set-mouse-mode-object-range");
270                                 if (tact->get_active() && !selection.time.empty()) {
271                                         /* show selected range */
272                                         selection_start->set_off (false);
273                                         selection_end->set_off (false);
274                                         selection_length->set_off (false);
275                                         selection_start->set (selection.time.start());
276                                         selection_end->set (selection.time.end_sample());
277                                         selection_length->set (selection.time.end_sample(), false, selection.time.start());
278                                 } else {
279                                         selection_start->set_off (true);
280                                         selection_end->set_off (true);
281                                         selection_length->set_off (true);
282                                 }
283                         } else {
284                                 s = max_samplepos;
285                                 e = 0;
286                                 for (PointSelection::iterator i = selection.points.begin(); i != selection.points.end(); ++i) {
287                                         samplepos_t const p = (*i)->line().session_position ((*i)->model ());
288                                         s = min (s, p);
289                                         e = max (e, p);
290                                 }
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, false, s);
297                         }
298                 } else {
299                         /* this is more efficient than tracking changes per region in large selections */
300                         std::set<boost::shared_ptr<ARDOUR::Playlist> > playlists;
301                         for (RegionSelection::iterator s = selection.regions.begin(); s != selection.regions.end(); ++s) {
302                                 boost::shared_ptr<Playlist> pl = (*s)->region()->playlist();
303                                 if (pl) {
304                                         playlists.insert (pl);
305                                 }
306                         }
307                         for (std::set<boost::shared_ptr<ARDOUR::Playlist> >::iterator ps = playlists.begin(); ps != playlists.end(); ++ps) {
308                                 (*ps)->ContentsChanged.connect (region_property_connections, invalidator (*this),
309                                                                 boost::bind (&TimeInfoBox::region_selection_changed, this), gui_context());
310                         }
311                         region_selection_changed ();
312                 }
313                 break;
314
315         case Editing::MouseRange:
316                 if (selection.time.empty()) {
317                         Glib::RefPtr<ToggleAction> tact = ActionManager::get_toggle_action ("MouseMode", "set-mouse-mode-object-range");
318
319                         if (tact->get_active() &&  !selection.regions.empty()) {
320                                 /* show selected regions */
321                                 s = selection.regions.start();
322                                 e = selection.regions.end_sample();
323                                 selection_start->set_off (false);
324                                 selection_end->set_off (false);
325                                 selection_length->set_off (false);
326                                 selection_start->set (s);
327                                 selection_end->set (e);
328                                 selection_length->set (e, false, s);
329                         } else {
330                                 selection_start->set_off (true);
331                                 selection_end->set_off (true);
332                                 selection_length->set_off (true);
333                         }
334                 } else {
335                         selection_start->set_off (false);
336                         selection_end->set_off (false);
337                         selection_length->set_off (false);
338                         selection_start->set (selection.time.start());
339                         selection_end->set (selection.time.end_sample());
340                         selection_length->set (selection.time.end_sample(), false, selection.time.start());
341                 }
342                 break;
343
344         default:
345                 selection_start->set_off (true);
346                 selection_end->set_off (true);
347                 selection_length->set_off (true);
348                 break;
349         }
350 }
351
352 void
353 TimeInfoBox::punch_location_changed (Location* loc)
354 {
355         if (loc && with_punch_clock) {
356                 watch_punch (loc);
357         }
358 }
359
360 void
361 TimeInfoBox::watch_punch (Location* punch)
362 {
363         assert (with_punch_clock);
364         punch_connections.drop_connections ();
365
366         punch->start_changed.connect (punch_connections, MISSING_INVALIDATOR, boost::bind (&TimeInfoBox::punch_changed, this, _1), gui_context());
367         punch->end_changed.connect (punch_connections, MISSING_INVALIDATOR, boost::bind (&TimeInfoBox::punch_changed, this, _1), gui_context());
368
369         punch_changed (punch);
370 }
371
372 void
373 TimeInfoBox::punch_changed (Location* loc)
374 {
375         assert (with_punch_clock);
376         if (!loc) {
377                 punch_start->set_off (true);
378                 punch_end->set_off (true);
379                 return;
380         }
381
382         punch_start->set_off (false);
383         punch_end->set_off (false);
384
385         punch_start->set (loc->start());
386         punch_end->set (loc->end());
387 }