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