Use boost::shared_ptr instead of raw pointers for RouteUI track (etc) accessors ...
[ardour.git] / gtk2_ardour / editor_markers.cc
index 568226fb6083e3e1e8203325384e3d1a324a8fca..29a212f2f594e6ad6ecd6c78e7fa7ee0e4735c4c 100644 (file)
@@ -15,7 +15,6 @@
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id$
 */
 
 #include <sigc++/retype.h>
 
 #include <libgnomecanvas/libgnomecanvas.h>
 #include <gtkmm2ext/gtk_ui.h>
+#include <gtkmm2ext/window_title.h>
 
 #include <ardour/location.h>
+#include <pbd/memento_command.h>
 
 #include "editor.h"
 #include "marker.h"
 #include "selection.h"
-#include "simplerect.h"
 #include "editing.h"
 #include "gui_thread.h"
 #include "simplerect.h"
+#include "actions.h"
+#include "prompter.h"
 
 #include "i18n.h"
 
 using namespace std;
 using namespace sigc;
 using namespace ARDOUR;
+using namespace PBD;
 using namespace Gtk;
+using namespace Gtkmm2ext;
 
 void
 Editor::clear_marker_display ()
@@ -55,6 +59,8 @@ Editor::clear_marker_display ()
 void
 Editor::add_new_location (Location *location)
 {
+       ENSURE_GUI_THREAD (bind (mem_fun(*this, &Editor::add_new_location), location));
+
        LocationMarkers *lam = new LocationMarkers;
        uint32_t color;
 
@@ -266,8 +272,8 @@ Editor::LocationMarkers::set_name (const string& str)
 }
 
 void
-Editor::LocationMarkers::set_position (jack_nframes_t startf, 
-                                      jack_nframes_t endf) 
+Editor::LocationMarkers::set_position (nframes_t startf, 
+                                      nframes_t endf) 
 {
        start->set_position (startf);
        if (end) { end->set_position (endf); }
@@ -281,14 +287,17 @@ Editor::LocationMarkers::set_color_rgba (uint32_t rgba)
 }
 
 void
-Editor::mouse_add_new_marker (jack_nframes_t where)
+Editor::mouse_add_new_marker (nframes_t where)
 {
+       string markername;
        if (session) {
-               Location *location = new Location (where, where, "mark", Location::IsMark);
+               session->locations()->next_available_name(markername,"mark");
+               Location *location = new Location (where, where, markername, Location::IsMark);
                session->begin_reversible_command (_("add marker"));
-               session->add_undo (session->locations()->get_memento());
+                XMLNode &before = session->locations()->get_state();
                session->locations()->add (location, true);
-               session->add_redo_no_execute (session->locations()->get_memento());
+                XMLNode &after = session->locations()->get_state();
+               session->add_command (new MementoCommand<Locations>(*(session->locations()), &before, &after));
                session->commit_reversible_command ();
        }
 }
@@ -307,17 +316,7 @@ Editor::remove_marker (ArdourCanvas::Item& item, GdkEvent* event)
        Location* loc = find_location_from_marker (marker, is_start);
 
        if (session && loc) {
-               if (loc->is_end()) {
-                       /* you can't hide or delete this marker */
-                       return;
-               }
-               if (loc->is_auto_loop() || loc->is_auto_punch()) {
-                       // just hide them
-                       loc->set_hidden (true, this);
-               }
-               else {
-                       Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::really_remove_marker), loc));
-               }
+               Glib::signal_idle().connect (bind (mem_fun(*this, &Editor::really_remove_marker), loc));
        }
 }
 
@@ -325,9 +324,10 @@ gint
 Editor::really_remove_marker (Location* loc)
 {
        session->begin_reversible_command (_("remove marker"));
-       session->add_undo (session->locations()->get_memento());
+       XMLNode &before = session->locations()->get_state();
        session->locations()->remove (loc);
-       session->add_redo_no_execute (session->locations()->get_memento());
+       XMLNode &after = session->locations()->get_state();
+       session->add_command (new MementoCommand<Locations>(*(session->locations()), &before, &after));
        session->commit_reversible_command ();
        return FALSE;
 }
@@ -368,7 +368,6 @@ Editor::tm_marker_context_menu (GdkEventButton* ev, ArdourCanvas::Item* item)
 
 }
 
