fix rect redraw problem(s); make zoom range rect visible again at the right time...
[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 (3, 2),
51           start_clock (X_("regionstart"), true, X_("AudioRegionEditorClock"), true),
52           end_clock (X_("regionend"), true, X_("AudioRegionEditorClock"), true),
53           length_clock (X_("regionlength"), true, X_("AudioRegionEditorClock"), true, true),
54           sync_offset_clock (X_("regionsyncoffset"), true, X_("AudioRegionEditorClock"), true, true)
55
56 {
57         start_clock.set_session (&_session);
58         end_clock.set_session (&_session);
59         length_clock.set_session (&_session);
60
61         name_entry.set_name ("AudioRegionEditorEntry");
62         name_label.set_name ("AudioRegionEditorLabel");
63
64         name_hbox.set_spacing (5);
65         name_hbox.pack_start (name_label, false, false);
66         name_hbox.pack_start (name_entry, false, false);
67
68         ARDOUR_UI::instance()->tooltips().set_tip (audition_button, _("audition this region"));
69
70         audition_button.unset_flags (Gtk::CAN_FOCUS);
71         
72         audition_button.set_events (audition_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
73
74         top_row_button_hbox.set_border_width (5);
75         top_row_button_hbox.set_spacing (5);
76         top_row_button_hbox.set_homogeneous (false);
77         top_row_button_hbox.pack_end (audition_button, false, false);
78         
79         top_row_hbox.pack_start (name_hbox, true, true);
80         top_row_hbox.pack_end (top_row_button_hbox, true, true);
81
82         start_label.set_name ("AudioRegionEditorLabel");
83         start_label.set_text (_("START:"));
84         end_label.set_name ("AudioRegionEditorLabel");
85         end_label.set_text (_("END:"));
86         length_label.set_name ("AudioRegionEditorLabel");
87         length_label.set_text (_("LENGTH:"));
88         
89         time_table.set_col_spacings (2);
90         time_table.set_row_spacings (5);
91         time_table.set_border_width (5);
92
93         start_alignment.set (1.0, 0.5);
94         end_alignment.set (1.0, 0.5);
95         length_alignment.set (1.0, 0.5);
96
97         start_alignment.add (start_label);
98         end_alignment.add (end_label);
99         length_alignment.add (length_label);
100
101         time_table.attach (start_alignment, 0, 1, 0, 1, Gtk::FILL, Gtk::FILL);
102         time_table.attach (start_clock, 1, 2, 0, 1, Gtk::FILL, Gtk::FILL);
103
104         time_table.attach (end_alignment, 0, 1, 1, 2, Gtk::FILL, Gtk::FILL);
105         time_table.attach (end_clock, 1, 2, 1, 2, Gtk::FILL, Gtk::FILL);
106
107         time_table.attach (length_alignment, 0, 1, 2, 3, Gtk::FILL, Gtk::FILL);
108         time_table.attach (length_clock, 1, 2, 2, 3, Gtk::FILL, Gtk::FILL);
109
110         lower_hbox.pack_start (time_table, true, true);
111         lower_hbox.pack_start (sep1, false, false);
112         lower_hbox.pack_start (sep2, false, false);
113
114         get_vbox()->pack_start (top_row_hbox, true, true);
115         get_vbox()->pack_start (sep3, false, false);
116         get_vbox()->pack_start (lower_hbox, true, true);
117
118         set_name ("AudioRegionEditorWindow");
119         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
120
121         signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), static_cast<Window *> (this)));
122
123         WindowTitle title(string_compose (_("Region %1"), _region->name()));
124         title += Glib::get_application_name();
125
126         set_title (title.get_string());
127
128         show_all();
129
130         name_changed ();
131         bounds_changed (Change (StartChanged|LengthChanged|PositionChanged));
132
133         _region->StateChanged.connect (mem_fun(*this, &AudioRegionEditor::region_changed));
134         
135         spin_arrow_grab = false;
136         
137         connect_editor_events ();
138 }
139
140 AudioRegionEditor::~AudioRegionEditor ()
141 {
142 }
143
144 void
145 AudioRegionEditor::region_changed (Change what_changed)
146 {
147         if (what_changed & NameChanged) {
148                 name_changed ();
149         }
150         if (what_changed & BoundsChanged) {
151                 bounds_changed (what_changed);
152         }
153 }
154
155 gint 
156 AudioRegionEditor::bpressed (GdkEventButton* ev, Gtk::SpinButton* but, void (AudioRegionEditor::*pmf)())
157 {
158         switch (ev->button) {
159         case 1:
160         case 2:
161         case 3:
162                 if (ev->type == GDK_BUTTON_PRESS) { /* no double clicks here */
163                         if (!spin_arrow_grab) {
164                                 // GTK2FIX probably nuke the region editor
165                                 // if ((ev->window == but->gobj()->panel)) {
166                                 // spin_arrow_grab = true;
167                                 // (this->*pmf)();
168                                 // } 
169                         } 
170                 } 
171                 break;
172         default:
173                 break;
174         }
175         return FALSE;
176 }
177
178 gint 
179 AudioRegionEditor::breleased (GdkEventButton* ev, Gtk::SpinButton* but, void (AudioRegionEditor::*pmf)())
180 {
181         if (spin_arrow_grab) {
182                 (this->*pmf)();
183                 spin_arrow_grab = false;
184         }
185         return FALSE;
186 }
187
188 void
189 AudioRegionEditor::connect_editor_events ()
190 {
191         name_entry.signal_changed().connect (mem_fun(*this, &AudioRegionEditor::name_entry_changed));
192
193         start_clock.ValueChanged.connect (mem_fun(*this, &AudioRegionEditor::start_clock_changed));
194         end_clock.ValueChanged.connect (mem_fun(*this, &AudioRegionEditor::end_clock_changed));
195         length_clock.ValueChanged.connect (mem_fun(*this, &AudioRegionEditor::length_clock_changed));
196
197         audition_button.signal_toggled().connect (mem_fun(*this, &AudioRegionEditor::audition_button_toggled));
198         _session.AuditionActive.connect (mem_fun(*this, &AudioRegionEditor::audition_state_changed));
199 }
200
201 void
202 AudioRegionEditor::start_clock_changed ()
203 {
204         _session.begin_reversible_command (_("change region start position"));
205
206         boost::shared_ptr<Playlist> pl = _region->playlist();
207
208         if (pl) {
209                 XMLNode &before = pl->get_state();
210                 _region->set_position (start_clock.current_time(), this);
211                 XMLNode &after = pl->get_state();
212                 _session.add_command(new MementoCommand<Playlist>(*pl, &before, &after));
213         }
214
215         _session.commit_reversible_command ();
216 }
217
218 void
219 AudioRegionEditor::end_clock_changed ()
220 {
221         _session.begin_reversible_command (_("change region end position"));
222
223         boost::shared_ptr<Playlist> pl = _region->playlist();
224         
225         if (pl) {
226                 XMLNode &before = pl->get_state();
227                 _region->trim_end (end_clock.current_time(), this);
228                 XMLNode &after = pl->get_state();
229                 _session.add_command(new MementoCommand<Playlist>(*pl, &before, &after));
230         }
231
232         _session.commit_reversible_command ();
233
234         end_clock.set (_region->position() + _region->length(), true);
235 }
236
237 void
238 AudioRegionEditor::length_clock_changed ()
239 {
240         nframes_t frames = length_clock.current_time();
241         
242         _session.begin_reversible_command (_("change region length"));
243         
244         boost::shared_ptr<Playlist> pl = _region->playlist();
245
246         if (pl) {
247                 XMLNode &before = pl->get_state();
248                 _region->trim_end (_region->position() + frames, this);
249                 XMLNode &after = pl->get_state();
250                 _session.add_command(new MementoCommand<Playlist>(*pl, &before, &after));
251         }
252
253         _session.commit_reversible_command ();
254
255         length_clock.set (_region->length());
256 }
257
258 void
259 AudioRegionEditor::audition_button_toggled ()
260 {
261         if (audition_button.get_active()) {
262                 _session.audition_region (_region);
263         } else {
264                 _session.cancel_audition ();
265         }
266 }
267
268 void
269 AudioRegionEditor::name_changed ()
270 {
271         if (name_entry.get_text() != _region->name()) {
272                 name_entry.set_text (_region->name());
273         }
274 }
275
276 void
277 AudioRegionEditor::bounds_changed (Change what_changed)
278 {
279         if (what_changed & Change ((PositionChanged|LengthChanged))) {
280                 start_clock.set (_region->position(), true);
281                 end_clock.set (_region->position() + _region->length(), true);
282                 length_clock.set (_region->length(), true);
283         }
284 }
285
286 void
287 AudioRegionEditor::activation ()
288 {
289         
290 }       
291
292 void
293 AudioRegionEditor::name_entry_changed ()
294 {
295         if (name_entry.get_text() != _region->name()) {
296                 _region->set_name (name_entry.get_text());
297         }
298 }
299
300 void
301 AudioRegionEditor::audition_state_changed (bool yn)
302 {
303         ENSURE_GUI_THREAD (bind (mem_fun(*this, &AudioRegionEditor::audition_state_changed), yn));
304
305         if (!yn) {
306                 audition_button.set_active (false);
307         }
308 }
309