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