Use more ScopeGuards.
[dcpomatic.git] / src / wx / content_panel.cc
index 6e08644177343d4ac1319a850d7be59a2ba14871..c18520c30b4b677453372736dd78e40f0ba70d70 100644 (file)
@@ -43,6 +43,7 @@
 #include "lib/image_content.h"
 #include "lib/log.h"
 #include "lib/playlist.h"
+#include "lib/scope_guard.h"
 #include "lib/string_text_file.h"
 #include "lib/string_text_file_content.h"
 #include "lib/text_content.h"
@@ -73,6 +74,67 @@ using namespace boost::placeholders;
 #endif
 
 
+class LimitedContentPanelSplitter : public wxSplitterWindow
+{
+public:
+       LimitedContentPanelSplitter(wxWindow* parent)
+               : wxSplitterWindow(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxSP_NOBORDER | wxSP_3DSASH | wxSP_LIVE_UPDATE)
+       {
+               /* This value doesn't really mean much but we just want to stop double-click on the
+                  divider from shrinking the bottom panel (#1601).
+                  */
+               SetMinimumPaneSize(64);
+
+               Bind(wxEVT_SIZE, boost::bind(&LimitedContentPanelSplitter::sized, this, _1));
+       }
+
+       bool OnSashPositionChange(int new_position) override
+       {
+               /* Try to stop the top bit of the splitter getting so small that buttons disappear */
+               auto const ok = new_position > 220;
+               if (ok) {
+                       Config::instance()->set_main_content_divider_sash_position(new_position);
+               }
+               return ok;
+       }
+
+       void first_shown(wxWindow* top, wxWindow* bottom)
+       {
+               int const sn = wxDisplay::GetFromWindow(this);
+               /* Fallback for when GetFromWindow fails for reasons that aren't clear */
+               int pos = -600;
+               if (sn >= 0) {
+                       wxRect const screen = wxDisplay(sn).GetClientArea();
+                       /* This is a hack to try and make the content notebook a sensible size; large on big displays but small
+                          enough on small displays to leave space for the content area.
+                          */
+                       pos = screen.height > 800 ? -600 : -_top_panel_minimum_size;
+               }
+               SplitHorizontally(top, bottom, Config::instance()->main_content_divider_sash_position().get_value_or(pos));
+               _first_shown = true;
+       }
+
+private:
+       void sized(wxSizeEvent& ev)
+       {
+               auto const height = GetSize().GetHeight();
+               if (_first_shown && (!_last_height || *_last_height != height) && height > _top_panel_minimum_size && GetSashPosition() < _top_panel_minimum_size) {
+                       /* The window is now fairly big but the top panel is small; this happens when the DCP-o-matic window
+                        * is shrunk and then made larger again.  Try to set a sensible top panel size in this case (#1839).
+                        */
+                       SetSashPosition(Config::instance()->main_content_divider_sash_position().get_value_or(_top_panel_minimum_size));
+               }
+
+               ev.Skip ();
+               _last_height = height;
+       }
+
+       bool _first_shown = false;
+       int const _top_panel_minimum_size = 350;
+       boost::optional<int> _last_height;
+};
+
+
 class ContentDropTarget : public wxFileDropTarget
 {
 public:
@@ -118,7 +180,48 @@ private:
 };
 
 
-ContentPanel::ContentPanel (wxNotebook* n, shared_ptr<Film> film, weak_ptr<FilmViewer> viewer)
+/** A wxListCtrl that can middle-ellipsize its text */
+class ContentListCtrl : public wxListCtrl
+{
+public:
+       ContentListCtrl(wxWindow* parent)
+               : wxListCtrl(parent, wxID_ANY, wxDefaultPosition, wxSize(320, 160), wxLC_REPORT | wxLC_NO_HEADER | wxLC_VIRTUAL)
+       {
+               _red.SetTextColour(*wxRED);
+       }
+
+       struct Item
+       {
+               wxString text;
+               bool error;
+       };
+
+       void set(vector<Item> const& items)
+       {
+               _items = items;
+               SetItemCount(items.size());
+       }
+
+       wxString OnGetItemText(long item, long) const override
+       {
+               DCPOMATIC_ASSERT(item >= 0 && item < static_cast<long>(_items.size()));
+               wxClientDC dc(const_cast<wxWindow*>(static_cast<wxWindow const*>(this)));
+               return wxControl::Ellipsize(_items[item].text, dc, wxELLIPSIZE_MIDDLE, GetSize().GetWidth());
+       }
+
+       wxListItemAttr* OnGetItemAttr(long item) const override
+       {
+               DCPOMATIC_ASSERT(item >= 0 && item < static_cast<long>(_items.size()));
+               return _items[item].error ? const_cast<wxListItemAttr*>(&_red) : nullptr;
+       }
+
+private:
+       std::vector<Item> _items;
+       wxListItemAttr _red;
+};
+
+
+ContentPanel::ContentPanel(wxNotebook* n, shared_ptr<Film> film, FilmViewer& viewer)
        : _parent (n)
        , _film (film)
        , _film_viewer (viewer)
