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