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