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