use newly factored canvas in gtk2_ardour
[ardour.git] / gtk2_ardour / verbose_cursor.cc
1 /*
2     Copyright (C) 2000-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 <string>
21 #include <gtkmm/enums.h>
22 #include "pbd/stacktrace.h"
23 #include "ardour/profile.h"
24
25 #include "canvas/debug.h"
26 #include "canvas/scroll_group.h"
27
28 #include "ardour_ui.h"
29 #include "audio_clock.h"
30 #include "editor.h"
31 #include "editor_drag.h"
32 #include "main_clock.h"
33 #include "utils.h"
34 #include "verbose_cursor.h"
35
36 #include "i18n.h"
37
38 using namespace std;
39 using namespace ARDOUR;
40
41 VerboseCursor::VerboseCursor (Editor* editor)
42         : _editor (editor)
43         , _visible (false)
44         , _xoffset (0)
45         , _yoffset (0)
46 {
47         _canvas_item = new ArdourCanvas::Text (_editor->get_hvscroll_group());
48         CANVAS_DEBUG_NAME (_canvas_item, "verbose canvas cursor");
49         _canvas_item->set_ignore_events (true);
50         _canvas_item->set_font_description (Pango::FontDescription (ARDOUR_UI::config()->get_canvasvar_LargerBoldFont()));
51 }
52
53 ArdourCanvas::Item *
54 VerboseCursor::canvas_item () const
55 {
56         return _canvas_item;
57 }
58
59 /** Set the contents and position of the cursor. Coordinates are in window space 
60  */
61 void
62 VerboseCursor::set (string const & text, double x, double y)
63 {
64         set_text (text);
65         set_position (x, y);
66 }
67
68 void
69 VerboseCursor::set_text (string const & text)
70 {
71         _canvas_item->set (text);
72 }
73
74 /** @param xoffset x offset to be applied on top of any set_position() call
75  *  before the next show ().
76  *  @param yoffset y offset as above.
77  */
78 void
79 VerboseCursor::show (double xoffset, double yoffset)
80 {
81         _xoffset = xoffset;
82         _yoffset = yoffset;
83
84         if (_visible) {
85                 return;
86         }
87
88         _canvas_item->raise_to_top ();
89         _canvas_item->show ();
90         _visible = true;
91 }
92
93 void
94 VerboseCursor::hide ()
95 {
96         _canvas_item->hide ();
97         _visible = false;
98 }
99
100 double
101 VerboseCursor::clamp_x (double x)
102 {
103         _editor->clamp_verbose_cursor_x (x);
104         return x;
105 }
106
107 double
108 VerboseCursor::clamp_y (double y)
109 {
110         _editor->clamp_verbose_cursor_y (y);
111         return y;
112 }
113
114 void
115 VerboseCursor::set_time (framepos_t frame, double x, double y)
116 {
117         char buf[128];
118         Timecode::Time timecode;
119         Timecode::BBT_Time bbt;
120         int hours, mins;
121         framepos_t frame_rate;
122         float secs;
123
124         if (_editor->_session == 0) {
125                 return;
126         }
127
128         AudioClock::Mode m;
129
130         if (Profile->get_sae() || Profile->get_small_screen() || Profile->get_trx()) {
131                 m = ARDOUR_UI::instance()->primary_clock->mode();
132         } else {
133                 m = ARDOUR_UI::instance()->secondary_clock->mode();
134         }
135
136         switch (m) {
137         case AudioClock::BBT:
138                 _editor->_session->bbt_time (frame, bbt);
139                 snprintf (buf, sizeof (buf), "%02" PRIu32 "|%02" PRIu32 "|%02" PRIu32, bbt.bars, bbt.beats, bbt.ticks);
140                 break;
141
142         case AudioClock::Timecode:
143                 _editor->_session->timecode_time (frame, timecode);
144                 snprintf (buf, sizeof (buf), "%02" PRId32 ":%02" PRId32 ":%02" PRId32 ":%02" PRId32, timecode.hours, timecode.minutes, timecode.seconds, timecode.frames);
145                 break;
146
147         case AudioClock::MinSec:
148                 /* XXX this is copied from show_verbose_duration_cursor() */
149                 frame_rate = _editor->_session->frame_rate();
150                 hours = frame / (frame_rate * 3600);
151                 frame = frame % (frame_rate * 3600);
152                 mins = frame / (frame_rate * 60);
153                 frame = frame % (frame_rate * 60);
154                 secs = (float) frame / (float) frame_rate;
155                 snprintf (buf, sizeof (buf), "%02" PRId32 ":%02" PRId32 ":%07.4f", hours, mins, secs);
156                 break;
157
158         default:
159                 snprintf (buf, sizeof(buf), "%" PRIi64, frame);
160                 break;
161         }
162
163         set (buf, x, y);
164 }
165
166 void
167 VerboseCursor::set_duration (framepos_t start, framepos_t end, double x, double y)
168 {
169         char buf[128];
170         Timecode::Time timecode;
171         Timecode::BBT_Time sbbt;
172         Timecode::BBT_Time ebbt;
173         int hours, mins;
174         framepos_t distance, frame_rate;
175         float secs;
176         Meter meter_at_start (_editor->_session->tempo_map().meter_at(start));
177
178         if (_editor->_session == 0) {
179                 return;
180         }
181
182         AudioClock::Mode m;
183
184         if (Profile->get_sae() || Profile->get_small_screen() || Profile->get_trx()) {
185                 m = ARDOUR_UI::instance()->primary_clock->mode ();
186         } else {
187                 m = ARDOUR_UI::instance()->secondary_clock->mode ();
188         }
189
190         switch (m) {
191         case AudioClock::BBT:
192         {
193                 _editor->_session->bbt_time (start, sbbt);
194                 _editor->_session->bbt_time (end, ebbt);
195
196                 /* subtract */
197                 /* XXX this computation won't work well if the
198                 user makes a selection that spans any meter changes.
199                 */
200
201                 /* use signed integers for the working values so that
202                    we can underflow.
203                 */
204
205                 int ticks = ebbt.ticks;
206                 int beats = ebbt.beats;
207                 int bars = ebbt.bars;
208
209                 ticks -= sbbt.ticks;
210                 if (ticks < 0) {
211                         ticks += int (Timecode::BBT_Time::ticks_per_beat);
212                         --beats;
213                 }
214
215                 beats -= sbbt.beats;
216                 if (beats < 0) {
217                         beats += int (meter_at_start.divisions_per_bar());
218                         --bars;
219                 }
220
221                 bars -= sbbt.bars;
222
223                 snprintf (buf, sizeof (buf), "%02" PRIu32 "|%02" PRIu32 "|%02" PRIu32, bars, beats, ticks);
224                 break;
225         }
226
227         case AudioClock::Timecode:
228                 _editor->_session->timecode_duration (end - start, timecode);
229                 snprintf (buf, sizeof (buf), "%02" PRId32 ":%02" PRId32 ":%02" PRId32 ":%02" PRId32, timecode.hours, timecode.minutes, timecode.seconds, timecode.frames);
230                 break;
231
232         case AudioClock::MinSec:
233                 /* XXX this stuff should be elsewhere.. */
234                 distance = end - start;
235                 frame_rate = _editor->_session->frame_rate();
236                 hours = distance / (frame_rate * 3600);
237                 distance = distance % (frame_rate * 3600);
238                 mins = distance / (frame_rate * 60);
239                 distance = distance % (frame_rate * 60);
240                 secs = (float) distance / (float) frame_rate;
241                 snprintf (buf, sizeof (buf), "%02" PRId32 ":%02" PRId32 ":%07.4f", hours, mins, secs);
242                 break;
243
244         default:
245                 snprintf (buf, sizeof(buf), "%" PRIi64, end - start);
246                 break;
247         }
248
249         set (buf, x, y);
250 }
251
252 void
253 VerboseCursor::set_color (uint32_t color)
254 {
255         _canvas_item->set_color (color);
256 }
257
258 /** Set the position of the verbose cursor.  Any x/y offsets
259  *  passed to the last call to show() will be applied to the
260  *  coordinates passed in here.
261  * 
262  *  Coordinates are in window space.
263  */
264 void
265 VerboseCursor::set_position (double x, double y)
266 {
267         _canvas_item->set_x_position (clamp_x (x + _xoffset));
268         _canvas_item->set_y_position (clamp_y (y + _yoffset));
269 }
270
271 bool
272 VerboseCursor::visible () const
273 {
274         return _visible;
275 }