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