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