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