Cleanup: rename some methods in FilePickerCtrl and use boost::filesystem::path more.
[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_picker_ctrl.h"
24 #include "wx_util.h"
25 #include <dcp/warnings.h>
26 LIBDCP_DISABLE_WARNINGS
27 #include <wx/filepicker.h>
28 #include <wx/stdpaths.h>
29 #include <wx/wx.h>
30 LIBDCP_ENABLE_WARNINGS
31 #include <boost/filesystem.hpp>
32
33
34 using namespace std;
35 using namespace boost;
36
37
38 FilePickerCtrl::FilePickerCtrl (wxWindow* parent, wxString prompt, wxString wildcard, bool open, bool warn_overwrite)
39         : wxPanel (parent)
40         , _prompt (prompt)
41         , _wildcard (wildcard)
42         , _open (open)
43         , _warn_overwrite (warn_overwrite)
44 {
45         _sizer = new wxBoxSizer (wxHORIZONTAL);
46
47         wxClientDC dc (parent);
48         wxSize size = dc.GetTextExtent (wxT ("This is the length of the file label it should be quite long"));
49         size.SetHeight (-1);
50
51         _file = new Button (this, _("(None)"), wxDefaultPosition, size, wxBU_LEFT);
52         _sizer->Add (_file, 1, wxEXPAND, 0);
53
54         SetSizerAndFit (_sizer);
55         _file->Bind (wxEVT_BUTTON, boost::bind (&FilePickerCtrl::browse_clicked, this));
56 }
57
58
59 void
60 FilePickerCtrl::set_path(boost::filesystem::path path)
61 {
62         _path = path;
63
64         if (!_path.empty()) {
65                 _file->SetLabel(std_to_wx(_path.filename().string()));
66         } else {
67                 _file->SetLabel (_("(None)"));
68         }
69
70         wxCommandEvent ev (wxEVT_FILEPICKER_CHANGED, wxID_ANY);
71         GetEventHandler()->ProcessEvent (ev);
72 }
73
74
75 boost::filesystem::path
76 FilePickerCtrl::path() const
77 {
78         return _path;
79 }
80
81
82 void
83 FilePickerCtrl::browse_clicked ()
84 {
85         long style = _open ? wxFD_OPEN : wxFD_SAVE;
86         if (_warn_overwrite) {
87                 style |= wxFD_OVERWRITE_PROMPT;
88         }
89         wxFileDialog dialog(this, _prompt, wxEmptyString, wxEmptyString, _wildcard, style);
90         dialog.SetPath(std_to_wx(_path.string()));
91         if (dialog.ShowModal() == wxID_OK) {
92                 set_path(boost::filesystem::path(wx_to_std(dialog.GetPath())));
93         }
94 }
95
96 void
97 FilePickerCtrl::set_wildcard(wxString w)
98 {
99         _wildcard = w;
100 }