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