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