switch to using boost::signals2 instead of sigc++, at least for libardour. not finish...
[ardour.git] / gtk2_ardour / audio_region_editor.cc
1 /*
2     Copyright (C) 2001 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/memento_command.h"
21
22 #include "ardour/session.h"
23 #include "ardour/audioregion.h"
24 #include "ardour/playlist.h"
25 #include "ardour/utils.h"
26 #include "ardour/dB.h"
27 #include <gtkmm2ext/utils.h>
28 #include <gtkmm2ext/stop_signal.h>
29 #include <cmath>
30
31 #include "audio_region_editor.h"
32 #include "audio_region_view.h"
33 #include "ardour_ui.h"
34 #include "utils.h"
35 #include "gui_thread.h"
36
37 #include "i18n.h"
38
39 using namespace ARDOUR;
40 using namespace PBD;
41 using namespace std;
42 using namespace Gtkmm2ext;
43
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 (_("Play")),
50           time_table (6, 2),
51           position_clock (X_("regionposition"), true, X_("AudioRegionEditorClock"), true, false),
52           end_clock (X_("regionend"), true, X_("AudioRegionEditorClock"), true, false),
53           length_clock (X_("regionlength"), true, X_("AudioRegionEditorClock"), true, false, true),
54           sync_offset_relative_clock (X_("regionsyncoffsetrelative"), true, X_("AudioRegionEditorClock"), true, false),
55           sync_offset_absolute_clock (X_("regionsyncoffsetabsolute"), true, X_("AudioRegionEditorClock"), true, false),
56           /* XXX cannot file start yet */
57           start_clock (X_("regionstart"), true, X_("AudioRegionEditorClock"), false, false),
58           gain_adjustment(accurate_coefficient_to_dB(_region->scale_amplitude()), -40.0, +40.0, 0.1, 1.0, 0)      
59
60 {
61         position_clock.set_session (_session);
62         end_clock.set_session (_session);
63         length_clock.set_session (_session);
64         sync_offset_relative_clock.set_session (_session);
65         sync_offset_absolute_clock.set_session (_session);
66         start_clock.set_session (_session);
67
68         name_entry.set_name ("AudioRegionEditorEntry");
69         name_label.set_name ("AudioRegionEditorLabel");
70
71         name_hbox.set_spacing (5);
72         name_hbox.pack_start (name_label, false, false);
73         name_hbox.pack_start (name_entry, false, false);
74
75         ARDOUR_UI::instance()->tooltips().set_tip (audition_button, _("audition this region"));
76
77         audition_button.unset_flags (Gtk::CAN_FOCUS);
78
79         audition_button.set_events (audition_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
80
81         top_row_button_hbox.set_border_width (5);
82         top_row_button_hbox.set_spacing (5);
83         top_row_button_hbox.set_homogeneous (false);
84         top_row_button_hbox.pack_end (audition_button, false, false);
85
86         top_row_hbox.pack_start (name_hbox, true, true);
87         top_row_hbox.pack_end (top_row_button_hbox, true, true);
88
89         position_label.set_name ("AudioRegionEditorLabel");
90         position_label.set_text (_("Position"));
91         end_label.set_name ("AudioRegionEditorLabel");
92         end_label.set_text (_("End"));
93         length_label.set_name ("AudioRegionEditorLabel");
94         length_label.set_text (_("Length"));
95         sync_relative_label.set_name ("AudioRegionEditorLabel");
96         sync_relative_label.set_text (_("Sync point (relative to region position)"));
97         sync_absolute_label.set_name ("AudioRegionEditorLabel");
98         sync_absolute_label.set_text (_("Sync point (absolute)"));
99         start_label.set_name ("AudioRegionEditorLabel");
100         start_label.set_text (_("File start"));
101
102         time_table.set_col_spacings (2);
103         time_table.set_row_spacings (5);
104         time_table.set_border_width (5);
105
106         position_label.set_alignment (1, 0.5);
107         time_table.attach (position_label, 0, 1, 0, 1, Gtk::FILL, Gtk::FILL);
108         time_table.attach (position_clock, 1, 2, 0, 1, Gtk::FILL, Gtk::FILL);
109
110         end_label.set_alignment (1, 0.5);
111         time_table.attach (end_label, 0, 1, 1, 2, Gtk::FILL, Gtk::FILL);
112         time_table.attach (end_clock, 1, 2, 1, 2, Gtk::FILL, Gtk::FILL);
113         
114         length_label.set_alignment (1, 0.5);
115         time_table.attach (length_label, 0, 1, 2, 3, Gtk::FILL, Gtk::FILL);
116         time_table.attach (length_clock, 1, 2, 2, 3, Gtk::FILL, Gtk::FILL);
117         
118         sync_relative_label.set_alignment (1, 0.5);
119         time_table.attach (sync_relative_label, 0, 1, 3, 4, Gtk::FILL, Gtk::FILL);
120         time_table.attach (sync_offset_relative_clock, 1, 2, 3, 4, Gtk::FILL, Gtk::FILL);
121  
122         sync_absolute_label.set_alignment (1, 0.5);
123         time_table.attach (sync_absolute_label, 0, 1, 4, 5, Gtk::FILL, Gtk::FILL);
124         time_table.attach (sync_offset_absolute_clock, 1, 2, 4, 5, Gtk::FILL, Gtk::FILL);
125
126         start_label.set_alignment (1, 0.5);
127         time_table.attach (start_label, 0, 1, 5, 6, Gtk::FILL, Gtk::FILL);
128         time_table.attach (start_clock, 1, 2, 5, 6, Gtk::FILL, Gtk::FILL);
129
130         gain_label.set_name ("AudioRegionEditorLabel");
131         gain_label.set_text (_("Scale amplitude:"));
132         gain_label.set_alignment (1, 0.5);
133         gain_entry.configure (gain_adjustment, 0.0, 1);
134         time_table.attach (gain_label, 0, 1, 6, 7, Gtk::FILL, Gtk::FILL);
135         time_table.attach (gain_entry, 1, 2, 6, 7, Gtk::FILL, Gtk::FILL);
136         
137         lower_hbox.pack_start (time_table, true, true);
138         lower_hbox.pack_start (sep1, false, false);
139         lower_hbox.pack_start (sep2, false, false);
140
141         get_vbox()->pack_start (top_row_hbox, true, true);
142         get_vbox()->pack_start (sep3, false, false);
143         get_vbox()->pack_start (lower_hbox, true, true);
144
145         set_name ("AudioRegionEditorWindow");
146         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
147
148         signal_delete_event().connect (sigc::bind (sigc::ptr_fun (just_hide_it), static_cast<Window *> (this)));
149
150         set_title (string_compose (_("Region %1"), _region->name()));
151
152         show_all();
153
154         name_changed ();
155         bounds_changed (Change (StartChanged|LengthChanged|PositionChanged|Region::SyncOffsetChanged));
156         gain_changed ();
157
158         _region->StateChanged.connect (sigc::mem_fun(*this, &AudioRegionEditor::region_changed));
159
160         spin_arrow_grab = false;
161
162         connect_editor_events ();
163 }
164
165 AudioRegionEditor::~AudioRegionEditor ()
166 {
167 }
168
169 void
170 AudioRegionEditor::region_changed (Change what_changed)
171 {
172         if (what_changed & NameChanged) {
173                 name_changed ();
174         }
175
176         if (what_changed & Change (BoundsChanged|StartChanged|Region::SyncOffsetChanged)) {
177                 bounds_changed (what_changed);
178         }
179
180         if (what_changed & AudioRegion::ScaleAmplitudeChanged) {
181                 gain_changed ();
182         }
183 }
184
185 gint
186 AudioRegionEditor::bpressed (GdkEventButton* ev, Gtk::SpinButton* /*but*/, void (AudioRegionEditor::*/*pmf*/)())
187 {
188         switch (ev->button) {
189         case 1:
190         case 2:
191         case 3:
192                 if (ev->type == GDK_BUTTON_PRESS) { /* no double clicks here */
193                         if (!spin_arrow_grab) {
194                                 // GTK2FIX probably nuke the region editor
195                                 // if ((ev->window == but->gobj()->panel)) {
196                                 // spin_arrow_grab = true;
197                                 // (this->*pmf)();
198                                 // }
199                         }
200                 }
201                 break;
202         default:
203                 break;
204         }
205         return FALSE;
206 }
207
208 gint
209 AudioRegionEditor::breleased (GdkEventButton* /*ev*/, Gtk::SpinButton* /*but*/, void (AudioRegionEditor::*pmf)())
210 {
211         if (spin_arrow_grab) {
212                 (this->*pmf)();
213                 spin_arrow_grab = false;
214         }
215         return FALSE;
216 }
217
218 void
219 AudioRegionEditor::connect_editor_events ()
220 {
221         name_entry.signal_changed().connect (sigc::mem_fun(*this, &AudioRegionEditor::name_entry_changed));
222
223         position_clock.ValueChanged.connect (sigc::mem_fun(*this, &AudioRegionEditor::position_clock_changed));
224         end_clock.ValueChanged.connect (sigc::mem_fun(*this, &AudioRegionEditor::end_clock_changed));
225         length_clock.ValueChanged.connect (sigc::mem_fun(*this, &AudioRegionEditor::length_clock_changed));
226         sync_offset_absolute_clock.ValueChanged.connect (sigc::mem_fun (*this, &AudioRegionEditor::sync_offset_absolute_clock_changed));
227         sync_offset_relative_clock.ValueChanged.connect (sigc::mem_fun (*this, &AudioRegionEditor::sync_offset_relative_clock_changed));
228         gain_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &AudioRegionEditor::gain_adjustment_changed));
229
230         audition_button.signal_toggled().connect (sigc::mem_fun(*this, &AudioRegionEditor::audition_button_toggled));
231         _session->AuditionActive.connect (sigc::mem_fun(*this, &AudioRegionEditor::audition_state_changed));
232 }
233
234 void
235 AudioRegionEditor::position_clock_changed ()
236 {
237         _session->begin_reversible_command (_("change region start position"));
238
239         boost::shared_ptr<Playlist> pl = _region->playlist();
240
241         if (pl) {
242                 XMLNode &before = pl->get_state();
243                 _region->set_position (position_clock.current_time(), this);
244                 XMLNode &after = pl->get_state();
245                 _session->add_command(new MementoCommand<Playlist>(*pl, &before, &after));
246         }
247
248         _session->commit_reversible_command ();
249 }
250
251 void
252 AudioRegionEditor::end_clock_changed ()
253 {
254         _session->begin_reversible_command (_("change region end position"));
255
256         boost::shared_ptr<Playlist> pl = _region->playlist();
257
258         if (pl) {
259                 XMLNode &before = pl->get_state();
260                 _region->trim_end (end_clock.current_time(), this);
261                 XMLNode &after = pl->get_state();
262                 _session->add_command(new MementoCommand<Playlist>(*pl, &before, &after));
263         }
264
265         _session->commit_reversible_command ();
266
267         end_clock.set (_region->position() + _region->length() - 1, true);
268 }
269
270 void
271 AudioRegionEditor::length_clock_changed ()
272 {
273         nframes_t frames = length_clock.current_time();
274
275         _session->begin_reversible_command (_("change region length"));
276
277         boost::shared_ptr<Playlist> pl = _region->playlist();
278
279         if (pl) {
280                 XMLNode &before = pl->get_state();
281                 _region->trim_end (_region->position() + frames - 1, this);
282                 XMLNode &after = pl->get_state();
283                 _session->add_command(new MementoCommand<Playlist>(*pl, &before, &after));
284         }
285
286         _session->commit_reversible_command ();
287
288         length_clock.set (_region->length());
289 }
290
291 void
292 AudioRegionEditor::gain_changed ()
293 {
294         float const region_gain_dB = accurate_coefficient_to_dB (_region->scale_amplitude());
295         if (region_gain_dB != gain_adjustment.get_value()) {
296                 gain_adjustment.set_value(region_gain_dB);
297         }
298 }
299
300 void
301 AudioRegionEditor::gain_adjustment_changed ()
302 {
303         float const gain = dB_to_coefficient (gain_adjustment.get_value());
304         if (_region->scale_amplitude() != gain) {
305                 _region->set_scale_amplitude (gain);
306         }
307 }
308
309 void
310 AudioRegionEditor::audition_button_toggled ()
311 {
312         if (audition_button.get_active()) {
313                 _session->audition_region (_region);
314         } else {
315                 _session->cancel_audition ();
316         }
317 }
318
319 void
320 AudioRegionEditor::name_changed ()
321 {
322         if (name_entry.get_text() != _region->name()) {
323                 name_entry.set_text (_region->name());
324         }
325 }
326
327 void
328 AudioRegionEditor::bounds_changed (Change what_changed)
329 {
330         if ((what_changed & Change (PositionChanged|LengthChanged)) == Change (PositionChanged|LengthChanged)) {
331                 position_clock.set (_region->position(), true);
332                 end_clock.set (_region->position() + _region->length() - 1, true);
333                 length_clock.set (_region->length(), true);
334         } else if (what_changed & Change (PositionChanged)) {
335                 position_clock.set (_region->position(), true);
336                 end_clock.set (_region->position() + _region->length() - 1, true);
337         } else if (what_changed & Change (LengthChanged)) {
338                 end_clock.set (_region->position() + _region->length() - 1, true);
339                 length_clock.set (_region->length(), true);
340         }
341
342         if ((what_changed & Region::SyncOffsetChanged) || (what_changed & PositionChanged)) {
343                 int dir;
344                 nframes_t off = _region->sync_offset (dir);
345                 if (dir == -1) {
346                         off = -off;
347                 }
348
349                 if (what_changed & Region::SyncOffsetChanged) {
350                         sync_offset_relative_clock.set (off, true);
351                 }
352
353                 sync_offset_absolute_clock.set (off + _region->position (), true);
354         }
355
356         if (what_changed & StartChanged) {
357                 start_clock.set (_region->start(), true);
358         }
359 }
360
361 void
362 AudioRegionEditor::activation ()
363 {
364
365 }
366
367 void
368 AudioRegionEditor::name_entry_changed ()
369 {
370         if (name_entry.get_text() != _region->name()) {
371                 _region->set_name (name_entry.get_text());
372         }
373 }
374
375 void
376 AudioRegionEditor::audition_state_changed (bool yn)
377 {
378         ENSURE_GUI_THREAD (*this, &AudioRegionEditor::audition_state_changed, yn)
379
380         if (!yn) {
381                 audition_button.set_active (false);
382         }
383 }
384
385 void
386 AudioRegionEditor::sync_offset_absolute_clock_changed ()
387 {
388         _session->begin_reversible_command (_("change region sync point"));
389
390         XMLNode& before = _region->get_state ();
391         _region->set_sync_position (sync_offset_absolute_clock.current_time());
392         XMLNode& after = _region->get_state ();
393         _session->add_command (new MementoCommand<AudioRegion> (*_region.get(), &before, &after));
394         
395         _session->commit_reversible_command ();
396 }
397
398 void
399 AudioRegionEditor::sync_offset_relative_clock_changed ()
400 {
401         _session->begin_reversible_command (_("change region sync point"));
402
403         XMLNode& before = _region->get_state ();
404         _region->set_sync_position (sync_offset_relative_clock.current_time() + _region->position ());
405         XMLNode& after = _region->get_state ();
406         _session->add_command (new MementoCommand<AudioRegion> (*_region.get(), &before, &after));
407         
408         _session->commit_reversible_command ();
409 }
410
411 bool
412 AudioRegionEditor::on_delete_event (GdkEventAny* ev)
413 {
414         bounds_changed (Change (StartChanged|LengthChanged|PositionChanged|Region::SyncOffsetChanged));
415         return RegionEditor::on_delete_event (ev);
416 }