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