fix math bug with numthreads computation
[ardour2.git] / gtk2_ardour / region_editor.cc
blob27c74552816c0a9f9015a2eb6eca7f3422c12cfa
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 <gtkmm2ext/utils.h>
29 #include <cmath>
31 #include "region_editor.h"
32 #include "ardour_ui.h"
33 #include "utils.h"
34 #include "gui_thread.h"
36 #include "i18n.h"
38 using namespace ARDOUR;
39 using namespace PBD;
40 using namespace std;
41 using namespace Gtkmm2ext;
43 RegionEditor::RegionEditor (Session* s, boost::shared_ptr<Region> r)
44 : ArdourDialog (_("Region")),
45 _table (8, 2),
46 _region (r),
47 name_label (_("Name:")),
48 audition_button (_("Play")),
49 position_clock (X_("regionposition"), true, X_("RegionEditorClock"), true, false),
50 end_clock (X_("regionend"), true, X_("RegionEditorClock"), true, false),
51 length_clock (X_("regionlength"), true, X_("RegionEditorClock"), true, false, true),
52 sync_offset_relative_clock (X_("regionsyncoffsetrelative"), true, X_("RegionEditorClock"), true, false),
53 sync_offset_absolute_clock (X_("regionsyncoffsetabsolute"), true, X_("RegionEditorClock"), true, false),
54 /* XXX cannot file start yet */
55 start_clock (X_("regionstart"), true, X_("RegionEditorClock"), false, false)
57 set_session (s);
59 position_clock.set_session (_session);
60 end_clock.set_session (_session);
61 length_clock.set_session (_session);
62 sync_offset_relative_clock.set_session (_session);
63 sync_offset_absolute_clock.set_session (_session);
64 start_clock.set_session (_session);
66 ARDOUR_UI::instance()->set_tip (audition_button, _("audition this region"));
68 audition_button.unset_flags (Gtk::CAN_FOCUS);
70 audition_button.set_events (audition_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
72 name_entry.set_name ("RegionEditorEntry");
73 name_label.set_name ("RegionEditorLabel");
74 position_label.set_name ("RegionEditorLabel");
75 position_label.set_text (_("Position:"));
76 end_label.set_name ("RegionEditorLabel");
77 end_label.set_text (_("End:"));
78 length_label.set_name ("RegionEditorLabel");
79 length_label.set_text (_("Length:"));
80 sync_relative_label.set_name ("RegionEditorLabel");
81 sync_relative_label.set_text (_("Sync point (relative to region):"));
82 sync_absolute_label.set_name ("RegionEditorLabel");
83 sync_absolute_label.set_text (_("Sync point (absolute):"));
84 start_label.set_name ("RegionEditorLabel");
85 start_label.set_text (_("File start:"));
87 _table.set_col_spacings (12);
88 _table.set_row_spacings (6);
89 _table.set_border_width (12);
91 name_label.set_alignment (1, 0.5);
92 position_label.set_alignment (1, 0.5);
93 end_label.set_alignment (1, 0.5);
94 length_label.set_alignment (1, 0.5);
95 sync_relative_label.set_alignment (1, 0.5);
96 sync_absolute_label.set_alignment (1, 0.5);
97 start_label.set_alignment (1, 0.5);
99 Gtk::HBox* nb = Gtk::manage (new Gtk::HBox);
100 nb->set_spacing (6);
101 nb->pack_start (name_entry);
102 nb->pack_start (audition_button);
104 _table.attach (name_label, 0, 1, 0, 1, Gtk::FILL, Gtk::FILL);
105 _table.attach (*nb, 1, 2, 0, 1, Gtk::FILL, Gtk::FILL);
107 _table.attach (position_label, 0, 1, 1, 2, Gtk::FILL, Gtk::FILL);
108 _table.attach (position_clock, 1, 2, 1, 2, Gtk::FILL, Gtk::FILL);
110 _table.attach (end_label, 0, 1, 2, 3, Gtk::FILL, Gtk::FILL);
111 _table.attach (end_clock, 1, 2, 2, 3, Gtk::FILL, Gtk::FILL);
113 _table.attach (length_label, 0, 1, 3, 4, Gtk::FILL, Gtk::FILL);
114 _table.attach (length_clock, 1, 2, 3, 4, Gtk::FILL, Gtk::FILL);
116 _table.attach (sync_relative_label, 0, 1, 4, 5, Gtk::FILL, Gtk::FILL);
117 _table.attach (sync_offset_relative_clock, 1, 2, 4, 5, Gtk::FILL, Gtk::FILL);
119 _table.attach (sync_absolute_label, 0, 1, 5, 6, Gtk::FILL, Gtk::FILL);
120 _table.attach (sync_offset_absolute_clock, 1, 2, 5, 6, Gtk::FILL, Gtk::FILL);
122 _table.attach (start_label, 0, 1, 6, 7, Gtk::FILL, Gtk::FILL);
123 _table.attach (start_clock, 1, 2, 6, 7, Gtk::FILL, Gtk::FILL);
125 get_vbox()->pack_start (_table, true, true);
127 add_button (Gtk::Stock::CLOSE, Gtk::RESPONSE_ACCEPT);
129 set_name ("RegionEditorWindow");
130 add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
132 signal_delete_event().connect (sigc::bind (sigc::ptr_fun (just_hide_it), static_cast<Window *> (this)));
133 signal_response().connect (sigc::mem_fun (*this, &RegionEditor::handle_response));
135 set_title (string_compose (_("Region '%1'"), _region->name()));
137 show_all();
139 name_changed ();
141 PropertyChange change;
143 change.add (ARDOUR::Properties::start);
144 change.add (ARDOUR::Properties::length);
145 change.add (ARDOUR::Properties::position);
146 change.add (ARDOUR::Properties::sync_position);
148 bounds_changed (change);
150 _region->PropertyChanged.connect (state_connection, invalidator (*this), ui_bind (&RegionEditor::region_changed, this, _1), gui_context());
152 spin_arrow_grab = false;
154 connect_editor_events ();
157 void
158 RegionEditor::region_changed (const PBD::PropertyChange& what_changed)
160 if (what_changed.contains (ARDOUR::Properties::name)) {
161 name_changed ();
164 PropertyChange interesting_stuff;
166 interesting_stuff.add (ARDOUR::Properties::position);
167 interesting_stuff.add (ARDOUR::Properties::length);
168 interesting_stuff.add (ARDOUR::Properties::start);
169 interesting_stuff.add (ARDOUR::Properties::sync_position);
171 if (what_changed.contains (interesting_stuff)) {
172 bounds_changed (what_changed);
176 gint
177 RegionEditor::bpressed (GdkEventButton* ev, Gtk::SpinButton* /*but*/, void (RegionEditor::*/*pmf*/)())
179 switch (ev->button) {
180 case 1:
181 case 2:
182 case 3:
183 if (ev->type == GDK_BUTTON_PRESS) { /* no double clicks here */
184 if (!spin_arrow_grab) {
185 // GTK2FIX probably nuke the region editor
186 // if ((ev->window == but->gobj()->panel)) {
187 // spin_arrow_grab = true;
188 // (this->*pmf)();
189 // }
192 break;
193 default:
194 break;
196 return FALSE;
199 gint
200 RegionEditor::breleased (GdkEventButton* /*ev*/, Gtk::SpinButton* /*but*/, void (RegionEditor::*pmf)())
202 if (spin_arrow_grab) {
203 (this->*pmf)();
204 spin_arrow_grab = false;
206 return FALSE;
209 void
210 RegionEditor::connect_editor_events ()
212 name_entry.signal_changed().connect (sigc::mem_fun(*this, &RegionEditor::name_entry_changed));
214 position_clock.ValueChanged.connect (sigc::mem_fun(*this, &RegionEditor::position_clock_changed));
215 end_clock.ValueChanged.connect (sigc::mem_fun(*this, &RegionEditor::end_clock_changed));
216 length_clock.ValueChanged.connect (sigc::mem_fun(*this, &RegionEditor::length_clock_changed));
217 sync_offset_absolute_clock.ValueChanged.connect (sigc::mem_fun (*this, &RegionEditor::sync_offset_absolute_clock_changed));
218 sync_offset_relative_clock.ValueChanged.connect (sigc::mem_fun (*this, &RegionEditor::sync_offset_relative_clock_changed));
220 audition_button.signal_toggled().connect (sigc::mem_fun(*this, &RegionEditor::audition_button_toggled));
222 _session->AuditionActive.connect (audition_connection, invalidator (*this), ui_bind (&RegionEditor::audition_state_changed, this, _1), gui_context());
225 void
226 RegionEditor::position_clock_changed ()
228 _session->begin_reversible_command (_("change region start position"));
230 boost::shared_ptr<Playlist> pl = _region->playlist();
232 if (pl) {
233 _region->clear_history ();
234 _region->set_position (position_clock.current_time(), this);
235 _session->add_command(new StatefulDiffCommand (_region));
238 _session->commit_reversible_command ();
241 void
242 RegionEditor::end_clock_changed ()
244 _session->begin_reversible_command (_("change region end position"));
246 boost::shared_ptr<Playlist> pl = _region->playlist();
248 if (pl) {
249 _region->clear_history ();
250 _region->trim_end (end_clock.current_time(), this);
251 _session->add_command(new StatefulDiffCommand (_region));
254 _session->commit_reversible_command ();
256 end_clock.set (_region->position() + _region->length() - 1, true);
259 void
260 RegionEditor::length_clock_changed ()
262 nframes_t frames = length_clock.current_time();
264 _session->begin_reversible_command (_("change region length"));
266 boost::shared_ptr<Playlist> pl = _region->playlist();
268 if (pl) {
269 _region->clear_history ();
270 _region->trim_end (_region->position() + frames - 1, this);
271 _session->add_command(new StatefulDiffCommand (_region));
274 _session->commit_reversible_command ();
276 length_clock.set (_region->length());
279 void
280 RegionEditor::audition_button_toggled ()
282 if (audition_button.get_active()) {
283 _session->audition_region (_region);
284 } else {
285 _session->cancel_audition ();
289 void
290 RegionEditor::name_changed ()
292 if (name_entry.get_text() != _region->name()) {
293 name_entry.set_text (_region->name());
297 void
298 RegionEditor::bounds_changed (const PropertyChange& what_changed)
300 if (what_changed.contains (ARDOUR::Properties::position) && what_changed.contains (ARDOUR::Properties::length)) {
301 position_clock.set (_region->position(), true);
302 end_clock.set (_region->position() + _region->length() - 1, true);
303 length_clock.set (_region->length(), true);
304 } else if (what_changed.contains (ARDOUR::Properties::position)) {
305 position_clock.set (_region->position(), true);
306 end_clock.set (_region->position() + _region->length() - 1, true);
307 } else if (what_changed.contains (ARDOUR::Properties::length)) {
308 end_clock.set (_region->position() + _region->length() - 1, true);
309 length_clock.set (_region->length(), true);
312 if (what_changed.contains (ARDOUR::Properties::sync_position) || what_changed.contains (ARDOUR::Properties::position)) {
313 int dir;
314 nframes_t off = _region->sync_offset (dir);
315 if (dir == -1) {
316 off = -off;
319 if (what_changed.contains (ARDOUR::Properties::sync_position)) {
320 sync_offset_relative_clock.set (off, true);
323 sync_offset_absolute_clock.set (off + _region->position (), true);
326 if (what_changed.contains (ARDOUR::Properties::start)) {
327 start_clock.set (_region->start(), true);
331 void
332 RegionEditor::activation ()
337 void
338 RegionEditor::name_entry_changed ()
340 if (name_entry.get_text() != _region->name()) {
341 _region->set_name (name_entry.get_text());
345 void
346 RegionEditor::audition_state_changed (bool yn)
348 ENSURE_GUI_THREAD (*this, &RegionEditor::audition_state_changed, yn)
350 if (!yn) {
351 audition_button.set_active (false);
355 void
356 RegionEditor::sync_offset_absolute_clock_changed ()
358 _session->begin_reversible_command (_("change region sync point"));
360 _region->clear_history ();
361 _region->set_sync_position (sync_offset_absolute_clock.current_time());
362 _session->add_command (new StatefulDiffCommand (_region));
364 _session->commit_reversible_command ();
367 void
368 RegionEditor::sync_offset_relative_clock_changed ()
370 _session->begin_reversible_command (_("change region sync point"));
372 _region->clear_history ();
373 _region->set_sync_position (sync_offset_relative_clock.current_time() + _region->position ());
374 _session->add_command (new StatefulDiffCommand (_region));
376 _session->commit_reversible_command ();
379 bool
380 RegionEditor::on_delete_event (GdkEventAny* ev)
382 PropertyChange change;
384 change.add (ARDOUR::Properties::start);
385 change.add (ARDOUR::Properties::length);
386 change.add (ARDOUR::Properties::position);
387 change.add (ARDOUR::Properties::sync_position);
389 bounds_changed (change);
391 return true;
394 void
395 RegionEditor::handle_response (int)
397 hide ();