enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[ardour.git] / gtk2_ardour / 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 <cmath>
21
22 #include <gtkmm/listviewtext.h>
23
24 #include "pbd/memento_command.h"
25 #include "pbd/stateful_diff_command.h"
26
27 #include "ardour/region.h"
28 #include "ardour/session.h"
29 #include "ardour/source.h"
30
31 #include "ardour_ui.h"
32 #include "clock_group.h"
33 #include "main_clock.h"
34 #include "gui_thread.h"
35 #include "region_editor.h"
36 #include "public_editor.h"
37 #include "tooltips.h"
38
39 #include "pbd/i18n.h"
40
41 using namespace ARDOUR;
42 using namespace PBD;
43 using namespace std;
44 using namespace Gtkmm2ext;
45 using namespace ARDOUR_UI_UTILS;
46
47 RegionEditor::RegionEditor (Session* s, boost::shared_ptr<Region> r)
48         : ArdourDialog (_("Region"))
49         , _table (9, 2)
50         , _table_row (0)
51         , _region (r)
52         , name_label (_("Name:"))
53         , audition_button (_("Audition"))
54         , _clock_group (new ClockGroup)
55         , position_clock (X_("regionposition"), true, "", true, false)
56         , end_clock (X_("regionend"), true, "", true, false)
57         , length_clock (X_("regionlength"), true, "", true, false, true)
58         , sync_offset_relative_clock (X_("regionsyncoffsetrelative"), true, "", true, false)
59         , sync_offset_absolute_clock (X_("regionsyncoffsetabsolute"), true, "", true, false)
60           /* XXX cannot file start yet */
61         , start_clock (X_("regionstart"), true, "", false, false)
62         , _sources (1)
63 {
64         set_session (s);
65
66         _clock_group->set_clock_mode (ARDOUR_UI::instance()->secondary_clock->mode());
67         _clock_group->add (position_clock);
68         _clock_group->add (end_clock);
69         _clock_group->add (length_clock);
70         _clock_group->add (sync_offset_relative_clock);
71         _clock_group->add (sync_offset_absolute_clock);
72         _clock_group->add (start_clock);
73
74         position_clock.set_session (_session);
75         end_clock.set_session (_session);
76         length_clock.set_session (_session);
77         sync_offset_relative_clock.set_session (_session);
78         sync_offset_absolute_clock.set_session (_session);
79         start_clock.set_session (_session);
80
81         set_tooltip (audition_button, _("audition this region"));
82
83         audition_button.unset_flags (Gtk::CAN_FOCUS);
84
85         audition_button.set_events (audition_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
86
87         name_entry.set_name ("RegionEditorEntry");
88         name_label.set_name ("RegionEditorLabel");
89         position_label.set_name ("RegionEditorLabel");
90         position_label.set_text (_("Position:"));
91         end_label.set_name ("RegionEditorLabel");
92         end_label.set_text (_("End:"));
93         length_label.set_name ("RegionEditorLabel");
94         length_label.set_text (_("Length:"));
95         sync_relative_label.set_name ("RegionEditorLabel");
96         sync_relative_label.set_text (_("Sync point (relative to region):"));
97         sync_absolute_label.set_name ("RegionEditorLabel");
98         sync_absolute_label.set_text (_("Sync point (absolute):"));
99         start_label.set_name ("RegionEditorLabel");
100         start_label.set_text (_("File start:"));
101         _sources_label.set_name ("RegionEditorLabel");
102
103         if (_region->n_channels() > 1) {
104                 _sources_label.set_text (_("Sources:"));
105         } else {
106                 _sources_label.set_text (_("Source:"));
107         }
108
109         _table.set_col_spacings (12);
110         _table.set_row_spacings (6);
111         _table.set_border_width (12);
112
113         name_label.set_alignment (1, 0.5);
114         position_label.set_alignment (1, 0.5);
115         end_label.set_alignment (1, 0.5);
116         length_label.set_alignment (1, 0.5);
117         sync_relative_label.set_alignment (1, 0.5);
118         sync_absolute_label.set_alignment (1, 0.5);
119         start_label.set_alignment (1, 0.5);
120         _sources_label.set_alignment (1, 0.5);
121
122         Gtk::HBox* nb = Gtk::manage (new Gtk::HBox);
123         nb->set_spacing (6);
124         nb->pack_start (name_entry);
125         nb->pack_start (audition_button, false, false);
126
127         _table.attach (name_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
128         _table.attach (*nb, 1, 2, _table_row, _table_row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::FILL);
129         ++_table_row;
130
131         _table.attach (position_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
132         _table.attach (position_clock, 1, 2, _table_row, _table_row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::FILL);
133         ++_table_row;
134
135         _table.attach (end_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
136         _table.attach (end_clock, 1, 2, _table_row, _table_row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::FILL);
137         ++_table_row;
138
139         _table.attach (length_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
140         _table.attach (length_clock, 1, 2, _table_row, _table_row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::FILL);
141         ++_table_row;
142
143         _table.attach (sync_relative_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
144         _table.attach (sync_offset_relative_clock, 1, 2, _table_row, _table_row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::FILL);
145         ++_table_row;
146
147         _table.attach (sync_absolute_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
148         _table.attach (sync_offset_absolute_clock, 1, 2, _table_row, _table_row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::FILL);
149         ++_table_row;
150
151         _table.attach (start_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
152         _table.attach (start_clock, 1, 2, _table_row, _table_row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::FILL);
153         ++_table_row;
154
155         _table.attach (_sources_label, 0, 1, _table_row, _table_row + 1, Gtk::FILL, Gtk::FILL);
156         _table.attach (_sources, 1, 2, _table_row, _table_row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::FILL);
157         ++_table_row;
158
159         get_vbox()->pack_start (_table, true, true);
160
161         add_button (Gtk::Stock::CLOSE, Gtk::RESPONSE_ACCEPT);
162
163         set_name ("RegionEditorWindow");
164         add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
165
166         signal_response().connect (sigc::mem_fun (*this, &RegionEditor::handle_response));
167
168         set_title (string_compose (_("Region '%1'"), _region->name()));
169
170         for (uint32_t i = 0; i < _region->n_channels(); ++i) {
171                 _sources.append_text (_region->source(i)->name());
172         }
173
174         _sources.set_headers_visible (false);
175         Gtk::CellRendererText* t = dynamic_cast<Gtk::CellRendererText*> (_sources.get_column_cell_renderer(0));
176         assert (t);
177         t->property_ellipsize() = Pango::ELLIPSIZE_END;
178
179         show_all();
180
181         name_changed ();
182
183         PropertyChange change;
184
185         change.add (ARDOUR::Properties::start);
186         change.add (ARDOUR::Properties::length);
187         change.add (ARDOUR::Properties::position);
188         change.add (ARDOUR::Properties::sync_position);
189
190         bounds_changed (change);
191
192         _region->PropertyChanged.connect (state_connection, invalidator (*this), boost::bind (&RegionEditor::region_changed, this, _1), gui_context());
193
194         spin_arrow_grab = false;
195
196         connect_editor_events ();
197 }
198
199 RegionEditor::~RegionEditor ()
200 {
201         delete _clock_group;
202 }
203
204 void
205 RegionEditor::region_changed (const PBD::PropertyChange& what_changed)
206 {
207         if (what_changed.contains (ARDOUR::Properties::name)) {
208                 name_changed ();
209         }
210
211         PropertyChange interesting_stuff;
212
213         interesting_stuff.add (ARDOUR::Properties::position);
214         interesting_stuff.add (ARDOUR::Properties::length);
215         interesting_stuff.add (ARDOUR::Properties::start);
216         interesting_stuff.add (ARDOUR::Properties::sync_position);
217
218         if (what_changed.contains (interesting_stuff)) {
219                 bounds_changed (what_changed);
220         }
221 }
222
223 gint
224 RegionEditor::bpressed (GdkEventButton* ev, Gtk::SpinButton* /*but*/, void (RegionEditor::*/*pmf*/)())
225 {
226         switch (ev->button) {
227         case 1:
228         case 2:
229         case 3:
230                 if (ev->type == GDK_BUTTON_PRESS) { /* no double clicks here */
231                         if (!spin_arrow_grab) {
232                                 // GTK2FIX probably nuke the region editor
233                                 // if ((ev->window == but->gobj()->panel)) {
234                                 // spin_arrow_grab = true;
235                                 // (this->*pmf)();
236                                 // }
237                         }
238                 }
239                 break;
240         default:
241                 break;
242         }
243         return FALSE;
244 }
245
246 gint
247 RegionEditor::breleased (GdkEventButton* /*ev*/, Gtk::SpinButton* /*but*/, void (RegionEditor::*pmf)())
248 {
249         if (spin_arrow_grab) {
250                 (this->*pmf)();
251                 spin_arrow_grab = false;
252         }
253         return FALSE;
254 }
255
256 void
257 RegionEditor::connect_editor_events ()
258 {
259         name_entry.signal_changed().connect (sigc::mem_fun(*this, &RegionEditor::name_entry_changed));
260
261         position_clock.ValueChanged.connect (sigc::mem_fun(*this, &RegionEditor::position_clock_changed));
262         end_clock.ValueChanged.connect (sigc::mem_fun(*this, &RegionEditor::end_clock_changed));
263         length_clock.ValueChanged.connect (sigc::mem_fun(*this, &RegionEditor::length_clock_changed));
264         sync_offset_absolute_clock.ValueChanged.connect (sigc::mem_fun (*this, &RegionEditor::sync_offset_absolute_clock_changed));
265         sync_offset_relative_clock.ValueChanged.connect (sigc::mem_fun (*this, &RegionEditor::sync_offset_relative_clock_changed));
266
267         audition_button.signal_toggled().connect (sigc::mem_fun(*this, &RegionEditor::audition_button_toggled));
268
269         _session->AuditionActive.connect (audition_connection, invalidator (*this), boost::bind (&RegionEditor::audition_state_changed, this, _1), gui_context());
270 }
271
272 void
273 RegionEditor::position_clock_changed ()
274 {
275         bool in_command = false;
276         boost::shared_ptr<Playlist> pl = _region->playlist();
277
278         if (pl) {
279                 PublicEditor::instance().begin_reversible_command (_("change region start position"));
280                 in_command = true;
281
282                 _region->clear_changes ();
283                 _region->set_position (position_clock.current_time());
284                 _session->add_command(new StatefulDiffCommand (_region));
285         }
286
287         if (in_command) {
288                 PublicEditor::instance().commit_reversible_command ();
289         }
290 }
291
292 void
293 RegionEditor::end_clock_changed ()
294 {
295         bool in_command = false;
296         boost::shared_ptr<Playlist> pl = _region->playlist();
297
298         if (pl) {
299                 PublicEditor::instance().begin_reversible_command (_("change region end position"));
300                 in_command = true;
301
302                 _region->clear_changes ();
303                 _region->trim_end (end_clock.current_time());
304                 _session->add_command(new StatefulDiffCommand (_region));
305         }
306
307         if (in_command) {
308                 PublicEditor::instance().commit_reversible_command ();
309         }
310
311         end_clock.set (_region->position() + _region->length() - 1, true);
312 }
313
314 void
315 RegionEditor::length_clock_changed ()
316 {
317         framecnt_t frames = length_clock.current_time();
318         bool in_command = false;
319         boost::shared_ptr<Playlist> pl = _region->playlist();
320
321         if (pl) {
322                 PublicEditor::instance().begin_reversible_command (_("change region length"));
323                 in_command = true;
324
325                 _region->clear_changes ();
326                 _region->trim_end (_region->position() + frames - 1);
327                 _session->add_command(new StatefulDiffCommand (_region));
328         }
329
330         if (in_command) {
331                 PublicEditor::instance().commit_reversible_command ();
332         }
333
334         length_clock.set (_region->length());
335 }
336
337 void
338 RegionEditor::audition_button_toggled ()
339 {
340         if (audition_button.get_active()) {
341                 _session->audition_region (_region);
342         } else {
343                 _session->cancel_audition ();
344         }
345 }
346
347 void
348 RegionEditor::name_changed ()
349 {
350         if (name_entry.get_text() != _region->name()) {
351                 name_entry.set_text (_region->name());
352         }
353 }
354
355 void
356 RegionEditor::bounds_changed (const PropertyChange& what_changed)
357 {
358         if (what_changed.contains (ARDOUR::Properties::position) && what_changed.contains (ARDOUR::Properties::length)) {
359                 position_clock.set (_region->position(), true);
360                 end_clock.set (_region->position() + _region->length() - 1, true);
361                 length_clock.set (_region->length(), true);
362         } else if (what_changed.contains (ARDOUR::Properties::position)) {
363                 position_clock.set (_region->position(), true);
364                 end_clock.set (_region->position() + _region->length() - 1, true);
365         } else if (what_changed.contains (ARDOUR::Properties::length)) {
366                 end_clock.set (_region->position() + _region->length() - 1, true);
367                 length_clock.set (_region->length(), true);
368         }
369
370         if (what_changed.contains (ARDOUR::Properties::sync_position) || what_changed.contains (ARDOUR::Properties::position)) {
371                 int dir;
372                 frameoffset_t off = _region->sync_offset (dir);
373                 if (dir == -1) {
374                         off = -off;
375                 }
376
377                 if (what_changed.contains (ARDOUR::Properties::sync_position)) {
378                         sync_offset_relative_clock.set (off, true);
379                 }
380
381                 sync_offset_absolute_clock.set (off + _region->position (), true);
382         }
383
384         if (what_changed.contains (ARDOUR::Properties::start)) {
385                 start_clock.set (_region->start(), true);
386         }
387 }
388
389 void
390 RegionEditor::activation ()
391 {
392
393 }
394
395 void
396 RegionEditor::name_entry_changed ()
397 {
398         if (name_entry.get_text() != _region->name()) {
399                 _region->set_name (name_entry.get_text());
400         }
401 }
402
403 void
404 RegionEditor::audition_state_changed (bool yn)
405 {
406         ENSURE_GUI_THREAD (*this, &RegionEditor::audition_state_changed, yn)
407
408         if (!yn) {
409                 audition_button.set_active (false);
410         }
411 }
412
413 void
414 RegionEditor::sync_offset_absolute_clock_changed ()
415 {
416         PublicEditor::instance().begin_reversible_command (_("change region sync point"));
417
418         _region->clear_changes ();
419         _region->set_sync_position (sync_offset_absolute_clock.current_time());
420         _session->add_command (new StatefulDiffCommand (_region));
421
422         PublicEditor::instance().commit_reversible_command ();
423 }
424
425 void
426 RegionEditor::sync_offset_relative_clock_changed ()
427 {
428         PublicEditor::instance().begin_reversible_command (_("change region sync point"));
429
430         _region->clear_changes ();
431         _region->set_sync_position (sync_offset_relative_clock.current_time() + _region->position ());
432         _session->add_command (new StatefulDiffCommand (_region));
433
434         PublicEditor::instance().commit_reversible_command ();
435 }
436
437 bool
438 RegionEditor::on_delete_event (GdkEventAny*)
439 {
440         PropertyChange change;
441
442         change.add (ARDOUR::Properties::start);
443         change.add (ARDOUR::Properties::length);
444         change.add (ARDOUR::Properties::position);
445         change.add (ARDOUR::Properties::sync_position);
446
447         bounds_changed (change);
448
449         return true;
450 }
451
452 void
453 RegionEditor::handle_response (int)
454 {
455         hide ();
456 }