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