Use ScopeGuard for _no_check_selection
[dcpomatic.git] / src / wx / content_panel.cc
index d9400e34c504ee64081945539ba641a8422f04fb..77607ad16cb39ca01eb35c62f7b68e85f250c8cb 100644 (file)
 #include "audio_panel.h"
 #include "content_panel.h"
 #include "dcpomatic_button.h"
+#include "dir_dialog.h"
+#include "file_dialog.h"
 #include "film_viewer.h"
 #include "image_sequence_dialog.h"
 #include "text_panel.h"
 #include "timeline_dialog.h"
 #include "timing_panel.h"
 #include "video_panel.h"
+#include "wx_ptr.h"
 #include "wx_util.h"
 #include "lib/audio_content.h"
 #include "lib/case_insensitive_sorter.h"
@@ -568,6 +571,17 @@ ContentPanel::check_selection ()
 }
 
 
+optional<boost::filesystem::path>
+ContentPanel::add_files_override_path() const
+{
+       DCPOMATIC_ASSERT(_film->directory());
+       return Config::instance()->default_add_file_location() == Config::DefaultAddFileLocation::SAME_AS_PROJECT
+               ? _film->directory()->parent_path()
+               : boost::optional<boost::filesystem::path>();
+
+}
+
+
 void
 ContentPanel::add_file_clicked ()
 {
@@ -578,56 +592,31 @@ ContentPanel::add_file_clicked ()
                return;
        }
 
-       auto path = Config::instance()->add_files_path();
-
        /* The wxFD_CHANGE_DIR here prevents a `could not set working directory' error 123 on Windows when using
           non-Latin filenames or paths.
        */
-       auto d = new wxFileDialog (
+       FileDialog dialog(
                _splitter,
                _("Choose a file or files"),
-               std_to_wx(path ? path->string() : home_directory().string()),
-               wxT (""),
-               wxT ("All files|*.*|Subtitle files|*.srt;*.xml|Audio files|*.wav;*.w64;*.flac;*.aif;*.aiff"),
-               wxFD_MULTIPLE | wxFD_CHANGE_DIR
+               wxT("All files|*.*|Subtitle files|*.srt;*.xml|Audio files|*.wav;*.w64;*.flac;*.aif;*.aiff"),
+               wxFD_MULTIPLE | wxFD_CHANGE_DIR,
+               "AddFilesPath",
+               add_files_override_path()
                );
 
-       int const r = d->ShowModal ();
-
-       if (r != wxID_OK) {
-               d->Destroy ();
-               return;
-       }
-
-       wxArrayString paths;
-       d->GetPaths (paths);
-       vector<boost::filesystem::path> path_list;
-       for (unsigned int i = 0; i < paths.GetCount(); ++i) {
-               path_list.push_back (wx_to_std(paths[i]));
-       }
-       add_files (path_list);
-
-       if (!path_list.empty()) {
-               Config::instance()->set_add_files_path(path_list[0].parent_path());
+       if (dialog.show()) {
+               add_files(dialog.paths());
        }
-
-       d->Destroy ();
 }
 
 
 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 ();
-
-       if (r != wxID_OK) {
-               return;
+       DirDialog dialog(_splitter, _("Choose a folder"), wxDD_DIR_MUST_EXIST, "AddFilesPath", add_files_override_path());
+       if (dialog.show()) {
+               add_folder(dialog.path());
        }
-
-       add_folder(path);
 }
 
 
@@ -651,16 +640,12 @@ ContentPanel::add_folder(boost::filesystem::path folder)
        for (auto i: content) {
                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 ();
+                       ImageSequenceDialog dialog(_splitter);
 
-                       if (r != wxID_OK) {
+                       if (dialog.ShowModal() != wxID_OK) {
                                return;
                        }
-
-                       ic->set_video_frame_rate (frame_rate);
+                       ic->set_video_frame_rate(_film, dialog.frame_rate());
                }
 
                _film->examine_and_add_content (i);
@@ -671,16 +656,10 @@ 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 ();
-
-       if (r != wxID_OK) {
-               return;
+       DirDialog dialog(_splitter, _("Choose a DCP folder"), wxDD_DIR_MUST_EXIST, "AddFilesPath", add_files_override_path());
+       if (dialog.show()) {
+               add_dcp(dialog.path());
        }
-
-       add_dcp(path);
 }
 
 
@@ -730,12 +709,7 @@ ContentPanel::timeline_clicked ()
                return;
        }
 
-       if (_timeline_dialog) {
-               _timeline_dialog->Destroy ();
-               _timeline_dialog = nullptr;
-       }
-
-       _timeline_dialog = new TimelineDialog (this, _film, _film_viewer);
+       _timeline_dialog.reset(this, _film, _film_viewer);
        _timeline_dialog->set_selection (selected());
        _timeline_dialog->Show ();
 }
@@ -843,14 +817,16 @@ ContentPanel::set_selection (weak_ptr<Content> wc)
 void
 ContentPanel::set_selection (ContentList cl)
 {
-       _no_check_selection = true;
+       {
+               _no_check_selection = true;
+               ScopeGuard sg = [this]() { _no_check_selection = false; };
 
-       auto content = _film->content ();
-       for (size_t i = 0; i < content.size(); ++i) {
-               set_selected_state(i, find(cl.begin(), cl.end(), content[i]) != cl.end());
+               auto content = _film->content ();
+               for (size_t i = 0; i < content.size(); ++i) {
+                       set_selected_state(i, find(cl.begin(), cl.end(), content[i]) != cl.end());
+               }
        }
 
-       _no_check_selection = false;
        check_selection ();
 }