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