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