C++11 tidying.
[dcpomatic.git] / src / wx / system_font_dialog.cc
1 /*
2     Copyright (C) 2015-2021 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 "system_font_dialog.h"
23 #include "wx_util.h"
24 #include <wx/listctrl.h>
25 #include <boost/filesystem.hpp>
26 #include <iostream>
27
28
29 using std::cout;
30 using std::string;
31 using boost::optional;
32
33
34 SystemFontDialog::SystemFontDialog (wxWindow* parent)
35         : wxDialog (parent, wxID_ANY, _("Choose a font"))
36 {
37         auto sizer = new wxBoxSizer (wxVERTICAL);
38
39         boost::filesystem::path fonts = "c:\\Windows\\Fonts";
40         auto windir = getenv ("windir");
41         if (windir) {
42                 fonts = boost::filesystem::path (windir) / "Fonts";
43         }
44
45         for (auto i: boost::filesystem::directory_iterator (fonts)) {
46                 auto ext = i.path().extension().string();
47                 transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
48
49                 if (ext == ".ttf") {
50                         _fonts.push_back (i.path());
51                 }
52         }
53
54         sort (_fonts.begin(), _fonts.end());
55
56         _list = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER);
57         _list->InsertColumn (0, wxT (""));
58         _list->SetColumnWidth (0, 512);
59         sizer->Add (_list, 0, wxALL, DCPOMATIC_SIZER_X_GAP);
60
61         int n = 0;
62         for (auto i: _fonts) {
63                 _list->InsertItem (n++, std_to_wx (i.leaf().stem().string ()));
64         }
65
66         auto buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
67         if (buttons) {
68                 sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
69         }
70
71         SetSizerAndFit (sizer);
72
73         _list->Bind (wxEVT_LIST_ITEM_SELECTED, boost::bind (&SystemFontDialog::setup_sensitivity, this));
74         _list->Bind (wxEVT_LIST_ITEM_DESELECTED, boost::bind (&SystemFontDialog::setup_sensitivity, this));
75
76         setup_sensitivity ();
77 }
78
79
80 optional<boost::filesystem::path>
81 SystemFontDialog::get_font () const
82 {
83         int const s = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
84         if (s == -1) {
85                 return {};
86         }
87
88         if (s < int(_fonts.size())) {
89                 return _fonts[s];
90         }
91
92         return {};
93 }
94
95
96 void
97 SystemFontDialog::setup_sensitivity ()
98 {
99         auto ok = dynamic_cast<wxButton *> (FindWindowById(wxID_OK, this));
100         if (ok) {
101                 ok->Enable (_list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED) != -1);
102         }
103 }