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