2 Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 #include "fonts_dialog.h"
22 #include "system_font_dialog.h"
24 #include "lib/subtitle_content.h"
26 #include <boost/foreach.hpp>
31 using boost::shared_ptr;
33 FontsDialog::FontsDialog (wxWindow* parent, shared_ptr<SubtitleContent> content)
34 : wxDialog (parent, wxID_ANY, _("Fonts"))
36 , _set_from_system (0)
38 _fonts = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxSize (400, 200), wxLC_REPORT | wxLC_SINGLE_SEL);
45 _fonts->InsertColumn (0, ip);
51 ip.SetText (_("Font file"));
53 _fonts->InsertColumn (1, ip);
56 wxBoxSizer* sizer = new wxBoxSizer (wxHORIZONTAL);
57 sizer->Add (_fonts, 1, wxEXPAND);
60 wxSizer* s = new wxBoxSizer (wxVERTICAL);
61 _set_from_file = new wxButton (this, wxID_ANY, _("Set from .ttf file..."));
62 s->Add (_set_from_file, 0, wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
63 #ifdef DCPOMATIC_WINDOWS
64 _set_from_system = new wxButton (this, wxID_ANY, _("Set from system font..."));
65 s->Add (_set_from_system, 0, wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
67 sizer->Add (s, 0, wxLEFT, DCPOMATIC_SIZER_X_GAP);
70 wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
71 overall_sizer->Add (sizer, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_X_GAP);
73 wxSizer* buttons = CreateSeparatedButtonSizer (wxOK);
75 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
78 SetSizerAndFit (overall_sizer);
80 _set_from_file->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&FontsDialog::set_from_file_clicked, this));
81 if (_set_from_system) {
82 _set_from_system->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&FontsDialog::set_from_system_clicked, this));
84 _fonts->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, boost::bind (&FontsDialog::selection_changed, this));
85 _fonts->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, boost::bind (&FontsDialog::selection_changed, this));
93 shared_ptr<SubtitleContent> content = _content.lock ();
98 _fonts->DeleteAllItems ();
99 list<shared_ptr<Font> > fonts = content->fonts ();
101 for (list<shared_ptr<Font> >::const_iterator i = fonts.begin(); i != fonts.end(); ++i) {
104 _fonts->InsertItem (item);
105 _fonts->SetItem (n, 0, std_to_wx ((*i)->id ()));
107 _fonts->SetItem (n, 1, (*i)->file().get().leaf().string ());
112 update_sensitivity ();
116 FontsDialog::set_from_file_clicked ()
118 /* The wxFD_CHANGE_DIR here prevents a `could not set working directory' error 123 on Windows when using
119 non-Latin filenames or paths.
121 wxString default_dir = "";
122 #ifdef DCPOMATIC_LINUX
123 if (boost::filesystem::exists ("/usr/share/fonts/truetype")) {
124 default_dir = "/usr/share/fonts/truetype";
126 default_dir = "/usr/share/fonts";
130 default_dir = "/System/Library/Fonts";
133 wxFileDialog* d = new wxFileDialog (this, _("Choose a font file"), default_dir, wxT (""), wxT ("*.ttf"), wxFD_CHANGE_DIR);
134 int const r = d->ShowModal ();
141 set_selected_font_file (wx_to_std (d->GetPath ()));
146 FontsDialog::set_from_system_clicked ()
148 SystemFontDialog* d = new SystemFontDialog (this);
149 int const r = d->ShowModal ();
156 set_selected_font_file (d->get_font().get ());
161 FontsDialog::set_selected_font_file (boost::filesystem::path file)
163 shared_ptr<SubtitleContent> content = _content.lock ();
168 int item = _fonts->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
173 string id = wx_to_std (_fonts->GetItemText (item, 0));
175 BOOST_FOREACH (shared_ptr<Font> i, content->fonts ()) {
185 FontsDialog::selection_changed ()
187 update_sensitivity ();
191 FontsDialog::update_sensitivity ()
193 int const item = _fonts->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
194 _set_from_file->Enable (item != -1);
195 if (_set_from_system) {
196 _set_from_system->Enable (item != -1);