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