Tidying.
[dcpomatic.git] / src / wx / fonts_dialog.cc
1 /*
2     Copyright (C) 2014-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 "dcpomatic_button.h"
23 #include "fonts_dialog.h"
24 #include "system_font_dialog.h"
25 #include "wx_util.h"
26 #include "lib/content.h"
27 #include "lib/font.h"
28 #include "lib/text_content.h"
29 #include <wx/wx.h>
30 #include <memory>
31
32
33 using std::list;
34 using std::shared_ptr;
35 using std::string;
36 using namespace dcpomatic;
37
38
39 FontsDialog::FontsDialog (wxWindow* parent, shared_ptr<Content> content, shared_ptr<TextContent> caption)
40         : wxDialog (parent, wxID_ANY, _("Fonts"))
41         , _content (content)
42         , _caption (caption)
43 {
44         _fonts = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxSize (550, 200), wxLC_REPORT | wxLC_SINGLE_SEL);
45
46         {
47                 wxListItem ip;
48                 ip.SetId (0);
49                 ip.SetText (_("ID"));
50                 ip.SetWidth (100);
51                 _fonts->InsertColumn (0, ip);
52         }
53
54         {
55                 wxListItem ip;
56                 ip.SetId (1);
57                 ip.SetText (_("File"));
58                 ip.SetWidth (450);
59                 _fonts->InsertColumn (1, ip);
60         }
61
62         auto sizer = new wxBoxSizer (wxHORIZONTAL);
63         sizer->Add (_fonts, 1, wxEXPAND | wxLEFT | wxRIGHT, DCPOMATIC_SIZER_X_GAP);
64
65         _edit = new Button (this, _("Edit..."));
66         sizer->Add (_edit, 0, wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
67
68         auto overall_sizer = new wxBoxSizer (wxVERTICAL);
69         overall_sizer->Add (sizer, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_X_GAP);
70
71         auto buttons = CreateSeparatedButtonSizer (wxOK);
72         if (buttons) {
73                 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
74         }
75
76         SetSizerAndFit (overall_sizer);
77
78         _edit->Bind (wxEVT_BUTTON, boost::bind (&FontsDialog::edit_clicked, this));
79         _fonts->Bind (wxEVT_LIST_ITEM_SELECTED, boost::bind (&FontsDialog::selection_changed, this));
80         _fonts->Bind (wxEVT_LIST_ITEM_DESELECTED, boost::bind (&FontsDialog::selection_changed, this));
81
82         setup ();
83 }
84
85
86 void
87 FontsDialog::setup ()
88 {
89         auto content = _content.lock ();
90         auto caption = _caption.lock ();
91         if (!content || !caption) {
92                 return;
93         }
94
95         _fonts->DeleteAllItems ();
96         size_t n = 0;
97         for (auto i: caption->fonts ()) {
98                 wxListItem item;
99                 item.SetId (n);
100                 _fonts->InsertItem (item);
101                 _fonts->SetItem (n, 0, std_to_wx (i->id ()));
102                 if (i->file()) {
103                         _fonts->SetItem (n, 1, i->file()->leaf().string ());
104                 }
105                 ++n;
106         }
107
108         setup_sensitivity ();
109 }
110
111
112 void
113 FontsDialog::selection_changed ()
114 {
115         setup_sensitivity ();
116 }
117
118
119 void
120 FontsDialog::setup_sensitivity ()
121 {
122         int const item = _fonts->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
123         _edit->Enable (item != -1);
124 }
125
126
127 void
128 FontsDialog::edit_clicked ()
129 {
130         auto content = _content.lock ();
131         auto caption = _caption.lock ();
132         if (!content || !caption) {
133                 return;
134         }
135
136         int const item = _fonts->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
137         auto const id = wx_to_std (_fonts->GetItemText(item, 0));
138         shared_ptr<Font> font;
139         for (auto i: caption->fonts()) {
140                 if (i->id() == id) {
141                         font = i;
142                 }
143         }
144
145         if (!font) {
146                 return;
147         }
148
149         /* The wxFD_CHANGE_DIR here prevents a `could not set working directory' error 123 on Windows when using
150            non-Latin filenames or paths.
151         */
152         wxString default_dir = "";
153 #ifdef DCPOMATIC_LINUX
154         if (boost::filesystem::exists ("/usr/share/fonts/truetype")) {
155                 default_dir = "/usr/share/fonts/truetype";
156         } else {
157                 default_dir = "/usr/share/fonts";
158         }
159 #endif
160 #ifdef DCPOMATIC_OSX
161         default_dir = "/System/Library/Fonts";
162 #endif
163
164         auto d = new wxFileDialog (this, _("Choose a font file"), default_dir, wxT(""), wxT("*.ttf;*.otf;*.ttc"), wxFD_CHANGE_DIR);
165         int const r = d->ShowModal ();
166
167         if (r != wxID_OK) {
168                 d->Destroy ();
169                 return;
170         }
171
172         font->set_file (wx_to_std(d->GetPath()));
173         d->Destroy ();
174
175         setup ();
176 }