-
 void
 Editor::marker_context_menu (GdkEventButton* ev, ArdourCanvas::Item* item)
 {
@@ -382,18 +381,25 @@ Editor::marker_context_menu (GdkEventButton* ev, ArdourCanvas::Item* item)
        Location * loc = find_location_from_marker (marker, is_start);
        if (loc == transport_loop_location() || loc == transport_punch_location()) {
                if (transport_marker_menu == 0) {
-                       build_transport_marker_menu ();
+                       build_range_marker_menu (true);
                }
                marker_menu_item = item;
                transport_marker_menu->popup (1, ev->time);
        } else {
-               if (marker_menu == 0) {
-                 if (loc->is_mark()) {
-                       build_marker_menu ();
-                 } else {
-                       build_range_marker_menu ();
-                 }
-               }
+
+               if (loc->is_mark()) {
+                       bool start_or_end = loc->is_start() || loc->is_end();
+                       Menu *markerMenu;
+                       if (start_or_end) {
+                               if (start_end_marker_menu == 0)
+                                       build_marker_menu (true);
+                               markerMenu = start_end_marker_menu;
+                       } else {
+                               if (marker_menu == 0)
+                                       build_marker_menu (false);
+                               markerMenu = marker_menu;
+                       }
+
 
                // GTK2FIX use action group sensitivity
 #ifdef GTK2FIX
@@ -410,11 +416,19 @@ Editor::marker_context_menu (GdkEventButton* ev, ArdourCanvas::Item* item)
                }
 #endif         
                marker_menu_item = item;
-               marker_menu->popup (1, ev->time);
+               markerMenu->popup (1, ev->time);
+               }
+
+               if (loc->is_range_marker()) {
+                      if (range_marker_menu == 0){
+                             build_range_marker_menu (false);
+                      }
+                      marker_menu_item = item;
+                      range_marker_menu->popup (1, ev->time);
+               }
        }
 }
 
