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