2 Copyright (C) 2015-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
22 #include "system_font_dialog.h"
24 #include <dcp/warnings.h>
25 LIBDCP_DISABLE_WARNINGS
26 #include <wx/listctrl.h>
27 LIBDCP_ENABLE_WARNINGS
28 #include <boost/filesystem.hpp>
32 using boost::optional;
35 SystemFontDialog::SystemFontDialog (wxWindow* parent)
36 : wxDialog (parent, wxID_ANY, _("Choose a font"))
38 auto sizer = new wxBoxSizer (wxVERTICAL);
40 boost::filesystem::path fonts = "c:\\Windows\\Fonts";
41 auto windir = getenv ("windir");
43 fonts = boost::filesystem::path (windir) / "Fonts";
46 for (auto i: boost::filesystem::directory_iterator (fonts)) {
47 auto ext = i.path().extension().string();
48 transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
51 _fonts.push_back (i.path());
55 sort (_fonts.begin(), _fonts.end());
57 _list = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER);
58 _list->InsertColumn (0, wxT (""));
59 _list->SetColumnWidth (0, 512);
60 sizer->Add (_list, 0, wxALL, DCPOMATIC_SIZER_X_GAP);
63 for (auto i: _fonts) {
64 _list->InsertItem (n++, std_to_wx (i.leaf().stem().string ()));
67 auto buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
69 sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
72 SetSizerAndFit (sizer);
74 _list->Bind (wxEVT_LIST_ITEM_SELECTED, boost::bind (&SystemFontDialog::setup_sensitivity, this));
75 _list->Bind (wxEVT_LIST_ITEM_DESELECTED, boost::bind (&SystemFontDialog::setup_sensitivity, this));
81 optional<boost::filesystem::path>
82 SystemFontDialog::get_font () const
84 int const s = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
89 if (s < int(_fonts.size())) {
98 SystemFontDialog::setup_sensitivity ()
100 auto ok = dynamic_cast<wxButton *> (FindWindowById(wxID_OK, this));
102 ok->Enable (_list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED) != -1);