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