a bit more styling for that lock dialog
[ardour.git] / gtk2_ardour / editor_ops.cc
index abc90d2f5920de67dafdf52d30932389f08d82c8..759c795e3852fd0b47bcc5c194d4b569bccff7bf 100644 (file)
@@ -57,6 +57,7 @@
 
 #include "canvas/canvas.h"
 
+#include "actions.h"
 #include "ardour_ui.h"
 #include "audio_region_view.h"
 #include "audio_streamview.h"
@@ -70,7 +71,6 @@
 #include "editor_drag.h"
 #include "editor_regions.h"
 #include "editor_routes.h"
-#include "gtk-custom-hruler.h"
 #include "gui_thread.h"
 #include "insert_time_dialog.h"
 #include "interthread_progress_window.h"
@@ -512,6 +512,60 @@ Editor::nudge_backward_capture_offset ()
        commit_reversible_command ();
 }
 
+struct RegionSelectionPositionSorter {
+        bool operator() (RegionView* a, RegionView* b) {
+                return a->region()->position() < b->region()->position();
+        }
+};
+
+void
+Editor::sequence_regions ()
+{
+       framepos_t r_end;
+       framepos_t r_end_prev;
+
+       int iCount=0;
+
+       if (!_session) {
+               return;
+       }
+
+       RegionSelection rs = get_regions_from_selection_and_entered ();
+       rs.sort(RegionSelectionPositionSorter());
+
+       if (!rs.empty()) {
+
+               begin_reversible_command (_("sequence regions"));
+               for (RegionSelection::iterator i = rs.begin(); i != rs.end(); ++i) {
+                       boost::shared_ptr<Region> r ((*i)->region());
+
+                       r->clear_changes();
+
+                       if(r->locked())
+                       {
+                               continue;
+                       }
+                       if(r->position_locked())
+                       {
+                               continue;
+                       }
+                       if(iCount>0)
+                       {
+                               r_end_prev=r_end;
+                               r->set_position(r_end_prev);
+                       }
+
+                       _session->add_command (new StatefulDiffCommand (r));
+
+                       r_end=r->position() + r->length();
+
+                       iCount++;
+               }
+               commit_reversible_command ();
+       } 
+} 
+
+
 /* DISPLAY MOTION */
 
 void
@@ -1279,23 +1333,26 @@ Editor::scroll_tracks_up_line ()
 bool
 Editor::scroll_down_one_track ()
 {
-       double vertical_pos = vertical_adjustment.get_value () + vertical_adjustment.get_page_size() - 1.0; 
-
        TrackViewList::reverse_iterator next = track_views.rend();
        std::pair<TimeAxisView*,double> res;
+       const double bottom_of_trackviews = vertical_adjustment.get_value() + vertical_adjustment.get_page_size() - 1;
 
        for (TrackViewList::reverse_iterator t = track_views.rbegin(); t != track_views.rend(); ++t) {
                if ((*t)->hidden()) {
                        continue;
                }
                
-               res = (*t)->covers_y_position (vertical_pos);
+               /* If this is the bottom visible trackview, we want to display
+                  the next one.
+               */
+
+               res = (*t)->covers_y_position (bottom_of_trackviews);
 
                if (res.first) {
                        break;
                }
 
-               next = t;
+               ++next; // moves "next" towards the "front" since it is a reverse iterator
        }
 
        /* move to the track below the first one that covers the */
@@ -1322,9 +1379,11 @@ Editor::scroll_up_one_track ()
                        continue;
                }
 
-               res = (*t)->covers_y_position(vertical_pos);
+               /* find the trackview at the top of the trackview group */
+               res = (*t)->covers_y_position (vertical_pos);
                
                if (res.first) {
+                       cerr << res.first->name() << " covers the top\n";
                        break;
                }
 
@@ -1404,8 +1463,23 @@ Editor::clamp_samples_per_pixel (framecnt_t& fpp) const
                clamped = true;
        }
 
