summaryrefslogtreecommitdiff
path: root/src/wx/file_dialog.cc
blob: a726b306aee5b24afbccf4ad4a74258866682b05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
    Copyright (C) 2023 Carl Hetherington <cth@carlh.net>

    This file is part of DCP-o-matic.

    DCP-o-matic is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    DCP-o-matic is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.

*/


#include "file_dialog.h"
#include "wx_util.h"
#include "lib/config.h"
#include "lib/cross.h"
#include <boost/filesystem.hpp>
#include <vector>


using std::string;
using std::vector;
using boost::optional;


wxString initial_path(
	std::string initial_path_key,
	optional<boost::filesystem::path> override_path
	)
{
	if (override_path) {
		return std_to_wx(override_path->parent_path().string());
	}

	return std_to_wx(Config::instance()->initial_path(initial_path_key).get_value_or(home_directory()).string());
}


FileDialog::FileDialog(
	wxWindow* parent,
	wxString title,
	wxString allowed,
	long style,
	std::string initial_path_key,
	boost::optional<std::string> initial_filename,
	optional<boost::filesystem::path> override_path
	)
	: wxFileDialog(
		parent,
		title,
		initial_path(initial_path_key, override_path),
		std_to_wx(initial_filename.get_value_or("")),
		allowed,
		style
		)
	, _initial_path_key(initial_path_key)
	, _multiple(style & wxFD_MULTIPLE)
{

}


boost::filesystem::path
FileDialog::path() const
{
	return wx_to_std(GetPath());
}


vector<boost::filesystem::path>
FileDialog::paths() const
{
	wxArrayString wx_paths;
	GetPaths(wx_paths);
	vector<boost::filesystem::path> paths;
	for (unsigned int i = 0; i < wx_paths.GetCount(); ++i) {
		paths.push_back(wx_to_std(wx_paths[i]));
	}
	return paths;
}


bool
FileDialog::show()
{
	auto response = ShowModal();
	if (response != wxID_OK) {
		return false;
	}

	if (_multiple) {
		auto p = paths();
		DCPOMATIC_ASSERT(!p.empty());
		Config::instance()->set_initial_path(_initial_path_key, p[0].parent_path());
	} else {
		Config::instance()->set_initial_path(_initial_path_key, path().parent_path());
	}

	return true;
}