mp4chaps Lua script: don't clutter global environment
[ardour.git] / gtk2_ardour / location_ui.cc
1 /*
2     Copyright (C) 2000 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 #include <cstdlib>
22
23 #include <gtkmm2ext/utils.h>
24
25 #include "ardour/session.h"
26 #include "pbd/memento_command.h"
27 #include "widgets/tooltips.h"
28
29 #include "ardour_ui.h"
30 #include "clock_group.h"
31 #include "enums_convert.h"
32 #include "main_clock.h"
33 #include "gui_thread.h"
34 #include "keyboard.h"
35 #include "location_ui.h"
36 #include "utils.h"
37 #include "public_editor.h"
38 #include "ui_config.h"
39
40 #include "pbd/i18n.h"
41
42 using namespace std;
43 using namespace ARDOUR;
44 using namespace ArdourWidgets;
45 using namespace PBD;
46 using namespace Gtk;
47 using namespace Gtkmm2ext;
48
49 LocationEditRow::LocationEditRow(Session * sess, Location * loc, int32_t num)
50         : SessionHandlePtr (0) /* explicitly set below */
51         , location(0)
52         , item_table (1, 6, false)
53         , start_clock (X_("locationstart"), true, "", true, false)
54         , start_to_playhead_button (_("Use PH"))
55         , locate_to_start_button (_("Goto"))
56         , end_clock (X_("locationend"), true, "", true, false)
57         , end_to_playhead_button (_("Use PH"))
58         , locate_to_end_button (_("Goto"))
59         , length_clock (X_("locationlength"), true, "", true, false, true)
60         , cd_check_button (_("CD"))
61         , hide_check_button (_("Hide"))
62         , lock_check_button (_("Lock"))
63         , glue_check_button (_("Glue"))
64         , _clock_group (0)
65 {
66         i_am_the_modifier = 0;
67
68         remove_button.set_icon (ArdourIcon::CloseCross);
69         remove_button.set_events (remove_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
70
71         number_label.set_name ("LocationEditNumberLabel");
72         name_label.set_name ("LocationEditNameLabel");
73         name_entry.set_name ("LocationEditNameEntry");
74         cd_check_button.set_name ("LocationEditCdButton");
75         hide_check_button.set_name ("LocationEditHideButton");
76         lock_check_button.set_name ("LocationEditLockButton");
77         glue_check_button.set_name ("LocationEditGlueButton");
78         isrc_label.set_name ("LocationEditNumberLabel");
79         isrc_entry.set_name ("LocationEditNameEntry");
80         scms_check_button.set_name ("LocationEditCdButton");
81         preemph_check_button.set_name ("LocationEditCdButton");
82         performer_label.set_name ("LocationEditNumberLabel");
83         performer_entry.set_name ("LocationEditNameEntry");
84         composer_label.set_name ("LocationEditNumberLabel");
85         composer_entry.set_name ("LocationEditNameEntry");
86
87         isrc_label.set_text (X_("ISRC:"));
88         performer_label.set_text (_("Performer:"));
89         composer_label.set_text (_("Composer:"));
90         scms_label.set_text (X_("SCMS"));
91         preemph_label.set_text (_("Pre-Emphasis"));
92
93         isrc_entry.set_size_request (112, -1);
94         isrc_entry.set_max_length(12);
95         isrc_entry.set_editable (true);
96
97         performer_entry.set_size_request (100, -1);
98         performer_entry.set_editable (true);
99
100         composer_entry.set_size_request (100, -1);
101         composer_entry.set_editable (true);
102
103         name_label.set_alignment (0, 0.5);
104
105         Gtk::HBox* front_spacing = manage (new HBox);
106         front_spacing->set_size_request (20, -1);
107         Gtk::HBox* mid_spacing = manage (new HBox);
108         mid_spacing->set_size_request (20, -1);
109
110         cd_track_details_hbox.set_spacing (4);
111         cd_track_details_hbox.pack_start (*front_spacing, false, false);
112         cd_track_details_hbox.pack_start (isrc_label, false, false);
113         cd_track_details_hbox.pack_start (isrc_entry, false, false);
114         cd_track_details_hbox.pack_start (performer_label, false, false);
115         cd_track_details_hbox.pack_start (performer_entry, true, true);
116         cd_track_details_hbox.pack_start (composer_label, false, false);
117         cd_track_details_hbox.pack_start (composer_entry, true, true);
118         cd_track_details_hbox.pack_start (*mid_spacing, false, false);
119         cd_track_details_hbox.pack_start (scms_label, false, false);
120         cd_track_details_hbox.pack_start (scms_check_button, false, false);
121         cd_track_details_hbox.pack_start (preemph_label, false, false);
122         cd_track_details_hbox.pack_start (preemph_check_button, false, false);
123
124         isrc_entry.signal_changed().connect (sigc::mem_fun(*this, &LocationEditRow::isrc_entry_changed));
125         performer_entry.signal_changed().connect (sigc::mem_fun(*this, &LocationEditRow::performer_entry_changed));
126         composer_entry.signal_changed().connect (sigc::mem_fun(*this, &LocationEditRow::composer_entry_changed));
127         scms_check_button.signal_toggled().connect(sigc::mem_fun(*this, &LocationEditRow::scms_toggled));
128         preemph_check_button.signal_toggled().connect(sigc::mem_fun(*this, &LocationEditRow::preemph_toggled));
129
130         set_session (sess);
131
132         start_hbox.set_spacing (2);
133         start_hbox.pack_start (locate_to_start_button, false, false);
134         start_hbox.pack_start (start_clock, false, false);
135         start_hbox.pack_start (start_to_playhead_button, false, false);
136
137         /* this is always in this location, no matter what the location is */
138
139         item_table.attach (remove_button, 8, 9, 0, 1, SHRINK, SHRINK, 4, 1);
140         item_table.attach (start_hbox, 0, 1, 0, 1, FILL, Gtk::AttachOptions(0), 4, 0);
141
142         start_to_playhead_button.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &LocationEditRow::to_playhead_button_pressed), LocStart));
143         locate_to_start_button.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &LocationEditRow::locate_button_pressed), LocStart));
144         start_clock.ValueChanged.connect (sigc::bind (sigc::mem_fun (*this, &LocationEditRow::clock_changed), LocStart));
145         start_clock.signal_button_press_event().connect (sigc::bind (sigc::mem_fun (*this, &LocationEditRow::locate_to_clock), &start_clock), false);
146
147         end_hbox.set_spacing (2);
148         end_hbox.pack_start (locate_to_end_button, false, false);
149         end_hbox.pack_start (end_clock, false, false);
150         end_hbox.pack_start (end_to_playhead_button, false, false);
151
152         end_to_playhead_button.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &LocationEditRow::to_playhead_button_pressed), LocEnd));
153         locate_to_end_button.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &LocationEditRow::locate_button_pressed), LocEnd));
154         end_clock.ValueChanged.connect (sigc::bind (sigc::mem_fun (*this, &LocationEditRow::clock_changed), LocEnd));
155         end_clock.signal_button_press_event().connect (sigc::bind (sigc::mem_fun (*this, &LocationEditRow::locate_to_clock), &end_clock), false);
156
157         length_clock.ValueChanged.connect (sigc::bind ( sigc::mem_fun(*this, &LocationEditRow::clock_changed), LocLength));
158
159         cd_check_button.signal_toggled().connect(sigc::mem_fun(*this, &LocationEditRow::cd_toggled));
160         hide_check_button.signal_toggled().connect(sigc::mem_fun(*this, &LocationEditRow::hide_toggled));
161         lock_check_button.signal_toggled().connect(sigc::mem_fun(*this, &LocationEditRow::lock_toggled));
162         glue_check_button.signal_toggled().connect(sigc::mem_fun(*this, &LocationEditRow::glue_toggled));
163
164         remove_button.signal_clicked.connect(sigc::mem_fun(*this, &LocationEditRow::remove_button_pressed));
165
166         pack_start(item_table, true, true);
167
168         set_location (loc);
169         set_number (num);
170         cd_toggled(); // show/hide cd-track details
171 }
172
173 LocationEditRow::~LocationEditRow()
174 {
175         if (location) {
176                 connections.drop_connections ();
177         }
178
179         if (_clock_group) {
180                 _clock_group->remove (start_clock);
181                 _clock_group->remove (end_clock);
182                 _clock_group->remove (length_clock);
183         }
184 }
185
186 void
187 LocationEditRow::set_clock_group (ClockGroup& cg)
188 {
189         if (_clock_group) {
190                 _clock_group->remove (start_clock);
191                 _clock_group->remove (end_clock);
192                 _clock_group->remove (length_clock);
193         }
194
195         _clock_group = &cg;
196
197         _clock_group->add (start_clock);
198         _clock_group->add (end_clock);
199         _clock_group->add (length_clock);
200 }
201
202 void
203 LocationEditRow::set_session (Session *sess)
204 {
205         SessionHandlePtr::set_session (sess);
206
207         if (!_session) {
208                 return;
209         }
210
211         start_clock.set_session (_session);
212         end_clock.set_session (_session);
213         length_clock.set_session (_session);
214 }
215
216 void
217 LocationEditRow::set_number (int num)
218 {
219         number = num;
220
221         if (number >= 0 ) {
222                 number_label.set_text (string_compose ("%1", number));
223         }
224 }
225
226 void
227 LocationEditRow::set_location (Location *loc)
228 {
229         if (location) {
230                 connections.drop_connections ();
231         }
232
233         location = loc;
234
235         if (!location) {
236                 return;
237         }
238
239         ++i_am_the_modifier;
240
241         if (!hide_check_button.get_parent()) {
242                 item_table.attach (hide_check_button, 5, 6, 0, 1, FILL, Gtk::FILL, 4, 0);
243                 item_table.attach (lock_check_button, 6, 7, 0, 1, FILL, Gtk::FILL, 4, 0);
244                 item_table.attach (glue_check_button, 7, 8, 0, 1, FILL, Gtk::FILL, 4, 0);
245         }
246         hide_check_button.set_active (location->is_hidden());
247         lock_check_button.set_active (location->locked());
248         glue_check_button.set_active (location->position_lock_style() == MusicTime);
249
250         if (location->is_auto_loop() || location-> is_auto_punch()) {
251                 // use label instead of entry
252
253                 name_label.set_text (location->name());
254                 name_label.set_size_request (80, -1);
255
256                 remove_button.hide ();
257
258                 if (!name_label.get_parent()) {
259                         item_table.attach (name_label, 2, 3, 0, 1, EXPAND|FILL, FILL, 4, 0);
260                 }
261
262                 name_label.show();
263
264         } else {
265
266                 name_entry.set_text (location->name());
267                 name_entry.set_size_request (100, -1);
268                 name_entry.set_editable (true);
269                 name_entry.signal_changed().connect (sigc::mem_fun(*this, &LocationEditRow::name_entry_changed));
270
271                 if (!name_entry.get_parent()) {
272                         item_table.attach (name_entry, 2, 3, 0, 1, FILL | EXPAND, FILL, 4, 0);
273                 }
274                 name_entry.show();
275
276                 if (!cd_check_button.get_parent()) {
277                         item_table.attach (cd_check_button, 4, 5, 0, 1, FILL, Gtk::AttachOptions (0), 4, 0);
278                 }
279
280                 if (location->is_session_range()) {
281                         remove_button.set_sensitive (false);
282                 }
283
284                 cd_check_button.set_active (location->is_cd_marker());
285                 cd_check_button.show();
286
287                 if (location->start() == _session->current_start_frame()) {
288                         cd_check_button.set_sensitive (false);
289                 } else {
290                         cd_check_button.set_sensitive (true);
291                 }
292
293                 hide_check_button.show();
294                 lock_check_button.show();
295                 glue_check_button.show();
296         }
297
298         start_clock.set (location->start(), true);
299
300
301         if (!location->is_mark()) {
302                 if (!end_hbox.get_parent()) {
303                         item_table.attach (end_hbox, 1, 2, 0, 1, FILL, Gtk::AttachOptions (0), 4, 0);
304                 }
305                 if (!length_clock.get_parent()) {
306                         end_hbox.pack_start (length_clock, false, false, 4);
307                 }
308
309                 end_clock.set (location->end(), true);
310                 length_clock.set (location->length(), true);
311
312                 end_clock.show();
313                 length_clock.show();
314
315                 if (location->is_cd_marker()) {
316                         show_cd_track_details ();
317                 }
318
319                 set_tooltip (&remove_button, _("Remove this range"));
320                 set_tooltip (start_clock, _("Start time - middle click to locate here"));
321                 set_tooltip (end_clock, _("End time - middle click to locate here"));
322                 set_tooltip (length_clock, _("Length"));
323
324                 set_tooltip (&start_to_playhead_button, _("Set range start from playhead location"));
325                 set_tooltip (&end_to_playhead_button, _("Set range end from playhead location"));
326
327         } else {
328
329                 set_tooltip (&remove_button, _("Remove this marker"));
330                 set_tooltip (start_clock, _("Position - middle click to locate here"));
331
332                 set_tooltip (&start_to_playhead_button, _("Set marker time from playhead location"));
333
334                 end_clock.hide();
335                 length_clock.hide();
336         }
337
338         set_clock_editable_status ();
339
340         --i_am_the_modifier;
341
342         /* connect to per-location signals, since this row only cares about this location */
343
344         location->NameChanged.connect (connections, invalidator (*this), boost::bind (&LocationEditRow::name_changed, this), gui_context());
345         location->StartChanged.connect (connections, invalidator (*this), boost::bind (&LocationEditRow::start_changed, this), gui_context());
346         location->EndChanged.connect (connections, invalidator (*this), boost::bind (&LocationEditRow::end_changed, this), gui_context());
347         location->Changed.connect (connections, invalidator (*this), boost::bind (&LocationEditRow::location_changed, this), gui_context());
348         location->FlagsChanged.connect (connections, invalidator (*this), boost::bind (&LocationEditRow::flags_changed, this), gui_context());
349         location->LockChanged.connect (connections, invalidator (*this), boost::bind (&LocationEditRow::lock_changed, this), gui_context());
350         location->PositionLockStyleChanged.connect (connections, invalidator (*this), boost::bind (&LocationEditRow::position_lock_style_changed, this), gui_context());
351 }
352
353 void
354 LocationEditRow::name_entry_changed ()
355 {
356         ENSURE_GUI_THREAD (*this, &LocationEditRow::name_entry_changed);
357
358         if (i_am_the_modifier || !location) {
359                 return;
360         }
361
362         location->set_name (name_entry.get_text());
363 }
364
365
366 void
367 LocationEditRow::isrc_entry_changed ()
368 {
369         ENSURE_GUI_THREAD (*this, &LocationEditRow::isrc_entry_changed);
370
371         if (i_am_the_modifier || !location) return;
372
373         if (isrc_entry.get_text() != "" ) {
374
375                 location->cd_info["isrc"] = isrc_entry.get_text();
376
377         } else {
378                 location->cd_info.erase("isrc");
379         }
380 }
381
382 void
383 LocationEditRow::performer_entry_changed ()
384 {
385         ENSURE_GUI_THREAD (*this, &LocationEditRow::performer_entry_changed);
386
387         if (i_am_the_modifier || !location) return;
388
389         if (performer_entry.get_text() != "") {
390                 location->cd_info["performer"] = performer_entry.get_text();
391         } else {
392                 location->cd_info.erase("performer");
393         }
394 }
395
396 void
397 LocationEditRow::composer_entry_changed ()
398 {
399         ENSURE_GUI_THREAD (*this, &LocationEditRow::composer_entry_changed);
400
401         if (i_am_the_modifier || !location) return;
402
403         if (composer_entry.get_text() != "") {
404                 location->cd_info["composer"] = composer_entry.get_text();
405         } else {
406                 location->cd_info.erase("composer");
407         }
408 }
409
410 void
411 LocationEditRow::to_playhead_button_pressed (LocationPart part)
412 {
413         if (!location) {
414                 return;
415         }
416
417         const int32_t divisions = PublicEditor::instance().get_grid_music_divisions (0);
418
419         switch (part) {
420                 case LocStart:
421                         location->set_start (_session->transport_frame (), false, true, divisions);
422                         break;
423                 case LocEnd:
424                         location->set_end (_session->transport_frame (), false, true,divisions);
425                         if (location->is_session_range()) {
426                                 _session->set_end_is_free (false);
427                         }
428                         break;
429                 default:
430                         break;
431         }
432 }
433
434 void
435 LocationEditRow::locate_button_pressed (LocationPart part)
436 {
437         switch (part) {
438                 case LocStart:
439                         _session->request_locate (start_clock.current_time());
440                         break;
441                 case LocEnd:
442                         _session->request_locate (end_clock.current_time());
443                         break;
444                 default:
445                         break;
446         }
447 }
448
449 bool
450 LocationEditRow::locate_to_clock (GdkEventButton* ev, AudioClock* clock)
451 {
452         if (Keyboard::is_button2_event (ev)) {
453                 _session->request_locate (clock->current_time());
454                 return true;
455         }
456         return false;
457 }
458
459 void
460 LocationEditRow::clock_changed (LocationPart part)
461 {
462         if (i_am_the_modifier || !location) {
463                 return;
464         }
465
466         const int32_t divisions = PublicEditor::instance().get_grid_music_divisions (0);
467
468         switch (part) {
469                 case LocStart:
470                         location->set_start (start_clock.current_time(), false, true, divisions);
471                         break;
472                 case LocEnd:
473                         location->set_end (end_clock.current_time(), false, true, divisions);
474                         if (location->is_session_range()) {
475                                 _session->set_end_is_free (false);
476                         }
477                         break;
478                 case LocLength:
479                         location->set_end (location->start() + length_clock.current_duration(), false, true, divisions);
480                         if (location->is_session_range()) {
481                                 _session->set_end_is_free (false);
482                         }
483                 default:
484                         break;
485         }
486 }
487
488 void
489 LocationEditRow::show_cd_track_details ()
490 {
491         if (location->cd_info.find("isrc") != location->cd_info.end()) {
492                 isrc_entry.set_text(location->cd_info["isrc"]);
493         }
494         if (location->cd_info.find("performer") != location->cd_info.end()) {
495                 performer_entry.set_text(location->cd_info["performer"]);
496         }
497         if (location->cd_info.find("composer") != location->cd_info.end()) {
498                 composer_entry.set_text(location->cd_info["composer"]);
499         }
500         if (location->cd_info.find("scms") != location->cd_info.end()) {
501                 scms_check_button.set_active(true);
502         }
503         if (location->cd_info.find("preemph") != location->cd_info.end()) {
504                 preemph_check_button.set_active(true);
505         }
506
507
508         if (!cd_track_details_hbox.get_parent()) {
509                 item_table.attach (cd_track_details_hbox, 0, 7, 1, 2, FILL | EXPAND, FILL, 4, 0);
510         }
511         // item_table.resize(2, 7);
512         cd_track_details_hbox.show_all();
513 }
514
515 void
516 LocationEditRow::cd_toggled ()
517 {
518         if (i_am_the_modifier || !location) {
519                 return;
520         }
521
522         //if (cd_check_button.get_active() == location->is_cd_marker()) {
523         //      return;
524         //}
525
526         if (cd_check_button.get_active()) {
527                 if (location->start() <= _session->current_start_frame()) {
528                         error << _("You cannot put a CD marker at the start of the session") << endmsg;
529                         cd_check_button.set_active (false);
530                         return;
531                 }
532         }
533
534         location->set_cd (cd_check_button.get_active(), this);
535
536         if (location->is_cd_marker()) {
537
538                 show_cd_track_details ();
539
540         } else if (cd_track_details_hbox.get_parent()){
541
542                 item_table.remove (cd_track_details_hbox);
543                 //        item_table.resize(1, 7);
544                 redraw_ranges(); /*     EMIT_SIGNAL */
545         }
546 }
547
548 void
549 LocationEditRow::hide_toggled ()
550 {
551         if (i_am_the_modifier || !location) {
552                 return;
553         }
554
555         location->set_hidden (hide_check_button.get_active(), this);
556 }
557
558 void
559 LocationEditRow::lock_toggled ()
560 {
561         if (i_am_the_modifier || !location) {
562                 return;
563         }
564
565         if (location->locked()) {
566                 location->unlock ();
567         } else {
568                 location->lock ();
569         }
570 }
571
572 void
573 LocationEditRow::glue_toggled ()
574 {
575         if (i_am_the_modifier || !location) {
576                 return;
577         }
578
579         if (location->position_lock_style() == AudioTime) {
580                 location->set_position_lock_style (MusicTime);
581         } else {
582                 location->set_position_lock_style (AudioTime);
583         }
584 }
585
586 void
587 LocationEditRow::remove_button_pressed ()
588 {
589         if (!location) {
590                 return;
591         }
592
593         remove_requested (location); /* EMIT_SIGNAL */
594 }
595
596
597
598 void
599 LocationEditRow::scms_toggled ()
600 {
601         if (i_am_the_modifier || !location) return;
602
603         if (scms_check_button.get_active()) {
604           location->cd_info["scms"] = "on";
605         } else {
606           location->cd_info.erase("scms");
607         }
608
609 }
610
611 void
612 LocationEditRow::preemph_toggled ()
613 {
614         if (i_am_the_modifier || !location) return;
615
616         if (preemph_check_button.get_active()) {
617           location->cd_info["preemph"] = "on";
618         } else {
619           location->cd_info.erase("preemph");
620         }
621 }
622
623 void
624 LocationEditRow::end_changed ()
625 {
626         ENSURE_GUI_THREAD (*this, &LocationEditRow::end_changed, loc)
627
628         if (!location) return;
629
630         // update end and length
631         i_am_the_modifier++;
632
633         end_clock.set (location->end());
634         length_clock.set (location->length());
635
636         i_am_the_modifier--;
637 }
638
639 void
640 LocationEditRow::start_changed ()
641 {
642         if (!location) return;
643
644         // update end and length
645         i_am_the_modifier++;
646
647         start_clock.set (location->start());
648
649         if (location->start() == _session->current_start_frame()) {
650                 cd_check_button.set_sensitive (false);
651         } else {
652                 cd_check_button.set_sensitive (true);
653         }
654
655         i_am_the_modifier--;
656 }
657
658 void
659 LocationEditRow::name_changed ()
660 {
661         if (!location) return;
662
663         // update end and length
664         i_am_the_modifier++;
665
666         name_entry.set_text(location->name());
667         name_label.set_text(location->name());
668
669         i_am_the_modifier--;
670
671 }
672
673 void
674 LocationEditRow::location_changed ()
675 {
676
677         if (!location) return;
678
679         i_am_the_modifier++;
680
681         start_clock.set (location->start());
682         end_clock.set (location->end());
683         length_clock.set (location->length());
684
685         set_clock_editable_status ();
686
687         i_am_the_modifier--;
688
689 }
690
691 void
692 LocationEditRow::flags_changed ()
693 {
694         if (!location) {
695                 return;
696         }
697
698         i_am_the_modifier++;
699
700         cd_check_button.set_active (location->is_cd_marker());
701         hide_check_button.set_active (location->is_hidden());
702         glue_check_button.set_active (location->position_lock_style() == MusicTime);
703
704         i_am_the_modifier--;
705 }
706
707 void
708 LocationEditRow::lock_changed ()
709 {
710         if (!location) {
711                 return;
712         }
713
714         i_am_the_modifier++;
715
716         lock_check_button.set_active (location->locked());
717
718         set_clock_editable_status ();
719
720         i_am_the_modifier--;
721 }
722
723 void
724 LocationEditRow::position_lock_style_changed ()
725 {
726         if (!location) {
727                 return;
728         }
729
730         i_am_the_modifier++;
731
732         glue_check_button.set_active (location->position_lock_style() == MusicTime);
733
734         i_am_the_modifier--;
735 }
736
737 void
738 LocationEditRow::focus_name()
739 {
740         name_entry.grab_focus ();
741 }
742
743 void
744 LocationEditRow::set_clock_editable_status ()
745 {
746         start_clock.set_editable (!location->locked());
747         end_clock.set_editable (!location->locked());
748         length_clock.set_editable (!location->locked());
749 }
750
751 /*------------------------------------------------------------------------*/
752
753 LocationUI::LocationUI (std::string state_node_name)
754         : add_location_button (_("New Marker"))
755         , add_range_button (_("New Range"))
756         , _mode (AudioClock::Frames)
757         , _mode_set (false)
758         , _state_node_name (state_node_name)
759 {
760         i_am_the_modifier = 0;
761
762         _clock_group = new ClockGroup;
763
764         VBox* vbox = manage (new VBox);
765
766         Table* table = manage (new Table (2, 2));
767         table->set_spacings (2);
768         table->set_col_spacing (0, 32);
769         int table_row = 0;
770
771         Label* l = manage (new Label (_("<b>Loop/Punch Ranges</b>")));
772         l->set_alignment (0, 0.5);
773         l->set_use_markup (true);
774         table->attach (*l, 0, 2, table_row, table_row + 1);
775         ++table_row;
776
777         loop_edit_row.set_clock_group (*_clock_group);
778         punch_edit_row.set_clock_group (*_clock_group);
779
780         loop_punch_box.set_border_width (6); // 5 + 1 px framebox-border
781         loop_punch_box.pack_start (loop_edit_row, false, false);
782         loop_punch_box.pack_start (punch_edit_row, false, false);
783
784         table->attach (loop_punch_box, 1, 2, table_row, table_row + 1);
785         ++table_row;
786
787         vbox->pack_start (*table, false, false);
788
789         table = manage (new Table (3, 2));
790         table->set_spacings (2);
791         table->set_col_spacing (0, 32);
792         table_row = 0;
793
794         table->attach (*manage (new Label ("")), 0, 2, table_row, table_row + 1, Gtk::SHRINK, Gtk::SHRINK);
795         ++table_row;
796
797         l = manage (new Label (_("<b>Markers (Including CD Index)</b>")));
798         l->set_alignment (0, 0.5);
799         l->set_use_markup (true);
800         table->attach (*l, 0, 2, table_row, table_row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK);
801         ++table_row;
802
803         location_rows.set_name("LocationLocRows");
804         location_rows_scroller.add (location_rows);
805         location_rows_scroller.set_name ("LocationLocRowsScroller");
806         location_rows_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
807         location_rows_scroller.set_size_request (-1, 130);
808
809         newest_location = 0;
810
811         loc_frame_box.set_spacing (5);
812         loc_frame_box.set_border_width (5);
813         loc_frame_box.set_name("LocationFrameBox");
814
815         loc_frame_box.pack_start (location_rows_scroller, true, true);
816
817         add_location_button.set_name ("LocationAddLocationButton");
818
819         table->attach (loc_frame_box, 0, 2, table_row, table_row + 1);
820         ++table_row;
821
822         loc_range_panes.add (*table);
823
824         table = manage (new Table (3, 2));
825         table->set_spacings (2);
826         table->set_col_spacing (0, 32);
827         table_row = 0;
828
829         table->attach (*manage (new Label ("")), 0, 2, table_row, table_row + 1, Gtk::SHRINK, Gtk::SHRINK);
830         ++table_row;
831
832         l = manage (new Label (_("<b>Ranges (Including CD Track Ranges)</b>")));
833         l->set_alignment (0, 0.5);
834         l->set_use_markup (true);
835         table->attach (*l, 0, 2, table_row, table_row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK);
836         ++table_row;
837
838         range_rows.set_name("LocationRangeRows");
839         range_rows_scroller.add (range_rows);
840         range_rows_scroller.set_name ("LocationRangeRowsScroller");
841         range_rows_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
842         range_rows_scroller.set_size_request (-1, 130);
843
844         range_frame_box.set_spacing (5);
845         range_frame_box.set_name("LocationFrameBox");
846         range_frame_box.set_border_width (5);
847         range_frame_box.pack_start (range_rows_scroller, true, true);
848
849         add_range_button.set_name ("LocationAddRangeButton");
850
851         table->attach (range_frame_box, 0, 2, table_row, table_row + 1);
852         ++table_row;
853
854         loc_range_panes.add (*table);
855
856         HBox* add_button_box = manage (new HBox);
857         add_button_box->pack_start (add_location_button, true, true);
858         add_button_box->pack_start (add_range_button, true, true);
859
860         vbox->pack_start (loc_range_panes, true, true);
861         vbox->pack_start (*add_button_box, false, false);
862
863         pack_start (*vbox);
864
865         add_location_button.signal_clicked().connect (sigc::mem_fun(*this, &LocationUI::add_new_location));
866         add_range_button.signal_clicked().connect (sigc::mem_fun(*this, &LocationUI::add_new_range));
867
868         show_all ();
869
870         signal_map().connect (sigc::mem_fun (*this, &LocationUI::refresh_location_list));
871 }
872
873 LocationUI::~LocationUI()
874 {
875         loop_edit_row.unset_clock_group ();
876         punch_edit_row.unset_clock_group ();
877         delete _clock_group;
878 }
879
880 gint
881 LocationUI::do_location_remove (ARDOUR::Location *loc)
882 {
883         /* this is handled internally by Locations, but there's
884            no point saving state etc. when we know the marker
885            cannot be removed.
886         */
887
888         if (loc->is_session_range()) {
889                 return FALSE;
890         }
891
892         PublicEditor::instance().begin_reversible_command (_("remove marker"));
893         XMLNode &before = _session->locations()->get_state();
894         _session->locations()->remove (loc);
895         XMLNode &after = _session->locations()->get_state();
896         _session->add_command(new MementoCommand<Locations>(*(_session->locations()), &before, &after));
897         PublicEditor::instance().commit_reversible_command ();
898
899         return FALSE;
900 }
901
902 void
903 LocationUI::location_remove_requested (ARDOUR::Location *loc)
904 {
905         // must do this to prevent problems when destroying
906         // the effective sender of this event
907
908         Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &LocationUI::do_location_remove), loc));
909 }
910
911
912 void
913 LocationUI::location_redraw_ranges ()
914 {
915         range_rows.hide();
916         range_rows.show();
917 }
918
919 struct LocationSortByStart {
920         bool operator() (Location *a, Location *b) {
921                 return a->start() < b->start();
922         }
923 };
924
925 void
926 LocationUI::location_added (Location* location)
927 {
928         if (location->is_auto_punch()) {
929                 punch_edit_row.set_location(location);
930         } else if (location->is_auto_loop()) {
931                 loop_edit_row.set_location(location);
932         } else if (location->is_range_marker() || location->is_mark()) {
933                 Locations::LocationList loc = _session->locations()->list ();
934                 loc.sort (LocationSortByStart ());
935
936                 LocationEditRow* erow = manage (new LocationEditRow (_session, location));
937
938                 erow->set_clock_group (*_clock_group);
939                 erow->remove_requested.connect (sigc::mem_fun (*this, &LocationUI::location_remove_requested));
940
941                 Box_Helpers::BoxList & children = location->is_range_marker() ? range_rows.children () : location_rows.children ();
942
943                 /* Step through the location list and the GUI list to find the place to insert */
944                 Locations::LocationList::iterator i = loc.begin ();
945                 Box_Helpers::BoxList::iterator j = children.begin ();
946                 while (i != loc.end()) {
947
948                         if (location->flags() != (*i)->flags()) {
949                                 /* Skip locations in the session list that aren't of the right type */
950                                 ++i;
951                                 continue;
952                         }
953
954                         if (*i == location) {
955                                 children.insert (j, Box_Helpers::Element (*erow, PACK_SHRINK, 1, PACK_START));
956                                 break;
957                         }
958
959                         ++i;
960
961                         if (j != children.end()) {
962                                 ++j;
963                         }
964                 }
965
966                 range_rows.show_all ();
967                 location_rows.show_all ();
968
969                 if (location == newest_location) {
970                         newest_location = 0;
971                         erow->focus_name();
972                 }
973         }
974 }
975
976 void
977 LocationUI::location_removed (Location* location)
978 {
979         ENSURE_GUI_THREAD (*this, &LocationUI::location_removed, location)
980
981         if (location->is_auto_punch()) {
982                 punch_edit_row.set_location(0);
983         } else if (location->is_auto_loop()) {
984                 loop_edit_row.set_location(0);
985         } else if (location->is_range_marker() || location->is_mark()) {
986                 Box_Helpers::BoxList& children = location->is_range_marker() ? range_rows.children () : location_rows.children ();
987                 for (Box_Helpers::BoxList::iterator i = children.begin(); i != children.end(); ++i) {
988                         LocationEditRow* r = dynamic_cast<LocationEditRow*> (i->get_widget());
989                         if (r && r->get_location() == location) {
990                                 children.erase (i);
991                                 break;
992                         }
993                 }
994         }
995 }
996
997 void
998 LocationUI::map_locations (const Locations::LocationList& locations)
999 {
1000         Locations::LocationList::iterator i;
1001         gint n;
1002         int mark_n = 0;
1003         Locations::LocationList temp = locations;
1004         LocationSortByStart cmp;
1005
1006         temp.sort (cmp);
1007
1008         for (n = 0, i = temp.begin(); i != temp.end(); ++n, ++i) {
1009
1010                 Location* location = *i;
1011
1012                 if (location->is_mark()) {
1013                         LocationEditRow* erow = manage (new LocationEditRow (_session, location, mark_n));
1014
1015                         erow->set_clock_group (*_clock_group);
1016                         erow->remove_requested.connect (sigc::mem_fun(*this, &LocationUI::location_remove_requested));
1017                         erow->redraw_ranges.connect (sigc::mem_fun(*this, &LocationUI::location_redraw_ranges));
1018
1019                         Box_Helpers::BoxList & loc_children = location_rows.children();
1020                         loc_children.push_back(Box_Helpers::Element(*erow, PACK_SHRINK, 1, PACK_START));
1021                 } else if (location->is_auto_punch()) {
1022                         punch_edit_row.set_session (_session);
1023                         punch_edit_row.set_location (location);
1024                         punch_edit_row.show_all();
1025                 } else if (location->is_auto_loop()) {
1026                         loop_edit_row.set_session (_session);
1027                         loop_edit_row.set_location (location);
1028                         loop_edit_row.show_all();
1029                 } else {
1030                         LocationEditRow* erow = manage (new LocationEditRow(_session, location));
1031
1032                         erow->set_clock_group (*_clock_group);
1033                         erow->remove_requested.connect (sigc::mem_fun(*this, &LocationUI::location_remove_requested));
1034
1035                         Box_Helpers::BoxList & range_children = range_rows.children();
1036                         range_children.push_back(Box_Helpers::Element(*erow,  PACK_SHRINK, 1, PACK_START));
1037                 }
1038         }
1039
1040         range_rows.show_all();
1041         location_rows.show_all();
1042 }
1043
1044 void
1045 LocationUI::add_new_location()
1046 {
1047         string markername;
1048
1049         if (_session) {
1050                 framepos_t where = _session->audible_frame();
1051                 _session->locations()->next_available_name(markername,"mark");
1052                 Location *location = new Location (*_session, where, where, markername, Location::IsMark);
1053                 if (UIConfiguration::instance().get_name_new_markers()) {
1054                         newest_location = location;
1055                 }
1056                 PublicEditor::instance().begin_reversible_command (_("add marker"));
1057                 XMLNode &before = _session->locations()->get_state();
1058                 _session->locations()->add (location, true);
1059                 XMLNode &after = _session->locations()->get_state();
1060                 _session->add_command (new MementoCommand<Locations>(*(_session->locations()), &before, &after));
1061                 PublicEditor::instance().commit_reversible_command ();
1062         }
1063
1064 }
1065
1066 void
1067 LocationUI::add_new_range()
1068 {
1069         string rangename;
1070
1071         if (_session) {
1072                 framepos_t where = _session->audible_frame();
1073                 _session->locations()->next_available_name(rangename,"unnamed");
1074                 Location *location = new Location (*_session, where, where, rangename, Location::IsRangeMarker);
1075                 PublicEditor::instance().begin_reversible_command (_("add range marker"));
1076                 XMLNode &before = _session->locations()->get_state();
1077                 _session->locations()->add (location, true);
1078                 XMLNode &after = _session->locations()->get_state();
1079                 _session->add_command (new MementoCommand<Locations>(*(_session->locations()), &before, &after));
1080                 PublicEditor::instance().commit_reversible_command ();
1081         }
1082 }
1083
1084 void
1085 LocationUI::refresh_location_list ()
1086 {
1087         ENSURE_GUI_THREAD (*this, &LocationUI::refresh_location_list)
1088         using namespace Box_Helpers;
1089
1090         // this is just too expensive to do when window is not shown
1091         if (!is_mapped()) {
1092                 return;
1093         }
1094
1095         BoxList & loc_children = location_rows.children();
1096         BoxList & range_children = range_rows.children();
1097
1098         loc_children.clear();
1099         range_children.clear();
1100
1101         if (_session) {
1102                 _session->locations()->apply (*this, &LocationUI::map_locations);
1103         }
1104 }
1105
1106 void
1107 LocationUI::set_session(ARDOUR::Session* s)
1108 {
1109         SessionHandlePtr::set_session (s);
1110
1111         if (_session) {
1112                 _session->locations()->added.connect (_session_connections, invalidator (*this), boost::bind (&LocationUI::location_added, this, _1), gui_context());
1113                 _session->locations()->removed.connect (_session_connections, invalidator (*this), boost::bind (&LocationUI::location_removed, this, _1), gui_context());
1114                 _session->locations()->changed.connect (_session_connections, invalidator (*this), boost::bind (&LocationUI::refresh_location_list, this), gui_context());
1115
1116                 _clock_group->set_clock_mode (clock_mode_from_session_instant_xml ());
1117         } else {
1118                 _mode_set = false;
1119         }
1120
1121         loop_edit_row.set_session (s);
1122         punch_edit_row.set_session (s);
1123
1124         refresh_location_list ();
1125 }
1126
1127 void
1128 LocationUI::session_going_away()
1129 {
1130         ENSURE_GUI_THREAD (*this, &LocationUI::session_going_away);
1131
1132         using namespace Box_Helpers;
1133         BoxList & loc_children = location_rows.children();
1134         BoxList & range_children = range_rows.children();
1135
1136         loc_children.clear();
1137         range_children.clear();
1138
1139         loop_edit_row.set_session (0);
1140         loop_edit_row.set_location (0);
1141
1142         punch_edit_row.set_session (0);
1143         punch_edit_row.set_location (0);
1144
1145         _mode_set = false;
1146
1147         SessionHandlePtr::session_going_away ();
1148 }
1149
1150 XMLNode &
1151 LocationUI::get_state () const
1152 {
1153         XMLNode* node = new XMLNode (_state_node_name);
1154         node->set_property (X_("clock-mode"), _clock_group->clock_mode ());
1155         return *node;
1156 }
1157
1158 int
1159 LocationUI::set_state (const XMLNode& node)
1160 {
1161         if (node.name() != _state_node_name) {
1162                 return -1;
1163         }
1164
1165         if (!node.get_property (X_("clock-mode"), _mode)) {
1166                 return -1;
1167         }
1168
1169         _mode_set = true;
1170         _clock_group->set_clock_mode (_mode);
1171         return 0;
1172 }
1173
1174 AudioClock::Mode
1175 LocationUI::clock_mode_from_session_instant_xml ()
1176 {
1177         if (_mode_set) {
1178                 return _mode;
1179         }
1180
1181         XMLNode* node = _session->instant_xml (_state_node_name);
1182         if (!node) {
1183                 return ARDOUR_UI::instance()->secondary_clock->mode();
1184         }
1185
1186         if (!node->get_property (X_("clock-mode"), _mode)) {
1187                 return ARDOUR_UI::instance()->secondary_clock->mode();
1188         }
1189
1190         _mode_set = true;
1191         return _mode;
1192 }
1193
1194
1195 /*------------------------*/
1196
1197 LocationUIWindow::LocationUIWindow ()
1198         : ArdourWindow (S_("Ranges|Locations"))
1199 {
1200         set_wmclass(X_("ardour_locations"), PROGRAM_NAME);
1201         set_name ("LocationWindow");
1202
1203         add (_ui);
1204 }
1205
1206 LocationUIWindow::~LocationUIWindow()
1207 {
1208 }
1209
1210 void
1211 LocationUIWindow::on_map ()
1212 {
1213         ArdourWindow::on_map ();
1214         _ui.refresh_location_list();
1215 }
1216
1217 bool
1218 LocationUIWindow::on_delete_event (GdkEventAny*)
1219 {
1220         return false;
1221 }
1222
1223 void
1224 LocationUIWindow::set_session (Session *s)
1225 {
1226         ArdourWindow::set_session (s);
1227         _ui.set_session (s);
1228         _ui.show_all ();
1229 }
1230
1231 void
1232 LocationUIWindow::session_going_away ()
1233 {
1234         ArdourWindow::session_going_away ();
1235         hide_all();
1236 }