partially revert some of the recent work on tempo to reflect new understanding of...
[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 "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"
31
32 #include "i18n.h"
33
34 using namespace std;
35 using namespace ARDOUR;
36
37 VerboseCursor::VerboseCursor (Editor* editor)
38         : _editor (editor)
39         , _visible (false)
40         , _xoffset (0)
41         , _yoffset (0)
42 {
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;
46 }
47
48 ArdourCanvas::Item *
49 VerboseCursor::canvas_item () const
50 {
51         return _canvas_item;
52 }
53
54 void
55 VerboseCursor::set (string const & text, double x, double y)
56 {
57         set_text (text);
58         set_position (x, y);
59 }
60
61 void
62 VerboseCursor::set_text (string const & text)
63 {
64         _canvas_item->property_text() = text.c_str();
65 }
66
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.
70  */
71 void
72 VerboseCursor::show (double xoffset, double yoffset)
73 {
74         _xoffset = xoffset;
75         _yoffset = yoffset;
76
77         if (_visible) {
78                 return;
79         }
80
81         _canvas_item->raise_to_top ();
82         _canvas_item->show ();
83         _visible = true;
84 }
85
86 void
87 VerboseCursor::hide ()
88 {
89         _canvas_item->hide ();
90         _visible = false;
91 }
92
93 double
94 VerboseCursor::clamp_x (double x)
95 {
96         if (x < 0) {
97                 x = 0;
98         } else {
99                 x = min (_editor->_canvas_width - 200.0, x);
100         }
101         return x;
102 }
103
104 double
105 VerboseCursor::clamp_y (double y)
106 {
107         if (y < _editor->canvas_timebars_vsize) {
108                 y = _editor->canvas_timebars_vsize;
109         } else {
110                 y = min (_editor->_canvas_height - 50, y);
111         }
112         return y;
113 }
114
115 void
116 VerboseCursor::set_time (framepos_t frame, double x, double y)
117 {
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;
124
125         if (_editor->_session == 0) {
126                 return;
127         }
128
129         AudioClock::Mode m;
130
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();
135         }
136
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;
142
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;
147
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;
158
159         default:
160                 snprintf (buf, sizeof(buf), "%" PRIi64, frame);
161                 break;
162         }
163
164         set (buf, x, y);
165 }
166
167 void
168 VerboseCursor::set_duration (framepos_t start, framepos_t end, double x, double y)
169 {
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));
178
179         if (_editor->_session == 0) {
180                 return;
181         }
182
183         AudioClock::Mode m;
184
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 ();
189         }
190
191         switch (m) {
192         case AudioClock::BBT:
193         {
194                 _editor->_session->bbt_time (start, sbbt);
195                 _editor->_session->bbt_time (end, ebbt);
196
197                 /* subtract */
198                 /* XXX this computation won't work well if the
199                 user makes a selection that spans any meter changes.
200                 */
201
202                 /* use signed integers for the working values so that
203                    we can underflow.
204                 */
205
206                 int ticks = ebbt.ticks;
207                 int beats = ebbt.beats;
208                 int bars = ebbt.bars;
209
210                 ticks -= sbbt.ticks;
211                 if (ticks < 0) {
212                         ticks += int (Timecode::BBT_Time::ticks_per_beat);
213                         --beats;
214                 }
215
216                 beats -= sbbt.beats;
217                 if (beats < 0) {
218                         beats += int (meter_at_start.divisions_per_bar());
219                         --bars;
220                 }
221
222                 bars -= sbbt.bars;
223
224                 snprintf (buf, sizeof (buf), "%02" PRIu32 "|%02" PRIu32 "|%02" PRIu32, bars, beats, ticks);
225                 break;
226         }
227
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;
232
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;
244
245         default:
246                 snprintf (buf, sizeof(buf), "%" PRIi64, end - start);
247                 break;
248         }
249
250         set (buf, x, y);
251 }
252
253 void
254 VerboseCursor::set_color (uint32_t color)
255 {
256         _canvas_item->property_fill_color_rgba() = color;
257 }
258
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.
262  */
263 void
264 VerboseCursor::set_position (double x, double y)
265 {
266         _canvas_item->property_x() = clamp_x (x + _xoffset);
267         _canvas_item->property_y() = clamp_y (y + _yoffset);
268 }
269
270 bool
271 VerboseCursor::visible () const
272 {
273         return _visible;
274 }