make AU Cocoa plugin views with "client-side-windows" versions of GTK+; make keyboard...
[ardour2.git] / gtk2_ardour / audio_region_editor.cc
blobb30b462841d8a88d30781b15424641a0ef957015
1 /*
2 Copyright (C) 2001 Paul Davis
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.
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.
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.
20 #include <pbd/memento_command.h>
22 #include <ardour/audioregion.h>
23 #include <ardour/playlist.h>
24 #include <ardour/utils.h>
25 #include <ardour/dB.h>
26 #include <gtkmm2ext/utils.h>
27 #include <gtkmm2ext/stop_signal.h>
28 #include <cmath>
30 #include "audio_region_editor.h"
31 #include "audio_region_view.h"
32 #include "ardour_ui.h"
33 #include "utils.h"
34 #include "gui_thread.h"
36 #include "i18n.h"
38 using namespace ARDOUR;
39 using namespace PBD;
40 using namespace sigc;
41 using namespace std;
42 using namespace Gtkmm2ext;
44 AudioRegionEditor::AudioRegionEditor (Session& s, boost::shared_ptr<AudioRegion> r, AudioRegionView& rv)
45 : RegionEditor (s),
46 _region (r),
47 _region_view (rv),
48 name_label (_("Name:")),
49 audition_button (_("Audition")),
50 time_table (5, 2),
51 position_clock (X_("regionposition"), true, X_("AudioRegionEditorClock"), true),
52 end_clock (X_("regionend"), true, X_("AudioRegionEditorClock"), true),
53 length_clock (X_("regionlength"), true, X_("AudioRegionEditorClock"), true, true),
54 /* XXX cannot edit sync point or start yet */
55 sync_offset_clock (X_("regionsyncoffset"), true, X_("AudioRegionEditorClock"), false),
56 start_clock (X_("regionstart"), true, X_("AudioRegionEditorClock"), false),
57 gain_adjustment(accurate_coefficient_to_dB(_region->scale_amplitude()), -40.0, +40.0, 0.1, 1.0, 0)
59 position_clock.set_session (&_session);
60 end_clock.set_session (&_session);
61 length_clock.set_session (&_session);
62 sync_offset_clock.set_session (&_session);
63 start_clock.set_session (&_session);
65 name_entry.set_name ("AudioRegionEditorEntry");
66 name_label.set_name ("AudioRegionEditorLabel");
68 name_hbox.set_spacing (5);
69 name_hbox.pack_start (name_label, false, false);
70 name_hbox.pack_start (name_entry, false, false);
72 ARDOUR_UI::instance()->tooltips().set_tip (audition_button, _("audition this region"));
74 audition_button.unset_flags (Gtk::CAN_FOCUS);
76 audition_button.set_events (audition_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
78 top_row_button_hbox.set_border_width (5);
79 top_row_button_hbox.set_spacing (5);
80 top_row_button_hbox.set_homogeneous (false);
81 top_row_button_hbox.pack_end (audition_button, false, false);
83 top_row_hbox.pack_start (name_hbox, true, true);
84 top_row_hbox.pack_end (top_row_button_hbox, true, true);
86 position_label.set_name ("AudioRegionEditorLabel");
87 position_label.set_text (_("Position:"));
88 end_label.set_name ("AudioRegionEditorLabel");
89 end_label.set_text (_("End:"));
90 length_label.set_name ("AudioRegionEditorLabel");
91 length_label.set_text (_("Length:"));
92 sync_label.set_name ("AudioRegionEditorLabel");
93 sync_label.set_text (_("Sync Point:"));
94 start_label.set_name ("AudioRegionEditorLabel");
95 start_label.set_text (_("File Start:"));
97 time_table.set_col_spacings (2);
98 time_table.set_row_spacings (5);
99 time_table.set_border_width (5);
101 position_alignment.set (1.0, 0.5);
102 end_alignment.set (1.0, 0.5);
103 length_alignment.set (1.0, 0.5);
104 sync_alignment.set (1.0, 0.5);
105 start_alignment.set (1.0, 0.5);
107 position_alignment.add (position_label);
108 end_alignment.add (end_label);
109 length_alignment.add (length_label);
110 sync_alignment.add (sync_label);
111 start_alignment.add (start_label);
113 time_table.attach (position_alignment, 0, 1, 0, 1, Gtk::FILL, Gtk::FILL);
114 time_table.attach (position_clock, 1, 2, 0, 1, Gtk::FILL, Gtk::FILL);
116 time_table.attach (end_alignment, 0, 1, 1, 2, Gtk::FILL, Gtk::FILL);
117 time_table.attach (end_clock, 1, 2, 1, 2, Gtk::FILL, Gtk::FILL);
119 time_table.attach (length_alignment, 0, 1, 2, 3, Gtk::FILL, Gtk::FILL);
120 time_table.attach (length_clock, 1, 2, 2, 3, Gtk::FILL, Gtk::FILL);
122 time_table.attach (sync_alignment, 0, 1, 3, 4, Gtk::FILL, Gtk::FILL);
123 time_table.attach (sync_offset_clock, 1, 2, 3, 4, Gtk::FILL, Gtk::FILL);
125 time_table.attach (start_alignment, 0, 1, 4, 5, Gtk::FILL, Gtk::FILL);
126 time_table.attach (start_clock, 1, 2, 4, 5, Gtk::FILL, Gtk::FILL);
128 gain_label.set_name ("AudioRegionEditorLabel");
129 gain_label.set_text (_("Scale amplitude:"));
130 gain_label.set_alignment (1, 0.5);
131 gain_entry.configure (gain_adjustment, 0.0, 1);
132 time_table.attach (gain_label, 0, 1, 6, 7, Gtk::FILL, Gtk::FILL);
133 time_table.attach (gain_entry, 1, 2, 6, 7, Gtk::FILL, Gtk::FILL);
135 lower_hbox.pack_start (time_table, true, true);
136 lower_hbox.pack_start (sep1, false, false);
137 lower_hbox.pack_start (sep2, false, false);
139 get_vbox()->pack_start (top_row_hbox, true, true);
140 get_vbox()->pack_start (sep3, false, false);
141 get_vbox()->pack_start (lower_hbox, true, true);
143 set_name ("AudioRegionEditorWindow");
144 add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
146 signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), static_cast<Window *> (this)));
148 set_title (string_compose (_("Region %1"), _region->name()));
150 show_all();
152 name_changed ();
153 bounds_changed (Change (StartChanged|LengthChanged|PositionChanged|StartChanged|Region::SyncOffsetChanged));
154 gain_changed ();
156 _region->StateChanged.connect (mem_fun(*this, &AudioRegionEditor::region_changed));
158 spin_arrow_grab = false;
160 connect_editor_events ();
163 AudioRegionEditor::~AudioRegionEditor ()
167 void
168 AudioRegionEditor::region_changed (Change what_changed)
170 if (what_changed & NameChanged) {
171 name_changed ();
174 if (what_changed & Change (BoundsChanged|StartChanged|Region::SyncOffsetChanged)) {
175 bounds_changed (what_changed);
178 if (what_changed & AudioRegion::ScaleAmplitudeChanged) {
179 gain_changed ();
183 gint
184 AudioRegionEditor::bpressed (GdkEventButton* ev, Gtk::SpinButton* but, void (AudioRegionEditor::*pmf)())
186 switch (ev->button) {
187 case 1:
188 case 2:
189 case 3:
190 if (ev->type == GDK_BUTTON_PRESS) { /* no double clicks here */
191 if (!spin_arrow_grab) {
192 // GTK2FIX probably nuke the region editor
193 // if ((ev->window == but->gobj()->panel)) {
194 // spin_arrow_grab = true;
195 // (this->*pmf)();
196 // }
199 break;
200 default:
201 break;
203 return FALSE;
206 gint
207 AudioRegionEditor::breleased (GdkEventButton* ev, Gtk::SpinButton* but, void (AudioRegionEditor::*pmf)())
209 if (spin_arrow_grab) {
210 (this->*pmf)();
211 spin_arrow_grab = false;
213 return FALSE;
216 void
217 AudioRegionEditor::connect_editor_events ()
219 name_entry.signal_changed().connect (mem_fun(*this, &AudioRegionEditor::name_entry_changed));
221 position_clock.ValueChanged.connect (mem_fun(*this, &AudioRegionEditor::position_clock_changed));
222 end_clock.ValueChanged.connect (mem_fun(*this, &AudioRegionEditor::end_clock_changed));
223 length_clock.ValueChanged.connect (mem_fun(*this, &AudioRegionEditor::length_clock_changed));
224 gain_adjustment.signal_value_changed().connect (mem_fun (*this, &AudioRegionEditor::gain_adjustment_changed));
226 audition_button.signal_toggled().connect (mem_fun(*this, &AudioRegionEditor::audition_button_toggled));
227 _session.AuditionActive.connect (mem_fun(*this, &AudioRegionEditor::audition_state_changed));
230 void
231 AudioRegionEditor::position_clock_changed ()
233 _session.begin_reversible_command (_("change region start position"));
235 boost::shared_ptr<Playlist> pl = _region->playlist();
237 if (pl) {
238 XMLNode &before = pl->get_state();
239 _region->set_position (position_clock.current_time(), this);
240 XMLNode &after = pl->get_state();
241 _session.add_command(new MementoCommand<Playlist>(*pl, &before, &after));
244 _session.commit_reversible_command ();
247 void
248 AudioRegionEditor::end_clock_changed ()
250 _session.begin_reversible_command (_("change region end position"));
252 boost::shared_ptr<Playlist> pl = _region->playlist();
254 if (pl) {
255 XMLNode &before = pl->get_state();
256 _region->trim_end (end_clock.current_time(), this);
257 XMLNode &after = pl->get_state();
258 _session.add_command(new MementoCommand<Playlist>(*pl, &before, &after));
261 _session.commit_reversible_command ();
263 end_clock.set (_region->position() + _region->length()-1, true);
266 void
267 AudioRegionEditor::length_clock_changed ()
269 nframes_t frames = length_clock.current_time();
271 _session.begin_reversible_command (_("change region length"));
273 boost::shared_ptr<Playlist> pl = _region->playlist();
275 if (pl) {
276 XMLNode &before = pl->get_state();
277 _region->trim_end (_region->position() + frames -1, this);
278 XMLNode &after = pl->get_state();
279 _session.add_command(new MementoCommand<Playlist>(*pl, &before, &after));
282 _session.commit_reversible_command ();
284 length_clock.set (_region->length());
287 void
288 AudioRegionEditor::gain_changed ()
290 float const region_gain_dB = accurate_coefficient_to_dB (_region->scale_amplitude());
291 if (region_gain_dB != gain_adjustment.get_value()) {
292 gain_adjustment.set_value(region_gain_dB);
296 void
297 AudioRegionEditor::gain_adjustment_changed ()
299 float const gain = dB_to_coefficient (gain_adjustment.get_value());
300 if (_region->scale_amplitude() != gain) {
301 _region->set_scale_amplitude (gain);
305 void
306 AudioRegionEditor::audition_button_toggled ()
308 if (audition_button.get_active()) {
309 _session.audition_region (_region);
310 } else {
311 _session.cancel_audition ();
315 void
316 AudioRegionEditor::name_changed ()
318 if (name_entry.get_text() != _region->name()) {
319 name_entry.set_text (_region->name());
323 void
324 AudioRegionEditor::bounds_changed (Change what_changed)
326 if ((what_changed & Change (PositionChanged|LengthChanged)) == Change (PositionChanged|LengthChanged)) {
327 position_clock.set (_region->position(), true);
328 end_clock.set (_region->position() + _region->length() -1, true);
329 length_clock.set (_region->length(), true);
330 } else if (what_changed & Change (PositionChanged)) {
331 position_clock.set (_region->position(), true);
332 end_clock.set (_region->position() + _region->length() -1, true);
333 } else if (what_changed & Change (LengthChanged)) {
334 end_clock.set (_region->position() + _region->length() -1, true);
335 length_clock.set (_region->length(), true);
338 if (what_changed & Region::SyncOffsetChanged) {
339 sync_offset_clock.set (_region->sync_position(), true);
342 if (what_changed & StartChanged) {
343 start_clock.set (_region->start(), true);
347 void
348 AudioRegionEditor::activation ()
353 void
354 AudioRegionEditor::name_entry_changed ()
356 if (name_entry.get_text() != _region->name()) {
357 _region->set_name (name_entry.get_text());
361 void
362 AudioRegionEditor::audition_state_changed (bool yn)
364 ENSURE_GUI_THREAD (bind (mem_fun(*this, &AudioRegionEditor::audition_state_changed), yn));
366 if (!yn) {
367 audition_button.set_active (false);