try to name/number new routes-from-templates to avoid colliding names if adding more...
[ardour2.git] / gtk2_ardour / audio_region_editor.cc
blobc36ddbc041ce69fb8fafd04ebe4cf24d355baf27
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>
22 #include <ardour/audioregion.h>
23 #include <ardour/playlist.h>
24 #include <ardour/utils.h>
25 #include <gtkmm2ext/utils.h>
26 #include <gtkmm2ext/stop_signal.h>
27 #include <gtkmm2ext/window_title.h>
28 #include <cmath>
30 #include "audio_region_editor.h"
31 #include "audio_region_view.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 sigc;
41 using namespace std;
42 using namespace Gtkmm2ext;
44 AudioRegionEditor::AudioRegionEditor (Session& s, boost::shared_ptr<AudioRegion> r, AudioRegionView& rv)
45 : RegionEditor (s),
46 _region (r),
47 _region_view (rv),
48 name_label (_("NAME:")),
49 audition_button (_("play")),
50 time_table (5, 2),
51 position_clock (X_("regionposition"), true, X_("AudioRegionEditorClock"), true),
52 end_clock (X_("regionend"), true, X_("AudioRegionEditorClock"), true),
53 length_clock (X_("regionlength"), true, X_("AudioRegionEditorClock"), true, true),
54 /* XXX cannot edit sync point or start yet */
55 sync_offset_clock (X_("regionsyncoffset"), true, X_("AudioRegionEditorClock"), false),
56 start_clock (X_("regionstart"), true, X_("AudioRegionEditorClock"), false)
59 position_clock.set_session (&_session);
60 end_clock.set_session (&_session);
61 length_clock.set_session (&_session);
62 sync_offset_clock.set_session (&_session);
63 start_clock.set_session (&_session);
65 name_entry.set_name ("AudioRegionEditorEntry");
66 name_label.set_name ("AudioRegionEditorLabel");
68 name_hbox.set_spacing (5);
69 name_hbox.pack_start (name_label, false, false);
70 name_hbox.pack_start (name_entry, false, false);
72 ARDOUR_UI::instance()->tooltips().set_tip (audition_button, _("audition this region"));
74 audition_button.unset_flags (Gtk::CAN_FOCUS);
76 audition_button.set_events (audition_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
78 top_row_button_hbox.set_border_width (5);
79 top_row_button_hbox.set_spacing (5);
80 top_row_button_hbox.set_homogeneous (false);
81 top_row_button_hbox.pack_end (audition_button, false, false);
83 top_row_hbox.pack_start (name_hbox, true, true);
84 top_row_hbox.pack_end (top_row_button_hbox, true, true);
86 position_label.set_name ("AudioRegionEditorLabel");
87 position_label.set_text (_("POSITION:"));
88 end_label.set_name ("AudioRegionEditorLabel");
89 end_label.set_text (_("END:"));
90 length_label.set_name ("AudioRegionEditorLabel");
91 length_label.set_text (_("LENGTH:"));
92 sync_label.set_name ("AudioRegionEditorLabel");
93 sync_label.set_text (_("SYNC POINT:"));
94 start_label.set_name ("AudioRegionEditorLabel");
95 start_label.set_text (_("FILE START:"));
97 time_table.set_col_spacings (2);
98 time_table.set_row_spacings (5);
99 time_table.set_border_width (5);
101 position_alignment.set (1.0, 0.5);
102 end_alignment.set (1.0, 0.5);
103 length_alignment.set (1.0, 0.5);
104 sync_alignment.set (1.0, 0.5);
105 start_alignment.set (1.0, 0.5);
107 position_alignment.add (position_label);
108 end_alignment.add (end_label);
109 length_alignment.add (length_label);
110 sync_alignment.add (sync_label);
111 start_alignment.add (start_label);
113 time_table.attach (position_alignment, 0, 1, 0, 1, Gtk::FILL, Gtk::FILL);
114 time_table.attach (position_clock, 1, 2, 0, 1, Gtk::FILL, Gtk::FILL);
116 time_table.attach (end_alignment, 0, 1, 1, 2, Gtk::FILL, Gtk::FILL);
117 time_table.attach (end_clock, 1, 2, 1, 2, Gtk::FILL, Gtk::FILL);
119 time_table.attach (length_alignment, 0, 1, 2, 3, Gtk::FILL, Gtk::FILL);
120 time_table.attach (length_clock, 1, 2, 2, 3, Gtk::FILL, Gtk::FILL);
122 time_table.attach (sync_alignment, 0, 1, 3, 4, Gtk::FILL, Gtk::FILL);
123 time_table.attach (sync_offset_clock, 1, 2, 3, 4, Gtk::FILL, Gtk::FILL);
125 time_table.attach (start_alignment, 0, 1, 4, 5, Gtk::FILL, Gtk::FILL);
126 time_table.attach (start_clock, 1, 2, 4, 5, Gtk::FILL, Gtk::FILL);
128 lower_hbox.pack_start (time_table, true, true);
129 lower_hbox.pack_start (sep1, false, false);
130 lower_hbox.pack_start (sep2, false, false);
132 get_vbox()->pack_start (top_row_hbox, true, true);
133 get_vbox()->pack_start (sep3, false, false);
134 get_vbox()->pack_start (lower_hbox, true, true);
136 set_name ("AudioRegionEditorWindow");
137 add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
139 signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), static_cast<Window *> (this)));
141 WindowTitle title(string_compose (_("Region %1"), _region->name()));
142 title += Glib::get_application_name();
144 set_title (title.get_string());
146 show_all();
148 name_changed ();
149 bounds_changed (Change (StartChanged|LengthChanged|PositionChanged|StartChanged|Region::SyncOffsetChanged));
151 _region->StateChanged.connect (mem_fun(*this, &AudioRegionEditor::region_changed));
153 spin_arrow_grab = false;
155 connect_editor_events ();
158 AudioRegionEditor::~AudioRegionEditor ()
162 void
163 AudioRegionEditor::region_changed (Change what_changed)
165 if (what_changed & NameChanged) {
166 name_changed ();
169 if (what_changed & Change (BoundsChanged|StartChanged|Region::SyncOffsetChanged)) {
170 bounds_changed (what_changed);
174 gint
175 AudioRegionEditor::bpressed (GdkEventButton* ev, Gtk::SpinButton* but, void (AudioRegionEditor::*pmf)())
177 switch (ev->button) {
178 case 1:
179 case 2:
180 case 3:
181 if (ev->type == GDK_BUTTON_PRESS) { /* no double clicks here */
182 if (!spin_arrow_grab) {
183 // GTK2FIX probably nuke the region editor
184 // if ((ev->window == but->gobj()->panel)) {
185 // spin_arrow_grab = true;
186 // (this->*pmf)();
187 // }
190 break;
191 default:
192 break;
194 return FALSE;
197 gint
198 AudioRegionEditor::breleased (GdkEventButton* ev, Gtk::SpinButton* but, void (AudioRegionEditor::*pmf)())
200 if (spin_arrow_grab) {
201 (this->*pmf)();
202 spin_arrow_grab = false;
204 return FALSE;
207 void
208 AudioRegionEditor::connect_editor_events ()
210 name_entry.signal_changed().connect (mem_fun(*this, &AudioRegionEditor::name_entry_changed));
212 position_clock.ValueChanged.connect (mem_fun(*this, &AudioRegionEditor::position_clock_changed));
213 end_clock.ValueChanged.connect (mem_fun(*this, &AudioRegionEditor::end_clock_changed));
214 length_clock.ValueChanged.connect (mem_fun(*this, &AudioRegionEditor::length_clock_changed));
216 audition_button.signal_toggled().connect (mem_fun(*this, &AudioRegionEditor::audition_button_toggled));
217 _session.AuditionActive.connect (mem_fun(*this, &AudioRegionEditor::audition_state_changed));
220 void
221 AudioRegionEditor::position_clock_changed ()
223 _session.begin_reversible_command (_("change region start position"));
225 boost::shared_ptr<Playlist> pl = _region->playlist();
227 if (pl) {
228 XMLNode &before = pl->get_state();
229 _region->set_position (position_clock.current_time(), this);
230 XMLNode &after = pl->get_state();
231 _session.add_command(new MementoCommand<Playlist>(*pl, &before, &after));
234 _session.commit_reversible_command ();
237 void
238 AudioRegionEditor::end_clock_changed ()
240 _session.begin_reversible_command (_("change region end position"));
242 boost::shared_ptr<Playlist> pl = _region->playlist();
244 if (pl) {
245 XMLNode &before = pl->get_state();
246 _region->trim_end (end_clock.current_time(), this);
247 XMLNode &after = pl->get_state();
248 _session.add_command(new MementoCommand<Playlist>(*pl, &before, &after));
251 _session.commit_reversible_command ();
253 end_clock.set (_region->position() + _region->length(), true);
256 void
257 AudioRegionEditor::length_clock_changed ()
259 nframes_t frames = length_clock.current_time();
261 _session.begin_reversible_command (_("change region length"));
263 boost::shared_ptr<Playlist> pl = _region->playlist();
265 if (pl) {
266 XMLNode &before = pl->get_state();
267 _region->trim_end (_region->position() + frames, this);
268 XMLNode &after = pl->get_state();
269 _session.add_command(new MementoCommand<Playlist>(*pl, &before, &after));
272 _session.commit_reversible_command ();
274 length_clock.set (_region->length());
277 void
278 AudioRegionEditor::audition_button_toggled ()
280 if (audition_button.get_active()) {
281 _session.audition_region (_region);
282 } else {
283 _session.cancel_audition ();
287 void
288 AudioRegionEditor::name_changed ()
290 if (name_entry.get_text() != _region->name()) {
291 name_entry.set_text (_region->name());
295 void
296 AudioRegionEditor::bounds_changed (Change what_changed)
298 if ((what_changed & Change (PositionChanged|LengthChanged)) == Change (PositionChanged|LengthChanged)) {
299 position_clock.set (_region->position(), true);
300 end_clock.set (_region->position() + _region->length(), true);
301 length_clock.set (_region->length(), true);
302 } else if (what_changed & Change (PositionChanged)) {
303 position_clock.set (_region->position(), true);
304 end_clock.set (_region->position() + _region->length(), true);
305 } else if (what_changed & Change (LengthChanged)) {
306 end_clock.set (_region->position() + _region->length(), true);
307 length_clock.set (_region->length(), true);
310 if (what_changed & Region::SyncOffsetChanged) {
311 sync_offset_clock.set (_region->sync_position(), true);
314 if (what_changed & StartChanged) {
315 start_clock.set (_region->start(), true);
319 void
320 AudioRegionEditor::activation ()
325 void
326 AudioRegionEditor::name_entry_changed ()
328 if (name_entry.get_text() != _region->name()) {
329 _region->set_name (name_entry.get_text());
333 void
334 AudioRegionEditor::audition_state_changed (bool yn)
336 ENSURE_GUI_THREAD (bind (mem_fun(*this, &AudioRegionEditor::audition_state_changed), yn));
338 if (!yn) {
339 audition_button.set_active (false);