remove almost-unusued stop_signal.h and clean up the one (unused) place where it...
[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) return;
456
457         remove_requested(location); /*  EMIT_SIGNAL */
458 }
459
460
461
462 void
463 LocationEditRow::scms_toggled ()
464 {
465         if (i_am_the_modifier || !location) return;
466
467         if (scms_check_button.get_active()) {
468           location->cd_info["scms"] = "on";
469         } else {
470           location->cd_info.erase("scms");
471         }
472
473 }
474
475 void
476 LocationEditRow::preemph_toggled ()
477 {
478         if (i_am_the_modifier || !location) return;
479
480         if (preemph_check_button.get_active()) {
481           location->cd_info["preemph"] = "on";
482         } else {
483           location->cd_info.erase("preemph");
484         }
485 }
486
487 void
488 LocationEditRow::end_changed (ARDOUR::Location *loc)
489 {
490         ENSURE_GUI_THREAD (*this, &LocationEditRow::end_changed, loc)
491
492         if (!location) return;
493
494         // update end and length
495         i_am_the_modifier++;
496
497         end_clock.set (location->end());
498         length_clock.set (location->length());
499
500         i_am_the_modifier--;
501 }
502
503 void
504 LocationEditRow::start_changed (ARDOUR::Location *loc)
505 {
506         ENSURE_GUI_THREAD (*this, &LocationEditRow::start_changed, loc)
507
508         if (!location) return;
509
510         // update end and length
511         i_am_the_modifier++;
512
513         start_clock.set (location->start());
514
515         if (location->start() == _session->current_start_frame()) {
516                 cd_check_button.set_sensitive (false);
517         } else {
518                 cd_check_button.set_sensitive (true);
519         }
520
521         i_am_the_modifier--;
522 }
523
524 void
525 LocationEditRow::name_changed (ARDOUR::Location *loc)
526 {
527         ENSURE_GUI_THREAD (*this, &LocationEditRow::name_changed, loc)
528
529         if (!location) return;
530
531         // update end and length
532         i_am_the_modifier++;
533
534         name_entry.set_text(location->name());
535         name_label.set_text(location->name());
536
537         i_am_the_modifier--;
538
539 }
540
541 void
542 LocationEditRow::location_changed (ARDOUR::Location *loc)
543 {
544         ENSURE_GUI_THREAD (*this, &LocationEditRow::location_changed, loc)
545
546         if (!location) return;
547
548         i_am_the_modifier++;
549
550         start_clock.set (location->start());
551         end_clock.set (location->end());
552         length_clock.set (location->length());
553
554         start_clock.set_sensitive (!location->locked());
555         end_clock.set_sensitive (!location->locked());
556         length_clock.set_sensitive (!location->locked());
557
558         i_am_the_modifier--;
559
560 }
561
562 void
563 LocationEditRow::flags_changed (ARDOUR::Location *loc, void *src)
564 {
565         ENSURE_GUI_THREAD (*this, &LocationEditRow::flags_changed, loc, src)
566
567         if (!location) return;
568
569         i_am_the_modifier++;
570
571         cd_check_button.set_active (location->is_cd_marker());
572         hide_check_button.set_active (location->is_hidden());
573
574         i_am_the_modifier--;
575 }
576
577 void
578 LocationEditRow::focus_name() {
579         name_entry.grab_focus();
580 }
581
582
583 LocationUI::LocationUI ()
584         : add_location_button (_("New Marker"))
585         , add_range_button (_("New Range"))
586 {
587         i_am_the_modifier = 0;
588
589         location_vpacker.set_spacing (5);
590         
591         add_location_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::ADD, Gtk::ICON_SIZE_BUTTON)));
592         add_range_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::ADD, Gtk::ICON_SIZE_BUTTON)));
593
594         loop_punch_box.pack_start (loop_edit_row, false, false);
595         loop_punch_box.pack_start (punch_edit_row, false, false);
596         
597         loop_punch_scroller.add (loop_punch_box);
598         loop_punch_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_NEVER);
599         loop_punch_scroller.set_shadow_type (Gtk::SHADOW_NONE);
600         
601         location_vpacker.pack_start (loop_punch_scroller, false, false);
602
603         location_rows.set_name("LocationLocRows");
604         location_rows_scroller.add (location_rows);
605         location_rows_scroller.set_name ("LocationLocRowsScroller");
606         location_rows_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
607         location_rows_scroller.set_size_request (-1, 130);
608
609         newest_location = 0;
610
611         loc_frame_box.set_spacing (5);
612         loc_frame_box.set_border_width (5);
613         loc_frame_box.set_name("LocationFrameBox");
614
615         loc_frame_box.pack_start (location_rows_scroller, true, true);
616
617         add_location_button.set_name ("LocationAddLocationButton");
618         
619         HBox* add_button_box = manage (new HBox);
620
621         // loc_frame_box.pack_start (add_location_button, false, false);
622         add_button_box->pack_start (add_location_button, true, true);
623
624         loc_frame.set_name ("LocationLocEditorFrame");
625         loc_frame.set_label (_("Markers (including CD index)"));
626         loc_frame.add (loc_frame_box);
627         loc_range_panes.pack1(loc_frame, true, false);
628
629
630         range_rows.set_name("LocationRangeRows");
631         range_rows_scroller.add (range_rows);
632         range_rows_scroller.set_name ("LocationRangeRowsScroller");
633         range_rows_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
634         range_rows_scroller.set_size_request (-1, 130);
635
636         range_frame_box.set_spacing (5);
637         range_frame_box.set_name("LocationFrameBox");
638         range_frame_box.set_border_width (5);
639         range_frame_box.pack_start (range_rows_scroller, true, true);
640
641         add_range_button.set_name ("LocationAddRangeButton");
642         //range_frame_box.pack_start (add_range_button, false, false);
643
644         add_button_box->pack_start (add_range_button, true, true);
645
646         range_frame.set_name ("LocationRangeEditorFrame");
647         range_frame.set_label (_("Ranges (including CD track ranges)"));
648         range_frame.add (range_frame_box);
649         loc_range_panes.pack2(range_frame, true, false);
650         location_vpacker.pack_start (loc_range_panes, true, true);
651         location_vpacker.pack_start (*add_button_box, false, false);
652
653         pack_start (location_vpacker, true, true);
654
655         add_location_button.signal_clicked().connect (sigc::mem_fun(*this, &LocationUI::add_new_location));
656         add_range_button.signal_clicked().connect (sigc::mem_fun(*this, &LocationUI::add_new_range));
657         
658         show_all ();
659 }
660
661 LocationUI::~LocationUI()
662 {
663 }
664
665 gint 
666 LocationUI::do_location_remove (ARDOUR::Location *loc)
667 {
668         /* this is handled internally by Locations, but there's
669            no point saving state etc. when we know the marker
670            cannot be removed.
671         */
672
673         if (loc->is_session_range()) {
674                 return FALSE;
675         }
676
677         _session->begin_reversible_command (_("remove marker"));
678         XMLNode &before = _session->locations()->get_state();
679         _session->locations()->remove (loc);
680         XMLNode &after = _session->locations()->get_state();
681         _session->add_command(new MementoCommand<Locations>(*(_session->locations()), &before, &after));
682         _session->commit_reversible_command ();
683
684         return FALSE;
685 }
686
687 void 
688 LocationUI::location_remove_requested (ARDOUR::Location *loc)
689 {
690         // must do this to prevent problems when destroying
691         // the effective sender of this event
692
693         Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &LocationUI::do_location_remove), loc));
694 }
695
696
697 void 
698 LocationUI::location_redraw_ranges ()
699 {
700         range_rows.hide();
701         range_rows.show();
702 }
703
704 void
705 LocationUI::location_added (Location* location)
706 {
707         ENSURE_GUI_THREAD (*this, &LocationUI::location_added, location)
708
709         if (location->is_auto_punch()) {
710                 punch_edit_row.set_location(location);
711         }
712         else if (location->is_auto_loop()) {
713                 loop_edit_row.set_location(location);
714         }
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         }
728         else if (location->is_auto_loop()) {
729                 loop_edit_row.set_location(0);
730         }
731         else {
732                 refresh_location_list ();
733         }
734 }
735
736 struct LocationSortByStart {
737     bool operator() (Location *a, Location *b) {
738             return a->start() < b->start();
739     }
740 };
741
742 void
743 LocationUI::map_locations (Locations::LocationList& locations)
744 {
745         Locations::LocationList::iterator i;
746         Location* location;
747         gint n;
748         int mark_n = 0;
749         Locations::LocationList temp = locations;
750         LocationSortByStart cmp;
751
752         temp.sort (cmp);
753         locations = temp;
754
755         Box_Helpers::BoxList & loc_children = location_rows.children();
756         Box_Helpers::BoxList & range_children = range_rows.children();
757         LocationEditRow * erow;
758
759         for (n = 0, i = locations.begin(); i != locations.end(); ++n, ++i) {
760
761                 location = *i;
762
763                 if (location->is_mark()) {
764                         mark_n++;
765                         erow = manage (new LocationEditRow(_session, location, mark_n));
766                         erow->remove_requested.connect (sigc::mem_fun(*this, &LocationUI::location_remove_requested));
767                         erow->redraw_ranges.connect (sigc::mem_fun(*this, &LocationUI::location_redraw_ranges));
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                 }
774                 else if (location->is_auto_punch()) {
775                         punch_edit_row.set_session (_session);
776                         punch_edit_row.set_location (location);
777                         punch_edit_row.show_all();
778                 }
779                 else if (location->is_auto_loop()) {
780                         loop_edit_row.set_session (_session);
781                         loop_edit_row.set_location (location);
782                         loop_edit_row.show_all();
783                 }
784                 else {
785                         erow = manage (new LocationEditRow(_session, location));
786                         erow->remove_requested.connect (sigc::mem_fun(*this, &LocationUI::location_remove_requested));
787                         range_children.push_back(Box_Helpers::Element(*erow,  PACK_SHRINK, 1, PACK_START));
788                 }
789         }
790
791         range_rows.show_all();
792         location_rows.show_all();
793 }
794
795 void
796 LocationUI::add_new_location()
797 {
798         string markername;
799
800         if (_session) {
801                 nframes_t where = _session->audible_frame();
802                 _session->locations()->next_available_name(markername,"mark");
803                 Location *location = new Location (where, where, markername, Location::IsMark);
804                 if (Config->get_name_new_markers()) {
805                         newest_location = location;
806                 }
807                 _session->begin_reversible_command (_("add marker"));
808                 XMLNode &before = _session->locations()->get_state();
809                 _session->locations()->add (location, true);
810                 XMLNode &after = _session->locations()->get_state();
811                 _session->add_command (new MementoCommand<Locations>(*(_session->locations()), &before, &after));
812                 _session->commit_reversible_command ();
813         }
814
815 }
816
817 void
818 LocationUI::add_new_range()
819 {
820         string rangename;
821
822         if (_session) {
823                 nframes_t where = _session->audible_frame();
824                 _session->locations()->next_available_name(rangename,"unnamed");
825                 Location *location = new Location (where, where, rangename, Location::IsRangeMarker);
826                 _session->begin_reversible_command (_("add range marker"));
827                 XMLNode &before = _session->locations()->get_state();
828                 _session->locations()->add (location, true);
829                 XMLNode &after = _session->locations()->get_state();
830                 _session->add_command (new MementoCommand<Locations>(*(_session->locations()), &before, &after));
831                 _session->commit_reversible_command ();
832         }
833 }
834
835 void
836 LocationUI::refresh_location_list ()
837 {
838         ENSURE_GUI_THREAD (*this, &LocationUI::refresh_location_list)
839         using namespace Box_Helpers;
840
841         // this is just too expensive to do when window is not shown
842         if (!is_visible()) return;
843
844         BoxList & loc_children = location_rows.children();
845         BoxList & range_children = range_rows.children();
846
847         loc_children.clear();
848         range_children.clear();
849
850         if (_session) {
851                 _session->locations()->apply (*this, &LocationUI::map_locations);
852         }
853
854 }
855
856 void
857 LocationUI::set_session(ARDOUR::Session* s)
858 {
859         SessionHandlePtr::set_session (s);
860
861         if (_session) {
862                 _session->locations()->changed.connect (_session_connections, invalidator (*this), boost::bind (&LocationUI::refresh_location_list, this), gui_context());
863                 _session->locations()->StateChanged.connect (_session_connections, invalidator (*this), boost::bind (&LocationUI::refresh_location_list, this), gui_context());
864                 _session->locations()->added.connect (_session_connections, invalidator (*this), ui_bind (&LocationUI::location_added, this, _1), gui_context());
865                 _session->locations()->removed.connect (_session_connections, invalidator (*this), ui_bind (&LocationUI::location_removed, this, _1), gui_context());
866         }
867
868         loop_edit_row.set_session (s);
869         punch_edit_row.set_session (s);
870
871         refresh_location_list ();
872 }
873
874 void
875 LocationUI::session_going_away()
876 {
877         ENSURE_GUI_THREAD (*this, &LocationUI::session_going_away);
878
879         using namespace Box_Helpers;
880         BoxList & loc_children = location_rows.children();
881         BoxList & range_children = range_rows.children();
882
883         loc_children.clear();
884         range_children.clear();
885
886         loop_edit_row.set_session (0);
887         loop_edit_row.set_location (0);
888
889         punch_edit_row.set_session (0);
890         punch_edit_row.set_location (0);
891
892         SessionHandlePtr::session_going_away ();
893 }
894
895 /*------------------------*/
896
897 LocationUIWindow::LocationUIWindow ()
898         : ArdourDialog (_("Locations"))
899 {
900         set_wmclass(X_("ardour_locations"), "Ardour");
901         set_name ("LocationWindow");
902
903         get_vbox()->pack_start (_ui);
904 }
905
906 LocationUIWindow::~LocationUIWindow()
907 {
908 }
909
910 void 
911 LocationUIWindow::on_show()
912 {
913         _ui.refresh_location_list();
914         ArdourDialog::on_show();
915 }
916
917 bool
918 LocationUIWindow::on_delete_event (GdkEventAny*)
919 {
920         hide ();
921         return true;
922 }
923
924 void
925 LocationUIWindow::set_session (Session *s)
926 {
927         ArdourDialog::set_session (s);
928         _ui.set_session (s);
929 }
930
931 void
932 LocationUIWindow::session_going_away ()
933 {
934         ArdourDialog::session_going_away ();
935         hide_all();
936 }