Supporters update.
[dcpomatic.git] / src / wx / file_picker_ctrl.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #include "dcpomatic_button.h"
23 #include "file_dialog.h"
24 #include "file_picker_ctrl.h"
25 #include "wx_util.h"
26 #include <dcp/warnings.h>
27 LIBDCP_DISABLE_WARNINGS
28 #include <wx/filepicker.h>
29 #include <wx/stdpaths.h>
30 #include <wx/wx.h>
31 LIBDCP_ENABLE_WARNINGS
32 #include <boost/filesystem.hpp>
33
34
35 using namespace std;
36 using namespace boost;
37
38
39 FilePickerCtrl::FilePickerCtrl(
40         wxWindow* parent,
41         wxString prompt,
42         wxString wildcard,
43         bool open,
44         bool warn_overwrite,
45         std::string initial_path_key,
46         optional<std::string> initial_filename,
47         optional<filesystem::path> override_path
48         )
49         : wxPanel (parent)
50         , _prompt (prompt)
51         , _wildcard (wildcard)
52         , _open (open)
53         , _warn_overwrite (warn_overwrite)
54         , _initial_path_key(initial_path_key)
55         , _initial_filename(initial_filename)
56         , _override_path(override_path)
57 {
58         _sizer = new wxBoxSizer (wxHORIZONTAL);
59
60         wxClientDC dc (parent);
61         wxSize size = dc.GetTextExtent (wxT ("This is the length of the file label it should be quite long"));
62         size.SetHeight (-1);
63
64         _file = new Button (this, _("(None)"), wxDefaultPosition, size, wxBU_LEFT);
65         _sizer->Add (_file, 1, wxEXPAND, 0);
66
67         SetSizerAndFit (_sizer);
68         _file->Bind (wxEVT_BUTTON, boost::bind (&FilePickerCtrl::browse_clicked, this));
69
70         set_filename(_initial_filename);
71 }
72
73
74 void
75 FilePickerCtrl::set_filename(optional<string> filename)
76 {
77         if (filename) {
78                 _file->SetLabel(std_to_wx(*filename));
79         } else {
80                 _file->SetLabel(_("None"));
81         }
82 }
83
84
85 void
86 FilePickerCtrl::set_path(optional<boost::filesystem::path> path)
87 {
88         _path = path;
89
90         if (_path) {
91                 set_filename(_path->filename().string());
92         } else {
93                 set_filename({});
94         }
95
96         wxCommandEvent ev (wxEVT_FILEPICKER_CHANGED, wxID_ANY);
97         GetEventHandler()->ProcessEvent (ev);
98 }
99
100
101 boost::optional<boost::filesystem::path>
102 FilePickerCtrl::path() const
103 {
104         return _path;
105 }
106
107
108 void
109 FilePickerCtrl::browse_clicked ()
110 {
111         long style = _open ? wxFD_OPEN : wxFD_SAVE;
112         if (_warn_overwrite) {
113                 style |= wxFD_OVERWRITE_PROMPT;
114         }
115         FileDialog dialog(this, _prompt, _wildcard, style, _initial_path_key, _initial_filename, _path);
116         if (dialog.show()) {
117                 set_path(dialog.path());
118         }
119 }
120
121 void
122 FilePickerCtrl::set_wildcard(wxString w)
123 {
124         _wildcard = w;
125 }