-
 void
 Editor::new_transport_marker_context_menu (GdkEventButton* ev, ArdourCanvas::Item* item)
 {
@@ -430,58 +444,75 @@ void
 Editor::transport_marker_context_menu (GdkEventButton* ev, ArdourCanvas::Item* item)
 {
        if (transport_marker_menu == 0) {
-               build_transport_marker_menu ();
+               build_range_marker_menu (true);
        }
 
        transport_marker_menu->popup (1, ev->time);
 }
 
 void
-Editor::build_marker_menu ()
+Editor::build_marker_menu (bool start_or_end)
 {
        using namespace Menu_Helpers;
 
-       marker_menu = new Menu;
-       MenuList& items = marker_menu->items();
-       marker_menu->set_name ("ArdourContextMenu");
+       Menu *markerMenu = new Menu;
+       if (start_or_end) {
+               start_end_marker_menu = markerMenu;
+       } else {
+               marker_menu = markerMenu;
+       }
+       MenuList& items = markerMenu->items();
+       markerMenu->set_name ("ArdourContextMenu");
 
-       items.push_back (MenuElem (_("Locate to"), mem_fun(*this, &Editor::marker_menu_set_playhead)));
-       items.push_back (MenuElem (_("Play from"), mem_fun(*this, &Editor::marker_menu_play_from)));
-       items.push_back (MenuElem (_("Loop range"), mem_fun(*this, &Editor::marker_menu_loop_range)));
-       items.push_back (MenuElem (_("Set from playhead"), mem_fun(*this, &Editor::marker_menu_set_from_playhead)));
-       items.push_back (MenuElem (_("Set from range"), mem_fun(*this, &Editor::marker_menu_set_from_selection)));
+       items.push_back (MenuElem (_("Locate to Mark"), mem_fun(*this, &Editor::marker_menu_set_playhead)));
+       items.push_back (MenuElem (_("Play from Mark"), mem_fun(*this, &Editor::marker_menu_play_from)));
+       items.push_back (MenuElem (_("Set Mark from Playhead"), mem_fun(*this, &Editor::marker_menu_set_from_playhead)));
 
        items.push_back (SeparatorElem());
 
-       items.push_back (MenuElem (_("Rename"), mem_fun(*this, &Editor::marker_menu_rename)));
-       items.push_back (MenuElem (_("Hide"), mem_fun(*this, &Editor::marker_menu_hide)));
-       items.push_back (MenuElem (_("Remove"), mem_fun(*this, &Editor::marker_menu_remove)));
+       items.push_back (MenuElem (_("Hide Mark"), mem_fun(*this, &Editor::marker_menu_hide)));
+       if (start_or_end) return;
+       items.push_back (MenuElem (_("Rename Mark"), mem_fun(*this, &Editor::marker_menu_rename)));
+       items.push_back (MenuElem (_("Remove Mark"), mem_fun(*this, &Editor::marker_menu_remove)));
 
 }
 
 void
-Editor::build_range_marker_menu ()
+Editor::build_range_marker_menu (bool loop_or_punch)
 {
        using namespace Menu_Helpers;
 
-       marker_menu = new Menu;
-       MenuList& items = marker_menu->items();
-       marker_menu->set_name ("ArdourContextMenu");
-
-       items.push_back (MenuElem (_("Locate to"), mem_fun(*this, &Editor::marker_menu_set_playhead)));
-       items.push_back (MenuElem (_("Play from"), mem_fun(*this, &Editor::marker_menu_play_from)));
-       items.push_back (MenuElem (_("Loop range"), mem_fun(*this, &Editor::marker_menu_loop_range)));
-       items.push_back (MenuElem (_("Set from playhead"), mem_fun(*this, &Editor::marker_menu_set_from_playhead)));
-       items.push_back (MenuElem (_("Set from range"), mem_fun(*this, &Editor::marker_menu_set_from_selection)));
+       Menu *markerMenu = new Menu;
+       if (loop_or_punch) {
+               transport_marker_menu = markerMenu;
+       } else {
+               range_marker_menu = markerMenu;
+       }
+       MenuList& items = markerMenu->items();
+       markerMenu->set_name ("ArdourContextMenu");
+
+       items.push_back (MenuElem (_("Locate to Range Mark"), mem_fun(*this, &Editor::marker_menu_set_playhead)));
+       items.push_back (MenuElem (_("Play from Range Mark"), mem_fun(*this, &Editor::marker_menu_play_from)));
+       if (! loop_or_punch) {
+               items.push_back (MenuElem (_("Play Range"), mem_fun(*this, &Editor::marker_menu_play_range)));
+               items.push_back (MenuElem (_("Loop Range"), mem_fun(*this, &Editor::marker_menu_loop_range)));
+       }
+       items.push_back (MenuElem (_("Set Range Mark from Playhead"), mem_fun(*this, &Editor::marker_menu_set_from_playhead)));
+       items.push_back (MenuElem (_("Set Range from Range Selection"), mem_fun(*this, &Editor::marker_menu_set_from_selection)));
 
        items.push_back (SeparatorElem());
 
-       items.push_back (MenuElem (_("Rename"), mem_fun(*this, &Editor::marker_menu_rename)));
-       items.push_back (MenuElem (_("Hide"), mem_fun(*this, &Editor::marker_menu_hide)));
-       items.push_back (MenuElem (_("Remove"), mem_fun(*this, &Editor::marker_menu_remove)));
+       items.push_back (MenuElem (_("Hide Range"), mem_fun(*this, &Editor::marker_menu_hide)));
+       if (! loop_or_punch) {
+               items.push_back (MenuElem (_("Rename Range"), mem_fun(*this, &Editor::marker_menu_rename)));
+               items.push_back (MenuElem (_("Remove Range"), mem_fun(*this, &Editor::marker_menu_remove)));
+       }
 
        items.push_back (SeparatorElem());
-       items.push_back (MenuElem (_("Select all in Range"), mem_fun(*this, &Editor::marker_menu_select_all_from_range)));
+
+       items.push_back (MenuElem (_("Separate Regions in Range"), mem_fun(*this, &Editor::marker_menu_separate_regions_using_location)));
+       items.push_back (MenuElem (_("Select All in Range"), mem_fun(*this, &Editor::marker_menu_select_all_selectables_using_range)));
+       items.push_back (MenuElem (_("Select Range"), mem_fun(*this, &Editor::marker_menu_select_using_range)));
 
 }
 
@@ -514,25 +545,25 @@ Editor::build_new_transport_marker_menu ()
 }
 
 void
-Editor::build_transport_marker_menu ()
+Editor::marker_menu_hide ()
 {
-       using namespace Menu_Helpers;
-
-       transport_marker_menu = new Menu;
-       MenuList& items = transport_marker_menu->items();
-       transport_marker_menu->set_name ("ArdourContextMenu");
+       Marker* marker;
 
-       items.push_back (MenuElem (_("Locate to"), mem_fun(*this, &Editor::marker_menu_set_playhead)));
-       items.push_back (MenuElem (_("Play from"), mem_fun(*this, &Editor::marker_menu_play_from)));
-       items.push_back (MenuElem (_("Set from playhead"), mem_fun(*this, &Editor::marker_menu_set_from_playhead)));
-       items.push_back (MenuElem (_("Set from range"), mem_fun(*this, &Editor::marker_menu_set_from_selection)));
-       items.push_back (SeparatorElem());
-       items.push_back (MenuElem (_("Hide"), mem_fun(*this, &Editor::marker_menu_hide)));
+       if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
+               fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
+               /*NOTREACHED*/
+       }
 
