Zoom session when the mouse pointer is moved up and down during a playhead drag.
[ardour2.git] / gtk2_ardour / region_editor.cc
blob5e9f13db435f930ccef56895748fd4d40018e623
1 /*
2 Copyright (C) 2001 Paul Davis
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.
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.
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.
20 #include "pbd/memento_command.h"
21 #include "pbd/stateful_diff_command.h"
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>
33 #include "ardour_ui.h"
34 #include "clock_group.h"
35 #include "gui_thread.h"
36 #include "region_editor.h"
37 #include "utils.h"
39 #include "i18n.h"
41 using namespace ARDOUR;
42 using namespace PBD;
43 using namespace std;
44 using namespace Gtkmm2ext;
46 RegionEditor::RegionEditor (Session* s, boost::shared_ptr<Region> r)
47 : ArdourDialog (_("Region"))
48 , _table (9, 2)
49 , _table_row (0)
50 , _region (r)
51 , name_label (_("Name:"))
52 , audition_button (_("Play"))
53 , _clock_group (new ClockGroup)
54 , position_clock (X_("regionposition"), true, X_("RegionEditorClock"), true, false)
55 , end_clock (X_("regionend"), true, X_("RegionEditorClock"), true, false)
56 , length_clock (X_("regionlength"), true, X_("RegionEditorClock"), true, false, true)
57 , sync_offset_relative_clock (X_("regionsyncoffsetrelative"), true, X_("RegionEditorClock"), true, false)
58 , sync_offset_absolute_clock (X_("regionsyncoffsetabsolute"), true, X_("RegionEditorClock"), true, false)
59 /* XXX cannot file start yet */
60 , start_clock (X_("regionstart"), true, X_("RegionEditorClock"), false, false)
61 , _sources (1)
63 set_session (s);
65 _clock_group->set_clock_mode (AudioClock::Frames);
66 _clock_group->add (position_clock);
67 _clock_group->add (end_clock);
68 _clock_group->add (length_clock);
69 _clock_group->add (sync_offset_relative_clock);
70 _clock_group->add (sync_offset_absolute_clock);
71 _clock_group->add (start_clock);
73 position_clock.set_session (_session);
74 end_clock.set_session (_session);
75 length_clock.set_session (_session);
76 sync_offset_relative_clock.set_session (_session);
77 sync_offset_absolute_clock.set_session (_session);
78 start_clock.set_session (_session);
80 ARDOUR_UI::instance()->set_tip (audition_button, _("audition this region"));
82 audition_button.unset_flags (Gtk::CAN_FOCUS);
84 audition_button.set_events (audition_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
86 name_entry.set_name ("RegionEditorEntry");
87 name_label.set_name ("RegionEditorLabel");
88 position_label.set_name ("RegionEditorLabel");
89 position_label.set_text (_("Position:"));
90 end_label.set_name ("RegionEditorLabel");
91 end_label.set_text (_("End:"));
92 length_label.set_name ("RegionEditorLabel");
93 length_label.set_text (_("Length:"));
94 sync_relative_label.set_name ("RegionEditorLabel");
95 sync_relative_label.set_text (_("Sync point (relative to region):"));
96 sync_absolute_label.set_name ("RegionEditorLabel");
97 sync_absolute_label.set_text (_("Sync point (absolute):"));
98 start_label.set_name ("RegionEditorLabel");
99 start_label.set_text (_("File start:"));
100 _sources_label.set_name ("RegionEditorLabel");
102 if (_region->n_channels() > 1) {
103 _sources_label.set_text (_("Sources:"));
104 } else {
105 _sources_label.set_text (_("Source:"));
108 _table.set_col_spacings (12);
109 _table.set_row_spacings (6);
110 _table.set_border_width (12);
112 name_label.set_alignment (1, 0.5);
113 position_label.set_alignment (1, 0.5);
114 end_label.set_alignment (1, 0.5);
115 length_label.set_alignment (1, 0.5);
116 sync_relative_label.set_alignment (1, 0.5);
117 sync_absolute_label.set_alignment (1, 0.5);
118 start_label.set_alignment (1, 0.5);
119 _sources_label.set_alignment (1, 0.5);
121 Gtk::HBox* nb = Gtk::manage (new Gtk::HBox);
122 nb->set_spacing (6);
123 nb->pack_start (name_entry);
124 nb->pack_start (audition_button);
126 _table.attach (name_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
127 _table.attach (*nb, 1, 2, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
128 ++_table_row;
130 _table.attach (position_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
131 _table.attach (position_clock, 1, 2, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
132 ++_table_row;
134 _table.attach (end_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
135 _table.attach (end_clock, 1, 2, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
136 ++_table_row;
138 _table.attach (length_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
139 _table.attach (length_clock, 1, 2, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
140 ++_table_row;
142 _table.attach (sync_relative_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
143 _table.attach (sync_offset_relative_clock, 1, 2, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
144 ++_table_row;
146 _table.attach (sync_absolute_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
147 _table.attach (sync_offset_absolute_clock, 1, 2, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
148 ++_table_row;
150 _table.attach (start_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
151 _table.attach (start_clock, 1, 2, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
152 ++_table_row;
154 _table.attach (_sources_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
155 _table.attach (_sources, 1, 2, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
156 ++_table_row;
158 get_vbox()->pack_start (_table, true, true);
160 add_button (Gtk::Stock::CLOSE, Gtk::RESPONSE_ACCEPT);
162 set_name ("RegionEditorWindow");
163 add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
165 signal_delete_event().connect (sigc::bind (sigc::ptr_fun (just_hide_it), static_cast<Window *> (this)));
166 signal_response().connect (sigc::mem_fun (*this, &RegionEditor::handle_response));
168 set_title (string_compose (_("Region '%1'"), _region->name()));
170 for (uint32_t i = 0; i < _region->n_channels(); ++i) {
171 _sources.append_text (_region->source(i)->name());
174 _sources.set_headers_visible (false);
175 Gtk::CellRendererText* t = dynamic_cast<Gtk::CellRendererText*> (_sources.get_column_cell_renderer(0));
176 assert (t);
177 t->property_ellipsize() = Pango::ELLIPSIZE_END;
179 show_all();
181 name_changed ();
183 PropertyChange change;
185 change.add (ARDOUR::Properties::start);
186 change.add (ARDOUR::Properties::length);
187 change.add (ARDOUR::Properties::position);
188 change.add (ARDOUR::Properties::sync_position);
190 bounds_changed (change);
192 _region->PropertyChanged.connect (state_connection, invalidator (*this), ui_bind (&RegionEditor::region_changed, this, _1), gui_context());
194 spin_arrow_grab = false;
196 connect_editor_events ();
199 RegionEditor::~RegionEditor ()
201 delete _clock_group;
204 void
205 RegionEditor::region_changed (const PBD::PropertyChange& what_changed)
207 if (what_changed.contains (ARDOUR::Properties::name)) {
208 name_changed ();
211 PropertyChange interesting_stuff;
213 interesting_stuff.add (ARDOUR::Properties::position);
214 interesting_stuff.add (ARDOUR::Properties::length);
215 interesting_stuff.add (ARDOUR::Properties::start);
216 interesting_stuff.add (ARDOUR::Properties::sync_position);
218 if (what_changed.contains (interesting_stuff)) {
219 bounds_changed (what_changed);
223 gint
224 RegionEditor::bpressed (GdkEventButton* ev, Gtk::SpinButton* /*but*/, void (RegionEditor::*/*pmf*/)())
226 switch (ev->button) {
227 case 1:
228 case 2:
229 case 3:
230 if (ev->type == GDK_BUTTON_PRESS) { /* no double clicks here */
231 if (!spin_arrow_grab) {
232 // GTK2FIX probably nuke the region editor
233 // if ((ev->window == but->gobj()->panel)) {
234 // spin_arrow_grab = true;
235 // (this->*pmf)();
236 // }
239 break;
240 default:
241 break;
243 return FALSE;
246 gint
247 RegionEditor::breleased (GdkEventButton* /*ev*/, Gtk::SpinButton* /*but*/, void (RegionEditor::*pmf)())
249 if (spin_arrow_grab) {
250 (this->*pmf)();
251 spin_arrow_grab = false;
253 return FALSE;
256 void
257 RegionEditor::connect_editor_events ()
259 name_entry.signal_changed().connect (sigc::mem_fun(*this, &RegionEditor::name_entry_changed));
261 position_clock.ValueChanged.connect (sigc::mem_fun(*this, &RegionEditor::position_clock_changed));
262 end_clock.ValueChanged.connect (sigc::mem_fun(*this, &RegionEditor::end_clock_changed));
263 length_clock.ValueChanged.connect (sigc::mem_fun(*this, &RegionEditor::length_clock_changed));
264 sync_offset_absolute_clock.ValueChanged.connect (sigc::mem_fun (*this, &RegionEditor::sync_offset_absolute_clock_changed));
265 sync_offset_relative_clock.ValueChanged.connect (sigc::mem_fun (*this, &RegionEditor::sync_offset_relative_clock_changed));
267 audition_button.signal_toggled().connect (sigc::mem_fun(*this, &RegionEditor::audition_button_toggled));
269 _session->AuditionActive.connect (audition_connection, invalidator (*this), ui_bind (&RegionEditor::audition_state_changed, this, _1), gui_context());
272 void
273 RegionEditor::position_clock_changed ()
275 _session->begin_reversible_command (_("change region start position"));
277 boost::shared_ptr<Playlist> pl = _region->playlist();
279 if (pl) {
280 _region->clear_changes ();
281 _region->set_position (position_clock.current_time(), this);
282 _session->add_command(new StatefulDiffCommand (_region));
285 _session->commit_reversible_command ();
288 void
289 RegionEditor::end_clock_changed ()
291 _session->begin_reversible_command (_("change region end position"));
293 boost::shared_ptr<Playlist> pl = _region->playlist();
295 if (pl) {
296 _region->clear_changes ();
297 _region->trim_end (end_clock.current_time(), this);
298 _session->add_command(new StatefulDiffCommand (_region));
301 _session->commit_reversible_command ();
303 end_clock.set (_region->position() + _region->length() - 1, true);
306 void
307 RegionEditor::length_clock_changed ()
309 framecnt_t frames = length_clock.current_time();
311 _session->begin_reversible_command (_("change region length"));
313 boost::shared_ptr<Playlist> pl = _region->playlist();
315 if (pl) {
316 _region->clear_changes ();
317 _region->trim_end (_region->position() + frames - 1, this);
318 _session->add_command(new StatefulDiffCommand (_region));
321 _session->commit_reversible_command ();
323 length_clock.set (_region->length());
326 void
327 RegionEditor::audition_button_toggled ()
329 if (audition_button.get_active()) {
330 _session->audition_region (_region);
331 } else {
332 _session->cancel_audition ();
336 void
337 RegionEditor::name_changed ()
339 if (name_entry.get_text() != _region->name()) {
340 name_entry.set_text (_region->name());
344 void
345 RegionEditor::bounds_changed (const PropertyChange& what_changed)
347 if (what_changed.contains (ARDOUR::Properties::position) && what_changed.contains (ARDOUR::Properties::length)) {
348 position_clock.set (_region->position(), true);
349 end_clock.set (_region->position() + _region->length() - 1, true);
350 length_clock.set (_region->length(), true);
351 } else if (what_changed.contains (ARDOUR::Properties::position)) {
352 position_clock.set (_region->position(), true);
353 end_clock.set (_region->position() + _region->length() - 1, true);
354 } else if (what_changed.contains (ARDOUR::Properties::length)) {
355 end_clock.set (_region->position() + _region->length() - 1, true);
356 length_clock.set (_region->length(), true);
359 if (what_changed.contains (ARDOUR::Properties::sync_position) || what_changed.contains (ARDOUR::Properties::position)) {
360 int dir;
361 frameoffset_t off = _region->sync_offset (dir);
362 if (dir == -1) {
363 off = -off;
366 if (what_changed.contains (ARDOUR::Properties::sync_position)) {
367 sync_offset_relative_clock.set (off, true);
370 sync_offset_absolute_clock.set (off + _region->position (), true);
373 if (what_changed.contains (ARDOUR::Properties::start)) {
374 start_clock.set (_region->start(), true);
378 void
379 RegionEditor::activation ()
384 void
385 RegionEditor::name_entry_changed ()
387 if (name_entry.get_text() != _region->name()) {
388 _region->set_name (name_entry.get_text());
392 void
393 RegionEditor::audition_state_changed (bool yn)
395 ENSURE_GUI_THREAD (*this, &RegionEditor::audition_state_changed, yn)
397 if (!yn) {
398 audition_button.set_active (false);
402 void
403 RegionEditor::sync_offset_absolute_clock_changed ()
405 _session->begin_reversible_command (_("change region sync point"));
407 _region->clear_changes ();
408 _region->set_sync_position (sync_offset_absolute_clock.current_time());
409 _session->add_command (new StatefulDiffCommand (_region));
411 _session->commit_reversible_command ();
414 void
415 RegionEditor::sync_offset_relative_clock_changed ()
417 _session->begin_reversible_command (_("change region sync point"));
419 _region->clear_changes ();
420 _region->set_sync_position (sync_offset_relative_clock.current_time() + _region->position ());
421 _session->add_command (new StatefulDiffCommand (_region));
423 _session->commit_reversible_command ();
426 bool
427 RegionEditor::on_delete_event (GdkEventAny*)
429 PropertyChange change;
431 change.add (ARDOUR::Properties::start);
432 change.add (ARDOUR::Properties::length);
433 change.add (ARDOUR::Properties::position);
434 change.add (ARDOUR::Properties::sync_position);
436 bounds_changed (change);
438 return true;
441 void
442 RegionEditor::handle_response (int)
444 hide ();