1 /*
2  * Copyright (C) 2012-2017 Robin Gareus <robin@gareus.org>
3  * Copyright (C) 2013-2017 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2015 Colin Fletcher <colin.m.fletcher@googlemail.com>
5  * Copyright (C) 2015 Tim Mayberry <mojofunk@gmail.com>
6  * Copyright (C) 2016 Nick Mainsbridge <mainsbridge@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 "pbd/unwind.h"
24 #include "ardour/tempo.h"
25 
26 #include "actions.h"
27 #include "main_clock.h"
28 #include "ui_config.h"
29 #include "public_editor.h"
30 
31 #include "pbd/i18n.h"
32 
33 using namespace Gtk;
34 using namespace ARDOUR;
35 
MainClock(const std::string & clock_name,const std::string & widget_name,bool primary)36 MainClock::MainClock (
37 	const std::string& clock_name,
38 	const std::string& widget_name,
39 	bool primary
40 	)
41 	: AudioClock (clock_name, false, widget_name, true, true, false, true)
42 	, _primary (primary)
43 	, _suspend_delta_mode_signal (false)
44 {
45 }
46 
47 void
set_session(ARDOUR::Session * s)48 MainClock::set_session (ARDOUR::Session *s)
49 {
50 	AudioClock::set_session (s);
51 	_left_btn.set_related_action (ActionManager::get_action (X_("Editor"), X_("edit-current-tempo")));
52 	_right_btn.set_related_action (ActionManager::get_action (X_("Editor"), X_("edit-current-meter")));
53 }
54 
55 void
build_ops_menu()56 MainClock::build_ops_menu ()
57 {
58 	using namespace Menu_Helpers;
59 
60 	AudioClock::build_ops_menu ();
61 
62 	MenuList& ops_items = ops_menu->items();
63 	ops_items.push_back (SeparatorElem ());
64 	RadioMenuItem::Group group;
65 	PBD::Unwinder<bool> uw (_suspend_delta_mode_signal, true);
66 	ClockDeltaMode mode;
67 	if (_primary) {
68 		mode = UIConfiguration::instance().get_primary_clock_delta_mode ();
69 	} else {
70 		mode = UIConfiguration::instance().get_secondary_clock_delta_mode ();
71 	}
72 
73 	ops_items.push_back (RadioMenuElem (group, _("Display absolute time"), sigc::bind (sigc::mem_fun (*this, &MainClock::set_display_delta_mode), NoDelta)));
74 	if (mode == NoDelta) {
75 		RadioMenuItem* i = dynamic_cast<RadioMenuItem *> (&ops_items.back ());
76 		i->set_active (true);
77 	}
78 	ops_items.push_back (RadioMenuElem (group, _("Display delta to edit cursor"), sigc::bind (sigc::mem_fun (*this, &MainClock::set_display_delta_mode), DeltaEditPoint)));
79 	if (mode == DeltaEditPoint) {
80 		RadioMenuItem* i = dynamic_cast<RadioMenuItem *> (&ops_items.back ());
81 		i->set_active (true);
82 	}
83 	ops_items.push_back (RadioMenuElem (group, _("Display delta to origin marker"), sigc::bind (sigc::mem_fun (*this, &MainClock::set_display_delta_mode), DeltaOriginMarker)));
84 	if (mode == DeltaOriginMarker) {
85 		RadioMenuItem* i = dynamic_cast<RadioMenuItem *> (&ops_items.back ());
86 		i->set_active (true);
87 	}
88 
89 	ops_items.push_back (SeparatorElem());
90 
91 	ops_items.push_back (MenuElem (_("Edit Tempo"), sigc::mem_fun(*this, &MainClock::edit_current_tempo)));
92 	ops_items.push_back (MenuElem (_("Edit Meter"), sigc::mem_fun(*this, &MainClock::edit_current_meter)));
93 	ops_items.push_back (MenuElem (_("Insert Tempo Change"), sigc::mem_fun(*this, &MainClock::insert_new_tempo)));
94 	ops_items.push_back (MenuElem (_("Insert Meter Change"), sigc::mem_fun(*this, &MainClock::insert_new_meter)));
95 }
96 
97 samplepos_t
absolute_time() const98 MainClock::absolute_time () const
99 {
100 	if (get_is_duration ()) {
101 		return current_time () + offset ();
102 	} else {
103 		return current_time ();
104 	}
105 }
106 
107 void
set(samplepos_t when,bool force,ARDOUR::samplecnt_t)108 MainClock::set (samplepos_t when, bool force, ARDOUR::samplecnt_t /*offset*/)
109 {
110 	ClockDeltaMode mode;
111 	if (_primary) {
112 		mode = UIConfiguration::instance().get_primary_clock_delta_mode ();
113 	} else {
114 		mode = UIConfiguration::instance().get_secondary_clock_delta_mode ();
115 	}
116 	if (!PublicEditor::instance().session()) {
117 		mode = NoDelta;
118 	}
119 
120 	switch (mode) {
121 		case NoDelta:
122 			AudioClock::set (when, force, 0);
123 			break;
124 		case DeltaEditPoint:
125 			AudioClock::set (when, force, PublicEditor::instance().get_preferred_edit_position (Editing::EDIT_IGNORE_PHEAD));
126 			break;
127 		case DeltaOriginMarker:
128 			{
129 				Location* loc = PublicEditor::instance().session()->locations()->clock_origin_location ();
130 				AudioClock::set (when, force, loc ? loc->start() : 0);
131 			}
132 			break;
133 	}
134 }
135 
136 void
set_display_delta_mode(ClockDeltaMode m)137 MainClock::set_display_delta_mode (ClockDeltaMode m)
138 {
139 	if (_suspend_delta_mode_signal) {
140 		return;
141 	}
142 	if (_primary) {
143 		UIConfiguration::instance().set_primary_clock_delta_mode (m);
144 	} else {
145 		UIConfiguration::instance().set_secondary_clock_delta_mode (m);
146 	}
147 }
148 
149 void
edit_current_tempo()150 MainClock::edit_current_tempo ()
151 {
152 	if (!PublicEditor::instance().session()) return;
153 	ARDOUR::TempoSection* ts = const_cast<ARDOUR::TempoSection*>(&PublicEditor::instance().session()->tempo_map().tempo_section_at_sample (absolute_time()));
154 	PublicEditor::instance().edit_tempo_section (ts);
155 }
156 
157 void
edit_current_meter()158 MainClock::edit_current_meter ()
159 {
160 	if (!PublicEditor::instance().session()) return;
161 	ARDOUR::MeterSection* ms = const_cast<ARDOUR::MeterSection*>(&PublicEditor::instance().session()->tempo_map().meter_section_at_sample (absolute_time()));
162 	PublicEditor::instance().edit_meter_section (ms);
163 }
164 
165 void
insert_new_tempo()166 MainClock::insert_new_tempo ()
167 {
168 	PublicEditor::instance().mouse_add_new_tempo_event (absolute_time ());
169 }
170 
171 void
insert_new_meter()172 MainClock::insert_new_meter ()
173 {
174 	PublicEditor::instance().mouse_add_new_meter_event (absolute_time ());
175 }
176