Fix dialog message to something sensible.
[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 }
333
334 void
335 Editor::refresh_location_display ()
336 {
337         ENSURE_GUI_THREAD(mem_fun(*this, &Editor::refresh_location_display));
338         
339         if (session) {
340                 session->locations()->apply (*this, &Editor::refresh_location_display_internal);
341         }
342 }
343
344 void
345 Editor::refresh_location_display_s (Change ignored)
346 {
347         ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::refresh_location_display_s), ignored));
348
349         if (session) {
350                 session->locations()->apply (*this, &Editor::refresh_location_display_internal);
351         }
352 }
353
354 void
355 Editor::LocationMarkers::hide() 
356 {
357         start->hide ();
358         if (end) { end->hide(); }
359 }
360
361 void
362 Editor::LocationMarkers::show() 
363 {
364         start->show ();
365         if (end) { end->show(); }
366 }
367
368 void
369 Editor::LocationMarkers::set_name (const string& str) 
370 {
371         start->set_name (str);
372         if (end) { end->set_name (str); }
373 }
374
375 void
376 Editor::LocationMarkers::set_position (nframes_t startf, 
377                                        nframes_t endf) 
378 {
379         start->set_position (startf);
380         if (end) { end->set_position (endf); }
381 }
382
383 void
384 Editor::LocationMarkers::set_color_rgba (uint32_t rgba) 
385 {
386         start->set_color_rgba (rgba);
387         if (end) { end->set_color_rgba (rgba); }
388 }
389
390 void
391 Editor::mouse_add_new_marker (nframes_t where, bool is_cd, bool is_xrun)
392 {
393         string markername, markerprefix;
394         int flags = (is_cd ? Location::IsCDMarker|Location::IsMark : Location::IsMark);
395
396         if (is_xrun) {
397                 markerprefix = "xrun";
398                 flags = Location::IsMark;
399         } else {
400                 markerprefix = "mark";
401         }
402
403         if (session) {
404                 session->locations()->next_available_name(markername, markerprefix);
405                 Location *location = new Location (where, where, markername, (Location::Flags) flags);
406                 session->begin_reversible_command (_("add marker"));
407                 XMLNode &before = session->locations()->get_state();
408                 session->locations()->add (location, true);
409                 XMLNode &after = session->locations()->get_state();
410                 session->add_command (new MementoCommand<Locations>(*(session->locations()), &before, &after));
411                 session->commit_reversible_command ();
412
413                 /* find the marker we just added */
414
415                 LocationMarkers *lam = find_location_markers (location);
416                 if (lam) {
417                         /* make it the selected marker */
418                         selection->set (lam->start);
419                 }
420         }
421 }
422
423 void
424 Editor::remove_marker (ArdourCanvas::Item& item, GdkEvent* event)
425 {
426         Marker* marker;
427         bool is_start;
428
429         if ((marker = static_cast<Marker*> (item.get_data ("marker"))) == 0) {
430                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
431                 /*NOTREACHED*/
432         }
433
434         if (entered_marker == marker) {
435                 entered_marker = NULL;
436         }
437
438         Location* loc = find_location_from_marker (marker, is_start);
439
440         if (session && loc) {
441                 Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::really_remove_marker), loc));
442         }
443 }
444
445 gint
446 Editor::really_remove_marker (Location* loc)
447 {
448         session->begin_reversible_command (_("remove marker"));
449         XMLNode &before = session->locations()->get_state();
450         session->locations()->remove (loc);
451         XMLNode &after = session->locations()->get_state();
452         session->add_command (new MementoCommand<Locations>(*(session->locations()), &before, &after));
453         session->commit_reversible_command ();
454         return FALSE;
455 }
456
457 void
458 Editor::location_gone (Location *location)
459 {
460         ENSURE_GUI_THREAD(bind (mem_fun(*this, &Editor::location_gone), location));
461         
462         LocationMarkerMap::iterator i;
463
464         if (location == transport_loop_location()) {
465                 update_loop_range_view (true);
466         }
467
468         if (location == transport_punch_location()) {
469                 update_punch_range_view (true);
470         }
471         
472         for (i = location_markers.begin(); i != location_markers.end(); ++i) {
473                 if ((*i).first == location) {
474                         delete (*i).second;
475                         location_markers.erase (i);
476                         break;
477                 }
478         }
479 }
480
481 void
482 Editor::tm_marker_context_menu (GdkEventButton* ev, ArdourCanvas::Item* item)
483 {
484         if (tm_marker_menu == 0) {
485                 build_tm_marker_menu ();
486         }
487
488         marker_menu_item = item;
489         tm_marker_menu->popup (1, ev->time);
490
491 }
492
493 void
494 Editor::marker_context_menu (GdkEventButton* ev, ArdourCanvas::Item* item)
495 {
496         Marker * marker;
497         if ((marker = reinterpret_cast<Marker *> (item->get_data("marker"))) == 0) {
498                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
499                 /*NOTREACHED*/
500         }
501         
502         bool is_start;
503         Location * loc = find_location_from_marker (marker, is_start);
504         if (loc == transport_loop_location() || loc == transport_punch_location()) {
505                 if (transport_marker_menu == 0) {
506                         build_range_marker_menu (true);
507                 }
508                 marker_menu_item = item;
509                 transport_marker_menu->popup (1, ev->time);
510         } else {
511
512                 if (loc->is_mark()) {
513                         bool start_or_end = loc->is_start() || loc->is_end();
514                         Menu *markerMenu;
515                         if (start_or_end) {
516                                 if (start_end_marker_menu == 0)
517                                         build_marker_menu (true);
518                                 markerMenu = start_end_marker_menu;
519                         } else {
520                                 if (marker_menu == 0)
521                                         build_marker_menu (false);
522                                 markerMenu = marker_menu;
523                         }
524
525
526                 // GTK2FIX use action group sensitivity
527 #ifdef GTK2FIX
528                 if (children.size() >= 3) {
529                         MenuItem * loopitem = &children[2];
530                         if (loopitem) {
531                                 if (loc->is_mark()) {
532                                         loopitem->set_sensitive(false);
533                                 }
534                                 else {
535                                         loopitem->set_sensitive(true);
536                                 }
537                         }
538                 }
539 #endif          
540                 marker_menu_item = item;
541                 markerMenu->popup (1, ev->time);
542                 }
543
544                 if (loc->is_range_marker()) {
545                        if (range_marker_menu == 0){
546                               build_range_marker_menu (false);
547                        }
548                        marker_menu_item = item;
549                        range_marker_menu->popup (1, ev->time);
550                 }
551         }
552 }
553
554 void
555 Editor::new_transport_marker_context_menu (GdkEventButton* ev, ArdourCanvas::Item* item)
556 {
557         if (new_transport_marker_menu == 0) {
558                 build_new_transport_marker_menu ();
559         }
560
561         new_transport_marker_menu->popup (1, ev->time);
562
563 }
564
565 void
566 Editor::transport_marker_context_menu (GdkEventButton* ev, ArdourCanvas::Item* item)
567 {
568         if (transport_marker_menu == 0) {
569                 build_range_marker_menu (true);
570         }
571
572         transport_marker_menu->popup (1, ev->time);
573 }
574
575 void
576 Editor::build_marker_menu (bool start_or_end)
577 {
578         using namespace Menu_Helpers;
579
580         Menu *markerMenu = new Menu;
581         if (start_or_end) {
582                 start_end_marker_menu = markerMenu;
583         } else {
584                 marker_menu = markerMenu;
585         }
586         MenuList& items = markerMenu->items();
587         markerMenu->set_name ("ArdourContextMenu");
588
589         items.push_back (MenuElem (_("Locate to here"), mem_fun(*this, &Editor::marker_menu_set_playhead)));
590         items.push_back (MenuElem (_("Play from here"), mem_fun(*this, &Editor::marker_menu_play_from)));
591         items.push_back (MenuElem (_("Move Mark to Playhead"), mem_fun(*this, &Editor::marker_menu_set_from_playhead)));
592
593         items.push_back (SeparatorElem());
594
595         items.push_back (MenuElem (_("Hide"), mem_fun(*this, &Editor::marker_menu_hide)));
596         if (start_or_end) return;
597         items.push_back (MenuElem (_("Rename"), mem_fun(*this, &Editor::marker_menu_rename)));
598         items.push_back (MenuElem (_("Lock"), bind (mem_fun(*this, &Editor::marker_menu_lock), true)));
599         items.push_back (MenuElem (_("Unlock"), bind (mem_fun(*this, &Editor::marker_menu_lock), false)));
600
601         items.push_back (SeparatorElem());
602
603         items.push_back (MenuElem (_("Remove"), mem_fun(*this, &Editor::marker_menu_remove)));
604 }
605
606 void
607 Editor::build_range_marker_menu (bool loop_or_punch)
608 {
609         using namespace Menu_Helpers;
610
611         Menu *markerMenu = new Menu;
612         if (loop_or_punch) {
613                 transport_marker_menu = markerMenu;
614         } else {
615                 range_marker_menu = markerMenu;
616         }
617
618         MenuList& items = markerMenu->items();
619         markerMenu->set_name ("ArdourContextMenu");
620
621         items.push_back (MenuElem (_("Play Range"), mem_fun(*this, &Editor::marker_menu_play_range)));
622         items.push_back (MenuElem (_("Locate to Range Mark"), mem_fun(*this, &Editor::marker_menu_set_playhead)));
623         items.push_back (MenuElem (_("Play from Range Mark"), mem_fun(*this, &Editor::marker_menu_play_from)));
624         if (!loop_or_punch) {
625                 items.push_back (MenuElem (_("Loop Range"), mem_fun(*this, &Editor::marker_menu_loop_range)));
626         }
627         items.push_back (MenuElem (_("Set Range Mark from Playhead"), mem_fun(*this, &Editor::marker_menu_set_from_playhead)));
628         if (!Profile->get_sae()) {
629                 items.push_back (MenuElem (_("Set Range from Range Selection"), mem_fun(*this, &Editor::marker_menu_set_from_selection)));
630         }
631
632         items.push_back (SeparatorElem());
633
634         if (!loop_or_punch) {
635                 items.push_back (MenuElem (_("Hide Range"), mem_fun(*this, &Editor::marker_menu_hide)));
636                 items.push_back (MenuElem (_("Rename Range"), mem_fun(*this, &Editor::marker_menu_rename)));
637                 items.push_back (MenuElem (_("Remove Range"), mem_fun(*this, &Editor::marker_menu_remove)));
638         }
639
640         items.push_back (SeparatorElem());
641
642         items.push_back (MenuElem (_("Separate Regions in Range"), mem_fun(*this, &Editor::marker_menu_separate_regions_using_location)));
643         items.push_back (MenuElem (_("Select All in Range"), mem_fun(*this, &Editor::marker_menu_select_all_selectables_using_range)));
644         if (!Profile->get_sae()) {
645                 items.push_back (MenuElem (_("Select Range"), mem_fun(*this, &Editor::marker_menu_select_using_range)));
646         }
647 }
648
649 void
650 Editor::build_tm_marker_menu ()
651 {
652         using namespace Menu_Helpers;
653
654         tm_marker_menu = new Menu;
655         MenuList& items = tm_marker_menu->items();
656         tm_marker_menu->set_name ("ArdourContextMenu");
657
658         items.push_back (MenuElem (_("Edit"), mem_fun(*this, &Editor::marker_menu_edit)));
659         items.push_back (MenuElem (_("Remove"), mem_fun(*this, &Editor::marker_menu_remove)));
660 }
661
662 void
663 Editor::build_new_transport_marker_menu ()
664 {
665         using namespace Menu_Helpers;
666
667         new_transport_marker_menu = new Menu;
668         MenuList& items = new_transport_marker_menu->items();
669         new_transport_marker_menu->set_name ("ArdourContextMenu");
670
671         items.push_back (MenuElem (_("Set Loop Range"), mem_fun(*this, &Editor::new_transport_marker_menu_set_loop)));
672         items.push_back (MenuElem (_("Set Punch Range"), mem_fun(*this, &Editor::new_transport_marker_menu_set_punch)));
673
674         new_transport_marker_menu->signal_unmap_event().connect ( mem_fun(*this, &Editor::new_transport_marker_menu_popdown)); 
675 }
676
677 void
678 Editor::marker_menu_hide ()
679 {
680         Marker* marker;
681
682         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
683                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
684                 /*NOTREACHED*/
685         }
686
687         Location* l;
688         bool is_start;
689         
690         if ((l = find_location_from_marker (marker, is_start)) != 0) {
691                 l->set_hidden (true, this);
692         }
693 }
694
695 void
696 Editor::marker_menu_select_using_range ()
697 {
698         Marker* marker;
699
700         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
701                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
702                 /*NOTREACHED*/
703         }
704
705         Location* l;
706         bool is_start;
707
708         if (((l = find_location_from_marker (marker, is_start)) != 0) && (l->end() > l->start())) {
709                 set_selection_from_range (*l);
710         }
711 }
712
713 void
714 Editor::marker_menu_select_all_selectables_using_range ()
715 {
716         Marker* marker;
717
718         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
719                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
720                 /*NOTREACHED*/
721         }
722
723         Location* l;
724         bool is_start;
725
726         if (((l = find_location_from_marker (marker, is_start)) != 0) && (l->end() > l->start())) {
727                 select_all_within (l->start(), l->end() - 1, 0,  DBL_MAX, track_views, Selection::Set);
728         }
729           
730 }
731
732 void
733 Editor::marker_menu_separate_regions_using_location ()
734 {
735         Marker* marker;
736
737         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
738                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
739                 /*NOTREACHED*/
740         }
741
742         Location* l;
743         bool is_start;
744
745         if (((l = find_location_from_marker (marker, is_start)) != 0) && (l->end() > l->start())) {
746                 separate_regions_using_location (*l);
747         }
748           
749 }
750
751 void
752 Editor::marker_menu_play_from ()
753 {
754         Marker* marker;
755
756         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
757                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
758                 /*NOTREACHED*/
759         }
760
761         Location* l;
762         bool is_start;
763         
764         if ((l = find_location_from_marker (marker, is_start)) != 0) {
765
766                 if (l->is_mark()) {
767                         session->request_locate (l->start(), true);
768                 }
769                 else {
770                         //session->request_bounded_roll (l->start(), l->end());
771                         
772                         if (is_start) {
773                                 session->request_locate (l->start(), true);
774                         } else {
775                                 session->request_locate (l->end(), true);
776                         }
777                 }
778         }
779 }
780
781 void
782 Editor::marker_menu_set_playhead ()
783 {
784         Marker* marker;
785
786         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
787                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
788                 /*NOTREACHED*/
789         }
790
791         Location* l;
792         bool is_start;
793         
794         if ((l = find_location_from_marker (marker, is_start)) != 0) {
795
796                 if (l->is_mark()) {
797                         session->request_locate (l->start(), false);
798                 }
799                 else {
800                         if (is_start) {
801                                 session->request_locate (l->start(), false);
802                         } else {
803                                 session->request_locate (l->end(), false);
804                         }
805                 }
806         }
807 }
808
809 void
810 Editor::marker_menu_set_from_playhead ()
811 {
812         Marker* marker;
813
814         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
815                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
816                 /*NOTREACHED*/
817         }
818
819         Location* l;
820         bool is_start;
821         
822         if ((l = find_location_from_marker (marker, is_start)) != 0) {
823
824                 if (l->is_mark()) {
825                         l->set_start (session->audible_frame ());
826                 }
827                 else {
828                         if (is_start) {
829                                 l->set_start (session->audible_frame ());
830                         } else {
831                                 l->set_end (session->audible_frame ());
832                         }
833                 }
834         }
835 }
836
837 void
838 Editor::marker_menu_set_from_selection ()
839 {
840         Marker* marker;
841
842         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
843                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
844                 /*NOTREACHED*/
845         }
846
847         Location* l;
848         bool is_start;
849         
850         if ((l = find_location_from_marker (marker, is_start)) != 0) {
851
852                 if (l->is_mark()) {
853                         // nothing for now
854                 } else {
855
856                         /* if range selection use first to last */
857
858                         if (mouse_mode == Editing::MouseRange) {
859                                 if (!selection->time.empty()) {
860                                         l->set_start (selection->time.start());
861                                         l->set_end (selection->time.end_frame());
862                                 }
863                         }
864                         else {
865                                 if (!selection->regions.empty()) {
866                                         l->set_start (selection->regions.start());
867                                         l->set_end (selection->regions.end_frame());
868                                 }
869                         }
870                 }
871         }
872 }
873
874
875 void
876 Editor::marker_menu_play_range ()
877 {
878         Marker* marker;
879
880         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
881                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
882                 /*NOTREACHED*/
883         }
884
885         Location* l;
886         bool is_start;
887         
888         if ((l = find_location_from_marker (marker, is_start)) != 0) {
889
890                 if (l->is_mark()) {
891                         session->request_locate (l->start(), true);
892                 }
893                 else {
894                         session->request_bounded_roll (l->start(), l->end());
895                         
896                 }
897         }
898 }
899
900 void
901 Editor::marker_menu_loop_range ()
902 {
903         Marker* marker;
904
905         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
906                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
907                 /*NOTREACHED*/
908         }
909
910         Location* l;
911         bool is_start;
912         
913         if ((l = find_location_from_marker (marker, is_start)) != 0) {
914                 Location* l2;
915                 if ((l2 = transport_loop_location()) != 0) {
916                         l2->set (l->start(), l->end());
917                         
918                         // enable looping, reposition and start rolling
919                         session->request_play_loop(true);
920                         session->request_locate (l2->start(), true);
921                 }
922         }
923 }
924
925 void
926 Editor::marker_menu_edit ()
927 {
928         MeterMarker* mm;
929         TempoMarker* tm;
930         Marker* marker;
931
932         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
933                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
934                 /*NOTREACHED*/
935         }
936
937         if ((mm = dynamic_cast<MeterMarker*> (marker)) != 0) {
938                 edit_meter_section (&mm->meter());
939         } else if ((tm = dynamic_cast<TempoMarker*> (marker)) != 0) {
940                 edit_tempo_section (&tm->tempo());
941         } else {
942                 fatal << X_("programming erorr: unhandled marker type in Editor::marker_menu_edit")
943                       << endmsg;
944                 /*NOTREACHED*/
945         }
946 }
947
948 void
949 Editor::marker_menu_remove ()
950 {
951         MeterMarker* mm;
952         TempoMarker* tm;
953         Marker* marker;
954
955         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
956                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
957                 /*NOTREACHED*/
958         }
959
960         if ((mm = dynamic_cast<MeterMarker*> (marker)) != 0) {
961                 remove_meter_marker (marker_menu_item);
962         } else if ((tm = dynamic_cast<TempoMarker*> (marker)) != 0) {
963                 remove_tempo_marker (marker_menu_item);
964         } else {
965                 remove_marker (*marker_menu_item, (GdkEvent*) 0);
966         }
967 }
968
969 void
970 Editor::marker_menu_lock (bool yn)
971 {
972
973         Marker* marker;
974
975         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
976                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
977                 /*NOTREACHED*/
978         }
979
980         Location* loc;
981         bool ignored;
982
983         loc = find_location_from_marker (marker, ignored);
984
985         if (!loc) return;
986
987         if (yn) {
988                 loc->lock();
989         } else {
990                 loc->unlock ();
991         }
992 }
993
994 void
995 Editor::marker_menu_rename ()
996 {
997         Marker* marker;
998
999         if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
1000                 fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
1001                 /*NOTREACHED*/
1002         }
1003
1004         Location* loc;
1005         bool is_start;
1006
1007         loc = find_location_from_marker (marker, is_start);
1008
1009         if (!loc) return;
1010         
1011         ArdourPrompter dialog (true);
1012         string txt;
1013
1014         dialog.set_prompt (_("New Name:"));
1015
1016         WindowTitle title(Glib::get_application_name());
1017         if (loc->is_mark()) {
1018                 title += _("Rename Mark");
1019         } else {
1020                 title += _("Rename Range");
1021         }
1022
1023         dialog.set_title(title.get_string());
1024
1025         dialog.set_name ("MarkRenameWindow");
1026         dialog.set_size_request (250, -1);
1027         dialog.set_position (Gtk::WIN_POS_MOUSE);
1028
1029         dialog.add_button (_("Rename"), RESPONSE_ACCEPT);
1030         dialog.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
1031         dialog.set_initial_text (loc->name());
1032
1033         dialog.show ();
1034
1035         switch (dialog.run ()) {
1036         case RESPONSE_ACCEPT:
1037                 break;
1038         default:
1039                 return;
1040         }
1041
1042         begin_reversible_command ( _("rename marker") );
1043         XMLNode &before = session->locations()->get_state();
1044
1045         dialog.get_result(txt);
1046         loc->set_name (txt);
1047         
1048         XMLNode &after = session->locations()->get_state();
1049         session->add_command (new MementoCommand<Locations>(*(session->locations()), &before, &after));
1050         commit_reversible_command ();
1051 }
1052
1053 gint
1054 Editor::new_transport_marker_menu_popdown (GdkEventAny *ev)
1055 {
1056         // hide rects
1057         transport_bar_drag_rect->hide();
1058         range_marker_drag_rect->hide();
1059
1060         return FALSE;
1061 }
1062
1063 void
1064 Editor::new_transport_marker_menu_set_loop ()
1065 {
1066         set_loop_range (temp_location->start(), temp_location->end(), _("set loop range"));
1067 }
1068
1069 void
1070 Editor::new_transport_marker_menu_set_punch ()
1071 {
1072         set_punch_range (temp_location->start(), temp_location->end(), _("set punch range"));
1073 }
1074
1075 void
1076 Editor::update_loop_range_view (bool visibility)
1077 {
1078         if (session == 0) {
1079                 return;
1080         }
1081
1082         Location* tll;
1083
1084         if (session->get_play_loop() && ((tll = transport_loop_location()) != 0)) {
1085
1086                 double x1 = frame_to_pixel (tll->start());
1087                 double x2 = frame_to_pixel (tll->end());
1088                 
1089                 transport_loop_range_rect->property_x1() = x1;
1090                 transport_loop_range_rect->property_x2() = x2;
1091                 
1092                 if (visibility) {
1093                         transport_loop_range_rect->show();
1094                 }
1095
1096         } else if (visibility) {
1097                 transport_loop_range_rect->hide();
1098         }
1099 }
1100
1101 void
1102 Editor::update_punch_range_view (bool visibility)
1103 {
1104         if (session == 0) {
1105                 return;
1106         }
1107
1108         Location* tpl;
1109
1110         if ((Config->get_punch_in() || Config->get_punch_out()) && ((tpl = transport_punch_location()) != 0)) {
1111
1112                 double x1 = frame_to_pixel (tpl->start());
1113                 double x2 = frame_to_pixel (tpl->end());
1114                 
1115                 guint track_canvas_width,track_canvas_height;
1116                 track_canvas->get_size(track_canvas_width,track_canvas_height);
1117                 
1118                 transport_punch_range_rect->property_x1() = x1;
1119                 transport_punch_range_rect->property_x2() = x2;
1120                 
1121                 transport_punch_range_rect->property_x1() = (Config->get_punch_in() ? x1 : 0);
1122                 transport_punch_range_rect->property_x2() = (Config->get_punch_out() ? x2 : track_canvas_width);
1123                 
1124                 if (visibility) {
1125                         transport_punch_range_rect->show();
1126                 }
1127         }
1128         else if (visibility) {
1129                 transport_punch_range_rect->hide();
1130         }
1131
1132 //      if (session->get_punch_in()) {
1133 //              double x = frame_to_pixel (transport_punch_location->start());
1134 //              gnome_canvas_item_set (transport_punchin_line, "x1", x, "x2", x, NULL);
1135                 
1136 //              if (visibility) {
1137 //                      gnome_canvas_item_show (transport_punchin_line);
1138 //              }
1139 //      }
1140 //      else if (visibility) {
1141 //              gnome_canvas_item_hide (transport_punchin_line);
1142 //      }
1143         
1144 //      if (session->get_punch_out()) {
1145 //              double x = frame_to_pixel (transport_punch_location->end());
1146                 
1147 //              gnome_canvas_item_set (transport_punchout_line, "x1", x, "x2", x, NULL);
1148                 
1149 //              if (visibility) {
1150 //                      gnome_canvas_item_show (transport_punchout_line);
1151 //              }
1152 //      }
1153 //      else if (visibility) {
1154 //              gnome_canvas_item_hide (transport_punchout_line);
1155 //      }
1156 }
1157
1158 void
1159 Editor::marker_selection_changed ()
1160 {
1161         if (session && session->deletion_in_progress()) {
1162                 return;
1163         }
1164
1165         for (LocationMarkerMap::iterator i = location_markers.begin(); i != location_markers.end(); ++i) {
1166                 LocationMarkers* lam = i->second;
1167
1168                 if (lam->start) {
1169                         lam->start->hide_line();
1170                 } 
1171
1172                 if (lam->end) {
1173                         lam->end->hide_line();
1174                 }
1175         }
1176
1177         edit_point_clock_connection_a.disconnect();
1178         edit_point_clock_connection_b.disconnect();
1179
1180         if (selection->markers.empty()) {
1181                 edit_point_clock.set (0);
1182                 return;
1183         }
1184
1185         for (MarkerSelection::iterator x = selection->markers.begin(); x != selection->markers.end(); ++x) {
1186                 (*x)->add_line (cursor_group, vertical_adjustment.get_value(), canvas_height);
1187                 (*x)->show_line ();
1188         }
1189
1190         edit_point_clock.set (selection->markers.front()->position());
1191
1192         bool ignored;
1193         Location* loc = find_location_from_marker (selection->markers.front(), ignored);
1194
1195         if (loc) {
1196                 edit_point_clock_connection_a = loc->changed.connect (mem_fun (*this, &Editor::selected_marker_moved));
1197                 edit_point_clock_connection_b = loc->start_changed.connect (mem_fun (*this, &Editor::selected_marker_moved));
1198         }
1199 }
1200
1201 void
1202 Editor::selected_marker_moved (Location* loc)
1203 {
1204         edit_point_clock.set (loc->start());
1205 }
1206
1207 struct SortLocationsByPosition { 
1208     bool operator() (Location* a, Location* b) {
1209             return a->start() < b->start();
1210     }
1211 };
1212
1213 void
1214 Editor::goto_nth_marker (int n)
1215 {
1216         if (!session) {
1217                 return;
1218         }
1219         const Locations::LocationList& l (session->locations()->list());
1220         Locations::LocationList ordered;
1221         ordered = l;
1222
1223         SortLocationsByPosition cmp;
1224         ordered.sort (cmp);
1225         
1226         for (Locations::LocationList::iterator i = ordered.begin(); n >= 0 && i != ordered.end(); ++i) {
1227                 if ((*i)->is_mark() && !(*i)->is_hidden() && !(*i)->is_start()) {
1228                         if (n == 0) {
1229                                 session->request_locate ((*i)->start(), session->transport_rolling());
1230                                 break;
1231                         }
1232                         --n;
1233                 }
1234         }
1235 }