add text entry cursor color to dark theme
[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
25 #include "ardour/session.h"
26 #include "pbd/memento_command.h"
27
28 #include "ardour_ui.h"
29 #include "clock_group.h"
30 #include "main_clock.h"
31 #include "gui_thread.h"
32 #include "keyboard.h"
33 #include "location_ui.h"
34 #include "prompter.h"
35 #include "utils.h"
36 #include "public_editor.h"
37 #include "tooltips.h"
38 #include "ui_config.h"
39
40 #include "i18n.h"
41
42 using namespace std;
43 using namespace ARDOUR;
44 using namespace ARDOUR_UI_UTILS;
45 using namespace PBD;
46 using namespace Gtk;
47 using namespace Gtkmm2ext;
48
49 LocationEditRow::LocationEditRow(Session * sess, Location * loc, int32_t num)
50         : SessionHandlePtr (0) /* explicitly set below */
51         , location(0)
52         , item_table (1, 6, false)
53         , start_clock (X_("locationstart"), true, "", true, false)
54         , start_to_playhead_button (_("Use PH"))
55         , locate_to_start_button (_("Goto"))
56         , end_clock (X_("locationend"), true, "", true, false)
57         , end_to_playhead_button (_("Use PH"))
58         , locate_to_end_button (_("Goto"))
59         , length_clock (X_("locationlength"), true, "", true, false, true)
60         , cd_check_button (_("CD"))
61         , hide_check_button (_("Hide"))
62         , lock_check_button (_("Lock"))
63         , glue_check_button (_("Glue"))
64         , _clock_group (0)
65 {
66         i_am_the_modifier = 0;
67
68         remove_button.set_icon (ArdourIcon::CloseCross);
69         remove_button.set_events (remove_button.get_events() & ~(Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK));
70
71         number_label.set_name ("LocationEditNumberLabel");
72         name_label.set_name ("LocationEditNameLabel");
73         name_entry.set_name ("LocationEditNameEntry");
74         cd_check_button.set_name ("LocationEditCdButton");
75         hide_check_button.set_name ("LocationEditHideButton");
76         lock_check_button.set_name ("LocationEditLockButton");
77         glue_check_button.set_name ("LocationEditGlueButton");
78         isrc_label.set_name ("LocationEditNumberLabel");
79         isrc_entry.set_name ("LocationEditNameEntry");
80         scms_check_button.set_name ("LocationEditCdButton");
81         preemph_check_button.set_name ("LocationEditCdButton");
82         performer_label.set_name ("LocationEditNumberLabel");
83         performer_entry.set_name ("LocationEditNameEntry");
84         composer_label.set_name ("LocationEditNumberLabel");
85         composer_entry.set_name ("LocationEditNameEntry");
86
87         isrc_label.set_text (X_("ISRC:"));
88         performer_label.set_text (_("Performer:"));
89         composer_label.set_text (_("Composer:"));
90         scms_label.set_text (X_("SCMS"));
91         preemph_label.set_text (_("Pre-Emphasis"));
92
93         isrc_entry.set_size_request (112, -1);
94         isrc_entry.set_max_length(12);
95         isrc_entry.set_editable (true);
96
97         performer_entry.set_size_request (100, -1);
98         performer_entry.set_editable (true);
99
100         composer_entry.set_size_request (100, -1);
101         composer_entry.set_editable (true);
102
103         name_label.set_alignment (0, 0.5);
104
105         Gtk::HBox* front_spacing = manage (new HBox);
106         front_spacing->set_size_request (20, -1);
107         Gtk::HBox* mid_spacing = manage (new HBox);
108         mid_spacing->set_size_request (20, -1);
109
110         cd_track_details_hbox.set_spacing (4);
111         cd_track_details_hbox.pack_start (*front_spacing, false, false);
112         cd_track_details_hbox.pack_start (isrc_label, false, false);
113         cd_track_details_hbox.pack_start (isrc_entry, false, false);
114         cd_track_details_hbox.pack_start (performer_label, false, false);
115         cd_track_details_hbox.pack_start (performer_entry, true, true);
116         cd_track_details_hbox.pack_start (composer_label, false, false);
117         cd_track_details_hbox.pack_start (composer_entry, true, true);
118         cd_track_details_hbox.pack_start (*mid_spacing, false, false);
119         cd_track_details_hbox.pack_start (scms_label, false, false);
120         cd_track_details_hbox.pack_start (scms_check_button, false, false);
121         cd_track_details_hbox.pack_start (preemph_label, false, false);
122         cd_track_details_hbox.pack_start (preemph_check_button, false, false);
123
124         isrc_entry.signal_changed().connect (sigc::mem_fun(*this, &LocationEditRow::isrc_entry_changed));
125         performer_entry.signal_changed().connect (sigc::mem_fun(*this, &LocationEditRow::performer_entry_changed));
126         composer_entry.signal_changed().connect (sigc::mem_fun(*this, &LocationEditRow::composer_entry_changed));
127         scms_check_button.signal_toggled().connect(sigc::mem_fun(*this, &LocationEditRow::scms_toggled));
128         preemph_check_button.signal_toggled().connect(sigc::mem_fun(*this, &LocationEditRow::preemph_toggled));
129
130         set_session (sess);
131
132         start_hbox.set_spacing (2);
133         start_hbox.pack_start (locate_to_start_button, false, false);
134         start_hbox.pack_start (start_clock, false, false);
135         start_hbox.pack_start (start_to_playhead_button, false, false);
136
137         /* this is always in this location, no matter what the location is */
138
139         item_table.attach (remove_button, 8, 9, 0, 1, SHRINK, SHRINK, 4, 1);
140         item_table.attach (start_hbox, 0, 1, 0, 1, FILL, Gtk::AttachOptions(0), 4, 0);
141
142         start_to_playhead_button.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &LocationEditRow::to_playhead_button_pressed), LocStart));
143         locate_to_start_button.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &LocationEditRow::locate_button_pressed), LocStart));
144         start_clock.ValueChanged.connect (sigc::bind (sigc::mem_fun (*this, &LocationEditRow::clock_changed), LocStart));
145         start_clock.signal_button_press_event().connect (sigc::bind (sigc::mem_fun (*this, &LocationEditRow::locate_to_clock), &start_clock), false);
146
147         end_hbox.set_spacing (2);
148         end_hbox.pack_start (locate_to_end_button, false, false);
149         end_hbox.pack_start (end_clock, false, false);
150         end_hbox.pack_start (end_to_playhead_button, false, false);
151
152         end_to_playhead_button.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &LocationEditRow::to_playhead_button_pressed), LocEnd));
153         locate_to_end_button.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &LocationEditRow::locate_button_pressed), LocEnd));
154         end_clock.ValueChanged.connect (sigc::bind (sigc::mem_fun (*this, &LocationEditRow::clock_changed), LocEnd));
155         end_clock.signal_button_press_event().connect (sigc::bind (sigc::mem_fun (*this, &LocationEditRow::locate_to_clock), &end_clock), false);
156
157         length_clock.ValueChanged.connect (sigc::bind ( sigc::mem_fun(*this, &LocationEditRow::clock_changed), LocLength));
158
159         cd_check_button.signal_toggled().connect(sigc::mem_fun(*this, &LocationEditRow::cd_toggled));
160         hide_check_button.signal_toggled().connect(sigc::mem_fun(*this, &LocationEditRow::hide_toggled));
161         lock_check_button.signal_toggled().connect(sigc::mem_fun(*this, &LocationEditRow::lock_toggled));
162         glue_check_button.signal_toggled().connect(sigc::mem_fun(*this, &LocationEditRow::glue_toggled));
163
164         remove_button.signal_clicked.connect(sigc::mem_fun(*this, &LocationEditRow::remove_button_pressed));
165
166         pack_start(item_table, true, true);
167
168         set_location (loc);
169         set_number (num);
170         cd_toggled(); // show/hide cd-track details
171 }
172
173 LocationEditRow::~LocationEditRow()
174 {
175         if (location) {
176                 connections.drop_connections ();
177         }
178
179         if (_clock_group) {
180                 _clock_group->remove (start_clock);
181                 _clock_group->remove (end_clock);
182                 _clock_group->remove (length_clock);
183         }
184 }
185
186 void
187 LocationEditRow::set_clock_group (ClockGroup& cg)
188 {
189         if (_clock_group) {
190                 _clock_group->remove (start_clock);
191                 _clock_group->remove (end_clock);
192                 _clock_group->remove (length_clock);
193         }
194
195         _clock_group = &cg;
196
197         _clock_group->add (start_clock);
198         _clock_group->add (end_clock);
199         _clock_group->add (length_clock);
200 }
201
202 void
203 LocationEditRow::set_session (Session *sess)
204 {
205         SessionHandlePtr::set_session (sess);
206
207         if (!_session) {
208                 return;
209         }
210
211         start_clock.set_session (_session);
212         end_clock.set_session (_session);
213         length_clock.set_session (_session);
214 }
215
216 void
217 LocationEditRow::set_number (int num)
218 {
219         number = num;
220
221         if (number >= 0 ) {
222                 number_label.set_text (string_compose ("%1", number));
223         }
224 }
225
226 void
227 LocationEditRow::set_location (Location *loc)
228 {
229         if (location) {
230                 connections.drop_connections ();
231         }
232
233         location = loc;
234
235         if (!location) {
236                 return;
237         }
238
239         ++i_am_the_modifier;
240
241         if (!hide_check_button.get_parent()) {
242                 item_table.attach (hide_check_button, 5, 6, 0, 1, FILL, Gtk::FILL, 4, 0);
243                 item_table.attach (lock_check_button, 6, 7, 0, 1, FILL, Gtk::FILL, 4, 0);
244                 item_table.attach (glue_check_button, 7, 8, 0, 1, FILL, Gtk::FILL, 4, 0);
245         }
246         hide_check_button.set_active (location->is_hidden());
247         lock_check_button.set_active (location->locked());
248         glue_check_button.set_active (location->position_lock_style() == MusicTime);
249
250         if (location->is_auto_loop() || location-> is_auto_punch()) {
251                 // use label instead of entry
252
253                 name_label.set_text (location->name());
254                 name_label.set_size_request (80, -1);
255
256                 remove_button.hide ();
257
258                 if (!name_label.get_parent()) {
259                         item_table.attach (name_label, 2, 3, 0, 1, EXPAND|FILL, FILL, 4, 0);
260                 }
261
262                 name_label.show();
263
264         } else {
265
266                 name_entry.set_text (location->name());
267                 name_entry.set_size_request (100, -1);
268                 name_entry.set_editable (true);
269                 name_entry.signal_changed().connect (sigc::mem_fun(*this, &LocationEditRow::name_entry_changed));
270
271                 if (!name_entry.get_parent()) {
272                         item_table.attach (name_entry, 2, 3, 0, 1, FILL | EXPAND, FILL, 4, 0);
273                 }
274                 name_entry.show();
275
276                 if (!cd_check_button.get_parent()) {
277                         item_table.attach (cd_check_button, 4, 5, 0, 1, FILL, Gtk::AttachOptions (0), 4, 0);
278                 }
279
280                 if (location->is_session_range()) {
281                         remove_button.set_sensitive (false);
282                 }
283
284                 cd_check_button.set_active (location->is_cd_marker());
285                 cd_check_button.show();
286
287                 if (location->start() == _session->current_start_frame()) {
288                         cd_check_button.set_sensitive (false);
289                 } else {
290                         cd_check_button.set_sensitive (true);
291                 }
292
293                 hide_check_button.show();
294                 lock_check_button.show();
295                 glue_check_button.show();
296         }
297
298         start_clock.set (location->start(), true);
299
300
301         if (!location->is_mark()) {
302                 if (!end_hbox.get_parent()) {
303                         item_table.attach (end_hbox, 1, 2, 0, 1, FILL, Gtk::AttachOptions (0), 4, 0);
304                 }
305                 if (!length_clock.get_parent()) {
306                         end_hbox.pack_start (length_clock, false, false, 4);
307                 }
308
309                 end_clock.set (location->end(), true);
310                 length_clock.set (location->length(), true);
311
312                 end_clock.show();
313                 length_clock.show();
314
315                 if (location->is_cd_marker()) {
316                         show_cd_track_details ();
317                 }
318
319                 set_tooltip (&remove_button, _("Remove this range"));
320                 set_tooltip (start_clock, _("Start time - middle click to locate here"));
321                 set_tooltip (end_clock, _("End time - middle click to locate here"));
322                 set_tooltip (length_clock, _("Length"));
323
324                 set_tooltip (&start_to_playhead_button, _("Set range start from playhead location"));
325                 set_tooltip (&end_to_playhead_button, _("Set range end from playhead location"));
326
327         } else {
328
329                 set_tooltip (&remove_button, _("Remove this marker"));
330                 set_tooltip (start_clock, _("Position - middle click to locate here"));
331
332                 set_tooltip (&start_to_playhead_button, _("Set marker time from playhead location"));
333
334                 end_clock.hide();
335                 length_clock.hide();
336         }
337
338         set_clock_editable_status ();
339
340         --i_am_the_modifier;
341
342         /* connect to per-location signals, since this row only cares about this location */
343
344         location->NameChanged.connect (connections, invalidator (*this), boost::bind (&LocationEditRow::name_changed, this), gui_context());
345         location->StartChanged.connect (connections, invalidator (*this), boost::bind (&LocationEditRow::start_changed, this), gui_context());
346         location->EndChanged.connect (connections, invalidator (*this), boost::bind (&LocationEditRow::end_changed, this), gui_context());
347         location->Changed.connect (connections, invalidator (*this), boost::bind (&LocationEditRow::location_changed, this), gui_context());
348         location->FlagsChanged.connect (connections, invalidator (*this), boost::bind (&LocationEditRow::flags_changed, this), gui_context());
349         location->LockChanged.connect (connections, invalidator (*this), boost::bind (&LocationEditRow::lock_changed, this), gui_context());
350         location->PositionLockStyleChanged.connect (connections, invalidator (*this), boost::bind (&LocationEditRow::position_lock_style_changed, this), gui_context());
351 }
352
353 void
354 LocationEditRow::name_entry_changed ()
355 {
356         ENSURE_GUI_THREAD (*this, &LocationEditRow::name_entry_changed);
357
358         if (i_am_the_modifier || !location) {
359                 return;
360         }
361
362         location->set_name (name_entry.get_text());
363 }
364
365
366 void
367 LocationEditRow::isrc_entry_changed ()
368 {
369         ENSURE_GUI_THREAD (*this, &LocationEditRow::isrc_entry_changed);
370
371         if (i_am_the_modifier || !location) return;
372
373         if (isrc_entry.get_text() != "" ) {
374
375                 location->cd_info["isrc"] = isrc_entry.get_text();
376
377         } else {
378                 location->cd_info.erase("isrc");
379         }
380 }
381
382 void
383 LocationEditRow::performer_entry_changed ()
384 {
385         ENSURE_GUI_THREAD (*this, &LocationEditRow::performer_entry_changed);
386
387         if (i_am_the_modifier || !location) return;
388
389         if (performer_entry.get_text() != "") {
390                 location->cd_info["performer"] = performer_entry.get_text();
391         } else {
392                 location->cd_info.erase("performer");
393         }
394 }
395
396 void
397 LocationEditRow::composer_entry_changed ()
398 {
399         ENSURE_GUI_THREAD (*this, &LocationEditRow::composer_entry_changed);
400
401         if (i_am_the_modifier || !location) return;
402
403         if (composer_entry.get_text() != "") {
404                 location->cd_info["composer"] = composer_entry.get_text();
405         } else {
406                 location->cd_info.erase("composer");
407         }
408 }
409
410 void
411 LocationEditRow::to_playhead_button_pressed (LocationPart part)
412 {
413         if (!location) {
414                 return;
415         }
416
417         switch (part) {
418                 case LocStart:
419                         location->set_start (_session->transport_frame ());
420                         break;
421                 case LocEnd:
422                         location->set_end (_session->transport_frame ());
423                         break;
424                 default:
425                         break;
426         }
427 }
428
429 void
430 LocationEditRow::locate_button_pressed (LocationPart part)
431 {
432         switch (part) {
433                 case LocStart:
434                         _session->request_locate (start_clock.current_time());
435                         break;
436                 case LocEnd:
437                         _session->request_locate (end_clock.current_time());
438                         break;
439                 default:
440                         break;
441         }
442 }
443
444 bool
445 LocationEditRow::locate_to_clock (GdkEventButton* ev, AudioClock* clock)
446 {
447         if (Keyboard::is_button2_event (ev)) {
448                 _session->request_locate (clock->current_time());
449                 return true;
450         }
451         return false;
452 }
453
454 void
455 LocationEditRow::clock_changed (LocationPart part)
456 {
457         if (i_am_the_modifier || !location) {
458                 return;
459         }
460
461         switch (part) {
462                 case LocStart:
463                         location->set_start (start_clock.current_time());
464                         break;
465                 case LocEnd:
466                         location->set_end (end_clock.current_time());
467                         break;
468                 case LocLength:
469                         location->set_end (location->start() + length_clock.current_duration());
470                 default:
471                         break;
472         }
473 }
474
475 void
476 LocationEditRow::show_cd_track_details ()
477 {
478         if (location->cd_info.find("isrc") != location->cd_info.end()) {
479                 isrc_entry.set_text(location->cd_info["isrc"]);
480         }
481         if (location->cd_info.find("performer") != location->cd_info.end()) {
482                 performer_entry.set_text(location->cd_info["performer"]);
483         }
484         if (location->cd_info.find("composer") != location->cd_info.end()) {
485                 composer_entry.set_text(location->cd_info["composer"]);
486         }
487         if (location->cd_info.find("scms") != location->cd_info.end()) {
488                 scms_check_button.set_active(true);
489         }
490         if (location->cd_info.find("preemph") != location->cd_info.end()) {
491                 preemph_check_button.set_active(true);
492         }
493
494
495         if (!cd_track_details_hbox.get_parent()) {
496                 item_table.attach (cd_track_details_hbox, 0, 7, 1, 2, FILL | EXPAND, FILL, 4, 0);
497         }
498         // item_table.resize(2, 7);
499         cd_track_details_hbox.show_all();
500 }
501
502 void
503 LocationEditRow::cd_toggled ()
504 {
505         if (i_am_the_modifier || !location) {
506                 return;
507         }
508
509         //if (cd_check_button.get_active() == location->is_cd_marker()) {
510         //      return;
511         //}
512
513         if (cd_check_button.get_active()) {
514                 if (location->start() <= _session->current_start_frame()) {
515                         error << _("You cannot put a CD marker at the start of the session") << endmsg;
516                         cd_check_button.set_active (false);
517                         return;
518                 }
519         }
520
521         location->set_cd (cd_check_button.get_active(), this);
522
523         if (location->is_cd_marker()) {
524
525                 show_cd_track_details ();
526
527         } else if (cd_track_details_hbox.get_parent()){
528
529                 item_table.remove (cd_track_details_hbox);
530                 //        item_table.resize(1, 7);
531                 redraw_ranges(); /*     EMIT_SIGNAL */
532         }
533 }
534
535 void
536 LocationEditRow::hide_toggled ()
537 {
538         if (i_am_the_modifier || !location) {
539                 return;
540         }
541
542         location->set_hidden (hide_check_button.get_active(), this);
543 }
544
545 void
546 LocationEditRow::lock_toggled ()
547 {
548         if (i_am_the_modifier || !location) {
549                 return;
550         }
551
552         if (location->locked()) {
553                 location->unlock ();
554         } else {
555                 location->lock ();
556         }
557 }
558
559 void
560 LocationEditRow::glue_toggled ()
561 {
562         if (i_am_the_modifier || !location) {
563                 return;
564         }
565
566         if (location->position_lock_style() == AudioTime) {
567                 location->set_position_lock_style (MusicTime);
568         } else {
569                 location->set_position_lock_style (AudioTime);
570         }
571 }
572
573 void
574 LocationEditRow::remove_button_pressed ()
575 {
576         if (!location) {
577                 return;
578         }
579
580         remove_requested (location); /* EMIT_SIGNAL */
581 }
582
583
584
585 void
586 LocationEditRow::scms_toggled ()
587 {
588         if (i_am_the_modifier || !location) return;
589
590         if (scms_check_button.get_active()) {
591           location->cd_info["scms"] = "on";
592         } else {
593           location->cd_info.erase("scms");
594         }
595
596 }
597
598 void
599 LocationEditRow::preemph_toggled ()
600 {
601         if (i_am_the_modifier || !location) return;
602
603         if (preemph_check_button.get_active()) {
604           location->cd_info["preemph"] = "on";
605         } else {
606           location->cd_info.erase("preemph");
607         }
608 }
609
610 void
611 LocationEditRow::end_changed ()
612 {
613         ENSURE_GUI_THREAD (*this, &LocationEditRow::end_changed, loc)
614
615         if (!location) return;
616
617         // update end and length
618         i_am_the_modifier++;
619
620         end_clock.set (location->end());
621         length_clock.set (location->length());
622
623         i_am_the_modifier--;
624 }
625
626 void
627 LocationEditRow::start_changed ()
628 {
629         if (!location) return;
630
631         // update end and length
632         i_am_the_modifier++;
633
634         start_clock.set (location->start());
635
636         if (location->start() == _session->current_start_frame()) {
637                 cd_check_button.set_sensitive (false);
638         } else {
639                 cd_check_button.set_sensitive (true);
640         }
641
642         i_am_the_modifier--;
643 }
644
645 void
646 LocationEditRow::name_changed ()
647 {
648         if (!location) return;
649
650         // update end and length
651         i_am_the_modifier++;
652
653         name_entry.set_text(location->name());
654         name_label.set_text(location->name());
655
656         i_am_the_modifier--;
657
658 }
659
660 void
661 LocationEditRow::location_changed ()
662 {
663
664         if (!location) return;
665
666         i_am_the_modifier++;
667
668         start_clock.set (location->start());
669         end_clock.set (location->end());
670         length_clock.set (location->length());
671
672         set_clock_editable_status ();
673
674         i_am_the_modifier--;
675
676 }
677
678 void
679 LocationEditRow::flags_changed ()
680 {
681         if (!location) {
682                 return;
683         }
684
685         i_am_the_modifier++;
686
687         cd_check_button.set_active (location->is_cd_marker());
688         hide_check_button.set_active (location->is_hidden());
689         glue_check_button.set_active (location->position_lock_style() == MusicTime);
690
691         i_am_the_modifier--;
692 }
693
694 void
695 LocationEditRow::lock_changed ()
696 {
697         if (!location) {
698                 return;
699         }
700
701         i_am_the_modifier++;
702
703         lock_check_button.set_active (location->locked());
704
705         set_clock_editable_status ();
706
707         i_am_the_modifier--;
708 }
709
710 void
711 LocationEditRow::position_lock_style_changed ()
712 {
713         if (!location) {
714                 return;
715         }
716
717         i_am_the_modifier++;
718
719         glue_check_button.set_active (location->position_lock_style() == MusicTime);
720
721         i_am_the_modifier--;
722 }
723
724 void
725 LocationEditRow::focus_name()
726 {
727         name_entry.grab_focus ();
728 }
729
730 void
731 LocationEditRow::set_clock_editable_status ()
732 {
733         start_clock.set_editable (!location->locked());
734         end_clock.set_editable (!location->locked());
735         length_clock.set_editable (!location->locked());
736 }
737
738 /*------------------------------------------------------------------------*/
739
740 LocationUI::LocationUI ()
741         : add_location_button (_("New Marker"))
742         , add_range_button (_("New Range"))
743 {
744         i_am_the_modifier = 0;
745
746         _clock_group = new ClockGroup;
747
748         VBox* vbox = manage (new VBox);
749
750         Table* table = manage (new Table (2, 2));
751         table->set_spacings (2);
752         table->set_col_spacing (0, 32);
753         int table_row = 0;
754
755         Label* l = manage (new Label (_("<b>Loop/Punch Ranges</b>")));
756         l->set_alignment (0, 0.5);
757         l->set_use_markup (true);
758         table->attach (*l, 0, 2, table_row, table_row + 1);
759         ++table_row;
760
761         loop_edit_row.set_clock_group (*_clock_group);
762         punch_edit_row.set_clock_group (*_clock_group);
763
764         loop_punch_box.set_border_width (6); // 5 + 1 px framebox-border
765         loop_punch_box.pack_start (loop_edit_row, false, false);
766         loop_punch_box.pack_start (punch_edit_row, false, false);
767
768         table->attach (loop_punch_box, 1, 2, table_row, table_row + 1);
769         ++table_row;
770
771         vbox->pack_start (*table, false, false);
772
773         table = manage (new Table (3, 2));
774         table->set_spacings (2);
775         table->set_col_spacing (0, 32);
776         table_row = 0;
777
778         table->attach (*manage (new Label ("")), 0, 2, table_row, table_row + 1, Gtk::SHRINK, Gtk::SHRINK);
779         ++table_row;
780
781         l = manage (new Label (_("<b>Markers (Including CD Index)</b>")));
782         l->set_alignment (0, 0.5);
783         l->set_use_markup (true);
784         table->attach (*l, 0, 2, table_row, table_row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK);
785         ++table_row;
786
787         location_rows.set_name("LocationLocRows");
788         location_rows_scroller.add (location_rows);
789         location_rows_scroller.set_name ("LocationLocRowsScroller");
790         location_rows_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
791         location_rows_scroller.set_size_request (-1, 130);
792
793         newest_location = 0;
794
795         loc_frame_box.set_spacing (5);
796         loc_frame_box.set_border_width (5);
797         loc_frame_box.set_name("LocationFrameBox");
798
799         loc_frame_box.pack_start (location_rows_scroller, true, true);
800
801         add_location_button.set_name ("LocationAddLocationButton");
802
803         table->attach (loc_frame_box, 0, 2, table_row, table_row + 1);
804         ++table_row;
805
806         loc_range_panes.add (*table);
807
808         table = manage (new Table (3, 2));
809         table->set_spacings (2);
810         table->set_col_spacing (0, 32);
811         table_row = 0;
812
813         table->attach (*manage (new Label ("")), 0, 2, table_row, table_row + 1, Gtk::SHRINK, Gtk::SHRINK);
814         ++table_row;
815
816         l = manage (new Label (_("<b>Ranges (Including CD Track Ranges)</b>")));
817         l->set_alignment (0, 0.5);
818         l->set_use_markup (true);
819         table->attach (*l, 0, 2, table_row, table_row + 1, Gtk::FILL | Gtk::EXPAND, Gtk::SHRINK);
820         ++table_row;
821
822         range_rows.set_name("LocationRangeRows");
823         range_rows_scroller.add (range_rows);
824         range_rows_scroller.set_name ("LocationRangeRowsScroller");
825         range_rows_scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
826         range_rows_scroller.set_size_request (-1, 130);
827
828         range_frame_box.set_spacing (5);
829         range_frame_box.set_name("LocationFrameBox");
830         range_frame_box.set_border_width (5);
831         range_frame_box.pack_start (range_rows_scroller, true, true);
832
833         add_range_button.set_name ("LocationAddRangeButton");
834
835         table->attach (range_frame_box, 0, 2, table_row, table_row + 1);
836         ++table_row;
837
838         loc_range_panes.add (*table);
839
840         HBox* add_button_box = manage (new HBox);
841         add_button_box->pack_start (add_location_button, true, true);
842         add_button_box->pack_start (add_range_button, true, true);
843
844         vbox->pack_start (loc_range_panes, true, true);
845         vbox->pack_start (*add_button_box, false, false);
846
847         pack_start (*vbox);
848
849         add_location_button.signal_clicked().connect (sigc::mem_fun(*this, &LocationUI::add_new_location));
850         add_range_button.signal_clicked().connect (sigc::mem_fun(*this, &LocationUI::add_new_range));
851
852         show_all ();
853
854         signal_map().connect (sigc::mem_fun (*this, &LocationUI::refresh_location_list));
855 }
856
857 LocationUI::~LocationUI()
858 {
859         loop_edit_row.unset_clock_group ();
860         punch_edit_row.unset_clock_group ();
861         delete _clock_group;
862 }
863
864 gint
865 LocationUI::do_location_remove (ARDOUR::Location *loc)
866 {
867         /* this is handled internally by Locations, but there's
868            no point saving state etc. when we know the marker
869            cannot be removed.
870         */
871
872         if (loc->is_session_range()) {
873                 return FALSE;
874         }
875
876         PublicEditor::instance().begin_reversible_command (_("remove marker"));
877         XMLNode &before = _session->locations()->get_state();
878         _session->locations()->remove (loc);
879         XMLNode &after = _session->locations()->get_state();
880         _session->add_command(new MementoCommand<Locations>(*(_session->locations()), &before, &after));
881         PublicEditor::instance().commit_reversible_command ();
882
883         return FALSE;
884 }
885
886 void
887 LocationUI::location_remove_requested (ARDOUR::Location *loc)
888 {
889         // must do this to prevent problems when destroying
890         // the effective sender of this event
891
892         Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &LocationUI::do_location_remove), loc));
893 }
894
895
896 void
897 LocationUI::location_redraw_ranges ()
898 {
899         range_rows.hide();
900         range_rows.show();
901 }
902
903 struct LocationSortByStart {
904         bool operator() (Location *a, Location *b) {
905                 return a->start() < b->start();
906         }
907 };
908
909 void
910 LocationUI::location_added (Location* location)
911 {
912         if (location->is_auto_punch()) {
913                 punch_edit_row.set_location(location);
914         } else if (location->is_auto_loop()) {
915                 loop_edit_row.set_location(location);
916         } else if (location->is_range_marker() || location->is_mark()) {
917                 Locations::LocationList loc = _session->locations()->list ();
918                 loc.sort (LocationSortByStart ());
919
920                 LocationEditRow* erow = manage (new LocationEditRow (_session, location));
921
922                 erow->set_clock_group (*_clock_group);
923                 erow->remove_requested.connect (sigc::mem_fun (*this, &LocationUI::location_remove_requested));
924
925                 Box_Helpers::BoxList & children = location->is_range_marker() ? range_rows.children () : location_rows.children ();
926
927                 /* Step through the location list and the GUI list to find the place to insert */
928                 Locations::LocationList::iterator i = loc.begin ();
929                 Box_Helpers::BoxList::iterator j = children.begin ();
930                 while (i != loc.end()) {
931
932                         if (location->flags() != (*i)->flags()) {
933                                 /* Skip locations in the session list that aren't of the right type */
934                                 ++i;
935                                 continue;
936                         }
937
938                         if (*i == location) {
939                                 children.insert (j, Box_Helpers::Element (*erow, PACK_SHRINK, 1, PACK_START));
940                                 break;
941                         }
942
943                         ++i;
944
945                         if (j != children.end()) {
946                                 ++j;
947                         }
948                 }
949
950                 range_rows.show_all ();
951                 location_rows.show_all ();
952
953                 if (location == newest_location) {
954                         newest_location = 0;
955                         erow->focus_name();
956                 }
957         }
958 }
959
960 void
961 LocationUI::location_removed (Location* location)
962 {
963         ENSURE_GUI_THREAD (*this, &LocationUI::location_removed, location)
964
965         if (location->is_auto_punch()) {
966                 punch_edit_row.set_location(0);
967         } else if (location->is_auto_loop()) {
968                 loop_edit_row.set_location(0);
969         } else if (location->is_range_marker() || location->is_mark()) {
970                 Box_Helpers::BoxList& children = location->is_range_marker() ? range_rows.children () : location_rows.children ();
971                 for (Box_Helpers::BoxList::iterator i = children.begin(); i != children.end(); ++i) {
972                         LocationEditRow* r = dynamic_cast<LocationEditRow*> (i->get_widget());
973                         if (r && r->get_location() == location) {
974                                 children.erase (i);
975                                 break;
976                         }
977                 }
978         }
979 }
980
981 void
982 LocationUI::map_locations (const Locations::LocationList& locations)
983 {
984         Locations::LocationList::iterator i;
985         gint n;
986         int mark_n = 0;
987         Locations::LocationList temp = locations;
988         LocationSortByStart cmp;
989
990         temp.sort (cmp);
991
992         for (n = 0, i = temp.begin(); i != temp.end(); ++n, ++i) {
993
994                 Location* location = *i;
995
996                 if (location->is_mark()) {
997                         LocationEditRow* erow = manage (new LocationEditRow (_session, location, mark_n));
998
999                         erow->set_clock_group (*_clock_group);
1000                         erow->remove_requested.connect (sigc::mem_fun(*this, &LocationUI::location_remove_requested));
1001                         erow->redraw_ranges.connect (sigc::mem_fun(*this, &LocationUI::location_redraw_ranges));
1002
1003                         Box_Helpers::BoxList & loc_children = location_rows.children();
1004                         loc_children.push_back(Box_Helpers::Element(*erow, PACK_SHRINK, 1, PACK_START));
1005                 } else if (location->is_auto_punch()) {
1006                         punch_edit_row.set_session (_session);
1007                         punch_edit_row.set_location (location);
1008                         punch_edit_row.show_all();
1009                 } else if (location->is_auto_loop()) {
1010                         loop_edit_row.set_session (_session);
1011                         loop_edit_row.set_location (location);
1012                         loop_edit_row.show_all();
1013                 } else {
1014                         LocationEditRow* erow = manage (new LocationEditRow(_session, location));
1015
1016                         erow->set_clock_group (*_clock_group);
1017                         erow->remove_requested.connect (sigc::mem_fun(*this, &LocationUI::location_remove_requested));
1018
1019                         Box_Helpers::BoxList & range_children = range_rows.children();
1020                         range_children.push_back(Box_Helpers::Element(*erow,  PACK_SHRINK, 1, PACK_START));
1021                 }
1022         }
1023
1024         range_rows.show_all();
1025         location_rows.show_all();
1026 }
1027
1028 void
1029 LocationUI::add_new_location()
1030 {
1031         string markername;
1032
1033         if (_session) {
1034                 framepos_t where = _session->audible_frame();
1035                 _session->locations()->next_available_name(markername,"mark");
1036                 Location *location = new Location (*_session, where, where, markername, Location::IsMark);
1037                 if (UIConfiguration::instance().get_name_new_markers()) {
1038                         newest_location = location;
1039                 }
1040                 PublicEditor::instance().begin_reversible_command (_("add marker"));
1041                 XMLNode &before = _session->locations()->get_state();
1042                 _session->locations()->add (location, true);
1043                 XMLNode &after = _session->locations()->get_state();
1044                 _session->add_command (new MementoCommand<Locations>(*(_session->locations()), &before, &after));
1045                 PublicEditor::instance().commit_reversible_command ();
1046         }
1047
1048 }
1049
1050 void
1051 LocationUI::add_new_range()
1052 {
1053         string rangename;
1054
1055         if (_session) {
1056                 framepos_t where = _session->audible_frame();
1057                 _session->locations()->next_available_name(rangename,"unnamed");
1058                 Location *location = new Location (*_session, where, where, rangename, Location::IsRangeMarker);
1059                 PublicEditor::instance().begin_reversible_command (_("add range marker"));
1060                 XMLNode &before = _session->locations()->get_state();
1061                 _session->locations()->add (location, true);
1062                 XMLNode &after = _session->locations()->get_state();
1063                 _session->add_command (new MementoCommand<Locations>(*(_session->locations()), &before, &after));
1064                 PublicEditor::instance().commit_reversible_command ();
1065         }
1066 }
1067
1068 void
1069 LocationUI::refresh_location_list ()
1070 {
1071         ENSURE_GUI_THREAD (*this, &LocationUI::refresh_location_list)
1072         using namespace Box_Helpers;
1073
1074         // this is just too expensive to do when window is not shown
1075         if (!is_mapped()) {
1076                 return;
1077         }
1078
1079         BoxList & loc_children = location_rows.children();
1080         BoxList & range_children = range_rows.children();
1081
1082         loc_children.clear();
1083         range_children.clear();
1084
1085         if (_session) {
1086                 _session->locations()->apply (*this, &LocationUI::map_locations);
1087         }
1088 }
1089
1090 void
1091 LocationUI::set_session(ARDOUR::Session* s)
1092 {
1093         SessionHandlePtr::set_session (s);
1094
1095         if (_session) {
1096                 _session->locations()->added.connect (_session_connections, invalidator (*this), boost::bind (&LocationUI::location_added, this, _1), gui_context());
1097                 _session->locations()->removed.connect (_session_connections, invalidator (*this), boost::bind (&LocationUI::location_removed, this, _1), gui_context());
1098                 _session->locations()->changed.connect (_session_connections, invalidator (*this), boost::bind (&LocationUI::refresh_location_list, this), gui_context());
1099
1100                 _clock_group->set_clock_mode (clock_mode_from_session_instant_xml ());
1101         }
1102
1103         loop_edit_row.set_session (s);
1104         punch_edit_row.set_session (s);
1105
1106         refresh_location_list ();
1107 }
1108
1109 void
1110 LocationUI::session_going_away()
1111 {
1112         ENSURE_GUI_THREAD (*this, &LocationUI::session_going_away);
1113
1114         using namespace Box_Helpers;
1115         BoxList & loc_children = location_rows.children();
1116         BoxList & range_children = range_rows.children();
1117
1118         loc_children.clear();
1119         range_children.clear();
1120
1121         loop_edit_row.set_session (0);
1122         loop_edit_row.set_location (0);
1123
1124         punch_edit_row.set_session (0);
1125         punch_edit_row.set_location (0);
1126
1127         SessionHandlePtr::session_going_away ();
1128 }
1129
1130 XMLNode &
1131 LocationUI::get_state () const
1132 {
1133         XMLNode* node = new XMLNode (X_("LocationUI"));
1134         node->add_property (X_("clock-mode"), enum_2_string (_clock_group->clock_mode ()));
1135         return *node;
1136 }
1137
1138 AudioClock::Mode
1139 LocationUI::clock_mode_from_session_instant_xml () const
1140 {
1141         XMLNode* node = _session->instant_xml (X_("LocationUI"));
1142         if (!node) {
1143                 return AudioClock::Frames;
1144         }
1145
1146         XMLProperty const * p = node->property (X_("clock-mode"));
1147         if (!p) {
1148                 return ARDOUR_UI::instance()->secondary_clock->mode();
1149         }
1150
1151         return (AudioClock::Mode) string_2_enum (p->value (), AudioClock::Mode);
1152 }
1153
1154
1155 /*------------------------*/
1156
1157 LocationUIWindow::LocationUIWindow ()
1158         : ArdourWindow (S_("Ranges|Locations"))
1159 {
1160         set_wmclass(X_("ardour_locations"), PROGRAM_NAME);
1161         set_name ("LocationWindow");
1162
1163         add (_ui);
1164 }
1165
1166 LocationUIWindow::~LocationUIWindow()
1167 {
1168 }
1169
1170 void
1171 LocationUIWindow::on_map ()
1172 {
1173         ArdourWindow::on_map ();
1174         _ui.refresh_location_list();
1175 }
1176
1177 bool
1178 LocationUIWindow::on_delete_event (GdkEventAny*)
1179 {
1180         return false;
1181 }
1182
1183 void
1184 LocationUIWindow::set_session (Session *s)
1185 {
1186         ArdourWindow::set_session (s);
1187         _ui.set_session (s);
1188         _ui.show_all ();
1189 }
1190
1191 void
1192 LocationUIWindow::session_going_away ()
1193 {
1194         ArdourWindow::session_going_away ();
1195         hide_all();
1196 }