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