Move video frame rate ('prepared-for') into Content.
[dcpomatic.git] / src / wx / content_panel.cc
index deeef7f80be5359bb43d640f7b7b7beea62afb30..2008241892879a5d87f9780b0f656e45309f9d93 100644 (file)
@@ -46,6 +46,7 @@ using std::list;
 using std::string;
 using std::cout;
 using std::vector;
+using std::exception;
 using boost::shared_ptr;
 using boost::weak_ptr;
 using boost::dynamic_pointer_cast;
@@ -123,7 +124,7 @@ ContentPanel::ContentPanel (wxNotebook* n, boost::shared_ptr<Film> film, FilmVie
        _content->Bind (wxEVT_DROP_FILES, boost::bind (&ContentPanel::files_dropped, this, _1));
        _add_file->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ContentPanel::add_file_clicked, this));
        _add_folder->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ContentPanel::add_folder_clicked, this));
-       _remove->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ContentPanel::remove_clicked, this));
+       _remove->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ContentPanel::remove_clicked, this, false));
        _earlier->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ContentPanel::earlier_clicked, this));
        _later->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ContentPanel::later_clicked, this));
        _timeline->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ContentPanel::timeline_clicked, this));
@@ -148,45 +149,42 @@ ContentPanel::selected ()
        return sel;
 }
 
-VideoContentList
+ContentList
 ContentPanel::selected_video ()
 {
-       VideoContentList vc;
+       ContentList vc;
 
        BOOST_FOREACH (shared_ptr<Content> i, selected ()) {
-               shared_ptr<VideoContent> t = dynamic_pointer_cast<VideoContent> (i);
-               if (t) {
-                       vc.push_back (t);
+               if (i->video) {
+                       vc.push_back (i);
                }
        }
 
        return vc;
 }
 
-AudioContentList
+ContentList
 ContentPanel::selected_audio ()
 {
-       AudioContentList ac;
+       ContentList ac;
 
        BOOST_FOREACH (shared_ptr<Content> i, selected ()) {
-               shared_ptr<AudioContent> t = dynamic_pointer_cast<AudioContent> (i);
-               if (t) {
-                       ac.push_back (t);
+               if (i->audio) {
+                       ac.push_back (i);
                }
        }
 
        return ac;
 }
 
-SubtitleContentList
+ContentList
 ContentPanel::selected_subtitle ()
 {
-       SubtitleContentList sc;
+       ContentList sc;
 
        BOOST_FOREACH (shared_ptr<Content> i, selected ()) {
-               shared_ptr<SubtitleContent> t = dynamic_pointer_cast<SubtitleContent> (i);
-               if (t) {
-                       sc.push_back (t);
+               if (i->subtitle) {
+                       sc.push_back (i);
                }
        }
 
@@ -228,6 +226,15 @@ ContentPanel::film_changed (Film::Property p)
 void
 ContentPanel::selection_changed ()
 {
+       if (_last_selected == selected()) {
+               /* This was triggered by a re-build of the view but the selection
+                  did not really change.
+               */
+               return;
+       }
+
+       _last_selected = selected ();
+
        setup_sensitivity ();
 
        BOOST_FOREACH (ContentSubPanel* i, _panels) {
@@ -302,7 +309,15 @@ ContentPanel::add_folder_clicked ()
                return;
        }
 
-       shared_ptr<Content> content = content_factory (_film, path);
+       shared_ptr<Content> content;
+
+       try {
+               content = content_factory (_film, path);
+       } catch (exception& e) {
+               error_dialog (_parent, e.what());
+               return;
+       }
+
        if (!content) {
                error_dialog (_parent, _("No content found in this folder."));
                return;
@@ -325,14 +340,23 @@ ContentPanel::add_folder_clicked ()
        _film->examine_and_add_content (content);
 }
 
-void
-ContentPanel::remove_clicked ()
+/** @return true if this remove "click" should be ignored */
+bool
+ContentPanel::remove_clicked (bool hotkey)
 {
+       /* If the method was called because Delete was pressed check that our notebook page
+          is visible and that the content list is focussed.
+       */
+       if (hotkey && (_parent->GetCurrentPage() != _panel || !_content->HasFocus())) {
+               return true;
+       }
+
        BOOST_FOREACH (shared_ptr<Content> i, selected ()) {
                _film->remove_content (i);
        }
 
        selection_changed ();
+       return false;
 }
 
 void
@@ -361,8 +385,8 @@ ContentPanel::setup_sensitivity ()
        _add_folder->Enable (_generally_sensitive);
 
        ContentList selection = selected ();
-       VideoContentList video_selection = selected_video ();
-       AudioContentList audio_selection = selected_audio ();
+       ContentList video_selection = selected_video ();
+       ContentList audio_selection = selected_audio ();
 
        _remove->Enable   (!selection.empty() && _generally_sensitive);
        _earlier->Enable  (selection.size() == 1 && _generally_sensitive);
@@ -371,7 +395,7 @@ ContentPanel::setup_sensitivity ()
 
        _video_panel->Enable    (video_selection.size() > 0 && _generally_sensitive);
        _audio_panel->Enable    (audio_selection.size() > 0 && _generally_sensitive);
-       _subtitle_panel->Enable (selection.size() == 1 && dynamic_pointer_cast<SubtitleContent> (selection.front()) && _generally_sensitive);
+       _subtitle_panel->Enable (selection.size() == 1 && selection.front()->subtitle && _generally_sensitive);
        _timing_panel->Enable   (selection.size() == 1 && _generally_sensitive);
 }
 
@@ -454,37 +478,14 @@ ContentPanel::setup ()
 {
        ContentList content = _film->content ();
 
-       /* First, check to see if anything has changed and bail if not; this avoids
-          flickering on OS X.
-       */
-
-       vector<string> existing;
-       for (int i = 0; i < _content->GetItemCount(); ++i) {
-               existing.push_back (wx_to_std (_content->GetItemText (i)));
-       }
-
-       vector<string> proposed;
-       BOOST_FOREACH (shared_ptr<Content> i, content) {
-               bool const valid = i->paths_valid ();
-
-               string s = i->summary ();
-               if (!valid) {
-                       s = _("MISSING: ") + s;
-               }
-
-               proposed.push_back (s);
-       }
-
-       if (existing == proposed) {
-               return;
-       }
-
-       /* Something has changed: set up the control */
-
-       string selected_summary;
+       Content* selected_content = 0;
        int const s = _content->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
        if (s != -1) {
-               selected_summary = wx_to_std (_content->GetItemText (s));
+               wxListItem item;
+               item.SetId (s);
+               item.SetMask (wxLIST_MASK_DATA);
+               _content->GetItem (item);
+               selected_content = reinterpret_cast<Content*> (item.GetData ());
        }
 
        _content->DeleteAllItems ();
@@ -505,9 +506,13 @@ ContentPanel::setup ()
                        s = _("NEEDS KDM: ") + s;
                }
 
-               _content->InsertItem (t, std_to_wx (s));
+               wxListItem item;
+               item.SetId (t);
+               item.SetText (std_to_wx (s));
+               item.SetData (i.get ());
+               _content->InsertItem (item);
 
-               if (i->summary() == selected_summary) {
+               if (i.get() == selected_content) {
                        _content->SetItemState (t, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
                }
 
@@ -516,7 +521,7 @@ ContentPanel::setup ()
                }
        }
 
-       if (selected_summary.empty () && !content.empty ()) {
+       if (!selected_content && !content.empty ()) {
                /* Select the item of content if none was selected before */
                _content->SetItemState (0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
        }