Update selection-clock when region properties change
[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 "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         Region::RegionPropertyChanged.connect (region_property_connections, invalidator (*this), boost::bind (&TimeInfoBox::region_property_change, this, _1, _2), gui_context());
142         Editor::instance().MouseModeChanged.connect (editor_connections, invalidator(*this), boost::bind (&TimeInfoBox::track_mouse_mode, this), gui_context());
143 }
144
145 TimeInfoBox::~TimeInfoBox ()
146 {
147         delete selection_length;
148         delete selection_end;
149         delete selection_start;
150
151         delete punch_start;
152         delete punch_end;
153 }
154
155 void
156 TimeInfoBox::track_mouse_mode ()
157 {
158         selection_changed ();
159 }
160
161 void
162 TimeInfoBox::region_property_change (boost::shared_ptr<ARDOUR::Region> r, const PBD::PropertyChange& what_changed)
163 {
164         Selection& selection (Editor::instance().get_selection());
165
166         if (selection.regions.empty()) {
167                 return;
168         }
169
170         PBD::PropertyChange our_interests;
171
172         our_interests.add (ARDOUR::Properties::position);
173         our_interests.add (ARDOUR::Properties::length);
174         our_interests.add (ARDOUR::Properties::start);
175
176         if (!what_changed.contains (our_interests)) {
177                 return;
178         }
179
180         if (selection.regions.contains (r)) {
181                 selection_changed ();
182         }
183 }
184
185 bool
186 TimeInfoBox::clock_button_release_event (GdkEventButton* ev, AudioClock* src)
187 {
188         if (!_session) {
189                 return false;
190         }
191
192         if (ev->button == 1) {
193                 if (!src->off()) {
194                         _session->request_locate (src->current_time ());
195                 }
196                 return true;
197         }
198
199         return false;
200 }
201
202 void
203 TimeInfoBox::sync_selection_mode (AudioClock* src)
204 {
205         if (!syncing_selection) {
206                 syncing_selection = true;
207                 selection_start->set_mode (src->mode());
208                 selection_end->set_mode (src->mode());
209                 selection_length->set_mode (src->mode());
210                 syncing_selection = false;
211         }
212 }
213
214 void
215 TimeInfoBox::sync_punch_mode (AudioClock* src)
216 {
217         if (!with_punch_clock) {
218                 return;
219         }
220         if (!syncing_punch) {
221                 syncing_punch = true;
222                 punch_start->set_mode (src->mode());
223                 punch_end->set_mode (src->mode());
224                 syncing_punch = false;
225         }
226 }
227
228
229 void
230 TimeInfoBox::set_session (Session* s)
231 {
232         SessionHandlePtr::set_session (s);
233
234         selection_start->set_session (s);
235         selection_end->set_session (s);
236         selection_length->set_session (s);
237
238         if (!with_punch_clock) {
239                 return;
240         }
241
242         punch_start->set_session (s);
243         punch_end->set_session (s);
244
245         if (s) {
246                 Location* punch = s->locations()->auto_punch_location ();
247
248                 if (punch) {
249                         watch_punch (punch);
250                 }
251
252                 punch_changed (punch);
253
254                 _session->auto_punch_location_changed.connect (_session_connections, MISSING_INVALIDATOR,
255                                 boost::bind (&TimeInfoBox::punch_location_changed, this, _1), gui_context());
256         }
257 }
258
259 void
260 TimeInfoBox::selection_changed ()
261 {
262         framepos_t s, e;
263         Selection& selection (Editor::instance().get_selection());
264
265         switch (Editor::instance().current_mouse_mode()) {
266
267         case Editing::MouseContent:
268                 /* displaying MIDI note selection is tricky */
269                 selection_start->set_off (true);
270                 selection_end->set_off (true);
271                 selection_length->set_off (true);
272                 break;
273
274         case Editing::MouseObject:
275                 if (selection.regions.empty()) {
276                         if (selection.points.empty()) {
277                                 Glib::RefPtr<Action> act = ActionManager::get_action ("MouseMode", "set-mouse-mode-object-range");
278                                 Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (act);
279
280                                 if (tact && tact->get_active() && !selection.time.empty()) {
281                                         /* show selected range */
282                                         selection_start->set_off (false);
283                                         selection_end->set_off (false);
284                                         selection_length->set_off (false);
285                                         selection_start->set (selection.time.start());
286                                         selection_end->set (selection.time.end_frame());
287                                         selection_length->set (selection.time.length());
288                                 } else {
289                                         selection_start->set_off (true);
290                                         selection_end->set_off (true);
291                                         selection_length->set_off (true);
292                                 }
293                         } else {
294                                 s = max_framepos;
295                                 e = 0;
296                                 for (PointSelection::iterator i = selection.points.begin(); i != selection.points.end(); ++i) {
297                                         framepos_t const p = (*i)->line().session_position ((*i)->model ());
298                                         s = min (s, p);
299                                         e = max (e, p);
300                                 }
301                                 selection_start->set_off (false);
302                                 selection_end->set_off (false);
303                                 selection_length->set_off (false);
304                                 selection_start->set (s);
305                                 selection_end->set (e);
306                                 selection_length->set (e - s + 1);
307                         }
308                 } else {
309                         s = selection.regions.start();
310                         e = selection.regions.end_frame();
311                         selection_start->set_off (false);
312                         selection_end->set_off (false);
313                         selection_length->set_off (false);
314                         selection_start->set (s);
315                         selection_end->set (e);
316                         selection_length->set (e - s + 1);
317                 }
318                 break;
319
320         case Editing::MouseRange:
321                 if (selection.time.empty()) {
322                         Glib::RefPtr<Action> act = ActionManager::get_action ("MouseMode", "set-mouse-mode-object-range");
323                         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (act);
324
325                         if (tact && tact->get_active() &&  !selection.regions.empty()) {
326                                 /* show selected regions */
327                                 s = selection.regions.start();
328                                 e = selection.regions.end_frame();
329                                 selection_start->set_off (false);
330                                 selection_end->set_off (false);
331                                 selection_length->set_off (false);
332                                 selection_start->set (s);
333                                 selection_end->set (e);
334                                 selection_length->set (e - s + 1);
335                         } else {
336                                 selection_start->set_off (true);
337                                 selection_end->set_off (true);
338                                 selection_length->set_off (true);
339                         }
340                 } else {
341                         selection_start->set_off (false);
342                         selection_end->set_off (false);
343                         selection_length->set_off (false);
344                         selection_start->set (selection.time.start());
345                         selection_end->set (selection.time.end_frame());
346                         selection_length->set (selection.time.length());
347                 }
348                 break;
349
350         default:
351                 selection_start->set_off (true);
352                 selection_end->set_off (true);
353                 selection_length->set_off (true);
354                 break;
355         }
356 }
357
358 void
359 TimeInfoBox::punch_location_changed (Location* loc)
360 {
361         if (loc && with_punch_clock) {
362                 watch_punch (loc);
363         }
364 }
365
366 void
367 TimeInfoBox::watch_punch (Location* punch)
368 {
369         assert (with_punch_clock);
370         punch_connections.drop_connections ();
371
372         punch->start_changed.connect (punch_connections, MISSING_INVALIDATOR, boost::bind (&TimeInfoBox::punch_changed, this, _1), gui_context());
373         punch->end_changed.connect (punch_connections, MISSING_INVALIDATOR, boost::bind (&TimeInfoBox::punch_changed, this, _1), gui_context());
374
375         punch_changed (punch);
376 }
377
378 void
379 TimeInfoBox::punch_changed (Location* loc)
380 {
381         assert (with_punch_clock);
382         if (!loc) {
383                 punch_start->set_off (true);
384                 punch_end->set_off (true);
385                 return;
386         }
387
388         punch_start->set_off (false);
389         punch_end->set_off (false);
390
391         punch_start->set (loc->start());
392         punch_end->set (loc->end());
393 }