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