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