-       if (max_framepos / fpp < 800) {
-               fpp = max_framepos / 800;
+       framecnt_t sr;
+
+       if (_session) {
+               sr = _session->frame_rate ();
+       } else {
+               sr = 48000;
+       }
+
+       const framecnt_t three_days = 3 * 24 * 60 * 60 * sr;
+       const framecnt_t lots_of_pixels = 4000;
+
+       /* if the zoom level is greater than what you'd get trying to display 3
+        * days of audio on a really big screen, scale it down.
+        */
+
+       if (fpp * lots_of_pixels > three_days) {
+               fpp = three_days / _track_canvas->width();
                clamped = true;
        }
 
@@ -1723,23 +1797,41 @@ Editor::temporal_zoom_to_frame (bool coarser, framepos_t frame)
        if (!_session) {
                return;
        }
-       double range_before = frame - leftmost_frame;
-       double new_fpp;
 
-       new_fpp = samples_per_pixel;
+       framecnt_t range_before = frame - leftmost_frame;
+       framecnt_t new_spp;
 
        if (coarser) {
-               new_fpp *= 1.61803399;
-               range_before *= 1.61803399;
+               if (samples_per_pixel <= 1) {
+                       new_spp = 2;
+               } else {
+                       new_spp = samples_per_pixel + (samples_per_pixel/2);
+               }
+               range_before += range_before/2;
        } else {
-               new_fpp = max(1.0,(new_fpp/1.61803399));
-               range_before /= 1.61803399;
+               if (samples_per_pixel >= 1) {
+                       new_spp = samples_per_pixel - (samples_per_pixel/2);
+               } else {
+                       /* could bail out here since we cannot zoom any finer,
+                          but leave that to the clamp_samples_per_pixel() and
+                          equality test below
+                       */
+                       new_spp = samples_per_pixel;
+               }
+
+               range_before -= range_before/2;
        }
 
-       if (new_fpp == samples_per_pixel)  {
+       clamp_samples_per_pixel (new_spp);
+
+       if (new_spp == samples_per_pixel)  {
                return;
        }
 
+       /* zoom focus is automatically taken as @param frame when this
+          method is used.
+       */
+       
        framepos_t new_leftmost = frame - (framepos_t)range_before;
 
        if (new_leftmost > frame) {
@@ -1750,7 +1842,7 @@ Editor::temporal_zoom_to_frame (bool coarser, framepos_t frame)
                new_leftmost = 0;
        }
 
-       reposition_and_zoom (new_leftmost, new_fpp);
+       reposition_and_zoom (new_leftmost, new_spp);
 }
 
 
@@ -3521,10 +3613,8 @@ Editor::freeze_thread ()
        /* create event pool because we may need to talk to the session */
        SessionEvent::create_per_thread_pool ("freeze events", 64);
        /* create per-thread buffers for process() tree to use */
-       current_interthread_info->process_thread.get_buffers ();
        clicked_routeview->audio_track()->freeze_me (*current_interthread_info);
        current_interthread_info->done = true;
-       current_interthread_info->process_thread.drop_buffers();
        return 0;
 }
 
@@ -4818,12 +4908,17 @@ Editor::fork_region ()
                MidiRegionView* const mrv = dynamic_cast<MidiRegionView*>(*r);
 
                if (mrv) {
-                       boost::shared_ptr<Playlist> playlist = mrv->region()->playlist();
-                       boost::shared_ptr<MidiRegion> newregion = mrv->midi_region()->clone ();
-
-                       playlist->clear_changes ();
-                       playlist->replace_region (mrv->region(), newregion, mrv->region()->position());
-                       _session->add_command(new StatefulDiffCommand (playlist));
+                       try {
+                               boost::shared_ptr<Playlist> playlist = mrv->region()->playlist();
+                               boost::shared_ptr<MidiSource> new_source = _session->create_midi_source_by_stealing_name (mrv->midi_view()->track());
+                               boost::shared_ptr<MidiRegion> newregion = mrv->midi_region()->clone (new_source);
+                               
+                               playlist->clear_changes ();
+                               playlist->replace_region (mrv->region(), newregion, mrv->region()->position());
+                               _session->add_command(new StatefulDiffCommand (playlist));
+                       } catch (...) {
+                               error << string_compose (_("Could not unlink %1"), mrv->region()->name()) << endmsg;
+                       }
                }
 
                r = tmp;
