Try again to fix OS X build.
[dcpomatic.git] / src / wx / timeline.cc
index a0bf50f05e527297a21f2dd805badc9c64a8be19..0ddd78ae006099cb2d4d8504121b32387e4cda00 100644 (file)
 #include <boost/weak_ptr.hpp>
 #include <boost/foreach.hpp>
 #include <list>
+#include <iterator>
 #include <iostream>
 
 using std::list;
 using std::cout;
 using std::min;
 using std::max;
+using std::begin;
+using std::end;
 using boost::shared_ptr;
 using boost::weak_ptr;
 using boost::dynamic_pointer_cast;
@@ -76,6 +79,7 @@ Timeline::Timeline (wxWindow* parent, ContentPanel* cp, shared_ptr<Film> film)
        , _x_scroll_rate (16)
        , _y_scroll_rate (16)
        , _pixels_per_track (48)
+       , _first_resize (true)
 {
 #ifndef __WXOSX__
        _labels_canvas->SetDoubleBuffered (true);
@@ -95,7 +99,13 @@ Timeline::Timeline (wxWindow* parent, ContentPanel* cp, shared_ptr<Film> film)
        _main_canvas->Bind   (wxEVT_RIGHT_DOWN, boost::bind (&Timeline::right_down,   this, _1));
        _main_canvas->Bind   (wxEVT_MOTION,     boost::bind (&Timeline::mouse_moved,  this, _1));
        _main_canvas->Bind   (wxEVT_SIZE,       boost::bind (&Timeline::resized,      this));
-       _main_canvas->Bind   (wxEVT_SCROLLWIN_THUMBTRACK,  boost::bind (&Timeline::scrolled,     this, _1));
+       _main_canvas->Bind   (wxEVT_SCROLLWIN_TOP,        boost::bind (&Timeline::scrolled,     this, _1));
+       _main_canvas->Bind   (wxEVT_SCROLLWIN_BOTTOM,     boost::bind (&Timeline::scrolled,     this, _1));
+       _main_canvas->Bind   (wxEVT_SCROLLWIN_LINEUP,     boost::bind (&Timeline::scrolled,     this, _1));
+       _main_canvas->Bind   (wxEVT_SCROLLWIN_LINEDOWN,   boost::bind (&Timeline::scrolled,     this, _1));
+       _main_canvas->Bind   (wxEVT_SCROLLWIN_PAGEUP,     boost::bind (&Timeline::scrolled,     this, _1));
+       _main_canvas->Bind   (wxEVT_SCROLLWIN_PAGEDOWN,   boost::bind (&Timeline::scrolled,     this, _1));
+       _main_canvas->Bind   (wxEVT_SCROLLWIN_THUMBTRACK, boost::bind (&Timeline::scrolled,     this, _1));
 
        film_changed (Film::CONTENT);
 
@@ -104,8 +114,6 @@ Timeline::Timeline (wxWindow* parent, ContentPanel* cp, shared_ptr<Film> film)
        _film_changed_connection = film->Changed.connect (bind (&Timeline::film_changed, this, _1));
        _film_content_changed_connection = film->ContentChanged.connect (bind (&Timeline::film_content_changed, this, _2, _3));
 
-       set_pixels_per_second (static_cast<double>(640) / film->length().seconds ());
-
        setup_scrollbars ();
        _labels_canvas->ShowScrollbars (wxSHOW_SB_NEVER, wxSHOW_SB_NEVER);
 }
@@ -301,6 +309,27 @@ place (TimelineViewList& views, int& tracks)
        return tracks - base;
 }
 
+/** Compare the mapped output channels of two TimelineViews, so we can into
+ *  order of first mapped DCP channel.
+ */
+struct AudioMappingComparator {
+       bool operator()(shared_ptr<TimelineView> a, shared_ptr<TimelineView> b) {
+               int la = -1;
+               shared_ptr<TimelineAudioContentView> cva = dynamic_pointer_cast<TimelineAudioContentView>(a);
+               if (cva) {
+                       list<int> oc = cva->content()->audio->mapping().mapped_output_channels();
+                       la = *min_element(begin(oc), end(oc));
+               }
+               int lb = -1;
+               shared_ptr<TimelineAudioContentView> cvb = dynamic_pointer_cast<TimelineAudioContentView>(b);
+               if (cvb) {
+                       list<int> oc = cvb->content()->audio->mapping().mapped_output_channels();
+                       lb = *min_element(begin(oc), end(oc));
+               }
+               return la < lb;
+       }
+};
+
 void
 Timeline::assign_tracks ()
 {
@@ -368,11 +397,16 @@ Timeline::assign_tracks ()
                ++_tracks;
        }
 
-       /* Audio */
+       /* Audio.  We're sorting the views so that we get the audio views in order of increasing
+          DCP channel index.
+       */
 
-       place<TimelineAudioContentView> (_views, _tracks);
+       TimelineViewList views = _views;
+       sort(views.begin(), views.end(), AudioMappingComparator());
+       int const audio_tracks = place<TimelineAudioContentView> (views, _tracks);
 
        _labels_view->set_3d (have_3d);
+       _labels_view->set_audio_tracks (audio_tracks);
        _labels_view->set_subtitle_tracks (subtitle_tracks);
        _labels_view->set_atmos (have_atmos);
 
