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