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