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