Remove erroneous assert which I added earlier.
[ardour2.git] / gtk2_ardour / verbose_cursor.cc
blob2ae215acecae8fa8d560c1d29e4de0eb6476a57d
1 /*
2 Copyright (C) 2000-2011 Paul Davis
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.
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.
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.
20 #include <string>
21 #include <gtkmm/enums.h>
22 #include "pbd/stacktrace.h"
23 #include "ardour/profile.h"
25 #include "ardour_ui.h"
26 #include "audio_clock.h"
27 #include "editor.h"
28 #include "editor_drag.h"
29 #include "utils.h"
30 #include "verbose_cursor.h"
32 #include "i18n.h"
34 using namespace std;
35 using namespace ARDOUR;
37 VerboseCursor::VerboseCursor (Editor* editor)
38 : _editor (editor)
39 , _visible (false)
40 , _xoffset (0)
41 , _yoffset (0)
43 _canvas_item = new ArdourCanvas::NoEventText (*_editor->track_canvas->root());
44 _canvas_item->property_font_desc() = get_font_for_style (N_("VerboseCanvasCursor"));
45 _canvas_item->property_anchor() = Gtk::ANCHOR_NW;
48 ArdourCanvas::Item *
49 VerboseCursor::canvas_item () const
51 return _canvas_item;
54 void
55 VerboseCursor::set (string const & text, double x, double y)
57 set_text (text);
58 set_position (x, y);
61 void
62 VerboseCursor::set_text (string const & text)
64 _canvas_item->property_text() = text.c_str();
67 /** @param xoffset x offset to be applied on top of any set_position() call
68 * before the next show ().
69 * @param yoffset y offset as above.
71 void
72 VerboseCursor::show (double xoffset, double yoffset)
74 _xoffset = xoffset;
75 _yoffset = yoffset;
77 if (_visible) {
78 return;
81 _canvas_item->raise_to_top ();
82 _canvas_item->show ();
83 _visible = true;
86 void
87 VerboseCursor::hide ()
89 _canvas_item->hide ();
90 _visible = false;
93 double
94 VerboseCursor::clamp_x (double x)
96 if (x < 0) {
97 x = 0;
98 } else {
99 x = min (_editor->_canvas_width - 200.0, x);
101 return x;
104 double
105 VerboseCursor::clamp_y (double y)
107 if (y < _editor->canvas_timebars_vsize) {
108 y = _editor->canvas_timebars_vsize;
109 } else {
110 y = min (_editor->_canvas_height - 50, y);
112 return y;
115 void
116 VerboseCursor::set_time (framepos_t frame, double x, double y)
118 char buf[128];
119 Timecode::Time timecode;
120 Timecode::BBT_Time bbt;
121 int hours, mins;
122 framepos_t frame_rate;
123 float secs;
125 if (_editor->_session == 0) {
126 return;
129 AudioClock::Mode m;
131 if (Profile->get_sae() || Profile->get_small_screen()) {
132 m = ARDOUR_UI::instance()->primary_clock->mode();
133 } else {
134 m = ARDOUR_UI::instance()->secondary_clock->mode();
137 switch (m) {
138 case AudioClock::BBT:
139 _editor->_session->bbt_time (frame, bbt);
140 snprintf (buf, sizeof (buf), "%02" PRIu32 "|%02" PRIu32 "|%02" PRIu32, bbt.bars, bbt.beats, bbt.ticks);
141 break;
143 case AudioClock::Timecode:
144 _editor->_session->timecode_time (frame, timecode);
145 snprintf (buf, sizeof (buf), "%02" PRId32 ":%02" PRId32 ":%02" PRId32 ":%02" PRId32, timecode.hours, timecode.minutes, timecode.seconds, timecode.frames);
146 break;
148 case AudioClock::MinSec:
149 /* XXX this is copied from show_verbose_duration_cursor() */
150 frame_rate = _editor->_session->frame_rate();
151 hours = frame / (frame_rate * 3600);
152 frame = frame % (frame_rate * 3600);
153 mins = frame / (frame_rate * 60);
154 frame = frame % (frame_rate * 60);
155 secs = (float) frame / (float) frame_rate;
156 snprintf (buf, sizeof (buf), "%02" PRId32 ":%02" PRId32 ":%07.4f", hours, mins, secs);
157 break;
159 default:
160 snprintf (buf, sizeof(buf), "%" PRIi64, frame);
161 break;
164 set (buf, x, y);
167 void
168 VerboseCursor::set_duration (framepos_t start, framepos_t end, double x, double y)
170 char buf[128];
171 Timecode::Time timecode;
172 Timecode::BBT_Time sbbt;
173 Timecode::BBT_Time ebbt;
174 int hours, mins;
175 framepos_t distance, frame_rate;
176 float secs;
177 Meter meter_at_start (_editor->_session->tempo_map().meter_at(start));
179 if (_editor->_session == 0) {
180 return;
183 AudioClock::Mode m;
185 if (Profile->get_sae() || Profile->get_small_screen()) {
186 m = ARDOUR_UI::instance()->primary_clock->mode ();
187 } else {
188 m = ARDOUR_UI::instance()->secondary_clock->mode ();
191 switch (m) {
192 case AudioClock::BBT:
194 _editor->_session->bbt_time (start, sbbt);
195 _editor->_session->bbt_time (end, ebbt);
197 /* subtract */
198 /* XXX this computation won't work well if the
199 user makes a selection that spans any meter changes.
202 /* use signed integers for the working values so that
203 we can underflow.
206 int ticks = ebbt.ticks;
207 int beats = ebbt.beats;
208 int bars = ebbt.bars;
210 ticks -= sbbt.ticks;
211 if (ticks < 0) {
212 ticks += int (Timecode::BBT_Time::ticks_per_beat);
213 --beats;
216 beats -= sbbt.beats;
217 if (beats < 0) {
218 beats += int (meter_at_start.beats_per_bar());
219 --bars;
222 bars -= sbbt.bars;
224 snprintf (buf, sizeof (buf), "%02" PRIu32 "|%02" PRIu32 "|%02" PRIu32, bars, beats, ticks);
225 break;
228 case AudioClock::Timecode:
229 _editor->_session->timecode_duration (end - start, timecode);
230 snprintf (buf, sizeof (buf), "%02" PRId32 ":%02" PRId32 ":%02" PRId32 ":%02" PRId32, timecode.hours, timecode.minutes, timecode.seconds, timecode.frames);
231 break;
233 case AudioClock::MinSec:
234 /* XXX this stuff should be elsewhere.. */
235 distance = end - start;
236 frame_rate = _editor->_session->frame_rate();
237 hours = distance / (frame_rate * 3600);
238 distance = distance % (frame_rate * 3600);
239 mins = distance / (frame_rate * 60);
240 distance = distance % (frame_rate * 60);
241 secs = (float) distance / (float) frame_rate;
242 snprintf (buf, sizeof (buf), "%02" PRId32 ":%02" PRId32 ":%07.4f", hours, mins, secs);
243 break;
245 default:
246 snprintf (buf, sizeof(buf), "%" PRIi64, end - start);
247 break;
250 set (buf, x, y);
253 void
254 VerboseCursor::set_color (uint32_t color)
256 _canvas_item->property_fill_color_rgba() = color;
259 /** Set the position of the verbose cursor. Any x/y offsets
260 * passed to the last call to show() will be applied to the
261 * coordinates passed in here.
263 void
264 VerboseCursor::set_position (double x, double y)
266 _canvas_item->property_x() = clamp_x (x + _xoffset);
267 _canvas_item->property_y() = clamp_y (y + _yoffset);
270 bool
271 VerboseCursor::visible () const
273 return _visible;