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