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