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