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