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