c34a29003c6f26f97f88c49fca22b1bcade32319
[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<ToggleAction> tact = ActionManager::get_toggle_action ("MouseMode", "set-mouse-mode-object-range");
269                                 if (tact->get_active() && !selection.time.empty()) {
270                                         /* show selected range */
271                                         selection_start->set_off (false);
272                                         selection_end->set_off (false);
273                                         selection_length->set_off (false);
274                                         selection_start->set (selection.time.start());
275                                         selection_end->set (selection.time.end_sample());
276                                         selection_length->set (selection.time.end_sample(), false, selection.time.start());
277                                 } else {
278                                         selection_start->set_off (true);
279                                         selection_end->set_off (true);
280                                         selection_length->set_off (true);
281                                 }
282                         } else {
283                                 s = max_samplepos;
284                                 e = 0;
285                                 for (PointSelection::iterator i = selection.points.begin(); i != selection.points.end(); ++i) {
286                                         samplepos_t const p = (*i)->line().session_position ((*i)->model ());
287                                         s = min (s, p);
288                                         e = max (e, p);
289                                 }
290                                 selection_start->set_off (false);
291                                 selection_end->set_off (false);
292                                 selection_length->set_off (false);
293                                 selection_start->set (s);
294                                 selection_end->set (e);
295                                 selection_length->set (e, false, s);
296                         }
297                 } else {
298                         /* this is more efficient than tracking changes per region in large selections */
299                         std::set<boost::shared_ptr<ARDOUR::Playlist> > playlists;
300                         for (RegionSelection::iterator s = selection.regions.begin(); s != selection.regions.end(); ++s) {
301                                 boost::shared_ptr<Playlist> pl = (*s)->region()->playlist();
302                                 if (pl) {
303                                         playlists.insert (pl);
304                                 }
305                         }
306                         for (std::set<boost::shared_ptr<ARDOUR::Playlist> >::iterator ps = playlists.begin(); ps != playlists.end(); ++ps) {
307                                 (*ps)->ContentsChanged.connect (region_property_connections, invalidator (*this),
308                                                                 boost::bind (&TimeInfoBox::region_selection_changed, this), gui_context());
309                         }
310                         region_selection_changed ();
311                 }
312                 break;
313
314         case Editing::MouseRange:
315                 if (selection.time.empty()) {
316                         Glib::RefPtr<ToggleAction> tact = ActionManager::get_toggle_action ("MouseMode", "set-mouse-mode-object-range");
317
318                         if (tact->get_active() &&  !selection.regions.empty()) {
319                                 /* show selected regions */
320                                 s = selection.regions.start();
321                                 e = selection.regions.end_sample();
322                                 selection_start->set_off (false);
323                                 selection_end->set_off (false);
324                                 selection_length->set_off (false);
325                                 selection_start->set (s);
326                                 selection_end->set (e);
327                                 selection_length->set (e, false, s);
328                         } else {
329                                 selection_start->set_off (true);
330                                 selection_end->set_off (true);
331                                 selection_length->set_off (true);
332                         }
333                 } else {
334                         selection_start->set_off (false);
335                         selection_end->set_off (false);
336                         selection_length->set_off (false);
337                         selection_start->set (selection.time.start());
338                         selection_end->set (selection.time.end_sample());
339                         selection_length->set (selection.time.end_sample(), false, selection.time.start());
340                 }
341                 break;
342
343         default:
344                 selection_start->set_off (true);
345                 selection_end->set_off (true);
346                 selection_length->set_off (true);
347                 break;
348         }
349 }
350
351 void
352 TimeInfoBox::punch_location_changed (Location* loc)
353 {
354         if (loc && with_punch_clock) {
355                 watch_punch (loc);
356         }
357 }
358
359 void
360 TimeInfoBox::watch_punch (Location* punch)
361 {
362         assert (with_punch_clock);
363         punch_connections.drop_connections ();
364
365         punch->start_changed.connect (punch_connections, MISSING_INVALIDATOR, boost::bind (&TimeInfoBox::punch_changed, this, _1), gui_context());
366         punch->end_changed.connect (punch_connections, MISSING_INVALIDATOR, boost::bind (&TimeInfoBox::punch_changed, this, _1), gui_context());
367
368         punch_changed (punch);
369 }
370
371 void
372 TimeInfoBox::punch_changed (Location* loc)
373 {
374         assert (with_punch_clock);
375         if (!loc) {
376                 punch_start->set_off (true);
377                 punch_end->set_off (true);
378                 return;
379         }
380
381         punch_start->set_off (false);
382         punch_end->set_off (false);
383
384         punch_start->set (loc->start());
385         punch_end->set (loc->end());
386 }