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