+       Location* l;
+       bool is_start;
+       
+       if ((l = find_location_from_marker (marker, is_start)) != 0) {
+               l->set_hidden (true, this);
+       }
 }
 
 void
-Editor::marker_menu_hide ()
+Editor::marker_menu_select_using_range ()
 {
        Marker* marker;
 
@@ -543,14 +574,14 @@ Editor::marker_menu_hide ()
 
        Location* l;
        bool is_start;
-       
-       if ((l = find_location_from_marker (marker, is_start)) != 0) {
-               l->set_hidden (true, this);
+
+       if (((l = find_location_from_marker (marker, is_start)) != 0) && (l->end() > l->start())) {
+               set_selection_from_range (*l);
        }
 }
 
 void
-Editor::marker_menu_select_all_from_range ()
+Editor::marker_menu_select_all_selectables_using_range ()
 {
        Marker* marker;
 
@@ -558,12 +589,31 @@ Editor::marker_menu_select_all_from_range ()
                fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
                /*NOTREACHED*/
        }
+
        Location* l;
        bool is_start;
 
-       if ((l = find_location_from_marker (marker, is_start)) != 0) {
+       if (((l = find_location_from_marker (marker, is_start)) != 0) && (l->end() > l->start())) {
+               select_all_within (l->start(), l->end() - 1, 0,  DBL_MAX, track_views, Selection::Set);
+       }
+         
+}
+
+void
+Editor::marker_menu_separate_regions_using_location ()
+{
+       Marker* marker;
+
+       if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
+               fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
+               /*NOTREACHED*/
+       }
+
+       Location* l;
+       bool is_start;
 
-               select_all_within (l->start(), l->end(), 0,  DBL_MAX, false);
+       if (((l = find_location_from_marker (marker, is_start)) != 0) && (l->end() > l->start())) {
+               separate_regions_using_location (*l);
        }
          
 }
@@ -683,15 +733,41 @@ Editor::marker_menu_set_from_selection ()
                                }
                        }
                        else {
-                               if (!selection->audio_regions.empty()) {
-                                       l->set_start (selection->audio_regions.start());
-                                       l->set_end (selection->audio_regions.end_frame());
+                               if (!selection->regions.empty()) {
+                                       l->set_start (selection->regions.start());
+                                       l->set_end (selection->regions.end_frame());
                                }
                        }
                }
        }
 }
 
+
+void
+Editor::marker_menu_play_range ()
+{
+       Marker* marker;
+
+       if ((marker = reinterpret_cast<Marker *> (marker_menu_item->get_data ("marker"))) == 0) {
+               fatal << _("programming error: marker canvas item has no marker object pointer!") << endmsg;
+               /*NOTREACHED*/
+       }
+
+       Location* l;
+       bool is_start;
+       
+       if ((l = find_location_from_marker (marker, is_start)) != 0) {
+
+               if (l->is_mark()) {
+                       session->request_locate (l->start(), true);
+               }
+               else {
+                       session->request_bounded_roll (l->start(), l->end());
+                       
+               }
+       }
+}
+
 void
 Editor::marker_menu_loop_range ()
 {
@@ -711,7 +787,7 @@ Editor::marker_menu_loop_range ()
                        l2->set (l->start(), l->end());
                        
                        // enable looping, reposition and start rolling
-                       session->request_auto_loop(true);
+                       session->request_play_loop(true);
                        session->request_locate (l2->start(), true);
                }
        }
@@ -778,29 +854,29 @@ Editor::marker_menu_rename ()
 
        if (!loc) return;
        
-       Dialog dialog;
-       Entry  entry;
-       
+       ArdourPrompter dialog (true);
+       string txt;
+
+       dialog.set_prompt (_("New Name:"));
+
+       WindowTitle title(Glib::get_application_name());
        if (loc->is_mark()) {
-               dialog.set_title (_("ardour: rename mark"));
+               title += _("Rename Mark");
        } else {
-               dialog.set_title (_("ardour: rename range"));
+               title += _("Rename Range");
        }
 
+       dialog.set_title(title.get_string());
+
        dialog.set_name ("MarkRenameWindow");
-       dialog.set_size_request (300, -1);
+       dialog.set_size_request (250, -1);
        dialog.set_position (Gtk::WIN_POS_MOUSE);
-       dialog.set_modal (true);
-
-       dialog.add_action_widget (entry, RESPONSE_ACCEPT);
-       dialog.add_button (Stock::OK, RESPONSE_ACCEPT);
-       dialog.add_button (Stock::CANCEL, RESPONSE_ACCEPT);
 
