Remove all use of nframes_t.
[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 (9, 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
92         if (_region->n_channels() > 1) {
93                 _sources_label.set_text (_("Sources:"));
94         } else {
95                 _sources_label.set_text (_("Source:"));
96         }
97
98         _table.set_col_spacings (12);
99         _table.set_row_spacings (6);
100         _table.set_border_width (12);
101
102         name_label.set_alignment (1, 0.5);
103         position_label.set_alignment (1, 0.5);
104         end_label.set_alignment (1, 0.5);
105         length_label.set_alignment (1, 0.5);
106         sync_relative_label.set_alignment (1, 0.5);
107         sync_absolute_label.set_alignment (1, 0.5);
108         start_label.set_alignment (1, 0.5);
109         _sources_label.set_alignment (1, 0.5);
110
111         Gtk::HBox* nb = Gtk::manage (new Gtk::HBox);
112         nb->set_spacing (6);
113         nb->pack_start (name_entry);
114         nb->pack_start (audition_button);
115
116         _table.attach (name_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
117         _table.attach (*nb, 1, 2, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
118         ++_table_row;
119
120         _table.attach (position_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
121         _table.attach (position_clock, 1, 2, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
122         ++_table_row;
123
124         _table.attach (end_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
125         _table.attach (end_clock, 1, 2, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
126         ++_table_row;
127         
128         _table.attach (length_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
129         _table.attach (length_clock, 1, 2, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
130         ++_table_row;
131         
132         _table.attach (sync_relative_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
133         _table.attach (sync_offset_relative_clock, 1, 2, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
134         ++_table_row;
135  
136         _table.attach (sync_absolute_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
137         _table.attach (sync_offset_absolute_clock, 1, 2, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
138         ++_table_row;
139
140         _table.attach (start_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
141         _table.attach (start_clock, 1, 2, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
142         ++_table_row;
143
144         _table.attach (_sources_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
145         _table.attach (_sources, 1, 2, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
146         ++_table_row;
147         
148         get_vbox()->pack_start (_table, true, true);
149
150         add_button (Gtk::Stock::CLOSE, Gtk::RESPONSE_ACCEPT);
151
152         set_name ("RegionEditorWindow");
153         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
154
155         signal_delete_event().connect (sigc::bind (sigc::ptr_fun (just_hide_it), static_cast<Window *> (this)));
156         signal_response().connect (sigc::mem_fun (*this, &RegionEditor::handle_response));
157
158         set_title (string_compose (_("Region '%1'"), _region->name()));
159
160         for (uint32_t i = 0; i < _region->n_channels(); ++i) {
161                 _sources.append_text (_region->source(i)->name());
162         }
163
164         _sources.set_headers_visible (false);
165         Gtk::CellRendererText* t = dynamic_cast<Gtk::CellRendererText*> (_sources.get_column_cell_renderer(0));
166         assert (t);
167         t->property_ellipsize() = Pango::ELLIPSIZE_END;
168
169         show_all();
170
171         name_changed ();
172
173         PropertyChange change;
174
175         change.add (ARDOUR::Properties::start);
176         change.add (ARDOUR::Properties::length);
177         change.add (ARDOUR::Properties::position);
178         change.add (ARDOUR::Properties::sync_position);
179
180         bounds_changed (change);
181
182         _region->PropertyChanged.connect (state_connection, invalidator (*this), ui_bind (&RegionEditor::region_changed, this, _1), gui_context());
183
184         spin_arrow_grab = false;
185
186         connect_editor_events ();
187 }
188
189 void
190 RegionEditor::region_changed (const PBD::PropertyChange& what_changed)
191 {
192         if (what_changed.contains (ARDOUR::Properties::name)) {
193                 name_changed ();
194         }
195
196         PropertyChange interesting_stuff;
197
198         interesting_stuff.add (ARDOUR::Properties::position);
199         interesting_stuff.add (ARDOUR::Properties::length);
200         interesting_stuff.add (ARDOUR::Properties::start);
201         interesting_stuff.add (ARDOUR::Properties::sync_position);
202
203         if (what_changed.contains (interesting_stuff)) {
204                 bounds_changed (what_changed);
205         }
206 }
207
208 gint
209 RegionEditor::bpressed (GdkEventButton* ev, Gtk::SpinButton* /*but*/, void (RegionEditor::*/*pmf*/)())
210 {
211         switch (ev->button) {
212         case 1:
213         case 2:
214         case 3:
215                 if (ev->type == GDK_BUTTON_PRESS) { /* no double clicks here */
216                         if (!spin_arrow_grab) {
217                                 // GTK2FIX probably nuke the region editor
218                                 // if ((ev->window == but->gobj()->panel)) {
219                                 // spin_arrow_grab = true;
220                                 // (this->*pmf)();
221                                 // }
222                         }
223                 }
224                 break;
225         default:
226                 break;
227         }
228         return FALSE;
229 }
230
231 gint
232 RegionEditor::breleased (GdkEventButton* /*ev*/, Gtk::SpinButton* /*but*/, void (RegionEditor::*pmf)())
233 {
234         if (spin_arrow_grab) {
235                 (this->*pmf)();
236                 spin_arrow_grab = false;
237         }
238         return FALSE;
239 }
240
241 void
242 RegionEditor::connect_editor_events ()
243 {
244         name_entry.signal_changed().connect (sigc::mem_fun(*this, &RegionEditor::name_entry_changed));
245
246         position_clock.ValueChanged.connect (sigc::mem_fun(*this, &RegionEditor::position_clock_changed));
247         end_clock.ValueChanged.connect (sigc::mem_fun(*this, &RegionEditor::end_clock_changed));
248         length_clock.ValueChanged.connect (sigc::mem_fun(*this, &RegionEditor::length_clock_changed));
249         sync_offset_absolute_clock.ValueChanged.connect (sigc::mem_fun (*this, &RegionEditor::sync_offset_absolute_clock_changed));
250         sync_offset_relative_clock.ValueChanged.connect (sigc::mem_fun (*this, &RegionEditor::sync_offset_relative_clock_changed));
251
252         audition_button.signal_toggled().connect (sigc::mem_fun(*this, &RegionEditor::audition_button_toggled));
253
254         _session->AuditionActive.connect (audition_connection, invalidator (*this), ui_bind (&RegionEditor::audition_state_changed, this, _1), gui_context());
255 }
256
257 void
258 RegionEditor::position_clock_changed ()
259 {
260         _session->begin_reversible_command (_("change region start position"));
261
262         boost::shared_ptr<Playlist> pl = _region->playlist();
263
264         if (pl) {
265                 _region->clear_changes ();
266                 _region->set_position (position_clock.current_time(), this);
267                 _session->add_command(new StatefulDiffCommand (_region));
268         }
269
270         _session->commit_reversible_command ();
271 }
272
273 void
274 RegionEditor::end_clock_changed ()
275 {
276         _session->begin_reversible_command (_("change region end position"));
277
278         boost::shared_ptr<Playlist> pl = _region->playlist();
279
280         if (pl) {
281                 _region->clear_changes ();
282                 _region->trim_end (end_clock.current_time(), this);
283                 _session->add_command(new StatefulDiffCommand (_region));
284         }
285
286         _session->commit_reversible_command ();
287
288         end_clock.set (_region->position() + _region->length() - 1, true);
289 }
290
291 void
292 RegionEditor::length_clock_changed ()
293 {
294         framecnt_t frames = length_clock.current_time();
295
296         _session->begin_reversible_command (_("change region length"));
297
298         boost::shared_ptr<Playlist> pl = _region->playlist();
299
300         if (pl) {
301                 _region->clear_changes ();
302                 _region->trim_end (_region->position() + frames - 1, this);
303                 _session->add_command(new StatefulDiffCommand (_region));
304         }
305
306         _session->commit_reversible_command ();
307
308         length_clock.set (_region->length());
309 }
310
311 void
312 RegionEditor::audition_button_toggled ()
313 {
314         if (audition_button.get_active()) {
315                 _session->audition_region (_region);
316         } else {
317                 _session->cancel_audition ();
318         }
319 }
320
321 void
322 RegionEditor::name_changed ()
323 {
324         if (name_entry.get_text() != _region->name()) {
325                 name_entry.set_text (_region->name());
326         }
327 }
328
329 void
330 RegionEditor::bounds_changed (const PropertyChange& what_changed)
331 {
332         if (what_changed.contains (ARDOUR::Properties::position) && what_changed.contains (ARDOUR::Properties::length)) {
333                 position_clock.set (_region->position(), true);
334                 end_clock.set (_region->position() + _region->length() - 1, true);
335                 length_clock.set (_region->length(), true);
336         } else if (what_changed.contains (ARDOUR::Properties::position)) {
337                 position_clock.set (_region->position(), true);
338                 end_clock.set (_region->position() + _region->length() - 1, true);
339         } else if (what_changed.contains (ARDOUR::Properties::length)) {
340                 end_clock.set (_region->position() + _region->length() - 1, true);
341                 length_clock.set (_region->length(), true);
342         }
343
344         if (what_changed.contains (ARDOUR::Properties::sync_position) || what_changed.contains (ARDOUR::Properties::position)) {
345                 int dir;
346                 frameoffset_t off = _region->sync_offset (dir);
347                 if (dir == -1) {
348                         off = -off;
349                 }
350
351                 if (what_changed.contains (ARDOUR::Properties::sync_position)) {
352                         sync_offset_relative_clock.set (off, true);
353                 }
354
355                 sync_offset_absolute_clock.set (off + _region->position (), true);
356         }
357
358         if (what_changed.contains (ARDOUR::Properties::start)) {
359                 start_clock.set (_region->start(), true);
360         }
361 }
362
363 void
364 RegionEditor::activation ()
365 {
366
367 }
368
369 void
370 RegionEditor::name_entry_changed ()
371 {
372         if (name_entry.get_text() != _region->name()) {
373                 _region->set_name (name_entry.get_text());
374         }
375 }
376
377 void
378 RegionEditor::audition_state_changed (bool yn)
379 {
380         ENSURE_GUI_THREAD (*this, &RegionEditor::audition_state_changed, yn)
381
382         if (!yn) {
383                 audition_button.set_active (false);
384         }
385 }
386
387 void
388 RegionEditor::sync_offset_absolute_clock_changed ()
389 {
390         _session->begin_reversible_command (_("change region sync point"));
391
392         _region->clear_changes ();
393         _region->set_sync_position (sync_offset_absolute_clock.current_time());
394         _session->add_command (new StatefulDiffCommand (_region));
395         
396         _session->commit_reversible_command ();
397 }
398
399 void
400 RegionEditor::sync_offset_relative_clock_changed ()
401 {
402         _session->begin_reversible_command (_("change region sync point"));
403
404         _region->clear_changes ();
405         _region->set_sync_position (sync_offset_relative_clock.current_time() + _region->position ());
406         _session->add_command (new StatefulDiffCommand (_region));
407         
408         _session->commit_reversible_command ();
409 }
410
411 bool
412 RegionEditor::on_delete_event (GdkEventAny*)
413 {
414         PropertyChange change;
415
416         change.add (ARDOUR::Properties::start);
417         change.add (ARDOUR::Properties::length);
418         change.add (ARDOUR::Properties::position);
419         change.add (ARDOUR::Properties::sync_position);
420
421         bounds_changed (change);
422
423         return true;
424 }
425
426 void
427 RegionEditor::handle_response (int)
428 {
429         hide ();
430 }