@@ -126,7 +229,7 @@ ContentPanel::ContentPanel (wxNotebook* n, shared_ptr<Film> film, weak_ptr<FilmV
        , _ignore_deselect (false)
        , _no_check_selection (false)
 {
-       _splitter = new LimitedSplitter (n);
+       _splitter = new LimitedContentPanelSplitter(n);
        _top_panel = new wxPanel (_splitter);
 
        _menu = new ContentMenu (_splitter, _film_viewer);
@@ -134,12 +237,12 @@ ContentPanel::ContentPanel (wxNotebook* n, shared_ptr<Film> film, weak_ptr<FilmV
        {
                auto s = new wxBoxSizer (wxHORIZONTAL);
 
-               _content = new wxListCtrl (_top_panel, wxID_ANY, wxDefaultPosition, wxSize (320, 160), wxLC_REPORT | wxLC_NO_HEADER);
+               _content = new ContentListCtrl(_top_panel);
                _content->DragAcceptFiles (true);
                s->Add (_content, 1, wxEXPAND | wxTOP | wxBOTTOM, 6);
 
                _content->InsertColumn (0, wxT(""));
-               _content->SetColumnWidth (0, 512);
+               _content->SetColumnWidth(0, 2048);
 
                auto b = new wxBoxSizer (wxVERTICAL);
 
@@ -371,9 +474,7 @@ ContentPanel::check_selection ()
        }
 
        if (go_to && Config::instance()->jump_to_selected() && signal_manager) {
-               auto fv = _film_viewer.lock ();
-               DCPOMATIC_ASSERT (fv);
-               signal_manager->when_idle(boost::bind(&FilmViewer::seek, fv.get(), go_to.get().ceil(_film->video_frame_rate()), true));
+               signal_manager->when_idle(boost::bind(&FilmViewer::seek, &_film_viewer, go_to.get().ceil(_film->video_frame_rate()), true));
        }
 
        if (_timeline_dialog) {
@@ -478,7 +579,7 @@ ContentPanel::add_file_clicked ()
                return;
        }
 
-       auto path = Config::instance()->add_files_path();
+       auto path = Config::instance()->initial_path("AddFilesPath");
 
        /* The wxFD_CHANGE_DIR here prevents a `could not set working directory' error 123 on Windows when using
           non-Latin filenames or paths.
@@ -508,7 +609,7 @@ ContentPanel::add_file_clicked ()
        add_files (path_list);
 
        if (!path_list.empty()) {
-               Config::instance()->set_add_files_path(path_list[0].parent_path());
+               Config::instance()->set_initial_path("AddFilesPath", path_list[0].parent_path());
        }
 
        d->Destroy ();
@@ -518,15 +619,16 @@ ContentPanel::add_file_clicked ()
 void
 ContentPanel::add_folder_clicked ()
 {
-       auto d = new wxDirDialog (_splitter, _("Choose a folder"), wxT(""), wxDD_DIR_MUST_EXIST);
-       int r = d->ShowModal ();
-       boost::filesystem::path const path (wx_to_std (d->GetPath ()));
-       d->Destroy ();
+       auto const initial_path = Config::instance()->initial_path("AddFilesPath");
 
+       auto d = new wxDirDialog(_splitter, _("Choose a folder"), std_to_wx(initial_path ? initial_path->string() : home_directory().string()), wxDD_DIR_MUST_EXIST);
+       ScopeGuard sg = [d]() { d->Destroy(); };
+       int r = d->ShowModal ();
        if (r != wxID_OK) {
                return;
        }
 
+       boost::filesystem::path const path(wx_to_std(d->GetPath()));
        add_folder(path);
 }
 
@@ -552,15 +654,12 @@ ContentPanel::add_folder(boost::filesystem::path folder)
                auto ic = dynamic_pointer_cast<ImageContent> (i);
                if (ic) {
                        auto e = new ImageSequenceDialog (_splitter);
-                       int const r = e->ShowModal();
-                       auto const frame_rate = e->frame_rate ();
-                       e->Destroy ();
+                       ScopeGuard sg = [e]() { e->Destroy(); };
 
-                       if (r != wxID_OK) {
+                       if (e->ShowModal() != wxID_OK) {
                                return;
                        }
-
-                       ic->set_video_frame_rate (frame_rate);
+                       ic->set_video_frame_rate(_film, e->frame_rate());
                }
 
                _film->examine_and_add_content (i);
@@ -571,15 +670,16 @@ ContentPanel::add_folder(boost::filesystem::path folder)
 void
 ContentPanel::add_dcp_clicked ()
 {
-       auto d = new wxDirDialog (_splitter, _("Choose a DCP folder"), wxT(""), wxDD_DIR_MUST_EXIST);
-       int r = d->ShowModal ();
-       boost::filesystem::path const path (wx_to_std (d->GetPath ()));
-       d->Destroy ();
+       auto const initial_path = Config::instance()->initial_path("AddFilesPath");
 
+       auto d = new wxDirDialog(_splitter, _("Choose a DCP folder"), std_to_wx(initial_path ? initial_path->string() : home_directory().string()), wxDD_DIR_MUST_EXIST);
+       ScopeGuard sg = [d]() { d->Destroy(); };
+       int r = d->ShowModal ();
        if (r != wxID_OK) {
                return;
        }
 
+       boost::filesystem::path const path(wx_to_std(d->GetPath()));
        add_dcp(path);
 }
 
@@ -802,7 +902,7 @@ ContentPanel::setup ()
                selected_content = reinterpret_cast<Content*> (item.GetData ());
        }
 
-       _content->DeleteAllItems ();
+       vector<ContentListCtrl::Item> items;
 
        for (auto i: content) {
                int const t = _content->GetItemCount ();
@@ -826,21 +926,15 @@ ContentPanel::setup ()
                        s = _("NEEDS OV: ") + s;
                }
 
-               wxListItem item;
-               item.SetId (t);
-               item.SetText (s);
-               item.SetData (i.get ());
-               _content->InsertItem (item);
+               items.push_back({s, !valid || needs_kdm || needs_assets});
 
                if (i.get() == selected_content) {
                        set_selected_state(t, true);
                }
-
-               if (!valid || needs_kdm || needs_assets) {
-                       _content->SetItemTextColour (t, *wxRED);
-               }
        }
 
+       _content->set(items);
+
        if (!selected_content && !content.empty ()) {
                /* Select the item of content if none was selected before */
                set_selected_state(0, true);
@@ -923,47 +1017,8 @@ ContentPanel::set_selected_state(int item, bool state)
 }
 
 
-LimitedSplitter::LimitedSplitter (wxWindow* parent)
-       : wxSplitterWindow (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxSP_NOBORDER | wxSP_3DSASH | wxSP_LIVE_UPDATE)
-       , _first_shown (false)
-       , _top_panel_minimum_size (350)
-{
-       /* This value doesn't really mean much but we just want to stop double-click on the
-          divider from shrinking the bottom panel (#1601).
-       */
-       SetMinimumPaneSize (64);
-
-       Bind (wxEVT_SIZE, boost::bind(&LimitedSplitter::sized, this, _1));
-}
-
-
-void
-LimitedSplitter::first_shown (wxWindow* top, wxWindow* bottom)
-{
-       int const sn = wxDisplay::GetFromWindow(this);
-       if (sn >= 0) {
-               wxRect const screen = wxDisplay(sn).GetClientArea();
-               /* This is a hack to try and make the content notebook a sensible size; large on big displays but small
-                  enough on small displays to leave space for the content area.
-                  */
-               SplitHorizontally (top, bottom, screen.height > 800 ? -600 : -_top_panel_minimum_size);
-       } else {
-               /* Fallback for when GetFromWindow fails for reasons that aren't clear */
-               SplitHorizontally (top, bottom, -600);
-       }
-       _first_shown = true;
-}
-
-
-void
-LimitedSplitter::sized (wxSizeEvent& ev)
+wxWindow*
+ContentPanel::window() const
 {
-       if (_first_shown && GetSize().GetHeight() > _top_panel_minimum_size && GetSashPosition() < _top_panel_minimum_size) {
-               /* The window is now fairly big but the top panel is small; this happens when the DCP-o-matic window
-                * is shrunk and then made larger again.  Try to set a sensible top panel size in this case (#1839).
-                */
-               SetSashPosition (_top_panel_minimum_size);
-       }
-
-       ev.Skip ();
+       return _splitter;
 }