0d3f89dfad6d47726de4d0f24fb5e62e5fb01497
[ardour.git] / gtk2_ardour / 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/region.h"
25 #include "ardour/playlist.h"
26 #include "ardour/utils.h"
27 #include "ardour/dB.h"
28 #include "ardour/source.h"
29 #include "gtkmm2ext/utils.h"
30 #include <gtkmm/listviewtext.h>
31 #include <cmath>
32
33 #include "region_editor.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 RegionEditor::RegionEditor (Session* s, boost::shared_ptr<Region> r)
46         : ArdourDialog (_("Region")),
47           _table (8, 2),
48           _table_row (0),
49           _region (r),
50           name_label (_("Name:")),
51           audition_button (_("Play")),
52           position_clock (X_("regionposition"), true, X_("RegionEditorClock"), true, false),
53           end_clock (X_("regionend"), true, X_("RegionEditorClock"), true, false),
54           length_clock (X_("regionlength"), true, X_("RegionEditorClock"), true, false, true),
55           sync_offset_relative_clock (X_("regionsyncoffsetrelative"), true, X_("RegionEditorClock"), true, false),
56           sync_offset_absolute_clock (X_("regionsyncoffsetabsolute"), true, X_("RegionEditorClock"), true, false),
57           /* XXX cannot file start yet */
58           start_clock (X_("regionstart"), true, X_("RegionEditorClock"), false, false),
59           _sources (1)
60 {
61         set_session (s);
62         
63         position_clock.set_session (_session);
64         end_clock.set_session (_session);
65         length_clock.set_session (_session);
66         sync_offset_relative_clock.set_session (_session);
67         sync_offset_absolute_clock.set_session (_session);
68         start_clock.set_session (_session);
69
70         ARDOUR_UI::instance()->set_tip (audition_button, _("audition this region"));
71
72         audition_button.unset_flags (Gtk::CAN_FOCUS);
73
74         audition_button.set_events (audition_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
75
76         name_entry.set_name ("RegionEditorEntry");
77         name_label.set_name ("RegionEditorLabel");
78         position_label.set_name ("RegionEditorLabel");
79         position_label.set_text (_("Position:"));
80         end_label.set_name ("RegionEditorLabel");
81         end_label.set_text (_("End:"));
82         length_label.set_name ("RegionEditorLabel");
83         length_label.set_text (_("Length:"));
84         sync_relative_label.set_name ("RegionEditorLabel");
85         sync_relative_label.set_text (_("Sync point (relative to region):"));
86         sync_absolute_label.set_name ("RegionEditorLabel");
87         sync_absolute_label.set_text (_("Sync point (absolute):"));
88         start_label.set_name ("RegionEditorLabel");
89         start_label.set_text (_("File start:"));
90         _sources_label.set_name ("RegionEditorLabel");
91         _sources_label.set_text (_("Sources:"));
92
93         _table.set_col_spacings (12);
94         _table.set_row_spacings (6);
95         _table.set_border_width (12);
96
97         name_label.set_alignment (1, 0.5);
98         position_label.set_alignment (1, 0.5);
99         end_label.set_alignment (1, 0.5);
100         length_label.set_alignment (1, 0.5);
101         sync_relative_label.set_alignment (1, 0.5);
102         sync_absolute_label.set_alignment (1, 0.5);
103         start_label.set_alignment (1, 0.5);
104         _sources_label.set_alignment (1, 0.5);
105
106         Gtk::HBox* nb = Gtk::manage (new Gtk::HBox);
107         nb->set_spacing (6);
108         nb->pack_start (name_entry);
109         nb->pack_start (audition_button);
110
111         _table.attach (name_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
112         _table.attach (*nb, 1, 2, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
113         ++_table_row;
114
115         _table.attach (position_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
116         _table.attach (position_clock, 1, 2, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
117         ++_table_row;
118
119         _table.attach (end_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
120         _table.attach (end_clock, 1, 2, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
121         ++_table_row;
122         
123         _table.attach (length_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
124         _table.attach (length_clock, 1, 2, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
125         ++_table_row;
126         
127         _table.attach (sync_relative_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
128         _table.attach (sync_offset_relative_clock, 1, 2, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
129         ++_table_row;
130  
131         _table.attach (sync_absolute_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
132         _table.attach (sync_offset_absolute_clock, 1, 2, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
133         ++_table_row;
134
135         _table.attach (start_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
136         _table.attach (start_clock, 1, 2, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
137         ++_table_row;
138
139         _table.attach (_sources_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
140         _table.attach (_sources, 1, 2, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
141         ++_table_row;
142
143         get_vbox()->pack_start (_table, true, true);
144
145         add_button (Gtk::Stock::CLOSE, Gtk::RESPONSE_ACCEPT);
146
147         set_name ("RegionEditorWindow");
148         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
149
150         signal_delete_event().connect (sigc::bind (sigc::ptr_fun (just_hide_it), static_cast<Window *> (this)));
151         signal_response().connect (sigc::mem_fun (*this, &RegionEditor::handle_response));
152
153         set_title (string_compose (_("Region '%1'"), _region->name()));
154
155         for (uint32_t i = 0; i < _region->n_channels(); ++i) {
156                 _sources.append_text (_region->source(i)->name());
157         }
158
159         _sources.set_headers_visible (false);
160         Gtk::CellRendererText* t = dynamic_cast<Gtk::CellRendererText*> (_sources.get_column_cell_renderer(0));
161         assert (t);
162         t->property_ellipsize() = Pango::ELLIPSIZE_END;
163
164         show_all();
165
166         name_changed ();
167
168         PropertyChange change;
169
170         change.add (ARDOUR::Properties::start);
171         change.add (ARDOUR::Properties::length);
172         change.add (ARDOUR::Properties::position);
173         change.add (ARDOUR::Properties::sync_position);
174
175         bounds_changed (change);
176
177         _region->PropertyChanged.connect (state_connection, invalidator (*this), ui_bind (&RegionEditor::region_changed, this, _1), gui_context());
178
179         spin_arrow_grab = false;
180
181         connect_editor_events ();
182 }
183
184 void
185 RegionEditor::region_changed (const PBD::PropertyChange& what_changed)
186 {
187         if (what_changed.contains (ARDOUR::Properties::name)) {
188                 name_changed ();
189         }
190
191         PropertyChange interesting_stuff;
192
193         interesting_stuff.add (ARDOUR::Properties::position);
194         interesting_stuff.add (ARDOUR::Properties::length);
195         interesting_stuff.add (ARDOUR::Properties::start);
196         interesting_stuff.add (ARDOUR::Properties::sync_position);
197
198         if (what_changed.contains (interesting_stuff)) {
199                 bounds_changed (what_changed);
200         }
201 }
202
203 gint
204 RegionEditor::bpressed (GdkEventButton* ev, Gtk::SpinButton* /*but*/, void (RegionEditor::*/*pmf*/)())
205 {
206         switch (ev->button) {
207         case 1:
208         case 2:
209         case 3:
210                 if (ev->type == GDK_BUTTON_PRESS) { /* no double clicks here */
211                         if (!spin_arrow_grab) {
212                                 // GTK2FIX probably nuke the region editor
213                                 // if ((ev->window == but->gobj()->panel)) {
214                                 // spin_arrow_grab = true;
215                                 // (this->*pmf)();
216                                 // }
217                         }
218                 }
219                 break;
220         default:
221                 break;
222         }
223         return FALSE;
224 }
225
226 gint
227 RegionEditor::breleased (GdkEventButton* /*ev*/, Gtk::SpinButton* /*but*/, void (RegionEditor::*pmf)())
228 {
229         if (spin_arrow_grab) {
230                 (this->*pmf)();
231                 spin_arrow_grab = false;
232         }
233         return FALSE;
234 }
235
236 void
237 RegionEditor::connect_editor_events ()
238 {
239         name_entry.signal_changed().connect (sigc::mem_fun(*this, &RegionEditor::name_entry_changed));
240
241         position_clock.ValueChanged.connect (sigc::mem_fun(*this, &RegionEditor::position_clock_changed));
242         end_clock.ValueChanged.connect (sigc::mem_fun(*this, &RegionEditor::end_clock_changed));
243         length_clock.ValueChanged.connect (sigc::mem_fun(*this, &RegionEditor::length_clock_changed));
244         sync_offset_absolute_clock.ValueChanged.connect (sigc::mem_fun (*this, &RegionEditor::sync_offset_absolute_clock_changed));
245         sync_offset_relative_clock.ValueChanged.connect (sigc::mem_fun (*this, &RegionEditor::sync_offset_relative_clock_changed));
246
247         audition_button.signal_toggled().connect (sigc::mem_fun(*this, &RegionEditor::audition_button_toggled));
248
249         _session->AuditionActive.connect (audition_connection, invalidator (*this), ui_bind (&RegionEditor::audition_state_changed, this, _1), gui_context());
250 }
251
252 void
253 RegionEditor::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_changes ();
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 RegionEditor::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_changes ();
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 RegionEditor::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_changes ();
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 RegionEditor::audition_button_toggled ()
308 {
309         if (audition_button.get_active()) {
310                 _session->audition_region (_region);
311         } else {
312                 _session->cancel_audition ();
313         }
314 }
315
316 void
317 RegionEditor::name_changed ()
318 {
319         if (name_entry.get_text() != _region->name()) {
320                 name_entry.set_text (_region->name());
321         }
322 }
323
324 void
325 RegionEditor::bounds_changed (const PropertyChange& what_changed)
326 {
327         if (what_changed.contains (ARDOUR::Properties::position) && what_changed.contains (ARDOUR::Properties::length)) {
328                 position_clock.set (_region->position(), true);
329                 end_clock.set (_region->position() + _region->length() - 1, true);
330                 length_clock.set (_region->length(), true);
331         } else if (what_changed.contains (ARDOUR::Properties::position)) {
332                 position_clock.set (_region->position(), true);
333                 end_clock.set (_region->position() + _region->length() - 1, true);
334         } else if (what_changed.contains (ARDOUR::Properties::length)) {
335                 end_clock.set (_region->position() + _region->length() - 1, true);
336                 length_clock.set (_region->length(), true);
337         }
338
339         if (what_changed.contains (ARDOUR::Properties::sync_position) || what_changed.contains (ARDOUR::Properties::position)) {
340                 int dir;
341                 nframes_t off = _region->sync_offset (dir);
342                 if (dir == -1) {
343                         off = -off;
344                 }
345
346                 if (what_changed.contains (ARDOUR::Properties::sync_position)) {
347                         sync_offset_relative_clock.set (off, true);
348                 }
349
350                 sync_offset_absolute_clock.set (off + _region->position (), true);
351         }
352
353         if (what_changed.contains (ARDOUR::Properties::start)) {
354                 start_clock.set (_region->start(), true);
355         }
356 }
357
358 void
359 RegionEditor::activation ()
360 {
361
362 }
363
364 void
365 RegionEditor::name_entry_changed ()
366 {
367         if (name_entry.get_text() != _region->name()) {
368                 _region->set_name (name_entry.get_text());
369         }
370 }
371
372 void
373 RegionEditor::audition_state_changed (bool yn)
374 {
375         ENSURE_GUI_THREAD (*this, &RegionEditor::audition_state_changed, yn)
376
377         if (!yn) {
378                 audition_button.set_active (false);
379         }
380 }
381
382 void
383 RegionEditor::sync_offset_absolute_clock_changed ()
384 {
385         _session->begin_reversible_command (_("change region sync point"));
386
387         _region->clear_changes ();
388         _region->set_sync_position (sync_offset_absolute_clock.current_time());
389         _session->add_command (new StatefulDiffCommand (_region));
390         
391         _session->commit_reversible_command ();
392 }
393
394 void
395 RegionEditor::sync_offset_relative_clock_changed ()
396 {
397         _session->begin_reversible_command (_("change region sync point"));
398
399         _region->clear_changes ();
400         _region->set_sync_position (sync_offset_relative_clock.current_time() + _region->position ());
401         _session->add_command (new StatefulDiffCommand (_region));
402         
403         _session->commit_reversible_command ();
404 }
405
406 bool
407 RegionEditor::on_delete_event (GdkEventAny*)
408 {
409         PropertyChange change;
410
411         change.add (ARDOUR::Properties::start);
412         change.add (ARDOUR::Properties::length);
413         change.add (ARDOUR::Properties::position);
414         change.add (ARDOUR::Properties::sync_position);
415
416         bounds_changed (change);
417
418         return true;
419 }
420
421 void
422 RegionEditor::handle_response (int)
423 {
424         hide ();
425 }