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