3ce093511ba187d5f00b042d8e2a9b8d214d65ad
[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 #include "canvas/tracking_text.h"
28
29 #include "ardour_ui.h"
30 #include "audio_clock.h"
31 #include "editor.h"
32 #include "editor_drag.h"
33 #include "main_clock.h"
34 #include "verbose_cursor.h"
35
36 #include "i18n.h"
37
38 using namespace std;
39 using namespace ARDOUR;
40
41 VerboseCursor::VerboseCursor (Editor* editor)
42         : _editor (editor)
43 {
44         _canvas_item = new ArdourCanvas::TrackingText (_editor->get_noscroll_group());
45         CANVAS_DEBUG_NAME (_canvas_item, "verbose canvas cursor");
46         _canvas_item->set_font_description (Pango::FontDescription (ARDOUR_UI::config()->get_canvasvar_LargerBoldFont()));
47 }
48
49 ArdourCanvas::Item *
50 VerboseCursor::canvas_item () const
51 {
52         return _canvas_item;
53 }
54
55 /** Set the contents of the cursor.
56  */
57 void
58 VerboseCursor::set (string const & text)
59 {
60         _canvas_item->set (text);
61 }
62
63 void
64 VerboseCursor::show ()
65 {
66         _canvas_item->show_and_track (true, true);
67         _canvas_item->parent()->raise_to_top ();
68 }
69
70 void
71 VerboseCursor::hide ()
72 {
73         _canvas_item->hide ();
74         _canvas_item->parent()->lower_to_bottom ();
75         /* reset back to a sensible default for the next time we display the VC */
76         _canvas_item->set_offset (ArdourCanvas::Duple (10, 10));
77 }
78
79 void
80 VerboseCursor::set_offset (ArdourCanvas::Duple const & d)
81 {
82         _canvas_item->set_offset (d);
83 }
84
85 void
86 VerboseCursor::set_time (framepos_t frame)
87 {
88         char buf[128];
89         Timecode::Time timecode;
90         Timecode::BBT_Time bbt;
91         int hours, mins;
92         framepos_t frame_rate;
93         float secs;
94
95         if (_editor->_session == 0) {
96                 return;
97         }
98
99         AudioClock::Mode m;
100
101         if (Profile->get_sae() || Profile->get_small_screen() || Profile->get_trx()) {
102                 m = ARDOUR_UI::instance()->primary_clock->mode();
103         } else {
104                 m = ARDOUR_UI::instance()->secondary_clock->mode();
105         }
106
107         switch (m) {
108         case AudioClock::BBT:
109                 _editor->_session->bbt_time (frame, bbt);
110                 snprintf (buf, sizeof (buf), "%02" PRIu32 "|%02" PRIu32 "|%02" PRIu32, bbt.bars, bbt.beats, bbt.ticks);
111                 break;
112
113         case AudioClock::Timecode:
114                 _editor->_session->timecode_time (frame, timecode);
115                 snprintf (buf, sizeof (buf), "%02" PRId32 ":%02" PRId32 ":%02" PRId32 ":%02" PRId32, timecode.hours, timecode.minutes, timecode.seconds, timecode.frames);
116                 break;
117
118         case AudioClock::MinSec:
119                 /* XXX this is copied from show_verbose_duration_cursor() */
120                 frame_rate = _editor->_session->frame_rate();
121                 hours = frame / (frame_rate * 3600);
122                 frame = frame % (frame_rate * 3600);
123                 mins = frame / (frame_rate * 60);
124                 frame = frame % (frame_rate * 60);
125                 secs = (float) frame / (float) frame_rate;
126                 snprintf (buf, sizeof (buf), "%02" PRId32 ":%02" PRId32 ":%07.4f", hours, mins, secs);
127                 break;
128
129         default:
130                 snprintf (buf, sizeof(buf), "%" PRIi64, frame);
131                 break;
132         }
133
134         _canvas_item->set (buf);
135 }
136
137 void
138 VerboseCursor::set_duration (framepos_t start, framepos_t end)
139 {
140         char buf[128];
141         Timecode::Time timecode;
142         Timecode::BBT_Time sbbt;
143         Timecode::BBT_Time ebbt;
144         int hours, mins;
145         framepos_t distance, frame_rate;
146         float secs;
147         Meter meter_at_start (_editor->_session->tempo_map().meter_at(start));
148
149         if (_editor->_session == 0) {
150                 return;
151         }
152
153         AudioClock::Mode m;
154
155         if (Profile->get_sae() || Profile->get_small_screen() || Profile->get_trx()) {
156                 m = ARDOUR_UI::instance()->primary_clock->mode ();
157         } else {
158                 m = ARDOUR_UI::instance()->secondary_clock->mode ();
159         }
160
161         switch (m) {
162         case AudioClock::BBT:
163         {
164                 _editor->_session->bbt_time (start, sbbt);
165                 _editor->_session->bbt_time (end, ebbt);
166
167                 /* subtract */
168                 /* XXX this computation won't work well if the
169                 user makes a selection that spans any meter changes.
170                 */
171
172                 /* use signed integers for the working values so that
173                    we can underflow.
174                 */
175
176                 int ticks = ebbt.ticks;
177                 int beats = ebbt.beats;
178                 int bars = ebbt.bars;
179
180                 ticks -= sbbt.ticks;
181                 if (ticks < 0) {
182                         ticks += int (Timecode::BBT_Time::ticks_per_beat);
183                         --beats;
184                 }
185
186                 beats -= sbbt.beats;
187                 if (beats < 0) {
188                         beats += int (meter_at_start.divisions_per_bar());
189                         --bars;
190                 }
191
192                 bars -= sbbt.bars;
193
194                 snprintf (buf, sizeof (buf), "%02" PRIu32 "|%02" PRIu32 "|%02" PRIu32, bars, beats, ticks);
195                 break;
196         }
197
198         case AudioClock::Timecode:
199                 _editor->_session->timecode_duration (end - start, timecode);
200                 snprintf (buf, sizeof (buf), "%02" PRId32 ":%02" PRId32 ":%02" PRId32 ":%02" PRId32, timecode.hours, timecode.minutes, timecode.seconds, timecode.frames);
201                 break;
202
203         case AudioClock::MinSec:
204                 /* XXX this stuff should be elsewhere.. */
205                 distance = end - start;
206                 frame_rate = _editor->_session->frame_rate();
207                 hours = distance / (frame_rate * 3600);
208                 distance = distance % (frame_rate * 3600);
209                 mins = distance / (frame_rate * 60);
210                 distance = distance % (frame_rate * 60);
211                 secs = (float) distance / (float) frame_rate;
212                 snprintf (buf, sizeof (buf), "%02" PRId32 ":%02" PRId32 ":%07.4f", hours, mins, secs);
213                 break;
214
215         default:
216                 snprintf (buf, sizeof(buf), "%" PRIi64, end - start);
217                 break;
218         }
219
220         _canvas_item->set (buf);
221 }
222
223 void
224 VerboseCursor::set_color (uint32_t color)
225 {
226         _canvas_item->set_color (color);
227 }
228
229 bool
230 VerboseCursor::visible () const
231 {
232         return _canvas_item->visible();
233 }