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