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