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