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