Put the sidechain ports into a dedicated tab in PortMatrix
[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 #include <gtkmm/stock.h>
24
25 #include "pbd/memento_command.h"
26 #include "pbd/stateful_diff_command.h"
27
28 #include "widgets/tooltips.h"
29
30 #include "ardour/region.h"
31 #include "ardour/session.h"
32 #include "ardour/source.h"
33
34 #include "ardour_ui.h"
35 #include "clock_group.h"
36 #include "main_clock.h"
37 #include "gui_thread.h"
38 #include "region_editor.h"
39 #include "public_editor.h"
40
41 #include "pbd/i18n.h"
42
43 using namespace ARDOUR;
44 using namespace PBD;
45 using namespace std;
46 using namespace Gtkmm2ext;
47
48 RegionEditor::RegionEditor (Session* s, boost::shared_ptr<Region> r)
49         : ArdourDialog (_("Region"))
50         , _table (9, 2)
51         , _table_row (0)
52         , _region (r)
53         , name_label (_("Name:"))
54         , audition_button (_("Audition"))
55         , _clock_group (new ClockGroup)
56         , position_clock (X_("regionposition"), true, "", true, false)
57         , end_clock (X_("regionend"), true, "", true, false)
58         , length_clock (X_("regionlength"), true, "", true, false, true)
59         , sync_offset_relative_clock (X_("regionsyncoffsetrelative"), true, "", true, false)
60         , sync_offset_absolute_clock (X_("regionsyncoffsetabsolute"), true, "", true, false)
61           /* XXX cannot file start yet */
62         , start_clock (X_("regionstart"), true, "", false, false)
63         , _sources (1)
64 {
65         set_session (s);
66
67         _clock_group->set_clock_mode (ARDOUR_UI::instance()->primary_clock->mode());
68         ARDOUR_UI::instance()->primary_clock->mode_changed.connect (sigc::mem_fun (*this, &RegionEditor::set_clock_mode_from_primary));
69
70         _clock_group->add (position_clock);
71         _clock_group->add (end_clock);
72         _clock_group->add (length_clock);
73         _clock_group->add (sync_offset_relative_clock);
74         _clock_group->add (sync_offset_absolute_clock);
75         _clock_group->add (start_clock);
76
77         position_clock.set_session (_session);
78         end_clock.set_session (_session);
79         length_clock.set_session (_session);
80         sync_offset_relative_clock.set_session (_session);
81         sync_offset_absolute_clock.set_session (_session);
82         start_clock.set_session (_session);
83
84         ArdourWidgets::set_tooltip (audition_button, _("audition this region"));
85
86         audition_button.unset_flags (Gtk::CAN_FOCUS);
87
88         audition_button.set_events (audition_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
89
90         name_entry.set_name ("RegionEditorEntry");
91         name_label.set_name ("RegionEditorLabel");
92         position_label.set_name ("RegionEditorLabel");
93         position_label.set_text (_("Position:"));
94         end_label.set_name ("RegionEditorLabel");
95         end_label.set_text (_("End:"));
96         length_label.set_name ("RegionEditorLabel");
97         length_label.set_text (_("Length:"));
98         sync_relative_label.set_name ("RegionEditorLabel");
99         sync_relative_label.set_text (_("Sync point (relative to region):"));
100         sync_absolute_label.set_name ("RegionEditorLabel");
101         sync_absolute_label.set_text (_("Sync point (absolute):"));
102         start_label.set_name ("RegionEditorLabel");
103         start_label.set_text (_("File start:"));
104         _sources_label.set_name ("RegionEditorLabel");
105
106         if (_region->n_channels() > 1) {
107                 _sources_label.set_text (_("Sources:"));
108         } else {
109                 _sources_label.set_text (_("Source:"));
110         }
111
112         _table.set_col_spacings (12);
113         _table.set_row_spacings (6);
114         _table.set_border_width (12);
115
116         name_label.set_alignment (1, 0.5);
117         position_label.set_alignment (1, 0.5);
118         end_label.set_alignment (1, 0.5);
119         length_label.set_alignment (1, 0.5);
120         sync_relative_label.set_alignment (1, 0.5);
121         sync_absolute_label.set_alignment (1, 0.5);
122         start_label.set_alignment (1, 0.5);
123         _sources_label.set_alignment (1, 0.5);
124
125         Gtk::HBox* nb = Gtk::manage (new Gtk::HBox);
126         nb->set_spacing (6);
127         nb->pack_start (name_entry);
128         nb->pack_start (audition_button, false, false);
129
130         _table.attach (name_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
131         _table.attach (*nb, 1, 2, _table_row, _table_row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::FILL);
132         ++_table_row;
133
134         _table.attach (position_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
135         _table.attach (position_clock, 1, 2, _table_row, _table_row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::FILL);
136         ++_table_row;
137
138         _table.attach (end_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
139         _table.attach (end_clock, 1, 2, _table_row, _table_row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::FILL);
140         ++_table_row;
141
142         _table.attach (length_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
143         _table.attach (length_clock, 1, 2, _table_row, _table_row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::FILL);
144         ++_table_row;
145
146         _table.attach (sync_relative_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
147         _table.attach (sync_offset_relative_clock, 1, 2, _table_row, _table_row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::FILL);
148         ++_table_row;
149
150         _table.attach (sync_absolute_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
151         _table.attach (sync_offset_absolute_clock, 1, 2, _table_row, _table_row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::FILL);
152         ++_table_row;
153
154         _table.attach (start_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
155         _table.attach (start_clock, 1, 2, _table_row, _table_row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::FILL);
156         ++_table_row;
157
158         _table.attach (_sources_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
159         _table.attach (_sources, 1, 2, _table_row, _table_row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::FILL);
160         ++_table_row;
161
162         get_vbox()->pack_start (_table, true, true);
163
164         add_button (Gtk::Stock::CLOSE, Gtk::RESPONSE_ACCEPT);
165
166         set_name ("RegionEditorWindow");
167         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
168
169         signal_response().connect (sigc::mem_fun (*this, &RegionEditor::handle_response));
170
171         set_title (string_compose (_("Region '%1'"), _region->name()));
172
173         for (uint32_t i = 0; i < _region->n_channels(); ++i) {
174                 _sources.append_text (_region->source(i)->name());
175         }
176
177         _sources.set_headers_visible (false);
178         Gtk::CellRendererText* t = dynamic_cast<Gtk::CellRendererText*> (_sources.get_column_cell_renderer(0));
179         assert (t);
180         t->property_ellipsize() = Pango::ELLIPSIZE_END;
181
182         show_all();
183
184         name_changed ();
185
186         PropertyChange change;
187
188         change.add (ARDOUR::Properties::start);
189         change.add (ARDOUR::Properties::length);
190         change.add (ARDOUR::Properties::position);
191         change.add (ARDOUR::Properties::sync_position);
192
193         bounds_changed (change);
194
195         _region->PropertyChanged.connect (state_connection, invalidator (*this), boost::bind (&RegionEditor::region_changed, this, _1), gui_context());
196
197         spin_arrow_grab = false;
198
199         connect_editor_events ();
200 }
201
202 RegionEditor::~RegionEditor ()
203 {
204         delete _clock_group;
205 }
206
207 void
208 RegionEditor::set_clock_mode_from_primary ()
209 {
210         _clock_group->set_clock_mode (ARDOUR_UI::instance()->primary_clock->mode());
211 }
212
213 void
214 RegionEditor::region_changed (const PBD::PropertyChange& what_changed)
215 {
216         if (what_changed.contains (ARDOUR::Properties::name)) {
217                 name_changed ();
218         }
219
220         PropertyChange interesting_stuff;
221
222         interesting_stuff.add (ARDOUR::Properties::position);
223         interesting_stuff.add (ARDOUR::Properties::length);
224         interesting_stuff.add (ARDOUR::Properties::start);
225         interesting_stuff.add (ARDOUR::Properties::sync_position);
226
227         if (what_changed.contains (interesting_stuff)) {
228                 bounds_changed (what_changed);
229         }
230 }
231
232 gint
233 RegionEditor::bpressed (GdkEventButton* ev, Gtk::SpinButton* /*but*/, void (RegionEditor::*/*pmf*/)())
234 {
235         switch (ev->button) {
236         case 1:
237         case 2:
238         case 3:
239                 if (ev->type == GDK_BUTTON_PRESS) { /* no double clicks here */
240                         if (!spin_arrow_grab) {
241                                 // GTK2FIX probably nuke the region editor
242                                 // if ((ev->window == but->gobj()->panel)) {
243                                 // spin_arrow_grab = true;
244                                 // (this->*pmf)();
245                                 // }
246                         }
247                 }
248                 break;
249         default:
250                 break;
251         }
252         return FALSE;
253 }
254
255 gint
256 RegionEditor::breleased (GdkEventButton* /*ev*/, Gtk::SpinButton* /*but*/, void (RegionEditor::*pmf)())
257 {
258         if (spin_arrow_grab) {
259                 (this->*pmf)();
260                 spin_arrow_grab = false;
261         }
262         return FALSE;
263 }
264
265 void
266 RegionEditor::connect_editor_events ()
267 {
268         name_entry.signal_changed().connect (sigc::mem_fun(*this, &RegionEditor::name_entry_changed));
269
270         position_clock.ValueChanged.connect (sigc::mem_fun(*this, &RegionEditor::position_clock_changed));
271         end_clock.ValueChanged.connect (sigc::mem_fun(*this, &RegionEditor::end_clock_changed));
272         length_clock.ValueChanged.connect (sigc::mem_fun(*this, &RegionEditor::length_clock_changed));
273         sync_offset_absolute_clock.ValueChanged.connect (sigc::mem_fun (*this, &RegionEditor::sync_offset_absolute_clock_changed));
274         sync_offset_relative_clock.ValueChanged.connect (sigc::mem_fun (*this, &RegionEditor::sync_offset_relative_clock_changed));
275
276         audition_button.signal_toggled().connect (sigc::mem_fun(*this, &RegionEditor::audition_button_toggled));
277
278         _session->AuditionActive.connect (audition_connection, invalidator (*this), boost::bind (&RegionEditor::audition_state_changed, this, _1), gui_context());
279 }
280
281 void
282 RegionEditor::position_clock_changed ()
283 {
284         bool in_command = false;
285         boost::shared_ptr<Playlist> pl = _region->playlist();
286
287         if (pl) {
288                 PublicEditor::instance().begin_reversible_command (_("change region start position"));
289                 in_command = true;
290
291                 _region->clear_changes ();
292                 _region->set_position (position_clock.current_time());
293                 _session->add_command(new StatefulDiffCommand (_region));
294         }
295
296         if (in_command) {
297                 PublicEditor::instance().commit_reversible_command ();
298         }
299 }
300
301 void
302 RegionEditor::end_clock_changed ()
303 {
304         bool in_command = false;
305         boost::shared_ptr<Playlist> pl = _region->playlist();
306
307         if (pl) {
308                 PublicEditor::instance().begin_reversible_command (_("change region end position"));
309                 in_command = true;
310
311                 _region->clear_changes ();
312                 _region->trim_end (end_clock.current_time());
313                 _session->add_command(new StatefulDiffCommand (_region));
314         }
315
316         if (in_command) {
317                 PublicEditor::instance().commit_reversible_command ();
318         }
319
320         end_clock.set (_region->position() + _region->length() - 1, true);
321 }
322
323 void
324 RegionEditor::length_clock_changed ()
325 {
326         samplecnt_t samples = length_clock.current_time();
327         bool in_command = false;
328         boost::shared_ptr<Playlist> pl = _region->playlist();
329
330         if (pl) {
331                 PublicEditor::instance().begin_reversible_command (_("change region length"));
332                 in_command = true;
333
334                 _region->clear_changes ();
335                 _region->trim_end (_region->position() + samples - 1);
336                 _session->add_command(new StatefulDiffCommand (_region));
337         }
338
339         if (in_command) {
340                 PublicEditor::instance().commit_reversible_command ();
341         }
342
343         length_clock.set (_region->length());
344 }
345
346 void
347 RegionEditor::audition_button_toggled ()
348 {
349         if (audition_button.get_active()) {
350                 _session->audition_region (_region);
351         } else {
352                 _session->cancel_audition ();
353         }
354 }
355
356 void
357 RegionEditor::name_changed ()
358 {
359         if (name_entry.get_text() != _region->name()) {
360                 name_entry.set_text (_region->name());
361         }
362 }
363
364 void
365 RegionEditor::bounds_changed (const PropertyChange& what_changed)
366 {
367         if (what_changed.contains (ARDOUR::Properties::position) && what_changed.contains (ARDOUR::Properties::length)) {
368                 position_clock.set (_region->position(), true);
369                 end_clock.set (_region->position() + _region->length() - 1, true);
370                 length_clock.set (_region->length(), true);
371         } else if (what_changed.contains (ARDOUR::Properties::position)) {
372                 position_clock.set (_region->position(), true);
373                 end_clock.set (_region->position() + _region->length() - 1, true);
374         } else if (what_changed.contains (ARDOUR::Properties::length)) {
375                 end_clock.set (_region->position() + _region->length() - 1, true);
376                 length_clock.set (_region->length(), true);
377         }
378
379         if (what_changed.contains (ARDOUR::Properties::sync_position) || what_changed.contains (ARDOUR::Properties::position)) {
380                 int dir;
381                 sampleoffset_t off = _region->sync_offset (dir);
382                 if (dir == -1) {
383                         off = -off;
384                 }
385
386                 if (what_changed.contains (ARDOUR::Properties::sync_position)) {
387                         sync_offset_relative_clock.set (off, true);
388                 }
389
390                 sync_offset_absolute_clock.set (off + _region->position (), true);
391         }
392
393         if (what_changed.contains (ARDOUR::Properties::start)) {
394                 start_clock.set (_region->start(), true);
395         }
396 }
397
398 void
399 RegionEditor::activation ()
400 {
401
402 }
403
404 void
405 RegionEditor::name_entry_changed ()
406 {
407         if (name_entry.get_text() != _region->name()) {
408                 _region->set_name (name_entry.get_text());
409         }
410 }
411
412 void
413 RegionEditor::audition_state_changed (bool yn)
414 {
415         ENSURE_GUI_THREAD (*this, &RegionEditor::audition_state_changed, yn)
416
417         if (!yn) {
418                 audition_button.set_active (false);
419         }
420 }
421
422 void
423 RegionEditor::sync_offset_absolute_clock_changed ()
424 {
425         PublicEditor::instance().begin_reversible_command (_("change region sync point"));
426
427         _region->clear_changes ();
428         _region->set_sync_position (sync_offset_absolute_clock.current_time());
429         _session->add_command (new StatefulDiffCommand (_region));
430
431         PublicEditor::instance().commit_reversible_command ();
432 }
433
434 void
435 RegionEditor::sync_offset_relative_clock_changed ()
436 {
437         PublicEditor::instance().begin_reversible_command (_("change region sync point"));
438
439         _region->clear_changes ();
440         _region->set_sync_position (sync_offset_relative_clock.current_time() + _region->position ());
441         _session->add_command (new StatefulDiffCommand (_region));
442
443         PublicEditor::instance().commit_reversible_command ();
444 }
445
446 bool
447 RegionEditor::on_delete_event (GdkEventAny*)
448 {
449         PropertyChange change;
450
451         change.add (ARDOUR::Properties::start);
452         change.add (ARDOUR::Properties::length);
453         change.add (ARDOUR::Properties::position);
454         change.add (ARDOUR::Properties::sync_position);
455
456         bounds_changed (change);
457
458         return true;
459 }
460
461 void
462 RegionEditor::handle_response (int)
463 {
464         hide ();
465 }