62fd046600006609c851f06258f0a9f1f75c2fea
[dcpomatic.git] / src / wx / system_font_dialog.cc
1 /*
2     Copyright (C) 2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "system_font_dialog.h"
21 #include "wx_util.h"
22 #include <wx/listctrl.h>
23 #include <boost/filesystem.hpp>
24 #include <boost/foreach.hpp>
25
26 using std::cout;
27 using std::string;
28 using boost::optional;
29
30 SystemFontDialog::SystemFontDialog (wxWindow* parent)
31         : wxDialog (parent, wxID_ANY, _("Choose a font"))
32 {
33         wxSizer* sizer = new wxBoxSizer (wxVERTICAL);
34
35         boost::filesystem::path fonts = "c:\\Windows\\Fonts";
36         char* windir = getenv ("windir");
37         if (windir) {
38                 fonts = boost::filesystem::path (windir) / "Fonts";
39         }
40
41         for (
42                 boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (fonts);
43                 i != boost::filesystem::directory_iterator ();
44                 ++i
45                 ) {
46
47                 string ext = i->path().extension().string ();
48                 transform (ext.begin(), ext.end(), ext.begin(), ::tolower);
49
50                 if (ext == ".ttf") {
51                         _fonts.push_back (i->path());
52                 }
53         }
54
55         sort (_fonts.begin (), _fonts.end ());
56
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);
61
62         int n = 0;
63         BOOST_FOREACH (boost::filesystem::path i, _fonts) {
64                 _list->InsertItem (n++, std_to_wx (i.leaf().stem().string ()));
65         }
66
67         wxSizer* buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
68         if (buttons) {
69                 sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
70         }
71
72         SetSizerAndFit (sizer);
73
74         _list->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, boost::bind (&SystemFontDialog::setup_sensitivity, this));
75         _list->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, boost::bind (&SystemFontDialog::setup_sensitivity, this));
76
77         setup_sensitivity ();
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 optional<boost::filesystem::path> ();
86         }
87
88         if (s < int (_fonts.size ())) {
89                 return _fonts[s];
90         }
91
92         return optional<boost::filesystem::path> ();
93 }
94
95 void
96 SystemFontDialog::setup_sensitivity ()
97 {
98         wxButton* ok = dynamic_cast<wxButton *> (FindWindowById (wxID_OK, this));
99         if (ok) {
100                 ok->Enable (_list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED) != -1);
101         }
102 }