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