@@ -393,9 +427,12 @@ Timeline::setup_scrollbars ()
        if (!film || !_pixels_per_second) {
                return;
        }
-       _labels_canvas->SetVirtualSize (_labels_view->bbox().width, tracks() * pixels_per_track() + 96);
+
+       int const h = tracks() * pixels_per_track() + tracks_y_offset() + _time_axis_view->bbox().height;
+
+       _labels_canvas->SetVirtualSize (_labels_view->bbox().width, h);
        _labels_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
-       _main_canvas->SetVirtualSize (*_pixels_per_second * film->length().seconds(), tracks() * pixels_per_track() + 96);
+       _main_canvas->SetVirtualSize (*_pixels_per_second * film->length().seconds(), h);
        _main_canvas->SetScrollRate (_x_scroll_rate, _y_scroll_rate);
 }
 
@@ -429,6 +466,8 @@ Timeline::left_down (wxMouseEvent& ev)
                break;
        case ZOOM:
        case ZOOM_ALL:
+       case SNAP:
+       case SEQUENCE:
                /* Nothing to do */
                break;
        }
@@ -500,6 +539,8 @@ Timeline::left_up (wxMouseEvent& ev)
                left_up_zoom (ev);
                break;
        case ZOOM_ALL:
+       case SNAP:
+       case SEQUENCE:
                break;
        }
 }
@@ -543,13 +584,14 @@ Timeline::left_up_zoom (wxMouseEvent& ev)
        DCPTime const time_right = DCPTime::from_seconds((bottom_right.x + vsx) / *_pixels_per_second);
        set_pixels_per_second (double(GetSize().GetWidth()) / (time_right.seconds() - time_left.seconds()));
 
-       double const tracks_top = double(top_left.y) / _pixels_per_track;
-       double const tracks_bottom = double(bottom_right.y) / _pixels_per_track;
+       double const tracks_top = double(top_left.y - tracks_y_offset()) / _pixels_per_track;
+       double const tracks_bottom = double(bottom_right.y - tracks_y_offset()) / _pixels_per_track;
        set_pixels_per_track (lrint(GetSize().GetHeight() / (tracks_bottom - tracks_top)));
 
        setup_scrollbars ();
-       _main_canvas->Scroll (time_left.seconds() * *_pixels_per_second / _x_scroll_rate, tracks_top * _pixels_per_track / _y_scroll_rate);
-       _labels_canvas->Scroll (0, tracks_top * _pixels_per_track / _y_scroll_rate);
+       int const y = (tracks_top * _pixels_per_track + tracks_y_offset()) / _y_scroll_rate;
+       _main_canvas->Scroll (time_left.seconds() * *_pixels_per_second / _x_scroll_rate, y);
+       _labels_canvas->Scroll (0, y);
 
        _zoom_point = optional<wxPoint> ();
        Refresh ();
@@ -572,6 +614,8 @@ Timeline::mouse_moved (wxMouseEvent& ev)
                mouse_moved_zoom (ev);
                break;
        case ZOOM_ALL:
+       case SNAP:
+       case SEQUENCE:
                break;
        }
 }
@@ -612,6 +656,8 @@ Timeline::right_down (wxMouseEvent& ev)
                Refresh ();
                break;
        case ZOOM_ALL:
+       case SNAP:
+       case SEQUENCE:
                break;
        }
 }
@@ -722,6 +768,10 @@ Timeline::film () const
 void
 Timeline::resized ()
 {
+       if (_main_canvas->GetSize().GetWidth() > 0 && _first_resize) {
+               zoom_all ();
+               _first_resize = false;
+       }
        setup_scrollbars ();
 }
 
@@ -791,7 +841,9 @@ void
 Timeline::scrolled (wxScrollWinEvent& ev)
 {
        if (ev.GetOrientation() == wxVERTICAL) {
-               _labels_canvas->Scroll (0, ev.GetPosition ());
+               int x, y;
+               _main_canvas->GetViewStart (&x, &y);
+               _labels_canvas->Scroll (0, y);
        }
        ev.Skip ();
 }
@@ -805,11 +857,23 @@ Timeline::tool_clicked (Tool t)
                _tool = t;
                break;
        case ZOOM_ALL:
-               shared_ptr<Film> film = _film.lock ();
-               DCPOMATIC_ASSERT (film);
-               set_pixels_per_second ((_main_canvas->GetSize().GetWidth() - 32) / film->length().seconds());
-               set_pixels_per_track ((_main_canvas->GetSize().GetHeight() - tracks_y_offset() - _time_axis_view->bbox().height - 32) / _tracks);
-               Refresh ();
+               zoom_all ();
+               break;
+       case SNAP:
+       case SEQUENCE:
                break;
        }
 }
+
+void
+Timeline::zoom_all ()
+{
+       shared_ptr<Film> film = _film.lock ();
+       DCPOMATIC_ASSERT (film);
+       set_pixels_per_second ((_main_canvas->GetSize().GetWidth() - 32) / film->length().seconds());
+       set_pixels_per_track ((_main_canvas->GetSize().GetHeight() - tracks_y_offset() - _time_axis_view->bbox().height - 32) / _tracks);
+       setup_scrollbars ();
+       _main_canvas->Scroll (0, 0);
+       _labels_canvas->Scroll (0, 0);
+       Refresh ();
+}