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