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