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