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