Fix crash when X11 is not available for VST UIs
[ardour.git] / gtk2_ardour / verbose_cursor.cc
1 /*
2  * Copyright (C) 2011-2012 Carl Hetherington <carl@carlh.net>
3  * Copyright (C) 2011-2014 David Robillard <d@drobilla.net>
4  * Copyright (C) 2011-2017 Paul Davis <paul@linuxaudiosystems.com>
5  * Copyright (C) 2014-2017 Robin Gareus <robin@gareus.org>
6  * Copyright (C) 2015 Tim Mayberry <mojofunk@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #include <string>
24 #include <gtkmm/enums.h>
25 #include "pbd/stacktrace.h"
26 #include "ardour/profile.h"
27
28 #include "canvas/debug.h"
29 #include "canvas/scroll_group.h"
30 #include "canvas/tracking_text.h"
31
32 #include "audio_clock.h"
33 #include "editor.h"
34 #include "editor_drag.h"
35 #include "main_clock.h"
36 #include "verbose_cursor.h"
37 #include "ardour_ui.h"
38 #include "ui_config.h"
39
40 #include "pbd/i18n.h"
41
42 using namespace std;
43 using namespace ARDOUR;
44
45 VerboseCursor::VerboseCursor (Editor* editor)
46         : _editor (editor)
47 {
48         _canvas_item = new ArdourCanvas::TrackingText (_editor->get_noscroll_group());
49         CANVAS_DEBUG_NAME (_canvas_item, "verbose canvas cursor");
50         _canvas_item->set_font_description (Pango::FontDescription (UIConfiguration::instance().get_LargerBoldFont()));
51         color_handler ();
52
53         UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &VerboseCursor::color_handler));
54 }
55
56 void
57 VerboseCursor::color_handler ()
58 {
59         _canvas_item->set_color (UIConfiguration::instance().color_mod ("verbose canvas cursor", "verbose canvas cursor"));
60 }
61
62 ArdourCanvas::Item *
63 VerboseCursor::canvas_item () const
64 {
65         return _canvas_item;
66 }
67
68 /** Set the contents of the cursor.
69  */
70 void
71 VerboseCursor::set (string const & text)
72 {
73         _canvas_item->set (text);
74 }
75
76 void
77 VerboseCursor::show ()
78 {
79         _canvas_item->show_and_track (true, true);
80         _canvas_item->parent()->raise_to_top ();
81 }
82
83 void
84 VerboseCursor::hide ()
85 {
86         _canvas_item->hide ();
87         _canvas_item->parent()->lower_to_bottom ();
88         /* reset back to a sensible default for the next time we display the VC */
89         _canvas_item->set_offset (ArdourCanvas::Duple (10, 10));
90 }
91
92 void
93 VerboseCursor::set_offset (ArdourCanvas::Duple const & d)
94 {
95         _canvas_item->set_offset (d);
96 }
97
98 void
99 VerboseCursor::set_time (samplepos_t sample)
100 {
101         char buf[128];
102         Timecode::Time timecode;
103         Timecode::BBT_Time bbt;
104
105         if (_editor->_session == 0) {
106                 return;
107         }
108
109         /* Take clock mode from the primary clock */
110
111         AudioClock::Mode m = ARDOUR_UI::instance()->primary_clock->mode();
112
113         switch (m) {
114         case AudioClock::BBT:
115                 _editor->_session->bbt_time (sample, bbt);
116                 snprintf (buf, sizeof (buf), "%02" PRIu32 "|%02" PRIu32 "|%02" PRIu32, bbt.bars, bbt.beats, bbt.ticks);
117                 break;
118
119         case AudioClock::Timecode:
120                 _editor->_session->timecode_time (sample, timecode);
121                 snprintf (buf, sizeof (buf), "%s", Timecode::timecode_format_time (timecode).c_str());
122                 break;
123
124         case AudioClock::MinSec:
125                 AudioClock::print_minsec (sample, buf, sizeof (buf), _editor->_session->sample_rate());
126                 break;
127
128         case AudioClock::Seconds:
129                 snprintf (buf, sizeof(buf), "%.1f", sample / (float)_editor->_session->sample_rate());
130                 break;
131
132         default:
133                 snprintf (buf, sizeof(buf), "%" PRIi64, sample);
134                 break;
135         }
136
137         _canvas_item->set (buf);
138 }
139
140 void
141 VerboseCursor::set_duration (samplepos_t start, samplepos_t end)
142 {
143         char buf[128];
144         Timecode::Time timecode;
145         Timecode::BBT_Time sbbt;
146         Timecode::BBT_Time ebbt;
147         Meter meter_at_start (_editor->_session->tempo_map().meter_at_sample (start));
148
149         if (_editor->_session == 0) {
150                 return;
151         }
152
153         AudioClock::Mode m = ARDOUR_UI::instance()->primary_clock->mode ();
154
155         switch (m) {
156         case AudioClock::BBT:
157         {
158                 _editor->_session->bbt_time (start, sbbt);
159                 _editor->_session->bbt_time (end, ebbt);
160
161                 /* subtract */
162                 /* XXX this computation won't work well if the
163                 user makes a selection that spans any meter changes.
164                 */
165
166                 /* use signed integers for the working values so that
167                    we can underflow.
168                 */
169
170                 int ticks = ebbt.ticks;
171                 int beats = ebbt.beats;
172                 int bars = ebbt.bars;
173
174                 ticks -= sbbt.ticks;
175                 if (ticks < 0) {
176                         ticks += int (Timecode::BBT_Time::ticks_per_beat);
177                         --beats;
178                 }
179
180                 beats -= sbbt.beats;
181                 if (beats < 0) {
182                         beats += int (meter_at_start.divisions_per_bar());
183                         --bars;
184                 }
185
186                 bars -= sbbt.bars;
187
188                 snprintf (buf, sizeof (buf), "%02" PRIu32 "|%02" PRIu32 "|%02" PRIu32, bars, beats, ticks);
189                 break;
190         }
191
192         case AudioClock::Timecode:
193                 _editor->_session->timecode_duration (end - start, timecode);
194                 snprintf (buf, sizeof (buf), "%s", Timecode::timecode_format_time (timecode).c_str());
195                 break;
196
197         case AudioClock::MinSec:
198                 AudioClock::print_minsec (end - start, buf, sizeof (buf), _editor->_session->sample_rate());
199                 break;
200
201         case AudioClock::Seconds:
202                 snprintf (buf, sizeof(buf), "%.1f", (end - start) / (float)_editor->_session->sample_rate());
203                 break;
204
205         default:
206                 snprintf (buf, sizeof(buf), "%" PRIi64, end - start);
207                 break;
208         }
209
210         _canvas_item->set (buf);
211 }
212
213 bool
214 VerboseCursor::visible () const
215 {
216         return _canvas_item->visible();
217 }