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