Fix some unused parameter warnings.
[ardour.git] / gtk2_ardour / audio_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
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>
29
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"
35
36 #include "i18n.h"
37
38 using namespace ARDOUR;
39 using namespace PBD;
40 using namespace sigc;
41 using namespace std;
42 using namespace Gtkmm2ext;
43
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)
57
58 {
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);
64
65         name_entry.set_name ("AudioRegionEditorEntry");
66         name_label.set_name ("AudioRegionEditorLabel");
67
68         name_hbox.set_spacing (5);
69         name_hbox.pack_start (name_label, false, false);
70         name_hbox.pack_start (name_entry, false, false);
71
72         ARDOUR_UI::instance()->tooltips().set_tip (audition_button, _("audition this region"));
73
74         audition_button.unset_flags (Gtk::CAN_FOCUS);
75         
76         audition_button.set_events (audition_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
77
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);
82         
83         top_row_hbox.pack_start (name_hbox, true, true);
84         top_row_hbox.pack_end (top_row_button_hbox, true, true);
85
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:"));
96         
97         time_table.set_col_spacings (2);
98         time_table.set_row_spacings (5);
99         time_table.set_border_width (5);
100
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);
106
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);
112
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);
115
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);
118
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);
121
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);
124
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);
127
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);
131
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);
135
136         set_name ("AudioRegionEditorWindow");
137         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
138
139         signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), static_cast<Window *> (this)));
140
141         WindowTitle title(string_compose (_("Region %1"), _region->name()));
142         title += Glib::get_application_name();
143
144         set_title (title.get_string());
145
146         show_all();
147
148         name_changed ();
149         bounds_changed (Change (StartChanged|LengthChanged|PositionChanged|StartChanged|Region::SyncOffsetChanged));
150
151         _region->StateChanged.connect (mem_fun(*this, &AudioRegionEditor::region_changed));
152         
153         spin_arrow_grab = false;
154         
155         connect_editor_events ();
156 }
157
158 AudioRegionEditor::~AudioRegionEditor ()
159 {
160 }
161
162 void
163 AudioRegionEditor::region_changed (Change what_changed)
164 {
165         if (what_changed & NameChanged) {
166                 name_changed ();
167         }
168
169         if (what_changed & Change (BoundsChanged|StartChanged|Region::SyncOffsetChanged)) {
170                 bounds_changed (what_changed);
171         }
172 }
173
174 gint 
175 AudioRegionEditor::bpressed (GdkEventButton* ev, Gtk::SpinButton* /*but*/, void (AudioRegionEditor::*/*pmf*/)())
176 {
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                                 // } 
188                         } 
189                 } 
190                 break;
191         default:
192                 break;
193         }
194         return FALSE;
195 }
196
197 gint 
198 AudioRegionEditor::breleased (GdkEventButton* /*ev*/, Gtk::SpinButton* /*but*/, void (AudioRegionEditor::*pmf)())
199 {
200         if (spin_arrow_grab) {
201                 (this->*pmf)();
202                 spin_arrow_grab = false;
203         }
204         return FALSE;
205 }
206
207 void
208 AudioRegionEditor::connect_editor_events ()
209 {
210         name_entry.signal_changed().connect (mem_fun(*this, &AudioRegionEditor::name_entry_changed));
211
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));
215
216         audition_button.signal_toggled().connect (mem_fun(*this, &AudioRegionEditor::audition_button_toggled));
217         _session.AuditionActive.connect (mem_fun(*this, &AudioRegionEditor::audition_state_changed));
218 }
219
220 void
221 AudioRegionEditor::position_clock_changed ()
222 {
223         _session.begin_reversible_command (_("change region start position"));
224
225         boost::shared_ptr<Playlist> pl = _region->playlist();
226
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));
232         }
233
234         _session.commit_reversible_command ();
235 }
236
237 void
238 AudioRegionEditor::end_clock_changed ()
239 {
240         _session.begin_reversible_command (_("change region end position"));
241
242         boost::shared_ptr<Playlist> pl = _region->playlist();
243         
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));
249         }
250
251         _session.commit_reversible_command ();
252
253         end_clock.set (_region->position() + _region->length(), true);
254 }
255
256 void
257 AudioRegionEditor::length_clock_changed ()
258 {
259         nframes_t frames = length_clock.current_time();
260         
261         _session.begin_reversible_command (_("change region length"));
262         
263         boost::shared_ptr<Playlist> pl = _region->playlist();
264
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));
270         }
271
272         _session.commit_reversible_command ();
273
274         length_clock.set (_region->length());
275 }
276
277 void
278 AudioRegionEditor::audition_button_toggled ()
279 {
280         if (audition_button.get_active()) {
281                 _session.audition_region (_region);
282         } else {
283                 _session.cancel_audition ();
284         }
285 }
286
287 void
288 AudioRegionEditor::name_changed ()
289 {
290         if (name_entry.get_text() != _region->name()) {
291                 name_entry.set_text (_region->name());
292         }
293 }
294
295 void
296 AudioRegionEditor::bounds_changed (Change what_changed)
297 {
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);
308         }
309
310         if (what_changed & Region::SyncOffsetChanged) {
311                 sync_offset_clock.set (_region->sync_position(), true);
312         }
313
314         if (what_changed & StartChanged) {
315                 start_clock.set (_region->start(), true);
316         }
317 }
318
319 void
320 AudioRegionEditor::activation ()
321 {
322         
323 }       
324
325 void
326 AudioRegionEditor::name_entry_changed ()
327 {
328         if (name_entry.get_text() != _region->name()) {
329                 _region->set_name (name_entry.get_text());
330         }
331 }
332
333 void
334 AudioRegionEditor::audition_state_changed (bool yn)
335 {
336         ENSURE_GUI_THREAD (bind (mem_fun(*this, &AudioRegionEditor::audition_state_changed), yn));
337
338         if (!yn) {
339                 audition_button.set_active (false);
340         }
341 }
342