summaryrefslogtreecommitdiff
path: root/gtk2_ardour/verbose_cursor.cc
blob: bf11733b6d25e93e7e69b45dfa2870d2edeb89aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
    Copyright (C) 2000-2011 Paul Davis

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include <string>
#include <gtkmm/enums.h>
#include "pbd/stacktrace.h"
#include "ardour/profile.h"

#include "canvas/debug.h"
#include "canvas/scroll_group.h"
#include "canvas/tracking_text.h"

#include "audio_clock.h"
#include "editor.h"
#include "editor_drag.h"
#include "main_clock.h"
#include "verbose_cursor.h"
#include "ardour_ui.h"
#include "ui_config.h"

#include "pbd/i18n.h"

using namespace std;
using namespace ARDOUR;

VerboseCursor::VerboseCursor (Editor* editor)
	: _editor (editor)
{
	_canvas_item = new ArdourCanvas::TrackingText (_editor->get_noscroll_group());
	CANVAS_DEBUG_NAME (_canvas_item, "verbose canvas cursor");
	_canvas_item->set_font_description (Pango::FontDescription (UIConfiguration::instance().get_LargerBoldFont()));
	color_handler ();

	UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &VerboseCursor::color_handler));
}

void
VerboseCursor::color_handler ()
{
	_canvas_item->set_color (UIConfiguration::instance().color_mod ("verbose canvas cursor", "verbose canvas cursor"));
}

ArdourCanvas::Item *
VerboseCursor::canvas_item () const
{
	return _canvas_item;
}

/** Set the contents of the cursor.
 */
void
VerboseCursor::set (string const & text)
{
	_canvas_item->set (text);
}

void
VerboseCursor::show ()
{
	_canvas_item->show_and_track (true, true);
	_canvas_item->parent()->raise_to_top ();
}

void
VerboseCursor::hide ()
{
	_canvas_item->hide ();
	_canvas_item->parent()->lower_to_bottom ();
	/* reset back to a sensible default for the next time we display the VC */
	_canvas_item->set_offset (ArdourCanvas::Duple (10, 10));
}

void
VerboseCursor::set_offset (ArdourCanvas::Duple const & d)
{
	_canvas_item->set_offset (d);
}

void
VerboseCursor::set_time (framepos_t frame)
{
	char buf[128];
	Timecode::Time timecode;
	Timecode::BBT_Time bbt;

	if (_editor->_session == 0) {
		return;
	}

	/* Take clock mode from the primary clock */

	AudioClock::Mode m = ARDOUR_UI::instance()->primary_clock->mode();

	switch (m) {
	case AudioClock::BBT:
		_editor->_session->bbt_time (frame, bbt);
		snprintf (buf, sizeof (buf), "%02" PRIu32 "|%02" PRIu32 "|%02" PRIu32, bbt.bars, bbt.beats, bbt.ticks);
		break;

	case AudioClock::Timecode:
		_editor->_session->timecode_time (frame, timecode);
		snprintf (buf, sizeof (buf), "%s", Timecode::timecode_format_time (timecode).c_str());
		break;

	case AudioClock::MinSec:
		AudioClock::print_minsec (frame, buf, sizeof (buf), _editor->_session->frame_rate());
		break;

	default:
		snprintf (buf, sizeof(buf), "%" PRIi64, frame);
		break;
	}

	_canvas_item->set (buf);
}

void
VerboseCursor::set_duration (framepos_t start, framepos_t end)
{
	char buf[128];
	Timecode::Time timecode;
	Timecode::BBT_Time sbbt;
	Timecode::BBT_Time ebbt;
	Meter meter_at_start (_editor->_session->tempo_map().meter_at_frame (start));

	if (_editor->_session == 0) {
		return;
	}

	AudioClock::Mode m = ARDOUR_UI::instance()->primary_clock->mode ();

	switch (m) {
	case AudioClock::BBT:
	{
		_editor->_session->bbt_time (start, sbbt);
		_editor->_session->bbt_time (end, ebbt);

		/* subtract */
		/* XXX this computation won't work well if the
		user makes a selection that spans any meter changes.
		*/

		/* use signed integers for the working values so that
		   we can underflow.
		*/

		int ticks = ebbt.ticks;
		int beats = ebbt.beats;
		int bars = ebbt.bars;

		ticks -= sbbt.ticks;
		if (ticks < 0) {
			ticks += int (Timecode::BBT_Time::ticks_per_beat);
			--beats;
		}

		beats -= sbbt.beats;
		if (beats < 0) {
			beats += int (meter_at_start.divisions_per_bar());
			--bars;
		}

		bars -= sbbt.bars;

		snprintf (buf, sizeof (buf), "%02" PRIu32 "|%02" PRIu32 "|%02" PRIu32, bars, beats, ticks);
		break;
	}

	case AudioClock::Timecode:
		_editor->_session->timecode_duration (end - start, timecode);
		snprintf (buf, sizeof (buf), "%s", Timecode::timecode_format_time (timecode).c_str());
		break;

	case AudioClock::MinSec:
		AudioClock::print_minsec (end - start, buf, sizeof (buf), _editor->_session->frame_rate());
		break;

	default:
		snprintf (buf, sizeof(buf), "%" PRIi64, end - start);
		break;
	}

	_canvas_item->set (buf);
}

bool
VerboseCursor::visible () const
{
	return _canvas_item->visible();
}