fix problems with undo/redo and region dragging; fix dragging in lock edit mode
[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 <sigc++/retype.h>
21 #include <cstdlib>
22 #include <cmath>
23
24 #include <libgnomecanvas/libgnomecanvas.h>
25 #include <gtkmm2ext/gtk_ui.h>
26 #include <gtkmm2ext/window_title.h>
27
28 #include <ardour/location.h>
29 #include <ardour/profile.h>
30 #include <pbd/memento_command.h>
31
32 #include "editor.h"
33 #include "marker.h"
34 #include "selection.h"
35 #include "editing.h"
36 #include "gui_thread.h"
37 #include "simplerect.h"
38 #include "actions.h"
39 #include "prompter.h"
40
41 #include "i18n.h"
42
43 using namespace std;
44 using namespace sigc;
45 using namespace ARDOUR;
46 using namespace PBD;
47 using namespace Gtk;
48 using namespace Gtkmm2ext;
49
50 void
51 Editor::clear_marker_display ()
52 {
53         for (LocationMarkerMap::iterator i = location_markers.begin(); i != location_markers.end(); ++i) {
54                 delete i->second;
55         }
56
57         location_markers.clear ();
58 }
59
60 void
61 Editor::add_new_location (Location *location)
62 {
63         ENSURE_GUI_THREAD (bind (mem_fun(*this, &Editor::add_new_location), location));
64
65         LocationMarkers *lam = new LocationMarkers;
66         uint32_t color;
67
68         if (location->is_cd_marker()) {
69                 color = location_cd_marker_color;
70         } else if (location->is_mark()) {
71                 color = location_marker_color;
72         } else if (location->is_auto_loop()) {
73                 color = location_loop_color;
74         } else if (location->is_auto_punch()) {
75                 color = location_punch_color;
76         } else {
77                 color = location_range_color;
78         }
79
80         if (location->is_mark()) {
81
82                 if (location->is_cd_marker() && ruler_cd_marker_action->get_active()) {
83                         lam->start = new Marker (*this, *cd_marker_group, color, location->name(), Marker::Mark, location->start());
84                 }
85                 else {
86                         lam->start = new Marker (*this, *marker_group, color, location->name(), Marker::Mark, location->start());
87                 }
88                 lam->end   = 0;
89                         
90         } else if (location->is_auto_loop()) {
91                 // transport marker
92                 lam->start = new Marker (*this, *transport_marker_group, color, 
93                                          location->name(), Marker::LoopStart, location->start());
94                 lam->end   = new Marker (*this, *transport_marker_group, color, 
95                                          location->name(), Marker::LoopEnd, location->end());
96                 
97         } else if (location->is_auto_punch()) {
98                 // transport marker
99                 lam->start = new Marker (*this, *transport_marker_group, color, 
100                                          location->name(), Marker::PunchIn, location->start());
101                 lam->end   = new Marker (*this, *transport_marker_group, color, 
102                                          location->name(), Marker::PunchOut, location->end());
103                 
104         } else {
105                 // range marker
106                 if (location->is_cd_marker() && ruler_cd_marker_action->get_active()) {
107                         lam->start = new Marker (*this, *cd_marker_group, color, 
108                                                  location->name(), Marker::Start, location->start());
109                         lam->end   = new Marker (*this, *cd_marker_group, color, 
110                                                  location->name(), Marker::End, location->end());
111                 }
112                 else {
113                         
114                         lam->start = new Marker (*this, *range_marker_group, color, 
115                                                  location->name(), Marker::Start, location->start());
116                         lam->end   = new Marker (*this, *range_marker_group, color, 
117                                                  location->name(), Marker::End, location->end());
118                 }
119         }
120
121         if (location->is_hidden ()) {
122                 lam->hide();
123         } else {
124                 lam->show ();
125         }
126
127         location->start_changed.connect (mem_fun(*this, &Editor::location_changed));
128         location->end_changed.connect (mem_fun(*this, &Editor::location_changed));
129         location->changed.connect (mem_fun(*this, &Editor::location_changed));
130         location->name_changed.connect (mem_fun(*this, &Editor::location_changed));
131         location->FlagsChanged.connect (mem_fun(*this, &Editor::location_flags_changed));
132
133         pair<Location*,LocationMarkers*> newpair;
134
135         newpair.first = location;
136         newpair.second = lam;
137
138         location_markers.insert (newpair);
139
140         if (select_new_marker && location->is_mark()) {
141                 selection->set (lam->start);
142                 select_new_marker = false;
143         }
144 }
145
146 void
147 Editor::location_changed (Location *location)
148 {
149         ENSURE_GUI_THREAD (bind (mem_fun(*this, &Editor::location_changed), location));
150
151         LocationMarkers *lam = find_location_markers (location);
152
153         if (lam == 0) {
154                 /* a location that isn't "marked" with markers */
155                 return;
156         }
157         
158         lam->set_name (location->name());
159         lam->set_position (location->start(), location->end());
160
161         if (location->is_auto_loop()) {
162                 update_loop_range_view ();
163         } else if (location->is_auto_punch()) {
164                 update_punch_range_view ();
165         }
166 }
167
168 void
169 Editor::location_flags_changed (Location *location, void *src)
170 {
171         ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::location_flags_changed), location, src));
172         
173         LocationMarkers *lam = find_location_markers (location);
174         
175         if (lam == 0) {
176                 /* a location that isn't "marked" with markers */
177                 return;
178         }
179
180         // move cd markers to/from cd marker bar as appropriate
181         ensure_cd_marker_updated (lam, location);
182
183         if (location->is_cd_marker()) {
184                 lam->set_color_rgba (location_cd_marker_color);
185         } else if (location->is_mark()) {
186                 lam->set_color_rgba (location_marker_color);
187         } else if (location->is_auto_punch()) {
188                 lam->set_color_rgba (location_punch_color);
189         } else if (location->is_auto_loop()) {
190                 lam->set_color_rgba (location_loop_color);
191         } else {
192                 lam->set_color_rgba (location_range_color);
193         }
194         
195         if (location->is_hidden()) {
196                 lam->hide();
197         } else {
198                 lam->show ();
199         }
200 }
201
202 void Editor::update_cd_marker_display ()
203 {
204         for (LocationMarkerMap::iterator i = location_markers.begin(); i != location_markers.end(); ++i) {
205                 LocationMarkers * lam = i->second;
206                 Location * location = i->first;
207
208                 ensure_cd_marker_updated (lam, location);
209         }
210 }
211
212 void Editor::ensure_cd_marker_updated (LocationMarkers * lam, Location * location)
213 {
214         if (location->is_cd_marker()
215             && (ruler_cd_marker_action->get_active() &&  lam->start->get_parent() != cd_marker_group))
216         {
217                 //cerr << "reparenting non-cd marker so it can be relocated: " << location->name() << endl;
218                 if (lam->start) {
219                         lam->start->reparent (*cd_marker_group);
220                 }
221                 if (lam->end) {
222                         lam->end->reparent (*cd_marker_group);
223                 }
224         }
225         else if ( (!location->is_cd_marker() || !ruler_cd_marker_action->get_active()) 
226                   && (lam->start->get_parent() == cd_marker_group))  
227         {
228                 //cerr << "reparenting non-cd marker so it can be relocated: " << location->name() << endl;
229                 if (location->is_mark()) {
230                         if (lam->start) {
231                                 lam->start->reparent (*marker_group);
232                         }
233                         if (lam->end) {
234                                 lam->end->reparent (*marker_group);
235                         }
236                 }
237                 else {
238                         if (lam->start) {
239                                 lam->start->reparent (*range_marker_group);
240                         }
241                         if (lam->end) {
242                                 lam->end->reparent (*range_marker_group);
243                         }
244                 }
245         }
246 }
247
248 Editor::LocationMarkers::~LocationMarkers ()
249 {
250         if (start) {
251                 delete start;
252         }
253
254         if (end) {
255                 delete end;
256         }
257 }
258
259 Editor::LocationMarkers *
260 Editor::find_location_markers (Location *location) const
261 {
262         LocationMarkerMap::const_iterator i;
263
264         for (i = location_markers.begin(); i != location_markers.end(); ++i) {
265                 if ((*i).first == location) {
266                         return (*i).second;
267                 }
268         }
269
270         return 0;
271 }
272
273 Location *
274 Editor::find_location_from_marker (Marker *marker, bool& is_start) const
275 {
276         LocationMarkerMap::const_iterator i;
277
278         for (i = location_markers.begin(); i != location_markers.end(); ++i) {
279                 LocationMarkers *lm = (*i).second;
280                 if (lm->start == marker) {
281                         is_start = true;
282                         return (*i).first;
283                 } else if (lm->end == marker) {
284                         is_start = false;
285                         return (*i).first;
286                 }
287         }
288
289         return 0;
290 }
291
292 void
293 Editor::refresh_location_display_internal (Locations::LocationList& locations)
294 {
295         /* invalidate all */
296
297         for (LocationMarkerMap::iterator i = location_markers.begin(); i != location_markers.end(); ++i) {
298                 i->second->valid = false;
299         }
300         
301         /* add new ones */
302
303         for (Locations::LocationList::iterator i = locations.begin(); i != locations.end(); ++i) {
304
305                 LocationMarkerMap::iterator x;
306
307                 if ((x = location_markers.find (*i)) != location_markers.end()) {
308                         x->second->valid = true;
309                         continue;
310                 }
311
312                 add_new_location (*i);
313         }
314
315         /* remove dead ones */
316
317         for (LocationMarkerMap::iterator i = location_markers.begin(); i != location_markers.end(); ) {
318
319                 LocationMarkerMap::iterator tmp;
320
321                 tmp = i;
322                 ++tmp;
323
324                 if (!i->second->valid) {
325                         delete i->second;
326                         location_markers.erase (i);
327                 } 
328
329                 i = tmp;
330         }
331
332         update_punch_range_view (false);
333         update_loop_range_view (false);
334 }
335
336 void
337 Editor::refresh_location_display ()
338 {
339         ENSURE_GUI_THREAD(mem_fun(*this, &Editor::refresh_location_display));
340         
341         if (session) {
342                 session->locations()->apply (*this, &Editor::refresh_location_display_internal);
343         }
344 }
345
346 void
347 Editor::refresh_location_display_s (Change ignored)
348 {
349         ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::refresh_location_display_s), ignored));
350
351         if (session) {
352                 session->locations()->apply (*this, &Editor::refresh_location_display_internal);
353         }
354 }
355
356 void
357 Editor::LocationMarkers::hide() 
358 {
359         start->hide ();
360         if (end) { end->hide(); }
361 }
362
363 void
364 Editor::LocationMarkers::show() 
365 {
366         start->show ();
367         if (end) { end->show(); }
368 }
369
370 void
371 Editor::LocationMarkers::set_name (const string& str) 
372 {
373         start->set_name (str);
374         if (end) { end->set_name (str); }
375 }
376
377 void
378 Editor::LocationMarkers::set_position (nframes_t startf, 
379                                        nframes_t endf) 
380 {
381         start->set_position (startf);
382         if (end) { end->set_position (endf); }
383 }
384
385 void
386 Editor::LocationMarkers::set_color_rgba (uint32_t rgba) 
387 {
388         start->set_color_rgba (rgba);
389         if (end) { end->set_color_rgba (rgba); }
390 }
391
392 void
393 Editor::mouse_add_new_marker (nframes_t where, bool is_cd, bool is_xrun)
394 {
395         string markername, markerprefix;
396         int flags = (is_cd ? Location::IsCDMarker|Location::IsMark : Location::IsMark);
397
398         if (is_xrun) {
399                 markerprefix = "xrun";
400                 flags = Location::IsMark;
401         } else {
402                 markerprefix = "mark";
403         }
404
405         if (session) {
406                 session->locations()->next_available_name(markername, markerprefix);
407                 Location *location = new Location (where, where, markername, (Location::Flags) flags);
408                 session->begin_reversible_command (_("add marker"));
409                 XMLNode &before = session->locations()->get_state();
410                 session->locations()->add (location, true);
411                 XMLNode &after = session->locations()->get_state();
412                 session->add_command (new MementoCommand<Locations>(*(session->locations()), &before, &after));
413                 session->commit_reversible_command ();
414
415                 /* find the marker we just added */
416
417                 LocationMarkers *lam = find_location_markers (location);
418                 if (lam) {
419                         /* make it the selected marker */
420                         selection->set (lam->start);
421                 }
422         }
423 }
424
425 void
426 Editor::remove_marker (ArdourCanvas::Item& item, GdkEvent* event)
427 {
428         Marker* marker;
429         bool is_start;
430
431         if ((marker = static_cast<Marker*> (item.get_data ("marker"))) == 0) {
432                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
433                 /*NOTREACHED*/
434         }
435
436         if (entered_marker == marker) {
437                 entered_marker = NULL;
438         }
439
440         Location* loc = find_location_from_marker (marker, is_start);
441
442         if (session && loc) {
443                 Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::really_remove_marker), loc));
444         }
445 }
446
447 gint
448 Editor::really_remove_marker (Location* loc)
449 {
450         session->begin_reversible_command (_("remove marker"));
451         XMLNode &before = session->locations()->get_state();
452         session->locations()->remove (loc);
453         XMLNode &after = session->locations()->get_state();
454         session->add_command (new MementoCommand<Locations>(*(session->locations()), &before, &after));
455         session->commit_reversible_command ();
456         return FALSE;
457 }
458
459 void
460 Editor::location_gone (Location *location)
461 {
462         ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::location_gone), location));
463         
464         LocationMarkerMap::iterator i;
465
466         if (location == transport_loop_location()) {
467                 update_loop_range_view (true);
468         }
469
470         if (location == transport_punch_location()) {
471                 update_punch_range_view (true);
472         }
473         
474         for (i = location_markers.begin(); i != location_markers.end(); ++i) {
475                 if ((*i).first == location) {
476                         delete (*i).second;
477                         location_markers.erase (i);
478                         break;
479                 }
480         }
481 }
482
483 void
484 Editor::tm_marker_context_menu (GdkEventButton* ev, ArdourCanvas::Item* item)
485 {
486         if (tm_marker_menu == 0) {
487                 build_tm_marker_menu ();
488         }
489
490         marker_menu_item = item;
491         tm_marker_menu->popup (1, ev->time);
492
493 }
494
495 void
496 Editor::marker_context_menu (GdkEventButton* ev, ArdourCanvas::Item* item)
497 {
498         Marker * marker;
499         if ((marker = reinterpret_cast<Marker *> (item->get_data("marker"))) == 0) {
500                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
501                 /*NOTREACHED*/
502         }
503         
504         bool is_start;
505         Location * loc = find_location_from_marker (marker, is_start);
506         if (loc == transport_loop_location() || loc == transport_punch_location()) {
507                 if (transport_marker_menu == 0) {
508                         build_range_marker_menu (true);
509                 }
510                 marker_menu_item = item;
511                 transport_marker_menu->popup (1, ev->time);
512         } else {
513
514                 if (loc->is_mark()) {
515                         bool start_or_end = loc->is_start() || loc->is_end();
516                         Menu *markerMenu;
517                         if (start_or_end) {
518                                 if (start_end_marker_menu == 0)
519                                         build_marker_menu (true);
520                                 markerMenu = start_end_marker_menu;
521                         } else {
522                                 if (marker_menu == 0)
523                                         build_marker_menu (false);
524                                 markerMenu = marker_menu;
525                         }
526
527
528                 // GTK2FIX use action group sensitivity
529 #ifdef GTK2FIX
530                 if (children.size() >= 3) {
531                         MenuItem * loopitem = &children[2];
532                         if (loopitem) {
533                                 if (loc->is_mark()) {
534                                         loopitem->set_sensitive(false);
535                                 }
536                                 else {
537                                         loopitem->set_sensitive(true);
538                                 }
539                         }
540                 }
541 #endif          
542                 marker_menu_item = item;
543                 markerMenu->popup (1, ev->time);
544                 }
545
546                 if (loc->is_range_marker()) {
547                        if (range_marker_menu == 0){
548                               build_range_marker_menu (false);
549                        }
550                        marker_menu_item = item;
551                        range_marker_menu->popup (1, ev->time);
552                 }
553         }
554 }
555
556 void
557 Editor::new_transport_marker_context_menu (GdkEventButton* ev, ArdourCanvas::Item* item)
558 {
559         if (new_transport_marker_menu == 0) {
560                 build_new_transport_marker_menu ();
561         }
562
563         new_transport_marker_menu->popup (1, ev->time);
564
565 }
566
567 void
568 Editor::transport_marker_context_menu (GdkEventButton* ev, ArdourCanvas::Item* item)
569 {
570         if (transport_marker_menu == 0) {
571                 build_range_marker_menu (true);
572         }
573
574         transport_marker_menu->popup (1, ev->time);
575 }
576
577 void
578 Editor::build_marker_menu (bool start_or_end)
579 {
580         using namespace Menu_Helpers;
581
582         Menu *markerMenu = new Menu;
583         if (start_or_end) {
584                 start_end_marker_menu = markerMenu;
585         } else {
586                 marker_menu = markerMenu;
587         }
588         MenuList& items = markerMenu->items();
589         markerMenu->set_name ("ArdourContextMenu");
590
591         items.push_back (MenuElem (_("Locate to here"), mem_fun(*this, &Editor::marker_menu_set_playhead)));
592         items.push_back (MenuElem (_("Play from here"), mem_fun(*this, &Editor::marker_menu_play_from)));
593         items.push_back (MenuElem (_("Move Mark to Playhead"), mem_fun(*this, &Editor::marker_menu_set_from_playhead)));
594
595         items.push_back (SeparatorElem());
596
597         items.push_back (MenuElem (_("Hide"), mem_fun(*this, &Editor::marker_menu_hide)));
598         if (start_or_end) return;
599         items.push_back (MenuElem (_("Rename"), mem_fun(*this, &Editor::marker_menu_rename)));
600         items.push_back (MenuElem (_("Lock"), bind (mem_fun(*this, &Editor::marker_menu_lock), true)));
601         items.push_back (MenuElem (_("Unlock"), bind (mem_fun(*this, &Editor::marker_menu_lock), false)));
602
603         items.push_back (SeparatorElem());
604
605         items.push_back (MenuElem (_("Remove"), mem_fun(*this, &Editor::marker_menu_remove)));
606 }
607
608 void
609 Editor::build_range_marker_menu (bool loop_or_punch)
610 {
611         using namespace Menu_Helpers;
612
613         Menu *markerMenu = new Menu;
614         if (loop_or_punch) {
615                 transport_marker_menu = markerMenu;
616         } else {
617                 range_marker_menu = markerMenu;
618         }
619
620         MenuList& items = markerMenu->items();
621         markerMenu->set_name ("ArdourContextMenu");
622
623         items.push_back (MenuElem (_("Play Range"), mem_fun(*this, &Editor::marker_menu_play_range)));
624         items.push_back (MenuElem (_("Locate to Range Mark"), mem_fun(*this, &Editor::marker_menu_set_playhead)));
625         items.push_back (MenuElem (_("Play from Range Mark"), mem_fun(*this, &Editor::marker_menu_play_from)));
626         if (!loop_or_punch) {
627                 items.push_back (MenuElem (_("Loop Range"), mem_fun(*this, &Editor::marker_menu_loop_range)));
628         }
629         items.push_back (MenuElem (_("Set Range Mark from Playhead"), mem_fun(*this, &Editor::marker_menu_set_from_playhead)));
630         if (!Profile->get_sae()) {
631                 items.push_back (MenuElem (_("Set Range from Range Selection"), mem_fun(*this, &Editor::marker_menu_set_from_selection)));
632         }
633
634         items.push_back (SeparatorElem());
635
636         if (!loop_or_punch) {
637                 items.push_back (MenuElem (_("Hide Range"), mem_fun(*this, &Editor::marker_menu_hide)));
638                 items.push_back (MenuElem (_("Rename Range"), mem_fun(*this, &Editor::marker_menu_rename)));
639                 items.push_back (MenuElem (_("Remove Range"), mem_fun(*this, &Editor::marker_menu_remove)));
640         }
641
642         items.push_back (SeparatorElem());
643
644         items.push_back (MenuElem (_("Separate Regions in Range"), mem_fun(*this, &Editor::marker_menu_separate_regions_using_location)));
645         items.push_back (MenuElem (_("Select All in Range"), mem_fun(*this, &Editor::marker_menu_select_all_selectables_using_range)));
646         if (!Profile->get_sae()) {
647                 items.push_back (MenuElem (_("Select Range"), mem_fun(*this, &Editor::marker_menu_select_using_range)));
648         }
649 }
650
651 void
652 Editor::build_tm_marker_menu ()
653 {
654         using namespace Menu_Helpers;
655
656         tm_marker_menu = new Menu;
657         MenuList& items = tm_marker_menu->items();
658         tm_marker_menu->set_name ("ArdourContextMenu");
659
660         items.push_back (MenuElem (_("Edit"), mem_fun(*this, &Editor::marker_menu_edit)));
661         items.push_back (MenuElem (_("Remove"), mem_fun(*this, &Editor::marker_menu_remove)));
662 }
663
664 void
665 Editor::build_new_transport_marker_menu ()
666 {
667         using namespace Menu_Helpers;
668
669         new_transport_marker_menu = new Menu;
670         MenuList& items = new_transport_marker_menu->items();
671         new_transport_marker_menu->set_name ("ArdourContextMenu");
672
673         items.push_back (MenuElem (_("Set Loop Range"), mem_fun(*this, &Editor::new_transport_marker_menu_set_loop)));
674         items.push_back (MenuElem (_("Set Punch Range"), mem_fun(*this, &Editor::new_transport_marker_menu_set_punch)));
675
676         new_transport_marker_menu->signal_unmap_event().connect ( mem_fun(*this, &Editor::new_transport_marker_menu_popdown)); 
677 }
678
679 void
680 Editor::marker_menu_hide ()
681 {
682         Marker* marker;
683
684         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
685                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
686                 /*NOTREACHED*/
687         }
688
689         Location* l;
690         bool is_start;
691         
692         if ((l = find_location_from_marker (marker, is_start)) != 0) {
693                 l->set_hidden (true, this);
694         }
695 }
696
697 void
698 Editor::marker_menu_select_using_range ()
699 {
700         Marker* marker;
701
702         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
703                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
704                 /*NOTREACHED*/
705         }
706
707         Location* l;
708         bool is_start;
709
710         if (((l = find_location_from_marker (marker, is_start)) != 0) && (l->end() > l->start())) {
711                 set_selection_from_range (*l);
712         }
713 }
714
715 void
716 Editor::marker_menu_select_all_selectables_using_range ()
717 {
718         Marker* marker;
719
720         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
721                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
722                 /*NOTREACHED*/
723         }
724
725         Location* l;
726         bool is_start;
727
728         if (((l = find_location_from_marker (marker, is_start)) != 0) && (l->end() > l->start())) {
729                 select_all_within (l->start(), l->end() - 1, 0,  DBL_MAX, track_views, Selection::Set);
730         }
731           
732 }
733
734 void
735 Editor::marker_menu_separate_regions_using_location ()
736 {
737         Marker* marker;
738
739         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
740                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
741                 /*NOTREACHED*/
742         }
743
744         Location* l;
745         bool is_start;
746
747         if (((l = find_location_from_marker (marker, is_start)) != 0) && (l->end() > l->start())) {
748                 separate_regions_using_location (*l);
749         }
750           
751 }
752
753 void
754 Editor::marker_menu_play_from ()
755 {
756         Marker* marker;
757
758         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
759                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
760                 /*NOTREACHED*/
761         }
762
763         Location* l;
764         bool is_start;
765         
766         if ((l = find_location_from_marker (marker, is_start)) != 0) {
767
768                 if (l->is_mark()) {
769                         session->request_locate (l->start(), true);
770                 }
771                 else {
772                         //session->request_bounded_roll (l->start(), l->end());
773                         
774                         if (is_start) {
775                                 session->request_locate (l->start(), true);
776                         } else {
777                                 session->request_locate (l->end(), true);
778                         }
779                 }
780         }
781 }
782
783 void
784 Editor::marker_menu_set_playhead ()
785 {
786         Marker* marker;
787
788         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
789                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
790                 /*NOTREACHED*/
791         }
792
793         Location* l;
794         bool is_start;
795         
796         if ((l = find_location_from_marker (marker, is_start)) != 0) {
797
798                 if (l->is_mark()) {
799                         session->request_locate (l->start(), false);
800                 }
801                 else {
802                         if (is_start) {
803                                 session->request_locate (l->start(), false);
804                         } else {
805                                 session->request_locate (l->end(), false);
806                         }
807                 }
808         }
809 }
810
811 void
812 Editor::marker_menu_set_from_playhead ()
813 {
814         Marker* marker;
815
816         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
817                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
818                 /*NOTREACHED*/
819         }
820
821         Location* l;
822         bool is_start;
823         
824         if ((l = find_location_from_marker (marker, is_start)) != 0) {
825
826                 if (l->is_mark()) {
827                         l->set_start (session->audible_frame ());
828                 }
829                 else {
830                         if (is_start) {
831                                 l->set_start (session->audible_frame ());
832                         } else {
833                                 l->set_end (session->audible_frame ());
834                         }
835                 }
836         }
837 }
838
839 void
840 Editor::marker_menu_set_from_selection ()
841 {
842         Marker* marker;
843
844         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
845                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
846                 /*NOTREACHED*/
847         }
848
849         Location* l;
850         bool is_start;
851         
852         if ((l = find_location_from_marker (marker, is_start)) != 0) {
853
854                 if (l->is_mark()) {
855                         // nothing for now
856                 } else {
857
858                         /* if range selection use first to last */
859
860                         if (mouse_mode == Editing::MouseRange) {
861                                 if (!selection->time.empty()) {
862                                         l->set_start (selection->time.start());
863                                         l->set_end (selection->time.end_frame());
864                                 }
865                         }
866                         else {
867                                 if (!selection->regions.empty()) {
868                                         l->set_start (selection->regions.start());
869                                         l->set_end (selection->regions.end_frame());
870                                 }
871                         }
872                 }
873         }
874 }
875
876
877 void
878 Editor::marker_menu_play_range ()
879 {
880         Marker* marker;
881
882         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
883                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
884                 /*NOTREACHED*/
885         }
886
887         Location* l;
888         bool is_start;
889         
890         if ((l = find_location_from_marker (marker, is_start)) != 0) {
891
892                 if (l->is_mark()) {
893                         session->request_locate (l->start(), true);
894                 }
895                 else {
896                         session->request_bounded_roll (l->start(), l->end());
897                         
898                 }
899         }
900 }
901
902 void
903 Editor::marker_menu_loop_range ()
904 {
905         Marker* marker;
906
907         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
908                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
909                 /*NOTREACHED*/
910         }
911
912         Location* l;
913         bool is_start;
914         
915         if ((l = find_location_from_marker (marker, is_start)) != 0) {
916                 Location* l2;
917                 if ((l2 = transport_loop_location()) != 0) {
918                         l2->set (l->start(), l->end());
919                         
920                         // enable looping, reposition and start rolling
921                         session->request_play_loop(true);
922                         session->request_locate (l2->start(), true);
923                 }
924         }
925 }
926
927 void
928 Editor::marker_menu_edit ()
929 {
930         MeterMarker* mm;
931         TempoMarker* tm;
932         Marker* marker;
933
934         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
935                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
936                 /*NOTREACHED*/
937         }
938
939         if ((mm = dynamic_cast<MeterMarker*> (marker)) != 0) {
940                 edit_meter_section (&mm->meter());
941         } else if ((tm = dynamic_cast<TempoMarker*> (marker)) != 0) {
942                 edit_tempo_section (&tm->tempo());
943         } else {
944                 fatal << X_("programming erorr: unhandled marker type in Editor::marker_menu_edit")
945                       << endmsg;
946                 /*NOTREACHED*/
947         }
948 }
949
950 void
951 Editor::marker_menu_remove ()
952 {
953         MeterMarker* mm;
954         TempoMarker* tm;
955         Marker* marker;
956
957         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
958                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
959                 /*NOTREACHED*/
960         }
961
962         if ((mm = dynamic_cast<MeterMarker*> (marker)) != 0) {
963                 remove_meter_marker (marker_menu_item);
964         } else if ((tm = dynamic_cast<TempoMarker*> (marker)) != 0) {
965                 remove_tempo_marker (marker_menu_item);
966         } else {
967                 remove_marker (*marker_menu_item, (GdkEvent*) 0);
968         }
969 }
970
971 void
972 Editor::marker_menu_lock (bool yn)
973 {
974
975         Marker* marker;
976
977         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
978                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
979                 /*NOTREACHED*/
980         }
981
982         Location* loc;
983         bool ignored;
984
985         loc = find_location_from_marker (marker, ignored);
986
987         if (!loc) return;
988
989         if (yn) {
990                 loc->lock();
991         } else {
992                 loc->unlock ();
993         }
994 }
995
996 void
997 Editor::marker_menu_rename ()
998 {
999         Marker* marker;
1000
1001         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
1002                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
1003                 /*NOTREACHED*/
1004         }
1005
1006         Location* loc;
1007         bool is_start;
1008
1009         loc = find_location_from_marker (marker, is_start);
1010
1011         if (!loc) return;
1012         
1013         ArdourPrompter dialog (true);
1014         string txt;
1015
1016         dialog.set_prompt (_("New Name:"));
1017
1018         WindowTitle title(Glib::get_application_name());
1019         if (loc->is_mark()) {
1020                 title += _("Rename Mark");
1021         } else {
1022                 title += _("Rename Range");
1023         }
1024
1025         dialog.set_title(title.get_string());
1026
1027         dialog.set_name ("MarkRenameWindow");
1028         dialog.set_size_request (250, -1);
1029         dialog.set_position (Gtk::WIN_POS_MOUSE);
1030
1031         dialog.add_button (_("Rename"), RESPONSE_ACCEPT);
1032         dialog.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
1033         dialog.set_initial_text (loc->name());
1034
1035         dialog.show ();
1036
1037         switch (dialog.run ()) {
1038         case RESPONSE_ACCEPT:
1039                 break;
1040         default:
1041                 return;
1042         }
1043
1044         begin_reversible_command ( _("rename marker") );
1045         XMLNode &before = session->locations()->get_state();
1046
1047         dialog.get_result(txt);
1048         loc->set_name (txt);
1049         
1050         XMLNode &after = session->locations()->get_state();
1051         session->add_command (new MementoCommand<Locations>(*(session->locations()), &before, &after));
1052         commit_reversible_command ();
1053 }
1054
1055 gint
1056 Editor::new_transport_marker_menu_popdown (GdkEventAny *ev)
1057 {
1058         // hide rects
1059         transport_bar_drag_rect->hide();
1060         range_marker_drag_rect->hide();
1061
1062         return FALSE;
1063 }
1064
1065 void
1066 Editor::new_transport_marker_menu_set_loop ()
1067 {
1068         set_loop_range (temp_location->start(), temp_location->end(), _("set loop range"));
1069 }
1070
1071 void
1072 Editor::new_transport_marker_menu_set_punch ()
1073 {
1074         set_punch_range (temp_location->start(), temp_location->end(), _("set punch range"));
1075 }
1076
1077 void
1078 Editor::update_loop_range_view (bool visibility)
1079 {
1080         if (session == 0) {
1081                 return;
1082         }
1083
1084         Location* tll;
1085
1086         if (session->get_play_loop() && ((tll = transport_loop_location()) != 0)) {
1087
1088                 double x1 = frame_to_pixel (tll->start());
1089                 double x2 = frame_to_pixel (tll->end());
1090                 
1091                 transport_loop_range_rect->property_x1() = x1;
1092                 transport_loop_range_rect->property_x2() = x2;
1093                 
1094                 if (visibility) {
1095                         transport_loop_range_rect->show();
1096                 }
1097
1098         } else if (visibility) {
1099                 transport_loop_range_rect->hide();
1100         }
1101 }
1102
1103 void
1104 Editor::update_punch_range_view (bool visibility)
1105 {
1106         if (session == 0) {
1107                 return;
1108         }
1109
1110         Location* tpl;
1111
1112         if ((Config->get_punch_in() || Config->get_punch_out()) && ((tpl = transport_punch_location()) != 0)) {
1113
1114                 double x1 = frame_to_pixel (tpl->start());
1115                 double x2 = frame_to_pixel (tpl->end());
1116                 
1117                 guint track_canvas_width,track_canvas_height;
1118                 track_canvas->get_size(track_canvas_width,track_canvas_height);
1119                 
1120                 transport_punch_range_rect->property_x1() = x1;
1121                 transport_punch_range_rect->property_x2() = x2;
1122                 
1123                 transport_punch_range_rect->property_x1() = (Config->get_punch_in() ? x1 : 0);
1124                 transport_punch_range_rect->property_x2() = (Config->get_punch_out() ? x2 : track_canvas_width);
1125                 
1126                 if (visibility) {
1127                         transport_punch_range_rect->show();
1128                 }
1129         }
1130         else if (visibility) {
1131                 transport_punch_range_rect->hide();
1132         }
1133
1134 //      if (session->get_punch_in()) {
1135 //              double x = frame_to_pixel (transport_punch_location->start());
1136 //              gnome_canvas_item_set (transport_punchin_line, "x1", x, "x2", x, NULL);
1137                 
1138 //              if (visibility) {
1139 //                      gnome_canvas_item_show (transport_punchin_line);
1140 //              }
1141 //      }
1142 //      else if (visibility) {
1143 //              gnome_canvas_item_hide (transport_punchin_line);
1144 //      }
1145         
1146 //      if (session->get_punch_out()) {
1147 //              double x = frame_to_pixel (transport_punch_location->end());
1148                 
1149 //              gnome_canvas_item_set (transport_punchout_line, "x1", x, "x2", x, NULL);
1150                 
1151 //              if (visibility) {
1152 //                      gnome_canvas_item_show (transport_punchout_line);
1153 //              }
1154 //      }
1155 //      else if (visibility) {
1156 //              gnome_canvas_item_hide (transport_punchout_line);
1157 //      }
1158 }
1159
1160 void
1161 Editor::marker_selection_changed ()
1162 {
1163         if (session && session->deletion_in_progress()) {
1164                 return;
1165         }
1166
1167         for (LocationMarkerMap::iterator i = location_markers.begin(); i != location_markers.end(); ++i) {
1168                 LocationMarkers* lam = i->second;
1169
1170                 if (lam->start) {
1171                         lam->start->hide_line();
1172                 } 
1173
1174                 if (lam->end) {
1175                         lam->end->hide_line();
1176                 }
1177         }
1178
1179         edit_point_clock_connection_a.disconnect();
1180         edit_point_clock_connection_b.disconnect();
1181
1182         if (selection->markers.empty()) {
1183                 edit_point_clock.set (0);
1184                 return;
1185         }
1186
1187         for (MarkerSelection::iterator x = selection->markers.begin(); x != selection->markers.end(); ++x) {
1188                 (*x)->add_line (cursor_group, vertical_adjustment.get_value(), canvas_height);
1189                 (*x)->show_line ();
1190         }
1191
1192         edit_point_clock.set (selection->markers.front()->position());
1193
1194         bool ignored;
1195         Location* loc = find_location_from_marker (selection->markers.front(), ignored);
1196
1197         if (loc) {
1198                 edit_point_clock_connection_a = loc->changed.connect (mem_fun (*this, &Editor::selected_marker_moved));
1199                 edit_point_clock_connection_b = loc->start_changed.connect (mem_fun (*this, &Editor::selected_marker_moved));
1200         }
1201 }
1202
1203 void
1204 Editor::selected_marker_moved (Location* loc)
1205 {
1206         edit_point_clock.set (loc->start());
1207 }
1208
1209 struct SortLocationsByPosition { 
1210     bool operator() (Location* a, Location* b) {
1211             return a->start() < b->start();
1212     }
1213 };
1214
1215 void
1216 Editor::goto_nth_marker (int n)
1217 {
1218         if (!session) {
1219                 return;
1220         }
1221         const Locations::LocationList& l (session->locations()->list());
1222         Locations::LocationList ordered;
1223         ordered = l;
1224
1225         SortLocationsByPosition cmp;
1226         ordered.sort (cmp);
1227         
1228         for (Locations::LocationList::iterator i = ordered.begin(); n >= 0 && i != ordered.end(); ++i) {
1229                 if ((*i)->is_mark() && !(*i)->is_hidden() && !(*i)->is_start()) {
1230                         if (n == 0) {
1231                                 session->request_locate ((*i)->start(), session->transport_rolling());
1232                                 break;
1233                         }
1234                         --n;
1235                 }
1236         }
1237 }