Better default font directories on Linux and OS X.
[dcpomatic.git] / src / wx / fonts_dialog.cc
1 /*
2     Copyright (C) 2014-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 "fonts_dialog.h"
21 #include "wx_util.h"
22 #include "system_font_dialog.h"
23 #include "lib/font.h"
24 #include "lib/subtitle_content.h"
25 #include <wx/wx.h>
26 #include <boost/foreach.hpp>
27
28 using std::list;
29 using std::string;
30 using std::cout;
31 using boost::shared_ptr;
32
33 FontsDialog::FontsDialog (wxWindow* parent, shared_ptr<SubtitleContent> content)
34         : wxDialog (parent, wxID_ANY, _("Fonts"))
35         , _content (content)
36         , _set_from_system (0)
37 {
38         _fonts = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxSize (400, 200), wxLC_REPORT | wxLC_SINGLE_SEL);
39
40         {
41                 wxListItem ip;
42                 ip.SetId (0);
43                 ip.SetText (_("ID"));
44                 ip.SetWidth (100);
45                 _fonts->InsertColumn (0, ip);
46         }
47
48         {
49                 wxListItem ip;
50                 ip.SetId (1);
51                 ip.SetText (_("Font file"));
52                 ip.SetWidth (300);
53                 _fonts->InsertColumn (1, ip);
54         }
55
56         wxBoxSizer* sizer = new wxBoxSizer (wxHORIZONTAL);
57         sizer->Add (_fonts, 1, wxEXPAND);
58
59         {
60                 wxSizer* s = new wxBoxSizer (wxVERTICAL);
61                 _set_from_file = new wxButton (this, wxID_ANY, _("Set from .ttf file..."));
62                 s->Add (_set_from_file, 0, wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
63 #ifdef DCPOMATIC_WINDOWS
64                 _set_from_system = new wxButton (this, wxID_ANY, _("Set from system font..."));
65                 s->Add (_set_from_system, 0, wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
66 #endif
67                 sizer->Add (s, 0, wxLEFT, DCPOMATIC_SIZER_X_GAP);
68         }
69
70         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
71         overall_sizer->Add (sizer, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_X_GAP);
72
73         wxSizer* buttons = CreateSeparatedButtonSizer (wxOK);
74         if (buttons) {
75                 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
76         }
77
78         SetSizerAndFit (overall_sizer);
79
80         _set_from_file->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&FontsDialog::set_from_file_clicked, this));
81         if (_set_from_system) {
82                 _set_from_system->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&FontsDialog::set_from_system_clicked, this));
83         }
84         _fonts->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, boost::bind (&FontsDialog::selection_changed, this));
85         _fonts->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, boost::bind (&FontsDialog::selection_changed, this));
86
87         setup ();
88 }
89
90 void
91 FontsDialog::setup ()
92 {
93         shared_ptr<SubtitleContent> content = _content.lock ();
94         if (!content) {
95                 return;
96         }
97
98         _fonts->DeleteAllItems ();
99         list<shared_ptr<Font> > fonts = content->fonts ();
100         size_t n = 0;
101         for (list<shared_ptr<Font> >::const_iterator i = fonts.begin(); i != fonts.end(); ++i) {
102                 wxListItem item;
103                 item.SetId (n);
104                 _fonts->InsertItem (item);
105                 _fonts->SetItem (n, 0, std_to_wx ((*i)->id ()));
106                 if ((*i)->file ()) {
107                         _fonts->SetItem (n, 1, (*i)->file().get().leaf().string ());
108                 }
109                 ++n;
110         }
111
112         update_sensitivity ();
113 }
114
115 void
116 FontsDialog::set_from_file_clicked ()
117 {
118         /* The wxFD_CHANGE_DIR here prevents a `could not set working directory' error 123 on Windows when using
119            non-Latin filenames or paths.
120         */
121         wxString default_dir = "";
122 #ifdef DCPOMATIC_LINUX
123         if (boost::filesystem::exists ("/usr/share/fonts/truetype")) {
124                 default_dir = "/usr/share/fonts/truetype";
125         } else {
126                 default_dir = "/usr/share/fonts";
127         }
128 #endif
129 #ifdef DCPOMATIC_OSX
130         default_dir = "/System/Library/Fonts";
131 #endif
132
133         wxFileDialog* d = new wxFileDialog (this, _("Choose a font file"), default_dir, wxT (""), wxT ("*.ttf"), wxFD_CHANGE_DIR);
134         int const r = d->ShowModal ();
135
136         if (r != wxID_OK) {
137                 d->Destroy ();
138                 return;
139         }
140
141         set_selected_font_file (wx_to_std (d->GetPath ()));
142         d->Destroy ();
143 }
144
145 void
146 FontsDialog::set_from_system_clicked ()
147 {
148         SystemFontDialog* d = new SystemFontDialog (this);
149         int const r = d->ShowModal ();
150
151         if (r != wxID_OK) {
152                 d->Destroy ();
153                 return;
154         }
155
156         set_selected_font_file (d->get_font().get ());
157         d->Destroy ();
158 }
159
160 void
161 FontsDialog::set_selected_font_file (boost::filesystem::path file)
162 {
163         shared_ptr<SubtitleContent> content = _content.lock ();
164         if (!content) {
165                 return;
166         }
167
168         int item = _fonts->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
169         if (item == -1) {
170                 return;
171         }
172
173         string id = wx_to_std (_fonts->GetItemText (item, 0));
174
175         BOOST_FOREACH (shared_ptr<Font> i, content->fonts ()) {
176                 if (i->id() == id) {
177                         i->set_file (file);
178                 }
179         }
180
181         setup ();
182 }
183
184 void
185 FontsDialog::selection_changed ()
186 {
187         update_sensitivity ();
188 }
189
190 void
191 FontsDialog::update_sensitivity ()
192 {
193         int const item = _fonts->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
194         _set_from_file->Enable (item != -1);
195         if (_set_from_system) {
196                 _set_from_system->Enable (item != -1);
197         }
198 }