fix for mis-merge of region dialog gain patch (#2879), from colinf
[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 <ardour/dB.h>
26 #include <gtkmm2ext/utils.h>
27 #include <gtkmm2ext/stop_signal.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 (_("Audition")),
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           gain_adjustment(accurate_coefficient_to_dB(_region->scale_amplitude()), -40.0, +40.0, 0.1, 1.0, 0)
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         gain_label.set_name ("AudioRegionEditorLabel");
129         gain_label.set_text (_("Scale amplitude:"));
130         gain_label.set_alignment (1, 0.5);
131         gain_entry.configure (gain_adjustment, 0.0, 1);
132         time_table.attach (gain_label, 0, 1, 6, 7, Gtk::FILL, Gtk::FILL);
133         time_table.attach (gain_entry, 1, 2, 6, 7, Gtk::FILL, Gtk::FILL);
134         
135         lower_hbox.pack_start (time_table, true, true);
136         lower_hbox.pack_start (sep1, false, false);
137         lower_hbox.pack_start (sep2, false, false);
138
139         get_vbox()->pack_start (top_row_hbox, true, true);
140         get_vbox()->pack_start (sep3, false, false);
141         get_vbox()->pack_start (lower_hbox, true, true);
142
143         set_name ("AudioRegionEditorWindow");
144         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
145
146         signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), static_cast<Window *> (this)));
147
148         set_title (string_compose (_("Region %1"), _region->name()));
149
150         show_all();
151
152         name_changed ();
153         bounds_changed (Change (StartChanged|LengthChanged|PositionChanged|StartChanged|Region::SyncOffsetChanged));
154         gain_changed ();
155
156         _region->StateChanged.connect (mem_fun(*this, &AudioRegionEditor::region_changed));
157         
158         spin_arrow_grab = false;
159         
160         connect_editor_events ();
161 }
162
163 AudioRegionEditor::~AudioRegionEditor ()
164 {
165 }
166
167 void
168 AudioRegionEditor::region_changed (Change what_changed)
169 {
170         if (what_changed & NameChanged) {
171                 name_changed ();
172         }
173
174         if (what_changed & Change (BoundsChanged|StartChanged|Region::SyncOffsetChanged)) {
175                 bounds_changed (what_changed);
176         }
177
178         if (what_changed & AudioRegion::ScaleAmplitudeChanged) {
179                 gain_changed ();
180         }
181 }
182
183 gint 
184 AudioRegionEditor::bpressed (GdkEventButton* ev, Gtk::SpinButton* but, void (AudioRegionEditor::*pmf)())
185 {
186         switch (ev->button) {
187         case 1:
188         case 2:
189         case 3:
190                 if (ev->type == GDK_BUTTON_PRESS) { /* no double clicks here */
191                         if (!spin_arrow_grab) {
192                                 // GTK2FIX probably nuke the region editor
193                                 // if ((ev->window == but->gobj()->panel)) {
194                                 // spin_arrow_grab = true;
195                                 // (this->*pmf)();
196                                 // } 
197                         } 
198                 } 
199                 break;
200         default:
201                 break;
202         }
203         return FALSE;
204 }
205
206 gint 
207 AudioRegionEditor::breleased (GdkEventButton* ev, Gtk::SpinButton* but, void (AudioRegionEditor::*pmf)())
208 {
209         if (spin_arrow_grab) {
210                 (this->*pmf)();
211                 spin_arrow_grab = false;
212         }
213         return FALSE;
214 }
215
216 void
217 AudioRegionEditor::connect_editor_events ()
218 {
219         name_entry.signal_changed().connect (mem_fun(*this, &AudioRegionEditor::name_entry_changed));
220
221         position_clock.ValueChanged.connect (mem_fun(*this, &AudioRegionEditor::position_clock_changed));
222         end_clock.ValueChanged.connect (mem_fun(*this, &AudioRegionEditor::end_clock_changed));
223         length_clock.ValueChanged.connect (mem_fun(*this, &AudioRegionEditor::length_clock_changed));
224         gain_adjustment.signal_value_changed().connect (mem_fun (*this, &AudioRegionEditor::gain_adjustment_changed));
225  
226         audition_button.signal_toggled().connect (mem_fun(*this, &AudioRegionEditor::audition_button_toggled));
227         _session.AuditionActive.connect (mem_fun(*this, &AudioRegionEditor::audition_state_changed));
228 }
229
230 void
231 AudioRegionEditor::position_clock_changed ()
232 {
233         _session.begin_reversible_command (_("change region start position"));
234
235         boost::shared_ptr<Playlist> pl = _region->playlist();
236
237         if (pl) {
238                 XMLNode &before = pl->get_state();
239                 _region->set_position (position_clock.current_time(), this);
240                 XMLNode &after = pl->get_state();
241                 _session.add_command(new MementoCommand<Playlist>(*pl, &before, &after));
242         }
243
244         _session.commit_reversible_command ();
245 }
246
247 void
248 AudioRegionEditor::end_clock_changed ()
249 {
250         _session.begin_reversible_command (_("change region end position"));
251
252         boost::shared_ptr<Playlist> pl = _region->playlist();
253         
254         if (pl) {
255                 XMLNode &before = pl->get_state();
256                 _region->trim_end (end_clock.current_time(), this);
257                 XMLNode &after = pl->get_state();
258                 _session.add_command(new MementoCommand<Playlist>(*pl, &before, &after));
259         }
260
261         _session.commit_reversible_command ();
262
263         end_clock.set (_region->position() + _region->length()-1, true);
264 }
265
266 void
267 AudioRegionEditor::length_clock_changed ()
268 {
269         nframes_t frames = length_clock.current_time();
270         
271         _session.begin_reversible_command (_("change region length"));
272         
273         boost::shared_ptr<Playlist> pl = _region->playlist();
274
275         if (pl) {
276                 XMLNode &before = pl->get_state();
277                 _region->trim_end (_region->position() + frames -1, this);
278                 XMLNode &after = pl->get_state();
279                 _session.add_command(new MementoCommand<Playlist>(*pl, &before, &after));
280         }
281
282         _session.commit_reversible_command ();
283
284         length_clock.set (_region->length());
285 }
286
287 void
288 AudioRegionEditor::gain_changed ()
289 {
290         float const region_gain_dB = accurate_coefficient_to_dB (_region->scale_amplitude());
291         if (region_gain_dB != gain_adjustment.get_value()) {
292                 gain_adjustment.set_value(region_gain_dB);
293         }
294 }
295
296 void
297 AudioRegionEditor::gain_adjustment_changed ()
298 {
299         float const gain = dB_to_coefficient (gain_adjustment.get_value());
300         if (_region->scale_amplitude() != gain) {
301                 _region->set_scale_amplitude (gain);
302         }
303 }
304
305 void
306 AudioRegionEditor::audition_button_toggled ()
307 {
308         if (audition_button.get_active()) {
309                 _session.audition_region (_region);
310         } else {
311                 _session.cancel_audition ();
312         }
313 }
314
315 void
316 AudioRegionEditor::name_changed ()
317 {
318         if (name_entry.get_text() != _region->name()) {
319                 name_entry.set_text (_region->name());
320         }
321 }
322
323 void
324 AudioRegionEditor::bounds_changed (Change what_changed)
325 {
326         if ((what_changed & Change (PositionChanged|LengthChanged)) == Change (PositionChanged|LengthChanged)) {
327                 position_clock.set (_region->position(), true);
328                 end_clock.set (_region->position() + _region->length() -1, true);
329                 length_clock.set (_region->length(), true);
330         } else if (what_changed & Change (PositionChanged)) {
331                 position_clock.set (_region->position(), true);
332                 end_clock.set (_region->position() + _region->length() -1, true);
333         } else if (what_changed & Change (LengthChanged)) {
334                 end_clock.set (_region->position() + _region->length() -1, true);
335                 length_clock.set (_region->length(), true);
336         }
337
338         if (what_changed & Region::SyncOffsetChanged) {
339                 sync_offset_clock.set (_region->sync_position(), true);
340         }
341
342         if (what_changed & StartChanged) {
343                 start_clock.set (_region->start(), true);
344         }
345 }
346
347 void
348 AudioRegionEditor::activation ()
349 {
350         
351 }       
352
353 void
354 AudioRegionEditor::name_entry_changed ()
355 {
356         if (name_entry.get_text() != _region->name()) {
357                 _region->set_name (name_entry.get_text());
358         }
359 }
360
361 void
362 AudioRegionEditor::audition_state_changed (bool yn)
363 {
364         ENSURE_GUI_THREAD (bind (mem_fun(*this, &AudioRegionEditor::audition_state_changed), yn));
365
366         if (!yn) {
367                 audition_button.set_active (false);
368         }
369 }
370