Merge branch 'ripple-mode-cc' into cairocanvas
[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 <cmath>
21
22 #include <gtkmm/listviewtext.h>
23
24 #include "pbd/memento_command.h"
25 #include "pbd/stateful_diff_command.h"
26
27 #include "ardour/region.h"
28 #include "ardour/session.h"
29 #include "ardour/source.h"
30
31 #include "ardour_ui.h"
32 #include "clock_group.h"
33 #include "main_clock.h"
34 #include "gui_thread.h"
35 #include "region_editor.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 RegionEditor::RegionEditor (Session* s, boost::shared_ptr<Region> r)
45         : ArdourDialog (_("Region"))
46         , _table (9, 2)
47         , _table_row (0)
48         , _region (r)
49         , name_label (_("Name:"))
50         , audition_button (_("Audition"))
51         , _clock_group (new ClockGroup)
52         , position_clock (X_("regionposition"), true, "", true, false)
53         , end_clock (X_("regionend"), true, "", true, false)
54         , length_clock (X_("regionlength"), true, "", true, false, true)
55         , sync_offset_relative_clock (X_("regionsyncoffsetrelative"), true, "", true, false)
56         , sync_offset_absolute_clock (X_("regionsyncoffsetabsolute"), true, "", true, false)
57           /* XXX cannot file start yet */
58         , start_clock (X_("regionstart"), true, "", false, false)
59         , _sources (1)
60 {
61         set_session (s);
62
63         _clock_group->set_clock_mode (ARDOUR_UI::instance()->secondary_clock->mode());
64         _clock_group->add (position_clock);
65         _clock_group->add (end_clock);
66         _clock_group->add (length_clock);
67         _clock_group->add (sync_offset_relative_clock);
68         _clock_group->add (sync_offset_absolute_clock);
69         _clock_group->add (start_clock);
70
71         position_clock.set_session (_session);
72         end_clock.set_session (_session);
73         length_clock.set_session (_session);
74         sync_offset_relative_clock.set_session (_session);
75         sync_offset_absolute_clock.set_session (_session);
76         start_clock.set_session (_session);
77
78         ARDOUR_UI::instance()->set_tip (audition_button, _("audition this region"));
79
80         audition_button.unset_flags (Gtk::CAN_FOCUS);
81
82         audition_button.set_events (audition_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
83
84         name_entry.set_name ("RegionEditorEntry");
85         name_label.set_name ("RegionEditorLabel");
86         position_label.set_name ("RegionEditorLabel");
87         position_label.set_text (_("Position:"));
88         end_label.set_name ("RegionEditorLabel");
89         end_label.set_text (_("End:"));
90         length_label.set_name ("RegionEditorLabel");
91         length_label.set_text (_("Length:"));
92         sync_relative_label.set_name ("RegionEditorLabel");
93         sync_relative_label.set_text (_("Sync point (relative to region):"));
94         sync_absolute_label.set_name ("RegionEditorLabel");
95         sync_absolute_label.set_text (_("Sync point (absolute):"));
96         start_label.set_name ("RegionEditorLabel");
97         start_label.set_text (_("File start:"));
98         _sources_label.set_name ("RegionEditorLabel");
99
100         if (_region->n_channels() > 1) {
101                 _sources_label.set_text (_("Sources:"));
102         } else {
103                 _sources_label.set_text (_("Source:"));
104         }
105
106         _table.set_col_spacings (12);
107         _table.set_row_spacings (6);
108         _table.set_border_width (12);
109
110         name_label.set_alignment (1, 0.5);
111         position_label.set_alignment (1, 0.5);
112         end_label.set_alignment (1, 0.5);
113         length_label.set_alignment (1, 0.5);
114         sync_relative_label.set_alignment (1, 0.5);
115         sync_absolute_label.set_alignment (1, 0.5);
116         start_label.set_alignment (1, 0.5);
117         _sources_label.set_alignment (1, 0.5);
118
119         Gtk::HBox* nb = Gtk::manage (new Gtk::HBox);
120         nb->set_spacing (6);
121         nb->pack_start (name_entry);
122         nb->pack_start (audition_button, false, false);
123
124         _table.attach (name_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
125         _table.attach (*nb, 1, 2, _table_row, _table_row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::FILL);
126         ++_table_row;
127
128         _table.attach (position_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
129         _table.attach (position_clock, 1, 2, _table_row, _table_row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::FILL);
130         ++_table_row;
131
132         _table.attach (end_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
133         _table.attach (end_clock, 1, 2, _table_row, _table_row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::FILL);
134         ++_table_row;
135
136         _table.attach (length_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
137         _table.attach (length_clock, 1, 2, _table_row, _table_row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::FILL);
138         ++_table_row;
139
140         _table.attach (sync_relative_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
141         _table.attach (sync_offset_relative_clock, 1, 2, _table_row, _table_row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::FILL);
142         ++_table_row;
143
144         _table.attach (sync_absolute_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
145         _table.attach (sync_offset_absolute_clock, 1, 2, _table_row, _table_row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::FILL);
146         ++_table_row;
147
148         _table.attach (start_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
149         _table.attach (start_clock, 1, 2, _table_row, _table_row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::FILL);
150         ++_table_row;
151
152         _table.attach (_sources_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
153         _table.attach (_sources, 1, 2, _table_row, _table_row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::FILL);
154         ++_table_row;
155
156         get_vbox()->pack_start (_table, true, true);
157
158         add_button (Gtk::Stock::CLOSE, Gtk::RESPONSE_ACCEPT);
159
160         set_name ("RegionEditorWindow");
161         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
162
163         signal_response().connect (sigc::mem_fun (*this, &RegionEditor::handle_response));
164
165         set_title (string_compose (_("Region '%1'"), _region->name()));
166
167         for (uint32_t i = 0; i < _region->n_channels(); ++i) {
168                 _sources.append_text (_region->source(i)->name());
169         }
170
171         _sources.set_headers_visible (false);
172         Gtk::CellRendererText* t = dynamic_cast<Gtk::CellRendererText*> (_sources.get_column_cell_renderer(0));
173         assert (t);
174         t->property_ellipsize() = Pango::ELLIPSIZE_END;
175
176         show_all();
177
178         name_changed ();
179
180         PropertyChange change;
181
182         change.add (ARDOUR::Properties::start);
183         change.add (ARDOUR::Properties::length);
184         change.add (ARDOUR::Properties::position);
185         change.add (ARDOUR::Properties::sync_position);
186
187         bounds_changed (change);
188
189         _region->PropertyChanged.connect (state_connection, invalidator (*this), boost::bind (&RegionEditor::region_changed, this, _1), gui_context());
190
191         spin_arrow_grab = false;
192
193         connect_editor_events ();
194 }
195
196 RegionEditor::~RegionEditor ()
197 {
198         delete _clock_group;
199 }
200
201 void
202 RegionEditor::region_changed (const PBD::PropertyChange& what_changed)
203 {
204         if (what_changed.contains (ARDOUR::Properties::name)) {
205                 name_changed ();
206         }
207
208         PropertyChange interesting_stuff;
209
210         interesting_stuff.add (ARDOUR::Properties::position);
211         interesting_stuff.add (ARDOUR::Properties::length);
212         interesting_stuff.add (ARDOUR::Properties::start);
213         interesting_stuff.add (ARDOUR::Properties::sync_position);
214
215         if (what_changed.contains (interesting_stuff)) {
216                 bounds_changed (what_changed);
217         }
218 }
219
220 gint
221 RegionEditor::bpressed (GdkEventButton* ev, Gtk::SpinButton* /*but*/, void (RegionEditor::*/*pmf*/)())
222 {
223         switch (ev->button) {
224         case 1:
225         case 2:
226         case 3:
227                 if (ev->type == GDK_BUTTON_PRESS) { /* no double clicks here */
228                         if (!spin_arrow_grab) {
229                                 // GTK2FIX probably nuke the region editor
230                                 // if ((ev->window == but->gobj()->panel)) {
231                                 // spin_arrow_grab = true;
232                                 // (this->*pmf)();
233                                 // }
234                         }
235                 }
236                 break;
237         default:
238                 break;
239         }
240         return FALSE;
241 }
242
243 gint
244 RegionEditor::breleased (GdkEventButton* /*ev*/, Gtk::SpinButton* /*but*/, void (RegionEditor::*pmf)())
245 {
246         if (spin_arrow_grab) {
247                 (this->*pmf)();
248                 spin_arrow_grab = false;
249         }
250         return FALSE;
251 }
252
253 void
254 RegionEditor::connect_editor_events ()
255 {
256         name_entry.signal_changed().connect (sigc::mem_fun(*this, &RegionEditor::name_entry_changed));
257
258         position_clock.ValueChanged.connect (sigc::mem_fun(*this, &RegionEditor::position_clock_changed));
259         end_clock.ValueChanged.connect (sigc::mem_fun(*this, &RegionEditor::end_clock_changed));
260         length_clock.ValueChanged.connect (sigc::mem_fun(*this, &RegionEditor::length_clock_changed));
261         sync_offset_absolute_clock.ValueChanged.connect (sigc::mem_fun (*this, &RegionEditor::sync_offset_absolute_clock_changed));
262         sync_offset_relative_clock.ValueChanged.connect (sigc::mem_fun (*this, &RegionEditor::sync_offset_relative_clock_changed));
263
264         audition_button.signal_toggled().connect (sigc::mem_fun(*this, &RegionEditor::audition_button_toggled));
265
266         _session->AuditionActive.connect (audition_connection, invalidator (*this), boost::bind (&RegionEditor::audition_state_changed, this, _1), gui_context());
267 }
268
269 void
270 RegionEditor::position_clock_changed ()
271 {
272         _session->begin_reversible_command (_("change region start position"));
273
274         boost::shared_ptr<Playlist> pl = _region->playlist();
275
276         if (pl) {
277                 _region->clear_changes ();
278                 _region->set_position (position_clock.current_time());
279                 _session->add_command(new StatefulDiffCommand (_region));
280         }
281
282         _session->commit_reversible_command ();
283 }
284
285 void
286 RegionEditor::end_clock_changed ()
287 {
288         _session->begin_reversible_command (_("change region end position"));
289
290         boost::shared_ptr<Playlist> pl = _region->playlist();
291
292         if (pl) {
293                 _region->clear_changes ();
294                 _region->trim_end (end_clock.current_time());
295                 _session->add_command(new StatefulDiffCommand (_region));
296         }
297
298         _session->commit_reversible_command ();
299
300         end_clock.set (_region->position() + _region->length() - 1, true);
301 }
302
303 void
304 RegionEditor::length_clock_changed ()
305 {
306         framecnt_t frames = length_clock.current_time();
307
308         _session->begin_reversible_command (_("change region length"));
309
310         boost::shared_ptr<Playlist> pl = _region->playlist();
311
312         if (pl) {
313                 _region->clear_changes ();
314                 _region->trim_end (_region->position() + frames - 1);
315                 _session->add_command(new StatefulDiffCommand (_region));
316         }
317
318         _session->commit_reversible_command ();
319
320         length_clock.set (_region->length());
321 }
322
323 void
324 RegionEditor::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 RegionEditor::name_changed ()
335 {
336         if (name_entry.get_text() != _region->name()) {
337                 name_entry.set_text (_region->name());
338         }
339 }
340
341 void
342 RegionEditor::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                 frameoffset_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 RegionEditor::activation ()
377 {
378
379 }
380
381 void
382 RegionEditor::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 RegionEditor::audition_state_changed (bool yn)
391 {
392         ENSURE_GUI_THREAD (*this, &RegionEditor::audition_state_changed, yn)
393
394         if (!yn) {
395                 audition_button.set_active (false);
396         }
397 }
398
399 void
400 RegionEditor::sync_offset_absolute_clock_changed ()
401 {
402         _session->begin_reversible_command (_("change region sync point"));
403
404         _region->clear_changes ();
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 RegionEditor::sync_offset_relative_clock_changed ()
413 {
414         _session->begin_reversible_command (_("change region sync point"));
415
416         _region->clear_changes ();
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 RegionEditor::on_delete_event (GdkEventAny*)
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 true;
436 }
437
438 void
439 RegionEditor::handle_response (int)
440 {
441         hide ();
442 }