Build correctly on mactel systems.
[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
579         get_vbox()->pack_start (location_hpacker);
580
581         location_vpacker.set_border_width (10);
582         location_vpacker.set_spacing (5);
583
584         location_vpacker.pack_start (loop_edit_row, false, false);
585         location_vpacker.pack_start (punch_edit_row, false, false);
586
587         location_rows.set_name("LocationLocRows");
588         location_rows_scroller.add (location_rows);
589         location_rows_scroller.set_name ("LocationLocRowsScroller");
590         location_rows_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
591         location_rows_scroller.set_size_request (-1, 130);
592
593         loc_frame_box.set_spacing (5);
594         loc_frame_box.set_border_width (5);
595         loc_frame_box.set_name("LocationFrameBox");
596         
597         loc_frame_box.pack_start (location_rows_scroller, true, true);
598
599         add_location_button.set_name ("LocationAddLocationButton");
600         loc_frame_box.pack_start (add_location_button, false, false);
601
602         loc_frame.set_name ("LocationLocEditorFrame");
603         loc_frame.set_label (_("Location (CD Index) Markers"));
604         loc_frame.add (loc_frame_box);
605         loc_range_panes.pack1(loc_frame, true, false);
606
607         
608         range_rows.set_name("LocationRangeRows");
609         range_rows_scroller.add (range_rows);
610         range_rows_scroller.set_name ("LocationRangeRowsScroller");
611         range_rows_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
612         range_rows_scroller.set_size_request (-1, 130);
613         
614         range_frame_box.set_spacing (5);
615         range_frame_box.set_name("LocationFrameBox");
616         range_frame_box.set_border_width (5);
617         range_frame_box.pack_start (range_rows_scroller, true, true);
618
619         add_range_button.set_name ("LocationAddRangeButton");
620         range_frame_box.pack_start (add_range_button, false, false);
621
622         range_frame.set_name ("LocationRangeEditorFrame");
623         range_frame.set_label (_("Range (CD Track) Markers"));
624         range_frame.add (range_frame_box);
625         loc_range_panes.pack2(range_frame, true, false);
626         location_vpacker.pack_start (loc_range_panes, true, true);
627         
628         location_hpacker.pack_start (location_vpacker, true, true);
629
630         add_location_button.signal_clicked().connect (mem_fun(*this, &LocationUI::add_new_location));
631         add_range_button.signal_clicked().connect (mem_fun(*this, &LocationUI::add_new_range));
632         
633         //add_events (Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK|Gdk::BUTTON_RELEASE_MASK);
634
635         
636 }
637
638 LocationUI::~LocationUI()
639 {
640 }
641
642
643
644 gint LocationUI::do_location_remove (ARDOUR::Location *loc)
645 {
646         /* this is handled internally by Locations, but there's
647            no point saving state etc. when we know the marker
648            cannot be removed.
649         */
650
651         if (loc->is_end()) {
652                 return FALSE;
653         }
654
655         session->begin_reversible_command (_("remove marker"));
656         session->add_undo (session->locations()->get_memento());
657         session->locations()->remove (loc);
658         session->add_redo_no_execute (session->locations()->get_memento());
659         session->commit_reversible_command ();
660
661         return FALSE;
662 }
663
664 void LocationUI::location_remove_requested (ARDOUR::Location *loc)
665 {
666         // must do this to prevent problems when destroying
667         // the effective sender of this event
668         
669   Glib::signal_idle().connect (bind (mem_fun(*this, &LocationUI::do_location_remove), loc));
670 }
671
672
673 void LocationUI::location_redraw_ranges ()
674 {
675
676         range_rows.hide();
677         range_rows.show();
678
679 }
680
681
682 void
683 LocationUI::location_added (Location* location)
684 {
685         ENSURE_GUI_THREAD(bind (mem_fun(*this, &LocationUI::location_added), location));
686         
687         if (location->is_auto_punch()) {
688                 punch_edit_row.set_location(location);
689         }
690         else if (location->is_auto_loop()) {
691                 loop_edit_row.set_location(location);
692         }
693         else {
694                 refresh_location_list ();
695         }
696 }
697
698 void
699 LocationUI::location_removed (Location* location)
700 {
701         ENSURE_GUI_THREAD(bind (mem_fun(*this, &LocationUI::location_removed), location));
702         
703         if (location->is_auto_punch()) {
704                 punch_edit_row.set_location(0);
705         }
706         else if (location->is_auto_loop()) {
707                 loop_edit_row.set_location(0);
708         }
709         else {
710                 refresh_location_list ();
711         }
712 }
713
714 struct LocationSortByStart {
715     bool operator() (Location *a, Location *b) {
716             return a->start() < b->start();
717     }
718 };
719
720 void
721 LocationUI::map_locations (Locations::LocationList& locations)
722 {
723         Locations::LocationList::iterator i;
724         Location* location;
725         gint n; 
726         int mark_n = 0;
727         Locations::LocationList temp = locations;
728         LocationSortByStart cmp;
729
730         temp.sort (cmp);
731         locations = temp;
732
733         Box_Helpers::BoxList & loc_children = location_rows.children();
734         Box_Helpers::BoxList & range_children = range_rows.children();
735         LocationEditRow * erow;
736         
737         for (n = 0, i = locations.begin(); i != locations.end(); ++n, ++i) {
738
739                 location = *i;
740
741                 if (location->is_mark()) {
742                         mark_n++;
743                         erow = manage (new LocationEditRow(session, location, mark_n));
744                         erow->remove_requested.connect (mem_fun(*this, &LocationUI::location_remove_requested));
745                         erow->redraw_ranges.connect (mem_fun(*this, &LocationUI::location_redraw_ranges));
746                         loc_children.push_back(Box_Helpers::Element(*erow, PACK_SHRINK, 1, PACK_START));
747                 }
748                 else if (location->is_auto_punch()) {
749                         punch_edit_row.set_session (session);
750                         punch_edit_row.set_location (location);
751                 }
752                 else if (location->is_auto_loop()) {
753                         loop_edit_row.set_session (session);
754                         loop_edit_row.set_location (location);
755                 }
756                 else {
757                         erow = manage (new LocationEditRow(session, location));
758                         erow->remove_requested.connect (mem_fun(*this, &LocationUI::location_remove_requested));
759                         range_children.push_back(Box_Helpers::Element(*erow,  PACK_SHRINK, 1, PACK_START));
760                 }
761         }
762
763         range_rows.show_all();
764         location_rows.show_all();
765 }
766
767 void
768 LocationUI::add_new_location()
769 {
770         if (session) {
771                 jack_nframes_t where = session->audible_frame();
772                 Location *location = new Location (where, where, "mark", Location::IsMark);
773                 session->begin_reversible_command (_("add marker"));
774                 session->add_undo (session->locations()->get_memento());
775                 session->locations()->add (location, true);
776                 session->add_redo_no_execute (session->locations()->get_memento());
777                 session->commit_reversible_command ();
778         }
779         
780 }
781
782 void
783 LocationUI::add_new_range()
784 {
785         if (session) {
786                 jack_nframes_t where = session->audible_frame();
787                 Location *location = new Location (where, where, "unnamed");
788                 session->begin_reversible_command (_("add range marker"));
789                 session->add_undo (session->locations()->get_memento());
790                 session->locations()->add (location, true);
791                 session->add_redo_no_execute (session->locations()->get_memento());
792                 session->commit_reversible_command ();
793         }
794 }
795
796
797 void
798 LocationUI::refresh_location_list_s (Change ignored)
799 {
800         ENSURE_GUI_THREAD(bind (mem_fun(*this, &LocationUI::refresh_location_list_s), ignored));
801         
802         refresh_location_list ();
803 }
804
805 void
806 LocationUI::refresh_location_list ()
807 {
808         ENSURE_GUI_THREAD(mem_fun(*this, &LocationUI::refresh_location_list));
809         using namespace Box_Helpers;
810
811         BoxList & loc_children = location_rows.children();
812         BoxList & range_children = range_rows.children();
813
814         loc_children.clear();
815         range_children.clear();
816
817         if (session) {
818                 session->locations()->apply (*this, &LocationUI::map_locations);
819         }
820         
821 }
822
823 void
824 LocationUI::set_session(ARDOUR::Session* sess)
825 {
826         ArdourDialog::set_session (sess);
827
828         if (session) {
829                 session->locations()->changed.connect (mem_fun(*this, &LocationUI::refresh_location_list));
830                 session->locations()->StateChanged.connect (mem_fun(*this, &LocationUI::refresh_location_list_s));
831                 session->locations()->added.connect (mem_fun(*this, &LocationUI::location_added));
832                 session->locations()->removed.connect (mem_fun(*this, &LocationUI::location_removed));
833                 session->going_away.connect (mem_fun(*this, &LocationUI::session_gone));
834         }
835         refresh_location_list ();
836 }
837
838 void
839 LocationUI::session_gone()
840 {
841         ENSURE_GUI_THREAD(mem_fun(*this, &LocationUI::session_gone));
842         
843         hide_all();
844
845         using namespace Box_Helpers;
846         BoxList & loc_children = location_rows.children();
847         BoxList & range_children = range_rows.children();
848
849         loc_children.clear();
850         range_children.clear();
851
852         loop_edit_row.set_session (0);
853         loop_edit_row.set_location (0);
854
855         punch_edit_row.set_session (0);
856         punch_edit_row.set_location (0);
857
858         ArdourDialog::session_gone ();
859 }
860
861 bool
862 LocationUI::on_delete_event (GdkEventAny* ev)
863 {
864         hide ();
865         return true;
866 }