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