more clock tweaking
[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 {
41         selection_start = new AudioClock ("selection-start", false, "SelectionClockDisplay", false, false, false, false);
42         selection_end = new AudioClock ("selection-end", false, "SelectionClockDisplay", false, false, false, false);
43         selection_length = new AudioClock ("selection-length", false, "SelectionClockDisplay", false, false, true, false);
44
45         punch_start = new AudioClock ("punch-start", false, "PunchClockDisplay", false, false, false, false);
46         punch_end = new AudioClock ("punch-end", false, "PunchClockDisplay", false, false, false, false);
47
48         bool bg = true;
49
50         CairoEditableText& ss (selection_start->main_display());
51         ss.set_ypad (1);
52         ss.set_xpad (1);
53         ss.set_corner_radius (0);
54         ss.set_draw_background (bg);
55
56         CairoEditableText& se (selection_end->main_display());
57         se.set_ypad (1);
58         se.set_xpad (1);
59         se.set_corner_radius (0);
60         se.set_draw_background (bg);
61
62         CairoEditableText& sl (selection_length->main_display());
63         sl.set_ypad (1);
64         sl.set_xpad (2);
65         sl.set_corner_radius (0);
66         sl.set_draw_background (bg);
67
68         CairoEditableText& ps (punch_start->main_display());
69         ps.set_ypad (1);
70         ps.set_xpad (2);
71         ps.set_corner_radius (0);
72         ps.set_draw_background (bg);
73
74         CairoEditableText& pe (punch_end->main_display());
75         pe.set_ypad (1);
76         pe.set_xpad (2);
77         pe.set_corner_radius (0);
78         pe.set_draw_background (bg);
79
80         selection_title.set_markup (string_compose ("<span size=\"x-small\">%1</span>", _("Selection")));
81         punch_title.set_markup (string_compose ("<span size=\"x-small\">%1</span>", _("Punch")));
82
83         set_homogeneous (false);
84         set_spacings (0);
85         set_border_width (2);
86
87         /* a bit more spacing between the two "sides" */
88         set_col_spacing (1, 10);
89
90         Gtk::Label* l;
91
92         attach (selection_title, 0, 2, 0, 1);
93         l = manage (new Label);
94         l->set_markup (string_compose ("<span size=\"x-small\">%1</span>", _("Start")));
95         attach (*l, 0, 1, 1, 2);
96         attach (*selection_start, 1, 2, 1, 2);
97         l = manage (new Label);
98         l->set_markup (string_compose ("<span size=\"x-small\">%1</span>", _("End")));
99         attach (*l, 0, 1, 2, 3);
100         attach (*selection_end, 1, 2, 2, 3);
101         l = manage (new Label);
102         l->set_markup (string_compose ("<span size=\"x-small\">%1</span>", _("Length")));
103         attach (*l, 0, 1, 3, 4);
104         attach (*selection_length, 1, 2, 3, 4);
105
106         attach (punch_title, 2, 4, 0, 1);
107         l = manage (new Label);
108         l->set_markup (string_compose ("<span size=\"x-small\">%1</span>", _("Start")));
109         attach (*l, 2, 3, 1, 2);
110         attach (*punch_start, 3, 4, 1, 2);
111         l = manage (new Label);
112         l->set_markup (string_compose ("<span size=\"x-small\">%1</span>", _("End")));
113         attach (*l, 2, 3, 2, 3);
114         attach (*punch_end, 3, 4, 2, 3);
115
116         show_all ();
117
118         Editor::instance().get_selection().TimeChanged.connect (sigc::mem_fun (*this, &TimeInfoBox::selection_changed));
119 }
120
121 TimeInfoBox::~TimeInfoBox ()
122 {
123         delete selection_length;
124         delete selection_end;
125         delete selection_start;
126         
127         delete punch_start;
128         delete punch_end;
129 }
130
131 void
132 TimeInfoBox::set_session (Session* s)
133 {
134         SessionHandlePtr::set_session (s);
135
136         selection_start->set_session (s);
137         selection_end->set_session (s);
138         selection_length->set_session (s);
139
140         punch_start->set_session (s);
141         punch_end->set_session (s);
142
143         if (s) {
144                 Location* punch = s->locations()->auto_punch_location ();
145                 
146                 if (punch) {
147                         watch_punch (punch);
148                 }
149                 
150                 _session->auto_punch_location_changed.connect (_session_connections, MISSING_INVALIDATOR, 
151                                                                boost::bind (&TimeInfoBox::punch_location_changed, this, _1), gui_context());
152         }
153 }
154
155 void
156 TimeInfoBox::selection_changed ()
157 {
158         selection_start->set (Editor::instance().get_selection().time.start());
159         selection_end->set (Editor::instance().get_selection().time.end_frame());
160         selection_length->set (Editor::instance().get_selection().time.length());
161 }
162
163 void
164 TimeInfoBox::punch_location_changed (Location* loc)
165 {
166         if (loc) {
167                 watch_punch (loc);
168         } 
169 }
170
171 void
172 TimeInfoBox::watch_punch (Location* punch)
173 {
174         punch_connections.drop_connections ();
175
176         punch->start_changed.connect (punch_connections, MISSING_INVALIDATOR, boost::bind (&TimeInfoBox::punch_changed, this, _1), gui_context());
177         punch->end_changed.connect (punch_connections, MISSING_INVALIDATOR, boost::bind (&TimeInfoBox::punch_changed, this, _1), gui_context());
178
179         punch_changed (punch);
180 }
181
182 void
183 TimeInfoBox::punch_changed (Location* loc)
184 {
185         if (!loc) {
186                 punch_start->set (99999999);
187                 punch_end->set (999999999);
188                 return;
189         }
190
191         punch_start->set (loc->start());
192         punch_end->set (loc->end());
193 }       
194
195 bool
196 TimeInfoBox::on_expose_event (GdkEventExpose* ev)
197 {
198         {
199                 int x, y;
200                 Gtk::Widget* window_parent;
201                 Glib::RefPtr<Gdk::Window> win = Gtkmm2ext::window_to_draw_on (*this, &window_parent);
202
203                 if (win) {
204                 
205                         Cairo::RefPtr<Cairo::Context> context = win->create_cairo_context();
206
207 #if 0                   
208                         translate_coordinates (*window_parent, ev->area.x, ev->area.y, x, y);
209                         context->rectangle (x, y, ev->area.width, ev->area.height);
210                         context->clip ();
211 #endif
212                         translate_coordinates (*window_parent, 0, 0, x, y);
213                         context->set_source_rgba (0.149, 0.149, 0.149, 1.0);
214                         Gtkmm2ext::rounded_rectangle (context, x, y, get_allocation().get_width(), get_allocation().get_height(), 5);
215                         context->fill ();
216                 }
217         }
218
219         Table::on_expose_event (ev);
220
221         return false;
222 }