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