-       entry.set_text (loc->name());
-       entry.set_name ("MarkerNameDisplay");
+       dialog.add_button (_("Rename"), RESPONSE_ACCEPT);
+       dialog.set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
+       dialog.set_initial_text (loc->name());
 
-       dialog.show_all ();
-       entry.grab_focus ();
+       dialog.show ();
 
        switch (dialog.run ()) {
        case RESPONSE_ACCEPT:
@@ -809,7 +885,15 @@ Editor::marker_menu_rename ()
                return;
        }
 
-       loc->set_name (entry.get_text());
+       begin_reversible_command ( _("rename marker") );
+        XMLNode &before = session->locations()->get_state();
+
+       dialog.get_result(txt);
+       loc->set_name (txt);
+       
+        XMLNode &after = session->locations()->get_state();
+       session->add_command (new MementoCommand<Locations>(*(session->locations()), &before, &after));
+       commit_reversible_command ();
 }
 
 gint
@@ -833,16 +917,18 @@ Editor::new_transport_marker_menu_set_loop ()
 
        if ((tll = transport_loop_location()) == 0) {
                Location* loc = new Location (temp_location->start(), temp_location->end(), _("Loop"),  Location::IsAutoLoop);
-               session->add_undo (session->locations()->get_memento());
+                XMLNode &before = session->locations()->get_state();
                session->locations()->add (loc, true);
                session->set_auto_loop_location (loc);
-               session->add_redo_no_execute (session->locations()->get_memento());
+                XMLNode &after = session->locations()->get_state();
+               session->add_command (new MementoCommand<Locations>(*(session->locations()), &before, &after));
        }
        else {
-               session->add_undo (retype_return<void>(bind (mem_fun (*tll, &Location::set), tll->start(), tll->end())));
-               session->add_redo (retype_return<void>(bind (mem_fun (*tll, &Location::set), temp_location->start(), temp_location->end())));
+                XMLNode &before = tll->get_state();
                tll->set_hidden (false, this);
                tll->set (temp_location->start(), temp_location->end());
+                XMLNode &after = tll->get_state();
+                session->add_command (new MementoCommand<Location>(*tll, &before, &after));
        }
        
        commit_reversible_command ();
@@ -859,15 +945,17 @@ Editor::new_transport_marker_menu_set_punch ()
 
        if ((tpl = transport_punch_location()) == 0) {
                tpl = new Location (temp_location->start(), temp_location->end(), _("Punch"), Location::IsAutoPunch);
-               session->add_undo (session->locations()->get_memento());
+                XMLNode &before = session->locations()->get_state();
                session->locations()->add (tpl, true);
                session->set_auto_punch_location (tpl);
-               session->add_redo_no_execute (session->locations()->get_memento());
+                XMLNode &after = session->locations()->get_state();
+               session->add_command (new MementoCommand<Locations>(*(session->locations()), &before, &after));
        } else {
-               session->add_undo (retype_return<void>(bind (mem_fun (*tpl, &Location::set), tpl->start(), tpl->end())));
-               session->add_redo (retype_return<void>(bind (mem_fun (*tpl, &Location::set), temp_location->start(), temp_location->end())));
+                XMLNode &before = tpl->get_state();
                tpl->set_hidden(false, this);
                tpl->set(temp_location->start(), temp_location->end());
+                XMLNode &after = tpl->get_state();
+                session->add_command (new MementoCommand<Location>(*tpl, &before, &after));
        }
        
        commit_reversible_command ();
@@ -882,7 +970,7 @@ Editor::update_loop_range_view (bool visibility)
 
        Location* tll;
 
-       if (session->get_auto_loop() && ((tll = transport_loop_location()) != 0)) {
+       if (session->get_play_loop() && ((tll = transport_loop_location()) != 0)) {
 
                double x1 = frame_to_pixel (tll->start());
                double x2 = frame_to_pixel (tll->end());
@@ -893,8 +981,8 @@ Editor::update_loop_range_view (bool visibility)
                if (visibility) {
                        transport_loop_range_rect->show();
                }
-       }
-       else if (visibility) {
+
+       else if (visibility) {
                transport_loop_range_rect->hide();
        }
 }
@@ -908,7 +996,7 @@ Editor::update_punch_range_view (bool visibility)
 
        Location* tpl;
 
-       if ((session->get_punch_in() || session->get_punch_out()) && ((tpl = transport_punch_location()) != 0)) {
+       if ((Config->get_punch_in() || Config->get_punch_out()) && ((tpl = transport_punch_location()) != 0)) {
 
                double x1 = frame_to_pixel (tpl->start());
                double x2 = frame_to_pixel (tpl->end());