center Selection/Punch Clk titles over value-column
[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/session.h"
31
32 #include "time_info_box.h"
33 #include "audio_clock.h"
34 #include "editor.h"
35 #include "control_point.h"
36 #include "automation_line.h"
37
38 #include "i18n.h"
39
40 using namespace Gtk;
41 using namespace ARDOUR;
42 using std::min;
43 using std::max;
44
45 TimeInfoBox::TimeInfoBox ()
46         : left (2, 4)
47         , right (2, 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_start->set_draw_background (false);
61         selection_end->set_draw_background (false);
62         selection_length->set_draw_background (false);
63         punch_start->set_draw_background (false);
64         punch_end->set_draw_background (false);
65
66         selection_title.set_text (_("Selection"));
67         punch_title.set_text (_("Punch"));
68
69         set_homogeneous (false);
70         set_spacing (6);
71         set_border_width (2);
72
73         pack_start (left, true, true);
74         pack_start (right, true, true);
75
76         left.set_homogeneous (false);
77         left.set_spacings (0);
78         left.set_border_width (2);
79         left.set_col_spacings (2);
80
81         right.set_homogeneous (false);
82         right.set_spacings (0);
83         right.set_border_width (2);
84         right.set_col_spacings (2);
85
86         Gtk::Label* l;
87
88         selection_title.set_name ("TimeInfoSelectionTitle");
89         left.attach (selection_title, 1, 2, 0, 1);
90         l = manage (new Label);
91         l->set_text (_("Start"));
92         l->set_alignment (1.0, 0.5);
93         l->set_name (X_("TimeInfoSelectionLabel"));
94         left.attach (*l, 0, 1, 1, 2, FILL);
95         left.attach (*selection_start, 1, 2, 1, 2);
96
97         l = manage (new Label);
98         l->set_text (_("End"));
99         l->set_alignment (1.0, 0.5);
100         l->set_name (X_("TimeInfoSelectionLabel"));
101         left.attach (*l, 0, 1, 2, 3, FILL);
102         left.attach (*selection_end, 1, 2, 2, 3);
103
104         l = manage (new Label);
105         l->set_text (_("Length"));
106         l->set_alignment (1.0, 0.5);
107         l->set_name (X_("TimeInfoSelectionLabel"));
108         left.attach (*l, 0, 1, 3, 4, FILL);
109         left.attach (*selection_length, 1, 2, 3, 4);
110
111         punch_in_button.set_name ("punch button");
112         punch_out_button.set_name ("punch button");
113         punch_in_button.set_text (_("In"));
114         punch_out_button.set_text (_("Out"));
115
116         Glib::RefPtr<Action> act = ActionManager::get_action ("Transport", "TogglePunchIn");
117         punch_in_button.set_related_action (act);
118         act = ActionManager::get_action ("Transport", "TogglePunchOut");
119         punch_out_button.set_related_action (act);
120
121         Gtkmm2ext::UI::instance()->set_tip (punch_in_button, _("Start recording at auto-punch start"));
122         Gtkmm2ext::UI::instance()->set_tip (punch_out_button, _("Stop recording at auto-punch end"));
123
124         punch_title.set_name ("TimeInfoSelectionTitle");
125         right.attach (punch_title, 3, 4, 0, 1);
126         right.attach (punch_in_button, 2, 3, 1, 2, FILL, SHRINK);
127         right.attach (*punch_start, 3, 4, 1, 2);
128         right.attach (punch_out_button, 2, 3, 2, 3, FILL, SHRINK);
129         right.attach (*punch_end, 3, 4, 2, 3);
130
131         show_all ();
132
133         selection_start->mode_changed.connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::sync_selection_mode), selection_start));
134         selection_end->mode_changed.connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::sync_selection_mode), selection_end));
135         selection_length->mode_changed.connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::sync_selection_mode), selection_length));
136
137         punch_start->mode_changed.connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::sync_punch_mode), punch_start));
138         punch_end->mode_changed.connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::sync_punch_mode), punch_end));
139
140         selection_start->signal_button_release_event().connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::clock_button_release_event), selection_start), true);
141         selection_end->signal_button_release_event().connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::clock_button_release_event), selection_end), true);
142
143         punch_start->signal_button_release_event().connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::clock_button_release_event), punch_start), true);
144         punch_end->signal_button_release_event().connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::clock_button_release_event), punch_end), true);
145
146         Editor::instance().get_selection().TimeChanged.connect (sigc::mem_fun (*this, &TimeInfoBox::selection_changed));
147         Editor::instance().get_selection().RegionsChanged.connect (sigc::mem_fun (*this, &TimeInfoBox::selection_changed));
148
149         Editor::instance().MouseModeChanged.connect (editor_connections, invalidator(*this), boost::bind (&TimeInfoBox::track_mouse_mode, this), gui_context());
150 }
151
152 TimeInfoBox::~TimeInfoBox ()
153 {
154         delete selection_length;
155         delete selection_end;
156         delete selection_start;
157         
158         delete punch_start;
159         delete punch_end;
160 }
161
162 void
163 TimeInfoBox::track_mouse_mode ()
164 {
165         selection_changed ();
166 }
167
168 bool
169 TimeInfoBox::clock_button_release_event (GdkEventButton* ev, AudioClock* src)
170 {
171         if (!_session) {
172                 return false;
173         }
174
175         if (ev->button == 1) {
176                 if (!src->off()) {
177                         _session->request_locate (src->current_time ());
178                 }
179                 return true;
180         }
181
182         return false;
183 }
184
185 void
186 TimeInfoBox::sync_selection_mode (AudioClock* src)
187 {
188         if (!syncing_selection) {
189                 syncing_selection = true;
190                 selection_start->set_mode (src->mode());
191                 selection_end->set_mode (src->mode());
192                 selection_length->set_mode (src->mode());
193                 syncing_selection = false;
194         }
195 }
196
197 void
198 TimeInfoBox::sync_punch_mode (AudioClock* src)
199 {
200         if (!syncing_punch) {
201                 syncing_punch = true;
202                 punch_start->set_mode (src->mode());
203                 punch_end->set_mode (src->mode());
204                 syncing_punch = false;
205         }
206 }
207         
208
209 void
210 TimeInfoBox::set_session (Session* s)
211 {
212         SessionHandlePtr::set_session (s);
213
214         selection_start->set_session (s);
215         selection_end->set_session (s);
216         selection_length->set_session (s);
217
218         punch_start->set_session (s);
219         punch_end->set_session (s);
220
221         if (s) {
222                 Location* punch = s->locations()->auto_punch_location ();
223                 
224                 if (punch) {
225                         watch_punch (punch);
226                 }
227                 
228                 punch_changed (punch);
229
230                 _session->auto_punch_location_changed.connect (_session_connections, MISSING_INVALIDATOR, 
231                                                                boost::bind (&TimeInfoBox::punch_location_changed, this, _1), gui_context());
232         }
233 }
234
235 void
236 TimeInfoBox::selection_changed ()
237 {
238         framepos_t s, e;
239         Selection& selection (Editor::instance().get_selection());
240
241         switch (Editor::instance().current_mouse_mode()) {
242
243         case Editing::MouseObject:
244                 if (Editor::instance().internal_editing()) {
245                         /* displaying MIDI note selection is tricky */
246                         
247                         selection_start->set_off (true);
248                         selection_end->set_off (true);
249                         selection_length->set_off (true);
250
251                 } else {
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                 }
296                 break;
297
298         case Editing::MouseRange:
299                 if (selection.time.empty()) {
300                         Glib::RefPtr<Action> act = ActionManager::get_action ("MouseMode", "set-mouse-mode-object-range");
301                         Glib::RefPtr<ToggleAction> tact = Glib::RefPtr<ToggleAction>::cast_dynamic (act);
302
303                         if (tact && tact->get_active() &&  !selection.regions.empty()) {        
304                                 /* show selected regions */
305                                 s = selection.regions.start();
306                                 e = selection.regions.end_frame();
307                                 selection_start->set_off (false);
308                                 selection_end->set_off (false);
309                                 selection_length->set_off (false);
310                                 selection_start->set (s);
311                                 selection_end->set (e);
312                                 selection_length->set (e - s + 1);
313                         } else {
314                                 selection_start->set_off (true);
315                                 selection_end->set_off (true);
316                                 selection_length->set_off (true);
317                         }
318                 } else {
319                         selection_start->set_off (false);
320                         selection_end->set_off (false);
321                         selection_length->set_off (false);
322                         selection_start->set (selection.time.start());
323                         selection_end->set (selection.time.end_frame());
324                         selection_length->set (selection.time.length());
325                 }
326                 break;
327
328         default:
329                 selection_start->set_off (true);
330                 selection_end->set_off (true);
331                 selection_length->set_off (true);       
332                 break;
333         }
334 }
335
336 void
337 TimeInfoBox::punch_location_changed (Location* loc)
338 {
339         if (loc) {
340                 watch_punch (loc);
341         } 
342 }
343
344 void
345 TimeInfoBox::watch_punch (Location* punch)
346 {
347         punch_connections.drop_connections ();
348
349         punch->start_changed.connect (punch_connections, MISSING_INVALIDATOR, boost::bind (&TimeInfoBox::punch_changed, this, _1), gui_context());
350         punch->end_changed.connect (punch_connections, MISSING_INVALIDATOR, boost::bind (&TimeInfoBox::punch_changed, this, _1), gui_context());
351
352         punch_changed (punch);
353 }
354
355 void
356 TimeInfoBox::punch_changed (Location* loc)
357 {
358         if (!loc) {
359                 punch_start->set_off (true);
360                 punch_end->set_off (true);
361                 return;
362         }
363
364         punch_start->set_off (false);
365         punch_end->set_off (false);
366
367         punch_start->set (loc->start());
368         punch_end->set (loc->end());
369 }       
370