turn selection clocks off if there is no selection
[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 "pbd/compose.h"
21
22 #include "gtkmm2ext/cairocell.h"
23 #include "gtkmm2ext/gui_thread.h"
24 #include "gtkmm2ext/utils.h"
25
26 #include "ardour/location.h"
27 #include "ardour/session.h"
28
29 #include "time_info_box.h"
30 #include "audio_clock.h"
31 #include "editor.h"
32
33 #include "i18n.h"
34
35 using namespace Gtk;
36 using namespace ARDOUR;
37
38 TimeInfoBox::TimeInfoBox ()
39         : Table (4, 4)
40         , syncing_selection (false)
41         , syncing_punch (false)
42 {
43         selection_start = new AudioClock ("selection-start", false, "SelectionClockDisplay", false, false, false, false);
44         selection_end = new AudioClock ("selection-end", false, "SelectionClockDisplay", false, false, false, false);
45         selection_length = new AudioClock ("selection-length", false, "SelectionClockDisplay", false, false, true, false);
46
47         punch_start = new AudioClock ("punch-start", false, "PunchClockDisplay", false, false, false, false);
48         punch_end = new AudioClock ("punch-end", false, "PunchClockDisplay", false, false, false, false);
49
50         bool bg = true;
51
52         CairoEditableText& ss (selection_start->main_display());
53         ss.set_ypad (1);
54         ss.set_xpad (1);
55         ss.set_corner_radius (0);
56         ss.set_draw_background (bg);
57
58         CairoEditableText& se (selection_end->main_display());
59         se.set_ypad (1);
60         se.set_xpad (1);
61         se.set_corner_radius (0);
62         se.set_draw_background (bg);
63
64         CairoEditableText& sl (selection_length->main_display());
65         sl.set_ypad (1);
66         sl.set_xpad (2);
67         sl.set_corner_radius (0);
68         sl.set_draw_background (bg);
69
70         CairoEditableText& ps (punch_start->main_display());
71         ps.set_ypad (1);
72         ps.set_xpad (2);
73         ps.set_corner_radius (0);
74         ps.set_draw_background (bg);
75
76         CairoEditableText& pe (punch_end->main_display());
77         pe.set_ypad (1);
78         pe.set_xpad (2);
79         pe.set_corner_radius (0);
80         pe.set_draw_background (bg);
81
82         selection_title.set_markup (string_compose ("<span size=\"x-small\">%1</span>", _("Selection")));
83         punch_title.set_markup (string_compose ("<span size=\"x-small\">%1</span>", _("Punch")));
84
85         set_homogeneous (false);
86         set_spacings (0);
87         set_border_width (2);
88         set_col_spacings (2);
89
90         /* a bit more spacing between the two "sides" */
91         set_col_spacing (1, 10);
92
93         Gtk::Label* l;
94
95         attach (selection_title, 0, 2, 0, 1);
96         l = manage (new Label);
97         l->set_markup (string_compose ("<span size=\"x-small\">%1</span>", _("Start")));
98         attach (*l, 0, 1, 1, 2);
99         attach (*selection_start, 1, 2, 1, 2);
100         l = manage (new Label);
101         l->set_markup (string_compose ("<span size=\"x-small\">%1</span>", _("End")));
102         attach (*l, 0, 1, 2, 3);
103         attach (*selection_end, 1, 2, 2, 3);
104         l = manage (new Label);
105         l->set_markup (string_compose ("<span size=\"x-small\">%1</span>", _("Length")));
106         attach (*l, 0, 1, 3, 4);
107         attach (*selection_length, 1, 2, 3, 4);
108
109         attach (punch_title, 2, 4, 0, 1);
110         l = manage (new Label);
111         l->set_markup (string_compose ("<span size=\"x-small\">%1</span>", _("In")));
112         attach (*l, 2, 3, 1, 2);
113         attach (*punch_start, 3, 4, 1, 2);
114         l = manage (new Label);
115         l->set_markup (string_compose ("<span size=\"x-small\">%1</span>", _("Out")));
116         attach (*l, 2, 3, 2, 3);
117         attach (*punch_end, 3, 4, 2, 3);
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_start));
123         selection_length->mode_changed.connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::sync_selection_mode), selection_start));
124
125         punch_start->mode_changed.connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::sync_punch_mode), punch_start));
126         punch_end->mode_changed.connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::sync_punch_mode), punch_end));
127
128         selection_start->signal_button_release_event().connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::clock_button_release_event), selection_start), true);
129         selection_end->signal_button_release_event().connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::clock_button_release_event), selection_end), true);
130
131         punch_start->signal_button_release_event().connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::clock_button_release_event), punch_start), true);
132         punch_end->signal_button_release_event().connect (sigc::bind (sigc::mem_fun (*this, &TimeInfoBox::clock_button_release_event), punch_end), true);
133
134         Editor::instance().get_selection().TimeChanged.connect (sigc::mem_fun (*this, &TimeInfoBox::selection_changed));
135         Editor::instance().get_selection().RegionsChanged.connect (sigc::mem_fun (*this, &TimeInfoBox::selection_changed));
136
137         Editor::instance().MouseModeChanged.connect (editor_connections, invalidator(*this), ui_bind (&TimeInfoBox::track_mouse_mode, this), gui_context());
138 }
139
140 TimeInfoBox::~TimeInfoBox ()
141 {
142         delete selection_length;
143         delete selection_end;
144         delete selection_start;
145         
146         delete punch_start;
147         delete punch_end;
148 }
149
150 void
151 TimeInfoBox::track_mouse_mode ()
152 {
153         selection_changed ();
154 }
155
156 bool
157 TimeInfoBox::clock_button_release_event (GdkEventButton* ev, AudioClock* src)
158 {
159         if (!_session) {
160                 return false;
161         }
162
163         if (ev->button == 1) {
164                 _session->request_locate (src->current_time ());
165                 return true;
166         }
167
168         return false;
169 }
170
171 void
172 TimeInfoBox::sync_selection_mode (AudioClock* src)
173 {
174         if (!syncing_selection) {
175                 syncing_selection = true;
176                 selection_start->set_mode (src->mode());
177                 selection_end->set_mode (src->mode());
178                 selection_length->set_mode (src->mode());
179                 syncing_selection = false;
180         }
181 }
182
183 void
184 TimeInfoBox::sync_punch_mode (AudioClock* src)
185 {
186         if (!syncing_punch) {
187                 syncing_punch = true;
188                 punch_start->set_mode (src->mode());
189                 punch_end->set_mode (src->mode());
190                 syncing_punch = false;
191         }
192 }
193         
194
195 void
196 TimeInfoBox::set_session (Session* s)
197 {
198         SessionHandlePtr::set_session (s);
199
200         selection_start->set_session (s);
201         selection_end->set_session (s);
202         selection_length->set_session (s);
203
204         punch_start->set_session (s);
205         punch_end->set_session (s);
206
207         if (s) {
208                 Location* punch = s->locations()->auto_punch_location ();
209                 
210                 if (punch) {
211                         watch_punch (punch);
212                 }
213                 
214                 _session->auto_punch_location_changed.connect (_session_connections, MISSING_INVALIDATOR, 
215                                                                boost::bind (&TimeInfoBox::punch_location_changed, this, _1), gui_context());
216         }
217 }
218
219 void
220 TimeInfoBox::selection_changed ()
221 {
222         framepos_t s, e;
223         Selection& selection (Editor::instance().get_selection());
224
225         switch (Editor::instance().current_mouse_mode()) {
226         case Editing::MouseObject:
227                 if (selection.regions.empty()) {
228                         selection_start->set_off (true);
229                         selection_end->set_off (true);
230                         selection_length->set_off (true);
231                 } else {
232                         s = selection.regions.start();
233                         e = selection.regions.end_frame();
234                         selection_start->set_off (false);
235                         selection_end->set_off (false);
236                         selection_length->set_off (false);
237                         selection_start->set (s);
238                         selection_end->set (e);
239                         selection_length->set (e - s + 1);
240                 }
241                 break;
242
243         case Editing::MouseRange:
244                 if (selection.time.empty()) {
245                         selection_start->set_off (true);
246                         selection_end->set_off (true);
247                         selection_length->set_off (true);
248                 } else {
249                         selection_start->set_off (false);
250                         selection_end->set_off (false);
251                         selection_length->set_off (false);
252                         selection_start->set (selection.time.start());
253                         selection_end->set (selection.time.end_frame());
254                         selection_length->set (selection.time.length());
255                 }
256                 break;
257
258         default:
259                 selection_start->set_off (true);
260                 selection_end->set_off (true);
261                 selection_length->set_off (true);       
262                 break;
263         }
264 }
265
266 void
267 TimeInfoBox::punch_location_changed (Location* loc)
268 {
269         if (loc) {
270                 watch_punch (loc);
271         } 
272 }
273
274 void
275 TimeInfoBox::watch_punch (Location* punch)
276 {
277         punch_connections.drop_connections ();
278
279         punch->start_changed.connect (punch_connections, MISSING_INVALIDATOR, boost::bind (&TimeInfoBox::punch_changed, this, _1), gui_context());
280         punch->end_changed.connect (punch_connections, MISSING_INVALIDATOR, boost::bind (&TimeInfoBox::punch_changed, this, _1), gui_context());
281
282         punch_changed (punch);
283 }
284
285 void
286 TimeInfoBox::punch_changed (Location* loc)
287 {
288         if (!loc) {
289                 punch_start->set (99999999);
290                 punch_end->set (999999999);
291                 return;
292         }
293
294         punch_start->set (loc->start());
295         punch_end->set (loc->end());
296 }       
297
298 bool
299 TimeInfoBox::on_expose_event (GdkEventExpose* ev)
300 {
301         {
302                 int x, y;
303                 Gtk::Widget* window_parent;
304                 Glib::RefPtr<Gdk::Window> win = Gtkmm2ext::window_to_draw_on (*this, &window_parent);
305
306                 if (win) {
307                 
308                         Cairo::RefPtr<Cairo::Context> context = win->create_cairo_context();
309
310 #if 0                   
311                         translate_coordinates (*window_parent, ev->area.x, ev->area.y, x, y);
312                         context->rectangle (x, y, ev->area.width, ev->area.height);
313                         context->clip ();
314 #endif
315                         translate_coordinates (*window_parent, 0, 0, x, y);
316                         context->set_source_rgba (0.149, 0.149, 0.149, 1.0);
317                         Gtkmm2ext::rounded_rectangle (context, x, y, get_allocation().get_width(), get_allocation().get_height(), 5);
318                         context->fill ();
319                 }
320         }
321
322         Table::on_expose_event (ev);
323
324         return false;
325 }