update drobilla's fascistic dir-locals.el to force emacs users into whitespace submis...
[ardour.git] / gtk2_ardour / location_ui.cc
1 /*
2     Copyright (C) 2000 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <cmath>
21 #include <cstdlib>
22
23 #include <gtkmm2ext/utils.h>
24
25 #include "ardour/utils.h"
26 #include "ardour/configuration.h"
27 #include "ardour/session.h"
28 #include "pbd/memento_command.h"
29
30 #include "ardour_ui.h"
31 #include "clock_group.h"
32 #include "gui_thread.h"
33 #include "keyboard.h"
34 #include "location_ui.h"
35 #include "prompter.h"
36 #include "utils.h"
37
38 #include "i18n.h"
39
40 using namespace std;
41 using namespace ARDOUR;
42 using namespace PBD;
43 using namespace Gtk;
44 using namespace Gtkmm2ext;
45
46 LocationEditRow::LocationEditRow(Session * sess, Location * loc, int32_t num)
47         : SessionHandlePtr (0) /* explicitly set below */
48         , location(0) 
49         , item_table (1, 6, false)
50         , start_clock (X_("locationstart"), true, X_("LocationEditRowClock"), true, false)
51         , end_clock (X_("locationend"), true, X_("LocationEditRowClock"), true, false)
52         , length_clock (X_("locationlength"), true, X_("LocationEditRowClock"), true, false, true)
53         , cd_check_button (_("CD"))
54         , hide_check_button (_("Hide"))
55         , lock_check_button (_("Lock"))
56         , glue_check_button (_("Glue"))
57         , scms_check_button (_("SCMS"))
58         , preemph_check_button (_("Pre-Emphasis"))
59         , _clock_group (0)
60  {
61          i_am_the_modifier = 0;
62
63          start_go_button.set_image (*manage (new Image (Stock::JUMP_TO, Gtk::ICON_SIZE_SMALL_TOOLBAR)));
64          end_go_button.set_image (*manage (new Image (Stock::JUMP_TO, Gtk::ICON_SIZE_SMALL_TOOLBAR)));
65          remove_button.set_image (*manage (new Image (Stock::REMOVE, Gtk::ICON_SIZE_SMALL_TOOLBAR)));
66
67          number_label.set_name ("LocationEditNumberLabel");
68          name_label.set_name ("LocationEditNameLabel");
69          name_entry.set_name ("LocationEditNameEntry");
70          start_go_button.set_name ("LocationEditGoButton");
71          end_go_button.set_name ("LocationEditGoButton");
72          cd_check_button.set_name ("LocationEditCdButton");
73          hide_check_button.set_name ("LocationEditHideButton");
74          lock_check_button.set_name ("LocationEditLockButton");
75          glue_check_button.set_name ("LocationEditGlueButton");
76          remove_button.set_name ("LocationEditRemoveButton");
77          isrc_label.set_name ("LocationEditNumberLabel");
78          isrc_entry.set_name ("LocationEditNameEntry");
79          scms_check_button.set_name ("LocationEditCdButton");
80          preemph_check_button.set_name ("LocationEditCdButton");
81          performer_label.set_name ("LocationEditNumberLabel");
82          performer_entry.set_name ("LocationEditNameEntry");
83          composer_label.set_name ("LocationEditNumberLabel");
84          composer_entry.set_name ("LocationEditNameEntry");
85
86          Gtk::Button* start_to_playhead_button = manage (new Button (_("Use PH")));
87          Gtk::Button* end_to_playhead_button = manage (new Button (_("Use PH")));
88
89          ARDOUR_UI::instance()->tooltips().set_tip (*start_to_playhead_button, _("Set value to playhead"));
90          ARDOUR_UI::instance()->tooltips().set_tip (*end_to_playhead_button, _("Set value to playhead"));
91          
92          isrc_label.set_text ("ISRC: ");
93          isrc_label.set_size_request (30, -1);
94          performer_label.set_text ("Performer: ");
95          performer_label.set_size_request (60, -1);
96          composer_label.set_text ("Composer: ");
97          composer_label.set_size_request (60, -1);
98
99          isrc_entry.set_size_request (112, -1);
100          isrc_entry.set_max_length(12);
101          isrc_entry.set_editable (true);
102
103          performer_entry.set_size_request (100, -1);
104          performer_entry.set_editable (true);
105
106          composer_entry.set_size_request (100, -1);
107          composer_entry.set_editable (true);
108
109          name_label.set_alignment (0, 0.5);
110
111          cd_track_details_hbox.pack_start (isrc_label, false, false);
112          cd_track_details_hbox.pack_start (isrc_entry, false, false);
113          cd_track_details_hbox.pack_start (scms_check_button, false, false);
114          cd_track_details_hbox.pack_start (preemph_check_button, 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
120          isrc_entry.signal_changed().connect (sigc::mem_fun(*this, &LocationEditRow::isrc_entry_changed));
121          performer_entry.signal_changed().connect (sigc::mem_fun(*this, &LocationEditRow::performer_entry_changed));
122          composer_entry.signal_changed().connect (sigc::mem_fun(*this, &LocationEditRow::composer_entry_changed));
123          scms_check_button.signal_toggled().connect(sigc::mem_fun(*this, &LocationEditRow::scms_toggled));
124          preemph_check_button.signal_toggled().connect(sigc::mem_fun(*this, &LocationEditRow::preemph_toggled));
125
126          set_session (sess);
127
128          start_hbox.pack_start (start_go_button, false, false);
129          start_hbox.pack_start (start_clock, false, false);
130          start_hbox.pack_start (*start_to_playhead_button, false, false);
131
132          /* this is always in this location, no matter what the location is */
133
134          item_table.attach (start_hbox, 1, 2, 0, 1, FILL, FILL, 4, 0);
135
136          start_go_button.signal_clicked().connect(sigc::bind (sigc::mem_fun (*this, &LocationEditRow::go_button_pressed), LocStart));
137          start_to_playhead_button->signal_clicked().connect (sigc::bind (sigc::mem_fun (*this, &LocationEditRow::to_playhead_button_pressed), LocStart));
138          start_clock.ValueChanged.connect (sigc::bind (sigc::mem_fun (*this, &LocationEditRow::clock_changed), LocStart));
139          start_clock.ChangeAborted.connect (sigc::bind (sigc::mem_fun (*this, &LocationEditRow::change_aborted), LocStart));
140
141          end_hbox.pack_start (end_go_button, false, false);
142          end_hbox.pack_start (end_clock, false, false);
143          end_hbox.pack_start (*end_to_playhead_button, false, false);
144
145          end_go_button.signal_clicked().connect(sigc::bind (sigc::mem_fun (*this, &LocationEditRow::go_button_pressed), LocEnd));
146          end_to_playhead_button->signal_clicked().connect (sigc::bind (sigc::mem_fun (*this, &LocationEditRow::to_playhead_button_pressed), LocEnd));
147          end_clock.ValueChanged.connect (sigc::bind (sigc::mem_fun (*this, &LocationEditRow::clock_changed), LocEnd));
148          end_clock.ChangeAborted.connect (sigc::bind (sigc::mem_fun (*this, &LocationEditRow::change_aborted), LocEnd));
149
150          length_clock.ValueChanged.connect (sigc::bind ( sigc::mem_fun(*this, &LocationEditRow::clock_changed), LocLength));
151          length_clock.ChangeAborted.connect (sigc::bind (sigc::mem_fun (*this, &LocationEditRow::change_aborted), LocLength));
152
153          cd_check_button.signal_toggled().connect(sigc::mem_fun(*this, &LocationEditRow::cd_toggled));
154          hide_check_button.signal_toggled().connect(sigc::mem_fun(*this, &LocationEditRow::hide_toggled));
155          lock_check_button.signal_toggled().connect(sigc::mem_fun(*this, &LocationEditRow::lock_toggled));
156          glue_check_button.signal_toggled().connect(sigc::mem_fun(*this, &LocationEditRow::glue_toggled));
157
158          remove_button.signal_clicked().connect(sigc::mem_fun(*this, &LocationEditRow::remove_button_pressed));
159
160          pack_start(item_table, true, true);
161
162          set_location (loc);
163          set_number (num);
164  }
165
166  LocationEditRow::~LocationEditRow()
167  {
168          if (location) {
169                  connections.drop_connections ();
170          }
171
172          if (_clock_group) {
173                  _clock_group->remove (start_clock);
174                  _clock_group->remove (end_clock);
175                  _clock_group->remove (length_clock);
176          }
177  }
178
179  void
180  LocationEditRow::set_clock_group (ClockGroup& cg)
181  {
182          if (_clock_group) {
183                  _clock_group->remove (start_clock);
184                  _clock_group->remove (end_clock);
185                  _clock_group->remove (length_clock);
186          }
187
188          _clock_group = &cg;
189
190          _clock_group->add (start_clock);
191          _clock_group->add (end_clock);
192          _clock_group->add (length_clock);
193 }
194
195 void
196 LocationEditRow::set_session (Session *sess)
197 {
198         SessionHandlePtr::set_session (sess);
199
200         if (!_session) { 
201                 return;
202         }
203
204         start_clock.set_session (_session);
205         end_clock.set_session (_session);
206         length_clock.set_session (_session);
207
208 }
209
210 void
211 LocationEditRow::set_number (int num)
212 {
213         number = num;
214
215         if (number >= 0 ) {
216                 number_label.set_text (string_compose ("%1", number));
217         }
218 }
219
220 void
221 LocationEditRow::set_location (Location *loc)
222 {
223         if (location) {
224                 connections.drop_connections ();
225         }
226
227         location = loc;
228
229         if (!location) {
230                 return;
231         }
232
233         ++i_am_the_modifier;
234
235         if (!hide_check_button.get_parent()) {
236                 item_table.attach (hide_check_button, 5, 6, 0, 1, FILL, Gtk::FILL, 4, 0);
237                 item_table.attach (lock_check_button, 6, 7, 0, 1, FILL, Gtk::FILL, 4, 0);
238                 item_table.attach (glue_check_button, 7, 8, 0, 1, FILL, Gtk::FILL, 4, 0);
239         }
240         hide_check_button.set_active (location->is_hidden());
241         lock_check_button.set_active (location->locked());
242         glue_check_button.set_active (location->position_lock_style() == MusicTime);
243
244         if (location->is_auto_loop() || location-> is_auto_punch()) {
245                 // use label instead of entry
246
247                 name_label.set_text (location->name());
248                 name_label.set_size_request (80, -1);
249
250                 if (!name_label.get_parent()) {
251                         item_table.attach (name_label, 0, 1, 0, 1, FILL, FILL, 4, 0);
252                 }
253
254                 name_label.show();
255
256         } else {
257
258                 name_entry.set_text (location->name());
259                 name_entry.set_size_request (100, -1);
260                 name_entry.set_editable (true);
261                 name_entry.signal_changed().connect (sigc::mem_fun(*this, &LocationEditRow::name_entry_changed));
262
263                 if (!name_entry.get_parent()) {
264                         item_table.attach (name_entry, 0, 1, 0, 1, FILL | EXPAND, FILL, 4, 0);
265                 }
266                 name_entry.show();
267
268                 if (!cd_check_button.get_parent()) {
269                         item_table.attach (cd_check_button, 4, 5, 0, 1, FILL, FILL, 4, 0);
270                 }
271                 if (!remove_button.get_parent()) {
272                         item_table.attach (remove_button, 8, 9, 0, 1, FILL, FILL, 4, 0);
273                 }
274
275                 if (location->is_session_range()) {
276                         remove_button.set_sensitive (false);
277                 }
278
279                 cd_check_button.set_active (location->is_cd_marker());
280                 cd_check_button.show();
281
282                 if (location->start() == _session->current_start_frame()) {
283                         cd_check_button.set_sensitive (false);
284                 } else {
285                         cd_check_button.set_sensitive (true);
286                 }
287
288                 hide_check_button.show();
289                 lock_check_button.show();
290                 glue_check_button.show();
291         }
292
293         start_clock.set (location->start(), true);
294
295
296         if (!location->is_mark()) {
297                 if (!end_hbox.get_parent()) {
298                         item_table.attach (end_hbox, 2, 3, 0, 1, FILL, FILL, 4, 0);
299                 }
300                 if (!length_clock.get_parent()) {
301                         item_table.attach (length_clock, 3, 4, 0, 1, FILL, FILL, 4, 0);
302                 }
303
304                 end_clock.set (location->end(), true);
305                 length_clock.set (location->length(), true);
306
307                 end_go_button.show();
308                 end_clock.show();
309                 length_clock.show();
310
311                 ARDOUR_UI::instance()->set_tip (end_go_button, _("Jump to the end of this range"));
312                 ARDOUR_UI::instance()->set_tip (start_go_button, _("Jump to the start of this range"));
313                 ARDOUR_UI::instance()->set_tip (remove_button, _("Forget this range"));
314                 ARDOUR_UI::instance()->set_tip (start_clock, _("Start time"));
315                 ARDOUR_UI::instance()->set_tip (end_clock, _("End time"));
316                 ARDOUR_UI::instance()->set_tip (length_clock, _("Length"));
317
318         } else {
319
320                 ARDOUR_UI::instance()->set_tip (start_go_button, _("Jump to this marker"));
321                 ARDOUR_UI::instance()->set_tip (remove_button, _("Forget this marker"));
322                 ARDOUR_UI::instance()->set_tip (start_clock, _("Position"));
323
324                 end_go_button.hide();
325                 end_clock.hide();
326                 length_clock.hide();
327         }
328
329         set_clock_sensitivity ();
330
331         --i_am_the_modifier;
332
333         location->start_changed.connect (connections, invalidator (*this), ui_bind (&LocationEditRow::start_changed, this, _1), gui_context());
334         location->end_changed.connect (connections, invalidator (*this), ui_bind (&LocationEditRow::end_changed, this, _1), gui_context());
335         location->name_changed.connect (connections, invalidator (*this), ui_bind (&LocationEditRow::name_changed, this, _1), gui_context());
336         location->changed.connect (connections, invalidator (*this), ui_bind (&LocationEditRow::location_changed, this, _1), gui_context());
337         location->FlagsChanged.connect (connections, invalidator (*this), ui_bind (&LocationEditRow::flags_changed, this, _1, _2), gui_context());
338         location->LockChanged.connect (connections, invalidator (*this), ui_bind (&LocationEditRow::lock_changed, this, _1), gui_context());
339         location->PositionLockStyleChanged.connect (connections, invalidator (*this), ui_bind (&LocationEditRow::position_lock_style_changed, this, _1), gui_context());
340 }
341
342 void
343 LocationEditRow::name_entry_changed ()
344 {
345         ENSURE_GUI_THREAD (*this, &LocationEditRow::name_entry_changed)
346         
347         if (i_am_the_modifier || !location) {
348                 return;
349         }
350
351         location->set_name (name_entry.get_text());
352 }
353
354
355 void
356 LocationEditRow::isrc_entry_changed ()
357 {
358         ENSURE_GUI_THREAD (*this, &LocationEditRow::isrc_entry_changed)
359
360         if (i_am_the_modifier || !location) return;
361
362         if (isrc_entry.get_text() != "" ) {
363
364           location->cd_info["isrc"] = isrc_entry.get_text();
365
366         } else {
367           location->cd_info.erase("isrc");
368         }
369 }
370
371 void
372 LocationEditRow::performer_entry_changed ()
373 {
374         ENSURE_GUI_THREAD (*this, &LocationEditRow::performer_entry_changed)
375
376         if (i_am_the_modifier || !location) return;
377
378         if (performer_entry.get_text() != "") {
379           location->cd_info["performer"] = performer_entry.get_text();
380         } else {
381           location->cd_info.erase("performer");
382         }
383 }
384
385 void
386 LocationEditRow::composer_entry_changed ()
387 {
388         ENSURE_GUI_THREAD (*this, &LocationEditRow::composer_entry_changed)
389
390         if (i_am_the_modifier || !location) return;
391
392         if (composer_entry.get_text() != "") {
393         location->cd_info["composer"] = composer_entry.get_text();
394         } else {
395           location->cd_info.erase("composer");
396         }
397 }
398
399
400 void
401 LocationEditRow::go_button_pressed (LocationPart part)
402 {
403         if (!location) {
404                 return;
405         }
406
407         switch (part) {
408         case LocStart:
409                 ARDOUR_UI::instance()->do_transport_locate (location->start(), _session->transport_rolling ());
410                 break;
411         case LocEnd:
412                 ARDOUR_UI::instance()->do_transport_locate (location->end(), _session->transport_rolling ());
413                 break;
414         default:
415                 break;
416         }
417 }
418
419 void
420 LocationEditRow::to_playhead_button_pressed (LocationPart part)
421 {
422         if (!location) {
423                 return;
424         }
425
426         switch (part) {
427         case LocStart:
428                 location->set_start (_session->transport_frame ());
429                 break;
430         case LocEnd:
431                 location->set_end (_session->transport_frame ());
432                 break;
433         default:
434                 break;
435         }
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::change_aborted (LocationPart /*part*/)
461 {
462         if (i_am_the_modifier || !location) return;
463
464         set_location(location);
465 }
466
467 void
468 LocationEditRow::cd_toggled ()
469 {
470         if (i_am_the_modifier || !location) {
471                 return;
472         }
473
474         //if (cd_check_button.get_active() == location->is_cd_marker()) {
475         //      return;
476         //}
477
478         if (cd_check_button.get_active()) {
479                 if (location->start() <= _session->current_start_frame()) {
480                         error << _("You cannot put a CD marker at the start of the session") << endmsg;
481                         cd_check_button.set_active (false);
482                         return;
483                 }
484         }
485
486         location->set_cd (cd_check_button.get_active(), this);
487
488         if (location->is_cd_marker() && !(location->is_mark())) {
489
490                 if (location->cd_info.find("isrc") != location->cd_info.end()) {
491                         isrc_entry.set_text(location->cd_info["isrc"]);
492                 }
493                 if (location->cd_info.find("performer") != location->cd_info.end()) {
494                         performer_entry.set_text(location->cd_info["performer"]);
495                 }
496                 if (location->cd_info.find("composer") != location->cd_info.end()) {
497                         composer_entry.set_text(location->cd_info["composer"]);
498                 }
499                 if (location->cd_info.find("scms") != location->cd_info.end()) {
500                         scms_check_button.set_active(true);
501                 }
502                 if (location->cd_info.find("preemph") != location->cd_info.end()) {
503                         preemph_check_button.set_active(true);
504                 }
505
506                 if (!cd_track_details_hbox.get_parent()) {
507                         item_table.attach (cd_track_details_hbox, 0, 7, 1, 2, FILL | EXPAND, FILL, 4, 0);
508                 }
509                 // item_table.resize(2, 7);
510                 cd_track_details_hbox.show_all();
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 (ARDOUR::Location *loc)
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 (ARDOUR::Location*)
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 (ARDOUR::Location *)
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 (ARDOUR::Location*)
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_sensitivity ();
658
659         i_am_the_modifier--;
660
661 }
662
663 void
664 LocationEditRow::flags_changed (ARDOUR::Location*, void *)
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 (ARDOUR::Location*)
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_sensitivity ();
691
692         i_am_the_modifier--;
693 }
694
695 void
696 LocationEditRow::position_lock_style_changed (ARDOUR::Location*)
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         name_entry.grab_focus();
712 }
713
714 void
715 LocationEditRow::set_clock_sensitivity ()
716 {
717         start_clock.set_sensitive (!location->locked());
718         end_clock.set_sensitive (!location->locked());
719         length_clock.set_sensitive (!location->locked());
720 }
721
722 /*------------------------------------------------------------------------*/
723
724 LocationUI::LocationUI ()
725         : add_location_button (_("New Marker"))
726         , add_range_button (_("New Range"))
727 {
728         i_am_the_modifier = 0;
729
730         _clock_group = new ClockGroup;
731         _clock_group->set_clock_mode (AudioClock::Frames);
732
733         VBox* vbox = manage (new VBox);
734
735         Table* table = manage (new Table (2, 2));
736         table->set_spacings (4);
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 (4);
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, 1, 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 (4);
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, 1, 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
839 LocationUI::~LocationUI()
840 {
841         delete _clock_group;
842 }
843
844 gint 
845 LocationUI::do_location_remove (ARDOUR::Location *loc)
846 {
847         /* this is handled internally by Locations, but there's
848            no point saving state etc. when we know the marker
849            cannot be removed.
850         */
851
852         if (loc->is_session_range()) {
853                 return FALSE;
854         }
855
856         _session->begin_reversible_command (_("remove marker"));
857         XMLNode &before = _session->locations()->get_state();
858         _session->locations()->remove (loc);
859         XMLNode &after = _session->locations()->get_state();
860         _session->add_command(new MementoCommand<Locations>(*(_session->locations()), &before, &after));
861         _session->commit_reversible_command ();
862
863         return FALSE;
864 }
865
866 void 
867 LocationUI::location_remove_requested (ARDOUR::Location *loc)
868 {
869         // must do this to prevent problems when destroying
870         // the effective sender of this event
871
872         Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &LocationUI::do_location_remove), loc));
873 }
874
875
876 void 
877 LocationUI::location_redraw_ranges ()
878 {
879         range_rows.hide();
880         range_rows.show();
881 }
882
883 struct LocationSortByStart {
884     bool operator() (Location *a, Location *b) {
885             return a->start() < b->start();
886     }
887 };
888
889 void
890 LocationUI::location_added (Location* location)
891 {
892         if (location->is_auto_punch()) {
893                 punch_edit_row.set_location(location);
894         } else if (location->is_auto_loop()) {
895                 loop_edit_row.set_location(location);
896         } else if (location->is_range_marker() || location->is_mark()) {
897                 Locations::LocationList loc = _session->locations()->list ();
898                 loc.sort (LocationSortByStart ());
899
900                 LocationEditRow* erow = manage (new LocationEditRow (_session, location));
901
902                 erow->set_clock_group (*_clock_group);
903                 erow->remove_requested.connect (sigc::mem_fun (*this, &LocationUI::location_remove_requested));
904
905                 Box_Helpers::BoxList & children = location->is_range_marker() ? range_rows.children () : location_rows.children ();
906
907                 /* Step through the location list and the GUI list to find the place to insert */
908                 Locations::LocationList::iterator i = loc.begin ();
909                 Box_Helpers::BoxList::iterator j = children.begin ();
910                 while (i != loc.end()) {
911
912                         if (location->flags() != (*i)->flags()) {
913                                 /* Skip locations in the session list that aren't of the right type */
914                                 ++i;
915                                 continue;
916                         }
917
918                         if (*i == location) {
919                                 children.insert (j, Box_Helpers::Element (*erow, PACK_SHRINK, 1, PACK_START));
920                                 break;
921                         }
922
923                         ++i;
924                         
925                         if (j != children.end()) {
926                                 ++j;
927                         }
928                 }
929
930                 range_rows.show_all ();
931                 location_rows.show_all ();
932         }
933 }
934
935 void
936 LocationUI::location_removed (Location* location)
937 {
938         ENSURE_GUI_THREAD (*this, &LocationUI::location_removed, location)
939
940         if (location->is_auto_punch()) {
941                 punch_edit_row.set_location(0);
942         } else if (location->is_auto_loop()) {
943                 loop_edit_row.set_location(0);
944         } else if (location->is_range_marker() || location->is_mark()) {
945                 Box_Helpers::BoxList& children = location->is_range_marker() ? range_rows.children () : location_rows.children ();
946                 for (Box_Helpers::BoxList::iterator i = children.begin(); i != children.end(); ++i) {
947                         LocationEditRow* r = dynamic_cast<LocationEditRow*> (i->get_widget());
948                         if (r && r->get_location() == location) {
949                                 children.erase (i);
950                                 break;
951                         }
952                 }
953         }
954 }
955
956 void
957 LocationUI::map_locations (Locations::LocationList& locations)
958 {
959         Locations::LocationList::iterator i;
960         gint n;
961         int mark_n = 0;
962         Locations::LocationList temp = locations;
963         LocationSortByStart cmp;
964
965         temp.sort (cmp);
966         locations = temp;
967
968         for (n = 0, i = locations.begin(); i != locations.end(); ++n, ++i) {
969
970                 Location* location = *i;
971
972                 if (location->is_mark()) {
973                         LocationEditRow* erow = manage (new LocationEditRow (_session, location, mark_n));
974
975                         erow->set_clock_group (*_clock_group);
976                         erow->remove_requested.connect (sigc::mem_fun(*this, &LocationUI::location_remove_requested));
977                         erow->redraw_ranges.connect (sigc::mem_fun(*this, &LocationUI::location_redraw_ranges));
978
979                         Box_Helpers::BoxList & loc_children = location_rows.children();
980                         loc_children.push_back(Box_Helpers::Element(*erow, PACK_SHRINK, 1, PACK_START));
981                         if (location == newest_location) {
982                                 newest_location = 0;
983                                 erow->focus_name();
984                         }
985                 } else if (location->is_auto_punch()) {
986                         punch_edit_row.set_session (_session);
987                         punch_edit_row.set_location (location);
988                         punch_edit_row.show_all();
989                 } else if (location->is_auto_loop()) {
990                         loop_edit_row.set_session (_session);
991                         loop_edit_row.set_location (location);
992                         loop_edit_row.show_all();
993                 } else {
994                         LocationEditRow* erow = manage (new LocationEditRow(_session, location));
995
996                         erow->set_clock_group (*_clock_group);
997                         erow->remove_requested.connect (sigc::mem_fun(*this, &LocationUI::location_remove_requested));
998
999                         Box_Helpers::BoxList & range_children = range_rows.children();
1000                         range_children.push_back(Box_Helpers::Element(*erow,  PACK_SHRINK, 1, PACK_START));
1001                 }
1002         }
1003
1004         range_rows.show_all();
1005         location_rows.show_all();
1006 }
1007
1008 void
1009 LocationUI::add_new_location()
1010 {
1011         string markername;
1012
1013         if (_session) {
1014                 framepos_t where = _session->audible_frame();
1015                 _session->locations()->next_available_name(markername,"mark");
1016                 Location *location = new Location (*_session, where, where, markername, Location::IsMark);
1017                 if (Config->get_name_new_markers()) {
1018                         newest_location = location;
1019                 }
1020                 _session->begin_reversible_command (_("add marker"));
1021                 XMLNode &before = _session->locations()->get_state();
1022                 _session->locations()->add (location, true);
1023                 XMLNode &after = _session->locations()->get_state();
1024                 _session->add_command (new MementoCommand<Locations>(*(_session->locations()), &before, &after));
1025                 _session->commit_reversible_command ();
1026         }
1027
1028 }
1029
1030 void
1031 LocationUI::add_new_range()
1032 {
1033         string rangename;
1034
1035         if (_session) {
1036                 framepos_t where = _session->audible_frame();
1037                 _session->locations()->next_available_name(rangename,"unnamed");
1038                 Location *location = new Location (*_session, where, where, rangename, Location::IsRangeMarker);
1039                 _session->begin_reversible_command (_("add range marker"));
1040                 XMLNode &before = _session->locations()->get_state();
1041                 _session->locations()->add (location, true);
1042                 XMLNode &after = _session->locations()->get_state();
1043                 _session->add_command (new MementoCommand<Locations>(*(_session->locations()), &before, &after));
1044                 _session->commit_reversible_command ();
1045         }
1046 }
1047
1048 void
1049 LocationUI::refresh_location_list ()
1050 {
1051         ENSURE_GUI_THREAD (*this, &LocationUI::refresh_location_list)
1052         using namespace Box_Helpers;
1053
1054         // this is just too expensive to do when window is not shown
1055         if (!is_visible()) return;
1056
1057         BoxList & loc_children = location_rows.children();
1058         BoxList & range_children = range_rows.children();
1059
1060         loc_children.clear();
1061         range_children.clear();
1062
1063         if (_session) {
1064                 _session->locations()->apply (*this, &LocationUI::map_locations);
1065         }
1066
1067 }
1068
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), ui_bind (&LocationUI::location_added, this, _1), gui_context());
1078                 _session->locations()->removed.connect (_session_connections, invalidator (*this), ui_bind (&LocationUI::location_removed, this, _1), gui_context());
1079         }
1080
1081         loop_edit_row.set_session (s);
1082         punch_edit_row.set_session (s);
1083
1084         refresh_location_list ();
1085 }
1086
1087 void
1088 LocationUI::locations_changed (Locations::Change c)
1089 {
1090         /* removal is signalled by both a removed and a changed signal emission from Locations,
1091            so we don't need to refresh the list on a removal
1092         */
1093         if (c != Locations::REMOVAL) {
1094                 refresh_location_list ();
1095         }
1096 }
1097
1098 void
1099 LocationUI::session_going_away()
1100 {
1101         ENSURE_GUI_THREAD (*this, &LocationUI::session_going_away);
1102
1103         using namespace Box_Helpers;
1104         BoxList & loc_children = location_rows.children();
1105         BoxList & range_children = range_rows.children();
1106
1107         loc_children.clear();
1108         range_children.clear();
1109
1110         loop_edit_row.set_session (0);
1111         loop_edit_row.set_location (0);
1112
1113         punch_edit_row.set_session (0);
1114         punch_edit_row.set_location (0);
1115
1116         SessionHandlePtr::session_going_away ();
1117 }
1118
1119
1120 /*------------------------*/
1121
1122 LocationUIWindow::LocationUIWindow ()
1123         : ArdourDialog (_("Locations"))
1124 {
1125         set_wmclass(X_("ardour_locations"), PROGRAM_NAME);
1126         set_name ("LocationWindow");
1127
1128         get_vbox()->pack_start (_ui);
1129 }
1130
1131 LocationUIWindow::~LocationUIWindow()
1132 {
1133 }
1134
1135 void 
1136 LocationUIWindow::on_show()
1137 {
1138         _ui.refresh_location_list();
1139         ArdourDialog::on_show();
1140 }
1141
1142 bool
1143 LocationUIWindow::on_delete_event (GdkEventAny*)
1144 {
1145         hide ();
1146         return true;
1147 }
1148
1149 void
1150 LocationUIWindow::set_session (Session *s)
1151 {
1152         ArdourDialog::set_session (s);
1153         _ui.set_session (s);
1154 }
1155
1156 void
1157 LocationUIWindow::session_going_away ()
1158 {
1159         ArdourDialog::session_going_away ();
1160         hide_all();
1161 }