change appearance of panner data popup slightly (fixed width font so it doesn't bounc...
[ardour.git] / gtk2_ardour / editor_markers.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 <sigc++/retype.h>
21 #include <cstdlib>
22 #include <cmath>
23
24 #include <libgnomecanvas/libgnomecanvas.h>
25 #include <gtkmm2ext/gtk_ui.h>
26
27 #include "ardour/session.h"
28 #include "ardour/location.h"
29 #include "ardour/profile.h"
30 #include "pbd/memento_command.h"
31
32 #include "editor.h"
33 #include "marker.h"
34 #include "selection.h"
35 #include "editing.h"
36 #include "gui_thread.h"
37 #include "simplerect.h"
38 #include "actions.h"
39 #include "prompter.h"
40 #include "editor_drag.h"
41
42 #include "i18n.h"
43
44 using namespace std;
45 using namespace ARDOUR;
46 using namespace PBD;
47 using namespace Gtk;
48 using namespace Gtkmm2ext;
49
50 void
51 Editor::clear_marker_display ()
52 {
53         for (LocationMarkerMap::iterator i = location_markers.begin(); i != location_markers.end(); ++i) {
54                 delete i->second;
55         }
56
57         location_markers.clear ();
58         _sorted_marker_lists.clear ();
59 }
60
61 void
62 Editor::add_new_location (Location *location)
63 {
64         ENSURE_GUI_THREAD (*this, &Editor::add_new_location, location)
65
66         LocationMarkers *lam = new LocationMarkers;
67         uint32_t color;
68
69         /* make a note here of which group this marker ends up in */
70         ArdourCanvas::Group* group = 0;
71
72         if (location->is_cd_marker()) {
73                 color = location_cd_marker_color;
74         } else if (location->is_mark()) {
75                 color = location_marker_color;
76         } else if (location->is_auto_loop()) {
77                 color = location_loop_color;
78         } else if (location->is_auto_punch()) {
79                 color = location_punch_color;
80         } else {
81                 color = location_range_color;
82         }
83
84         if (location->is_mark()) {
85
86                 if (location->is_cd_marker() && ruler_cd_marker_action->get_active()) {
87                         lam->start = new Marker (*this, *cd_marker_group, color, location->name(), Marker::Mark, location->start());
88                         group = cd_marker_group;
89                 } else {
90                         lam->start = new Marker (*this, *marker_group, color, location->name(), Marker::Mark, location->start());
91                         group = marker_group;
92                 }
93                 
94                 lam->end = 0;
95
96         } else if (location->is_auto_loop()) {
97                 
98                 // transport marker
99                 lam->start = new Marker (*this, *transport_marker_group, color,
100                                          location->name(), Marker::LoopStart, location->start());
101                 lam->end   = new Marker (*this, *transport_marker_group, color,
102                                          location->name(), Marker::LoopEnd, location->end());
103                 group = transport_marker_group;
104
105         } else if (location->is_auto_punch()) {
106                 
107                 // transport marker
108                 lam->start = new Marker (*this, *transport_marker_group, color,
109                                          location->name(), Marker::PunchIn, location->start());
110                 lam->end   = new Marker (*this, *transport_marker_group, color,
111                                          location->name(), Marker::PunchOut, location->end());
112                 group = transport_marker_group;
113                 
114         } else if (location->is_session_range()) {
115
116                 // session range
117                 lam->start = new Marker (*this, *marker_group, color, _("start"), Marker::SessionStart, location->start());
118                 lam->end = new Marker (*this, *marker_group, color, _("end"), Marker::SessionEnd, location->end());
119                 group = marker_group;
120                 
121         } else {
122                 // range marker
123                 if (location->is_cd_marker() && ruler_cd_marker_action->get_active()) {
124                         lam->start = new Marker (*this, *cd_marker_group, color,
125                                                  location->name(), Marker::RangeStart, location->start());
126                         lam->end   = new Marker (*this, *cd_marker_group, color,
127                                                  location->name(), Marker::RangeEnd, location->end());
128                         group = cd_marker_group;
129                 } else {
130                         lam->start = new Marker (*this, *range_marker_group, color,
131                                                  location->name(), Marker::RangeStart, location->start());
132                         lam->end   = new Marker (*this, *range_marker_group, color,
133                                                  location->name(), Marker::RangeEnd, location->end());
134                         group = range_marker_group;
135                 }
136         }
137
138         if (location->is_hidden ()) {
139                 lam->hide();
140         } else {
141                 lam->show ();
142         }
143
144         location->start_changed.connect (*this, invalidator (*this), ui_bind (&Editor::location_changed, this, _1), gui_context());
145         location->end_changed.connect (*this, invalidator (*this), ui_bind (&Editor::location_changed, this, _1), gui_context());
146         location->changed.connect (*this, invalidator (*this), ui_bind (&Editor::location_changed, this, _1), gui_context());
147         location->name_changed.connect (*this, invalidator (*this), ui_bind (&Editor::location_changed, this, _1), gui_context());
148         location->FlagsChanged.connect (*this, invalidator (*this), ui_bind (&Editor::location_flags_changed, this, _1, _2), gui_context());
149
150         pair<Location*,LocationMarkers*> newpair;
151
152         newpair.first = location;
153         newpair.second = lam;
154
155         location_markers.insert (newpair);
156
157         if (select_new_marker && location->is_mark()) {
158                 selection->set (lam->start);
159                 select_new_marker = false;
160         }
161
162         lam->canvas_height_set (_canvas_height);
163         lam->set_show_lines (_show_marker_lines);
164
165         /* Add these markers to the appropriate sorted marker lists, which will render
166            them unsorted until the update_marker_labels() below sorts them out.
167         */
168         _sorted_marker_lists[group].push_back (lam->start);
169         if (lam->end) {
170                 _sorted_marker_lists[group].push_back (lam->end);
171         }
172
173         /* Do a full update of the markers in this group */
174         update_marker_labels (group);
175 }
176
177 void
178 Editor::location_changed (Location *location)
179 {
180         ENSURE_GUI_THREAD (*this, &Editor::location_changed, location)
181
182         LocationMarkers *lam = find_location_markers (location);
183
184         if (lam == 0) {
185                 /* a location that isn't "marked" with markers */
186                 return;
187         }
188
189         lam->set_name (location->name ());
190         lam->set_position (location->start(), location->end());
191
192         if (location->is_auto_loop()) {
193                 update_loop_range_view ();
194         } else if (location->is_auto_punch()) {
195                 update_punch_range_view ();
196         }
197
198         check_marker_label (lam->start);
199         if (lam->end) {
200                 check_marker_label (lam->end);
201         }
202 }
203
204 /** Look at a marker and check whether its label, and those of the previous and next markers,
205  *  need to have their labels updated (in case those labels need to be shortened or can be
206  *  lengthened)
207  */
208 void
209 Editor::check_marker_label (Marker* m)
210 {
211         /* Get a time-ordered list of markers from the last time anything changed */
212         std::list<Marker*>& sorted = _sorted_marker_lists[m->get_parent()];
213         
214         list<Marker*>::iterator i = find (sorted.begin(), sorted.end(), m);
215
216         list<Marker*>::iterator prev = sorted.end ();
217         list<Marker*>::iterator next = i;
218         ++next;
219
220         /* Look to see if the previous marker is still behind `m' in time */
221         if (i != sorted.begin()) {
222
223                 prev = i;
224                 --prev;
225
226                 if ((*prev)->position() > m->position()) {
227                         /* This marker is no longer in the correct order with the previous one, so
228                          * update all the markers in this group.
229                          */
230                         update_marker_labels (m->get_parent ());
231                         return;
232                 }
233         }
234
235         /* Look to see if the next marker is still ahead of `m' in time */
236         if (next != sorted.end() && (*next)->position() < m->position()) {
237                 /* This marker is no longer in the correct order with the next one, so
238                  * update all the markers in this group.
239                  */
240                 update_marker_labels (m->get_parent ());
241                 return;
242         }
243
244         if (prev != sorted.end()) {
245
246                 /* Update just the available space between the previous marker and this one */
247                 
248                 double const p = frame_to_pixel (m->position() - (*prev)->position());
249
250                 if (m->label_on_left()) {
251                         (*prev)->set_right_label_limit (p / 2);
252                 } else {
253                         (*prev)->set_right_label_limit (p);
254                 }
255                 
256                 if ((*prev)->label_on_left ()) {
257                         m->set_left_label_limit (p);
258                 } else {
259                         m->set_left_label_limit (p / 2);
260                 }
261         }
262
263         if (next != sorted.end()) {
264
265                 /* Update just the available space between this marker and the next */
266                 
267                 double const p = frame_to_pixel ((*next)->position() - m->position());
268
269                 if ((*next)->label_on_left()) {
270                         m->set_right_label_limit (p / 2);
271                 } else {
272                         m->set_right_label_limit (p);
273                 }
274
275                 if (m->label_on_left()) {
276                         (*next)->set_left_label_limit (p);
277                 } else {
278                         (*next)->set_left_label_limit (p / 2);
279                 }
280         }
281 }
282
283 struct MarkerComparator {
284         bool operator() (Marker const * a, Marker const * b) {
285                 return a->position() < b->position();
286         }
287 };
288
289 /** Update all marker labels in all groups */
290 void
291 Editor::update_marker_labels ()
292 {
293         for (std::map<ArdourCanvas::Group *, std::list<Marker *> >::iterator i = _sorted_marker_lists.begin(); i != _sorted_marker_lists.end(); ++i) {
294                 update_marker_labels (i->first);
295         }
296 }
297
298 /** Look at all markers in a group and update label widths */
299 void
300 Editor::update_marker_labels (ArdourCanvas::Group* group)
301 {
302         list<Marker*>& sorted = _sorted_marker_lists[group];
303
304         if (sorted.empty()) {
305                 return;
306         }
307
308         /* We sort the list of markers and then set up the space available between each one */
309         
310         sorted.sort (MarkerComparator ());
311
312         list<Marker*>::iterator i = sorted.begin ();
313
314         list<Marker*>::iterator prev = sorted.end ();
315         list<Marker*>::iterator next = i;
316         ++next;
317         
318         while (i != sorted.end()) {
319
320                 if (prev != sorted.end()) {
321                         double const p = frame_to_pixel ((*i)->position() - (*prev)->position());
322                         
323                         if ((*prev)->label_on_left()) {
324                                 (*i)->set_left_label_limit (p);
325                         } else {
326                                 (*i)->set_left_label_limit (p / 2);
327                         }
328                                 
329                 }
330
331                 if (next != sorted.end()) {
332                         double const p = frame_to_pixel ((*next)->position() - (*i)->position());
333
334                         if ((*next)->label_on_left()) {
335                                 (*i)->set_right_label_limit (p / 2);
336                         } else {
337                                 (*i)->set_right_label_limit (p);
338                         }
339                 }
340
341                 prev = i;
342                 ++i;
343                 ++next;
344         }
345 }
346
347 void
348 Editor::location_flags_changed (Location *location, void*)
349 {
350         ENSURE_GUI_THREAD (*this, &Editor::location_flags_changed, location, src)
351
352         LocationMarkers *lam = find_location_markers (location);
353
354         if (lam == 0) {
355                 /* a location that isn't "marked" with markers */
356                 return;
357         }
358
359         // move cd markers to/from cd marker bar as appropriate
360         ensure_cd_marker_updated (lam, location);
361
362         if (location->is_cd_marker()) {
363                 lam->set_color_rgba (location_cd_marker_color);
364         } else if (location->is_mark()) {
365                 lam->set_color_rgba (location_marker_color);
366         } else if (location->is_auto_punch()) {
367                 lam->set_color_rgba (location_punch_color);
368         } else if (location->is_auto_loop()) {
369                 lam->set_color_rgba (location_loop_color);
370         } else {
371                 lam->set_color_rgba (location_range_color);
372         }
373
374         if (location->is_hidden()) {
375                 lam->hide();
376         } else {
377                 lam->show ();
378         }
379 }
380
381 void Editor::update_cd_marker_display ()
382 {
383         for (LocationMarkerMap::iterator i = location_markers.begin(); i != location_markers.end(); ++i) {
384                 LocationMarkers * lam = i->second;
385                 Location * location = i->first;
386
387                 ensure_cd_marker_updated (lam, location);
388         }
389 }
390
391 void Editor::ensure_cd_marker_updated (LocationMarkers * lam, Location * location)
392 {
393         if (location->is_cd_marker()
394             && (ruler_cd_marker_action->get_active() &&  lam->start->get_parent() != cd_marker_group))
395         {
396                 //cerr << "reparenting non-cd marker so it can be relocated: " << location->name() << endl;
397                 if (lam->start) {
398                         lam->start->reparent (*cd_marker_group);
399                 }
400                 if (lam->end) {
401                         lam->end->reparent (*cd_marker_group);
402                 }
403         }
404         else if ( (!location->is_cd_marker() || !ruler_cd_marker_action->get_active())
405                   && (lam->start->get_parent() == cd_marker_group))
406         {
407                 //cerr << "reparenting non-cd marker so it can be relocated: " << location->name() << endl;
408                 if (location->is_mark()) {
409                         if (lam->start) {
410                                 lam->start->reparent (*marker_group);
411                         }
412                         if (lam->end) {
413                                 lam->end->reparent (*marker_group);
414                         }
415                 }
416                 else {
417                         if (lam->start) {
418                                 lam->start->reparent (*range_marker_group);
419                         }
420                         if (lam->end) {
421                                 lam->end->reparent (*range_marker_group);
422                         }
423                 }
424         }
425 }
426
427 Editor::LocationMarkers::~LocationMarkers ()
428 {
429         delete start;
430         delete end;
431 }
432
433 Editor::LocationMarkers *
434 Editor::find_location_markers (Location *location) const
435 {
436         LocationMarkerMap::const_iterator i;
437
438         for (i = location_markers.begin(); i != location_markers.end(); ++i) {
439                 if ((*i).first == location) {
440                         return (*i).second;
441                 }
442         }
443
444         return 0;
445 }
446
447 Location *
448 Editor::find_location_from_marker (Marker *marker, bool& is_start) const
449 {
450         LocationMarkerMap::const_iterator i;
451
452         for (i = location_markers.begin(); i != location_markers.end(); ++i) {
453                 LocationMarkers *lm = (*i).second;
454                 if (lm->start == marker) {
455                         is_start = true;
456                         return (*i).first;
457                 } else if (lm->end == marker) {
458                         is_start = false;
459                         return (*i).first;
460                 }
461         }
462
463         return 0;
464 }
465
466 void
467 Editor::refresh_location_display_internal (Locations::LocationList& locations)
468 {
469         /* invalidate all */
470
471         for (LocationMarkerMap::iterator i = location_markers.begin(); i != location_markers.end(); ++i) {
472                 i->second->valid = false;
473         }
474
475         /* add new ones */
476
477         for (Locations::LocationList::iterator i = locations.begin(); i != locations.end(); ++i) {
478
479                 LocationMarkerMap::iterator x;
480
481                 if ((x = location_markers.find (*i)) != location_markers.end()) {
482                         x->second->valid = true;
483                         continue;
484                 }
485
486                 add_new_location (*i);
487         }
488
489         /* remove dead ones */
490
491         for (LocationMarkerMap::iterator i = location_markers.begin(); i != location_markers.end(); ) {
492
493                 LocationMarkerMap::iterator tmp;
494
495                 tmp = i;
496                 ++tmp;
497
498                 if (!i->second->valid) {
499
500                         remove_sorted_marker (i->second->start);
501                         if (i->second->end) {
502                                 remove_sorted_marker (i->second->end);
503                         }
504                         
505                         delete i->second;
506                         location_markers.erase (i);
507                 }
508
509                 i = tmp;
510         }
511
512         update_punch_range_view (false);
513         update_loop_range_view (false);
514 }
515
516 void
517 Editor::refresh_location_display ()
518 {
519         ENSURE_GUI_THREAD (*this, &Editor::refresh_location_display)
520
521         if (_session) {
522                 _session->locations()->apply (*this, &Editor::refresh_location_display_internal);
523         }
524 }
525
526 void
527 Editor::refresh_location_display_s (const PropertyChange&)
528 {
529         ENSURE_GUI_THREAD (*this, &Editor::refresh_location_display_s, ignored)
530
531         if (_session) {
532                 _session->locations()->apply (*this, &Editor::refresh_location_display_internal);
533         }
534 }
535
536 void
537 Editor::LocationMarkers::hide()
538 {
539         start->hide ();
540         if (end) {
541                 end->hide ();
542         }
543 }
544
545 void
546 Editor::LocationMarkers::show()
547 {
548         start->show ();
549         if (end) {
550                 end->show ();
551         }
552 }
553
554 void
555 Editor::LocationMarkers::canvas_height_set (double h)
556 {
557         start->canvas_height_set (h);
558         if (end) {
559                 end->canvas_height_set (h);
560         }
561 }
562
563 void
564 Editor::LocationMarkers::set_name (const string& str)
565 {
566         /* XXX: hack: don't change names of session start/end markers */
567
568         if (start->type() != Marker::SessionStart) {
569                 start->set_name (str);
570         }
571         
572         if (end && end->type() != Marker::SessionEnd) {
573                 end->set_name (str);
574         }
575 }
576
577 void
578 Editor::LocationMarkers::set_position (framepos_t startf,
579                                        framepos_t endf)
580 {
581         start->set_position (startf);
582         if (end) {
583                 end->set_position (endf);
584         }
585 }
586
587 void
588 Editor::LocationMarkers::set_color_rgba (uint32_t rgba)
589 {
590         start->set_color_rgba (rgba);
591         if (end) {
592                 end->set_color_rgba (rgba);
593         }
594 }
595
596 void
597 Editor::LocationMarkers::set_show_lines (bool s)
598 {
599         start->set_show_line (s);
600         if (end) {
601                 end->set_show_line (s);
602         }
603 }
604
605 void
606 Editor::LocationMarkers::set_selected (bool s)
607 {
608         start->set_selected (s);
609         if (end) {
610                 end->set_selected (s);
611         }
612 }
613
614 void
615 Editor::LocationMarkers::setup_lines ()
616 {
617         start->setup_line ();
618         if (end) {
619                 end->setup_line ();
620         }
621 }
622
623 void
624 Editor::mouse_add_new_marker (framepos_t where, bool is_cd, bool is_xrun)
625 {
626         string markername, markerprefix;
627         int flags = (is_cd ? Location::IsCDMarker|Location::IsMark : Location::IsMark);
628
629         if (is_xrun) {
630                 markerprefix = "xrun";
631                 flags = Location::IsMark;
632         } else {
633                 markerprefix = "mark";
634         }
635
636         if (_session) {
637                 _session->locations()->next_available_name(markername, markerprefix);
638                 if (!is_xrun && !choose_new_marker_name(markername)) {
639                         return;
640                 }
641                 Location *location = new Location (*_session, where, where, markername, (Location::Flags) flags);
642                 _session->begin_reversible_command (_("add marker"));
643                 XMLNode &before = _session->locations()->get_state();
644                 _session->locations()->add (location, true);
645                 XMLNode &after = _session->locations()->get_state();
646                 _session->add_command (new MementoCommand<Locations>(*(_session->locations()), &before, &after));
647                 _session->commit_reversible_command ();
648
649                 /* find the marker we just added */
650
651                 LocationMarkers *lam = find_location_markers (location);
652                 if (lam) {
653                         /* make it the selected marker */
654                         selection->set (lam->start);
655                 }
656         }
657 }
658
659 void
660 Editor::remove_marker (ArdourCanvas::Item& item, GdkEvent*)
661 {
662         Marker* marker;
663         bool is_start;
664
665         if ((marker = static_cast<Marker*> (item.get_data ("marker"))) == 0) {
666                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
667                 /*NOTREACHED*/
668         }
669
670         if (entered_marker == marker) {
671                 entered_marker = NULL;
672         }
673
674         Location* loc = find_location_from_marker (marker, is_start);
675
676         if (_session && loc) {
677                 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::really_remove_marker), loc));
678         }
679 }
680
681 gint
682 Editor::really_remove_marker (Location* loc)
683 {
684         _session->begin_reversible_command (_("remove marker"));
685         XMLNode &before = _session->locations()->get_state();
686         _session->locations()->remove (loc);
687         XMLNode &after = _session->locations()->get_state();
688         _session->add_command (new MementoCommand<Locations>(*(_session->locations()), &before, &after));
689         _session->commit_reversible_command ();
690         return FALSE;
691 }
692
693 void
694 Editor::location_gone (Location *location)
695 {
696         ENSURE_GUI_THREAD (*this, &Editor::location_gone, location)
697
698         LocationMarkerMap::iterator i;
699
700         if (location == transport_loop_location()) {
701                 update_loop_range_view (true);
702         }
703
704         if (location == transport_punch_location()) {
705                 update_punch_range_view (true);
706         }
707
708         for (i = location_markers.begin(); i != location_markers.end(); ++i) {
709                 if (i->first == location) {
710
711                         remove_sorted_marker (i->second->start);
712                         if (i->second->end) {
713                                 remove_sorted_marker (i->second->end);
714                         }
715                         
716                         
717                         delete i->second;
718                         location_markers.erase (i);
719                         break;
720                 }
721         }
722 }
723
724 void
725 Editor::tempo_or_meter_marker_context_menu (GdkEventButton* ev, ArdourCanvas::Item* item)
726 {
727         marker_menu_item = item;
728         
729         MeterMarker* mm;
730         TempoMarker* tm;
731         dynamic_cast_marker_object (marker_menu_item->get_data ("marker"), &mm, &tm);
732
733         bool can_remove = false;
734
735         if (mm) {
736                 can_remove = mm->meter().movable ();
737         } else if (tm) {
738                 can_remove = tm->tempo().movable ();
739         } else {
740                 return;
741         }
742         
743         delete tempo_or_meter_marker_menu;
744         build_tempo_or_meter_marker_menu (can_remove);
745         tempo_or_meter_marker_menu->popup (1, ev->time);
746 }
747
748 void
749 Editor::marker_context_menu (GdkEventButton* ev, ArdourCanvas::Item* item)
750 {
751         Marker * marker;
752         if ((marker = reinterpret_cast<Marker *> (item->get_data("marker"))) == 0) {
753                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
754                 /*NOTREACHED*/
755         }
756
757         bool is_start;
758         Location * loc = find_location_from_marker (marker, is_start);
759
760         if (loc == transport_loop_location() || loc == transport_punch_location() || loc->is_session_range ()) {
761
762                 if (transport_marker_menu == 0) {
763                         build_range_marker_menu (true);
764                 }
765                 
766                 marker_menu_item = item;
767                 transport_marker_menu->popup (1, ev->time);
768
769         } else if (loc->is_mark()) {
770                 
771                         delete marker_menu;
772                         build_marker_menu (loc);
773
774                 // GTK2FIX use action group sensitivity
775 #ifdef GTK2FIX
776                         if (children.size() >= 3) {
777                                 MenuItem * loopitem = &children[2];
778                                 if (loopitem) {
779                                         if (loc->is_mark()) {
780                                                 loopitem->set_sensitive(false);
781                                         }
782                                         else {
783                                                 loopitem->set_sensitive(true);
784                                         }
785                                 }
786                         }
787 #endif
788                         marker_menu_item = item;
789                         marker_menu->popup (1, ev->time);
790                         
791         } else if (loc->is_range_marker()) {
792                 if (range_marker_menu == 0) {
793                         build_range_marker_menu (false);
794                 }
795                 marker_menu_item = item;
796                 range_marker_menu->popup (1, ev->time);
797         }
798 }
799
800 void
801 Editor::new_transport_marker_context_menu (GdkEventButton* ev, ArdourCanvas::Item*)
802 {
803         if (new_transport_marker_menu == 0) {
804                 build_new_transport_marker_menu ();
805         }
806
807         new_transport_marker_menu->popup (1, ev->time);
808
809 }
810
811 void
812 Editor::transport_marker_context_menu (GdkEventButton* ev, ArdourCanvas::Item*)
813 {
814         if (transport_marker_menu == 0) {
815                 build_range_marker_menu (true);
816         }
817
818         transport_marker_menu->popup (1, ev->time);
819 }
820
821 void
822 Editor::build_marker_menu (Location* loc)
823 {
824         using namespace Menu_Helpers;
825
826         marker_menu = new Menu;
827         MenuList& items = marker_menu->items();
828         marker_menu->set_name ("ArdourContextMenu");
829
830         items.push_back (MenuElem (_("Locate to Here"), sigc::mem_fun(*this, &Editor::marker_menu_set_playhead)));
831         items.push_back (MenuElem (_("Play from Here"), sigc::mem_fun(*this, &Editor::marker_menu_play_from)));
832         items.push_back (MenuElem (_("Move Mark to Playhead"), sigc::mem_fun(*this, &Editor::marker_menu_set_from_playhead)));
833
834         items.push_back (SeparatorElem());
835
836         items.push_back (MenuElem (_("Create Range to Next Marker"), sigc::mem_fun(*this, &Editor::marker_menu_range_to_next)));
837
838         items.push_back (MenuElem (_("Hide"), sigc::mem_fun(*this, &Editor::marker_menu_hide)));
839         items.push_back (MenuElem (_("Rename"), sigc::mem_fun(*this, &Editor::marker_menu_rename)));
840
841         items.push_back (CheckMenuElem (_("Lock")));
842         CheckMenuItem* lock_item = static_cast<CheckMenuItem*> (&items.back());
843         if (loc->locked ()) {
844                 lock_item->set_active ();
845         }
846         lock_item->signal_activate().connect (sigc::mem_fun (*this, &Editor::toggle_marker_menu_lock));
847
848         items.push_back (CheckMenuElem (_("Glue to Bars and Beats")));
849         CheckMenuItem* glue_item = static_cast<CheckMenuItem*> (&items.back());
850         if (loc->position_lock_style() == MusicTime) {
851                 glue_item->set_active ();
852         }
853         glue_item->signal_activate().connect (sigc::mem_fun (*this, &Editor::toggle_marker_menu_glue));
854         
855         items.push_back (SeparatorElem());
856
857         items.push_back (MenuElem (_("Remove"), sigc::mem_fun(*this, &Editor::marker_menu_remove)));
858 }
859
860 void
861 Editor::build_range_marker_menu (bool loop_or_punch_or_session)
862 {
863         using namespace Menu_Helpers;
864
865         Menu *markerMenu = new Menu;
866         if (loop_or_punch_or_session) {
867                 transport_marker_menu = markerMenu;
868         } else {
869                 range_marker_menu = markerMenu;
870         }
871         MenuList& items = markerMenu->items();
872         markerMenu->set_name ("ArdourContextMenu");
873
874         items.push_back (MenuElem (_("Play Range"), sigc::mem_fun(*this, &Editor::marker_menu_play_range)));
875         items.push_back (MenuElem (_("Locate to Range Mark"), sigc::mem_fun(*this, &Editor::marker_menu_set_playhead)));
876         items.push_back (MenuElem (_("Play from Range Mark"), sigc::mem_fun(*this, &Editor::marker_menu_play_from)));
877         if (!loop_or_punch_or_session) {
878                 items.push_back (MenuElem (_("Loop Range"), sigc::mem_fun(*this, &Editor::marker_menu_loop_range)));
879         }
880         items.push_back (MenuElem (_("Set Range Mark from Playhead"), sigc::mem_fun(*this, &Editor::marker_menu_set_from_playhead)));
881         if (!Profile->get_sae()) {
882                 items.push_back (MenuElem (_("Set Range from Range Selection"), sigc::mem_fun(*this, &Editor::marker_menu_set_from_selection)));
883         }
884
885         items.push_back (SeparatorElem());
886         items.push_back (MenuElem (_("Export Range"), sigc::mem_fun(*this, &Editor::export_range)));
887         items.push_back (SeparatorElem());
888
889         if (!loop_or_punch_or_session) {
890                 items.push_back (MenuElem (_("Hide Range"), sigc::mem_fun(*this, &Editor::marker_menu_hide)));
891                 items.push_back (MenuElem (_("Rename Range"), sigc::mem_fun(*this, &Editor::marker_menu_rename)));
892                 items.push_back (MenuElem (_("Remove Range"), sigc::mem_fun(*this, &Editor::marker_menu_remove)));
893                 items.push_back (SeparatorElem());
894         }
895
896         items.push_back (MenuElem (_("Separate Regions in Range"), sigc::mem_fun(*this, &Editor::marker_menu_separate_regions_using_location)));
897         items.push_back (MenuElem (_("Select All in Range"), sigc::mem_fun(*this, &Editor::marker_menu_select_all_selectables_using_range)));
898         if (!Profile->get_sae()) {
899                 items.push_back (MenuElem (_("Select Range"), sigc::mem_fun(*this, &Editor::marker_menu_select_using_range)));
900         }
901 }
902
903 void
904 Editor::build_tempo_or_meter_marker_menu (bool can_remove)
905 {
906         using namespace Menu_Helpers;
907
908         tempo_or_meter_marker_menu = new Menu;
909         MenuList& items = tempo_or_meter_marker_menu->items();
910         tempo_or_meter_marker_menu->set_name ("ArdourContextMenu");
911
912         items.push_back (MenuElem (_("Edit"), sigc::mem_fun(*this, &Editor::marker_menu_edit)));
913         items.push_back (MenuElem (_("Remove"), sigc::mem_fun(*this, &Editor::marker_menu_remove)));
914
915         items.back().set_sensitive (can_remove);
916 }
917
918 void
919 Editor::build_new_transport_marker_menu ()
920 {
921         using namespace Menu_Helpers;
922
923         new_transport_marker_menu = new Menu;
924         MenuList& items = new_transport_marker_menu->items();
925         new_transport_marker_menu->set_name ("ArdourContextMenu");
926
927         items.push_back (MenuElem (_("Set Loop Range"), sigc::mem_fun(*this, &Editor::new_transport_marker_menu_set_loop)));
928         items.push_back (MenuElem (_("Set Punch Range"), sigc::mem_fun(*this, &Editor::new_transport_marker_menu_set_punch)));
929
930         new_transport_marker_menu->signal_unmap().connect ( sigc::mem_fun(*this, &Editor::new_transport_marker_menu_popdown));
931 }
932
933 void
934 Editor::marker_menu_hide ()
935 {
936         Marker* marker;
937
938         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
939                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
940                 /*NOTREACHED*/
941         }
942
943         Location* l;
944         bool is_start;
945
946         if ((l = find_location_from_marker (marker, is_start)) != 0) {
947                 l->set_hidden (true, this);
948         }
949 }
950
951 void
952 Editor::marker_menu_select_using_range ()
953 {
954         Marker* marker;
955
956         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
957                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
958                 /*NOTREACHED*/
959         }
960
961         Location* l;
962         bool is_start;
963
964         if (((l = find_location_from_marker (marker, is_start)) != 0) && (l->end() > l->start())) {
965                 set_selection_from_range (*l);
966         }
967 }
968
969 void
970 Editor::marker_menu_select_all_selectables_using_range ()
971 {
972         Marker* marker;
973
974         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
975                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
976                 /*NOTREACHED*/
977         }
978
979         Location* l;
980         bool is_start;
981
982         if (((l = find_location_from_marker (marker, is_start)) != 0) && (l->end() > l->start())) {
983                 select_all_within (l->start(), l->end() - 1, 0,  DBL_MAX, track_views, Selection::Set, false);
984         }
985
986 }
987
988 void
989 Editor::marker_menu_separate_regions_using_location ()
990 {
991         Marker* marker;
992
993         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
994                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
995                 /*NOTREACHED*/
996         }
997
998         Location* l;
999         bool is_start;
1000
1001         if (((l = find_location_from_marker (marker, is_start)) != 0) && (l->end() > l->start())) {
1002                 separate_regions_using_location (*l);
1003         }
1004
1005 }
1006
1007 void
1008 Editor::marker_menu_play_from ()
1009 {
1010         Marker* marker;
1011
1012         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
1013                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
1014                 /*NOTREACHED*/
1015         }
1016
1017         Location* l;
1018         bool is_start;
1019
1020         if ((l = find_location_from_marker (marker, is_start)) != 0) {
1021
1022                 if (l->is_mark()) {
1023                         _session->request_locate (l->start(), true);
1024                 }
1025                 else {
1026                         //_session->request_bounded_roll (l->start(), l->end());
1027
1028                         if (is_start) {
1029                                 _session->request_locate (l->start(), true);
1030                         } else {
1031                                 _session->request_locate (l->end(), true);
1032                         }
1033                 }
1034         }
1035 }
1036
1037 void
1038 Editor::marker_menu_set_playhead ()
1039 {
1040         Marker* marker;
1041
1042         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
1043                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
1044                 /*NOTREACHED*/
1045         }
1046
1047         Location* l;
1048         bool is_start;
1049
1050         if ((l = find_location_from_marker (marker, is_start)) != 0) {
1051
1052                 if (l->is_mark()) {
1053                         _session->request_locate (l->start(), false);
1054                 }
1055                 else {
1056                         if (is_start) {
1057                                 _session->request_locate (l->start(), false);
1058                         } else {
1059                                 _session->request_locate (l->end(), false);
1060                         }
1061                 }
1062         }
1063 }
1064
1065 void
1066 Editor::marker_menu_range_to_next ()
1067 {
1068         Marker* marker;
1069         if (!_session) {
1070                 return;
1071         }
1072
1073         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
1074                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
1075                 /*NOTREACHED*/
1076         }
1077
1078         Location* l;
1079         bool is_start;
1080
1081         if ((l = find_location_from_marker (marker, is_start)) == 0) {
1082                 return;
1083         }
1084
1085         framepos_t start;
1086         framepos_t end;
1087         _session->locations()->marks_either_side (marker->position(), start, end);
1088
1089         if (end != max_framepos) {
1090                 string range_name = l->name();
1091                 range_name += "-range";
1092
1093                 Location* newrange = new Location (*_session, marker->position(), end, range_name, Location::IsRangeMarker);
1094                 _session->locations()->add (newrange);
1095         }
1096 }
1097
1098 void
1099 Editor::marker_menu_set_from_playhead ()
1100 {
1101         Marker* marker;
1102
1103         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
1104                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
1105                 /*NOTREACHED*/
1106         }
1107
1108         Location* l;
1109         bool is_start;
1110
1111         if ((l = find_location_from_marker (marker, is_start)) != 0) {
1112
1113                 if (l->is_mark()) {
1114                         l->set_start (_session->audible_frame ());
1115                 }
1116                 else {
1117                         if (is_start) {
1118                                 l->set_start (_session->audible_frame ());
1119                         } else {
1120                                 l->set_end (_session->audible_frame ());
1121                         }
1122                 }
1123         }
1124 }
1125
1126 void
1127 Editor::marker_menu_set_from_selection ()
1128 {
1129         Marker* marker;
1130
1131         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
1132                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
1133                 /*NOTREACHED*/
1134         }
1135
1136         Location* l;
1137         bool is_start;
1138
1139         if ((l = find_location_from_marker (marker, is_start)) != 0) {
1140
1141                 if (l->is_mark()) {
1142                         // nothing for now
1143                 }
1144                 else {
1145
1146                         /* if range selection use first to last */
1147
1148                         if (mouse_mode == Editing::MouseRange) {
1149                                 if (!selection->time.empty()) {
1150                                         l->set_start (selection->time.start());
1151                                         l->set_end (selection->time.end_frame());
1152                                 }
1153                         }
1154                         else {
1155                                 if (!selection->regions.empty()) {
1156                                         l->set_start (selection->regions.start());
1157                                         l->set_end (selection->regions.end_frame());
1158                                 }
1159                         }
1160                 }
1161         }
1162 }
1163
1164
1165 void
1166 Editor::marker_menu_play_range ()
1167 {
1168         Marker* marker;
1169
1170         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
1171                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
1172                 /*NOTREACHED*/
1173         }
1174
1175         Location* l;
1176         bool is_start;
1177
1178         if ((l = find_location_from_marker (marker, is_start)) != 0) {
1179
1180                 if (l->is_mark()) {
1181                         _session->request_locate (l->start(), true);
1182                 }
1183                 else {
1184                         _session->request_bounded_roll (l->start(), l->end());
1185
1186                 }
1187         }
1188 }
1189
1190 void
1191 Editor::marker_menu_loop_range ()
1192 {
1193         Marker* marker;
1194
1195         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
1196                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
1197                 /*NOTREACHED*/
1198         }
1199
1200         Location* l;
1201         bool is_start;
1202
1203         if ((l = find_location_from_marker (marker, is_start)) != 0) {
1204                 Location* l2;
1205                 if ((l2 = transport_loop_location()) != 0) {
1206                         l2->set (l->start(), l->end());
1207
1208                         // enable looping, reposition and start rolling
1209                         _session->request_play_loop(true);
1210                         _session->request_locate (l2->start(), true);
1211                 }
1212         }
1213 }
1214
1215 void
1216 Editor::dynamic_cast_marker_object (void* p, MeterMarker** m, TempoMarker** t) const
1217 {
1218         Marker* marker = reinterpret_cast<Marker*> (p);
1219         if (!marker) {
1220                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
1221                 /*NOTREACHED*/
1222         }
1223
1224         *m = dynamic_cast<MeterMarker*> (marker);
1225         *t = dynamic_cast<TempoMarker*> (marker);
1226 }
1227
1228 void
1229 Editor::marker_menu_edit ()
1230 {
1231         MeterMarker* mm;
1232         TempoMarker* tm;
1233         dynamic_cast_marker_object (marker_menu_item->get_data ("marker"), &mm, &tm);
1234
1235         if (mm) {
1236                 edit_meter_section (&mm->meter());
1237         } else if (tm) {
1238                 edit_tempo_section (&tm->tempo());
1239         }
1240 }
1241
1242 void
1243 Editor::marker_menu_remove ()
1244 {
1245         MeterMarker* mm;
1246         TempoMarker* tm;
1247         dynamic_cast_marker_object (marker_menu_item->get_data ("marker"), &mm, &tm);
1248
1249         if (mm) {
1250                 remove_meter_marker (marker_menu_item);
1251         } else if (tm) {
1252                 remove_tempo_marker (marker_menu_item);
1253         } else {
1254                 remove_marker (*marker_menu_item, (GdkEvent*) 0);
1255         }
1256 }
1257
1258 void
1259 Editor::toggle_marker_menu_lock ()
1260 {
1261         Marker* marker;
1262
1263         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
1264                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
1265                 /*NOTREACHED*/
1266         }
1267
1268         Location* loc;
1269         bool ignored;
1270
1271         loc = find_location_from_marker (marker, ignored);
1272
1273         if (!loc) {
1274                 return;
1275         }
1276
1277         if (loc->locked()) {
1278                 loc->unlock ();
1279         } else {
1280                 loc->lock ();
1281         }
1282 }
1283
1284 void
1285 Editor::marker_menu_rename ()
1286 {
1287         Marker* marker;
1288
1289         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
1290                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
1291                 /*NOTREACHED*/
1292         }
1293
1294         Location* loc;
1295         bool is_start;
1296
1297         loc = find_location_from_marker (marker, is_start);
1298
1299         if (!loc) return;
1300
1301         ArdourPrompter dialog (true);
1302         string txt;
1303
1304         dialog.set_prompt (_("New Name:"));
1305
1306         if (loc->is_mark()) {
1307                 dialog.set_title (_("Rename Mark"));
1308         } else {
1309                 dialog.set_title (_("Rename Range"));
1310         }
1311
1312         dialog.set_name ("MarkRenameWindow");
1313         dialog.set_size_request (250, -1);
1314         dialog.set_position (Gtk::WIN_POS_MOUSE);
1315
1316         dialog.add_button (_("Rename"), RESPONSE_ACCEPT);
1317         dialog.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
1318         dialog.set_initial_text (loc->name());
1319
1320         dialog.show ();
1321
1322         switch (dialog.run ()) {
1323         case RESPONSE_ACCEPT:
1324                 break;
1325         default:
1326                 return;
1327         }
1328
1329         begin_reversible_command ( _("rename marker") );
1330         XMLNode &before = _session->locations()->get_state();
1331
1332         dialog.get_result(txt);
1333         loc->set_name (txt);
1334
1335         XMLNode &after = _session->locations()->get_state();
1336         _session->add_command (new MementoCommand<Locations>(*(_session->locations()), &before, &after));
1337         commit_reversible_command ();
1338 }
1339
1340 void
1341 Editor::new_transport_marker_menu_popdown ()
1342 {
1343         // hide rects
1344         transport_bar_drag_rect->hide();
1345
1346         _drags->abort ();
1347 }
1348
1349 void
1350 Editor::new_transport_marker_menu_set_loop ()
1351 {
1352         set_loop_range (temp_location->start(), temp_location->end(), _("set loop range"));
1353 }
1354
1355 void
1356 Editor::new_transport_marker_menu_set_punch ()
1357 {
1358         set_punch_range (temp_location->start(), temp_location->end(), _("set punch range"));
1359 }
1360
1361 void
1362 Editor::update_loop_range_view (bool visibility)
1363 {
1364         if (_session == 0) {
1365                 return;
1366         }
1367
1368         Location* tll;
1369
1370         if (_session->get_play_loop() && ((tll = transport_loop_location()) != 0)) {
1371
1372                 double x1 = frame_to_pixel (tll->start());
1373                 double x2 = frame_to_pixel (tll->end());
1374
1375                 transport_loop_range_rect->property_x1() = x1;
1376                 transport_loop_range_rect->property_x2() = x2;
1377
1378                 if (visibility) {
1379                         transport_loop_range_rect->show();
1380                 }
1381
1382         } else if (visibility) {
1383                 transport_loop_range_rect->hide();
1384         }
1385 }
1386
1387 void
1388 Editor::update_punch_range_view (bool visibility)
1389 {
1390         if (_session == 0) {
1391                 return;
1392         }
1393
1394         Location* tpl;
1395
1396         if ((_session->config.get_punch_in() || _session->config.get_punch_out()) && ((tpl = transport_punch_location()) != 0)) {
1397                 guint track_canvas_width,track_canvas_height;
1398                 track_canvas->get_size(track_canvas_width,track_canvas_height);
1399                 if (_session->config.get_punch_in()) {
1400                         transport_punch_range_rect->property_x1() = frame_to_pixel (tpl->start());
1401                         transport_punch_range_rect->property_x2() = (_session->config.get_punch_out() ? frame_to_pixel (tpl->end()) : frame_to_pixel (JACK_MAX_FRAMES));
1402                 } else {
1403                         transport_punch_range_rect->property_x1() = 0;
1404                         transport_punch_range_rect->property_x2() = (_session->config.get_punch_out() ? frame_to_pixel (tpl->end()) : track_canvas_width);
1405                 }
1406
1407                 if (visibility) {
1408                         transport_punch_range_rect->show();
1409                 }
1410         } else if (visibility) {
1411                 transport_punch_range_rect->hide();
1412         }
1413 }
1414
1415 void
1416 Editor::marker_selection_changed ()
1417 {
1418         if (_session && _session->deletion_in_progress()) {
1419                 return;
1420         }
1421
1422         for (LocationMarkerMap::iterator i = location_markers.begin(); i != location_markers.end(); ++i) {
1423                 i->second->set_selected (false);
1424         }
1425
1426         for (MarkerSelection::iterator x = selection->markers.begin(); x != selection->markers.end(); ++x) {
1427                 (*x)->set_selected (true);
1428         }
1429 }
1430
1431 struct SortLocationsByPosition {
1432     bool operator() (Location* a, Location* b) {
1433             return a->start() < b->start();
1434     }
1435 };
1436
1437 void
1438 Editor::goto_nth_marker (int n)
1439 {
1440         if (!_session) {
1441                 return;
1442         }
1443         const Locations::LocationList& l (_session->locations()->list());
1444         Locations::LocationList ordered;
1445         ordered = l;
1446
1447         SortLocationsByPosition cmp;
1448         ordered.sort (cmp);
1449
1450         for (Locations::LocationList::iterator i = ordered.begin(); n >= 0 && i != ordered.end(); ++i) {
1451                 if ((*i)->is_mark() && !(*i)->is_hidden() && !(*i)->is_session_range()) {
1452                         if (n == 0) {
1453                                 _session->request_locate ((*i)->start(), _session->transport_rolling());
1454                                 break;
1455                         }
1456                         --n;
1457                 }
1458         }
1459 }
1460
1461 void
1462 Editor::toggle_marker_menu_glue ()
1463 {
1464         Marker* marker;
1465
1466         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
1467                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
1468                 /*NOTREACHED*/
1469         }
1470
1471         Location* loc;
1472         bool ignored;
1473
1474         loc = find_location_from_marker (marker, ignored);
1475
1476         if (!loc) {
1477                 return;
1478         }
1479
1480         if (loc->position_lock_style() == MusicTime) {
1481                 loc->set_position_lock_style (AudioTime);
1482         } else {
1483                 loc->set_position_lock_style (MusicTime);
1484         }
1485
1486 }
1487
1488 void
1489 Editor::toggle_marker_lines ()
1490 {
1491         _show_marker_lines = !_show_marker_lines;
1492         
1493         for (LocationMarkerMap::iterator i = location_markers.begin(); i != location_markers.end(); ++i) {
1494                 i->second->set_show_lines (_show_marker_lines);
1495         }
1496 }
1497
1498 void
1499 Editor::remove_sorted_marker (Marker* m)
1500 {
1501         for (std::map<ArdourCanvas::Group *, std::list<Marker *> >::iterator i = _sorted_marker_lists.begin(); i != _sorted_marker_lists.end(); ++i) {
1502                 i->second.remove (m);
1503         }
1504 }