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