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