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