@@ -5644,16 +5739,21 @@ Editor::select_prev_route()
 void
 Editor::ensure_track_visible(TimeAxisView *track)
 {
-       if (track->hidden())
+       if (track->hidden()) {
                return;
+       }
+
+       /* compute visible area of trackview group, as offsets from top of
+        * trackview group.
+        */
 
        double const current_view_min_y = vertical_adjustment.get_value();
-       double const current_view_max_y = vertical_adjustment.get_value() + vertical_adjustment.get_page_size();
+       double const current_view_max_y = current_view_min_y + vertical_adjustment.get_page_size();
 
        double const track_min_y = track->y_position ();
        double const track_max_y = track->y_position () + track->effective_height ();
 
-       if (track_min_y >= current_view_min_y &&
+       if (track_min_y > current_view_min_y &&
            track_max_y <= current_view_max_y) {
                return;
        }
@@ -6495,12 +6595,13 @@ Editor::remove_tracks ()
 
        for (TrackSelection::iterator x = ts.begin(); x != ts.end(); ++x) {
                RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*> (*x);
-               if (rtv) {
-                       if (rtv->is_track()) {
-                               ntracks++;
-                       } else {
-                               nbusses++;
-                       }
+               if (!rtv) {
+                       continue;
+               }
+               if (rtv->is_track()) {
+                       ntracks++;
+               } else {
+                       nbusses++;
                }
                routes.push_back (rtv->_route);
 
@@ -6784,7 +6885,13 @@ Editor::fit_tracks (TrackViewList & tracks)
                ++visible_tracks;
        }
 
-       uint32_t h = (uint32_t) floor ((_visible_canvas_height - child_heights) / visible_tracks);
+       /* compute the per-track height from:
+
+          total canvas visible height - 
+                 height that will be taken by visible children of selected
+                 tracks - height of the ruler/hscroll area 
+       */
+       uint32_t h = (uint32_t) floor ((_visible_canvas_height - (child_heights + _trackview_group->canvas_origin().y)) / visible_tracks);
        double first_y_pos = DBL_MAX;
 
        if (h < TimeAxisView::preset_height (HeightSmall)) {
@@ -6809,9 +6916,7 @@ Editor::fit_tracks (TrackViewList & tracks)
 
        /* operate on all tracks, hide unselected ones that are in the middle of selected ones */
 
-       bool prev_was_selected = false;
-       bool is_selected = tracks.contains (all.front());
-       bool next_is_selected;
+       bool within_selected = false;
 
        for (TrackViewList::iterator t = all.begin(); t != all.end(); ++t) {
 
@@ -6819,26 +6924,16 @@ Editor::fit_tracks (TrackViewList & tracks)
 
                next = t;
                ++next;
-
-               if (next != all.end()) {
-                       next_is_selected = tracks.contains (*next);
-               } else {
-                       next_is_selected = false;
-               }
-
+               
                if ((*t)->marked_for_display ()) {
-                       if (is_selected) {
+                       if (tracks.contains (*t)) { 
                                (*t)->set_height (h);
                                first_y_pos = std::min ((*t)->y_position (), first_y_pos);
-                       } else {
-                               if (prev_was_selected && next_is_selected) {
-                                       hide_track_in_display (*t);
-                               }
+                               within_selected = true;
+                       } else if (within_selected) {
+                               hide_track_in_display (*t);
                        }
                }
-
-               prev_was_selected = is_selected;
-               is_selected = next_is_selected;
        }
 
        /*
@@ -7022,3 +7117,33 @@ Editor::toggle_midi_input_active (bool flip_others)
        
        _session->set_exclusive_input_active (rl, onoff, flip_others);
 }
+
+void
+Editor::lock ()
+{
+       if (!lock_dialog) {
+               lock_dialog = new ArdourDialog (string_compose (_("%1: Locked"), PROGRAM_NAME), true);
+
+               Gtk::Image* padlock = manage (new Gtk::Image (::get_icon ("padlock_closed")));
+               lock_dialog->get_vbox()->pack_start (*padlock);
+
+               ArdourButton* b = manage (new ArdourButton);
+               b->set_name ("lock button");
+               b->set_markup (string_compose ("<span size=\"large\" weight=\"bold\">%1</span>", _("Click me to unlock")));
+               b->signal_clicked.connect (sigc::mem_fun (*this, &Editor::unlock));
+               lock_dialog->get_vbox()->pack_start (*b);
+               
+               lock_dialog->get_vbox()->show_all ();
+               lock_dialog->set_size_request (200, 200);
+       }
+       
+       ActionManager::disable_all_actions ();
+       lock_dialog->present ();
+}
+
+void
+Editor::unlock ()
+{
+       lock_dialog->hide ();
+       ActionManager::pop_action_state ();
+}