Fix incorrect preview of italic subtitles (#728).
[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 "font_files_dialog.h"
24 #include "lib/font.h"
25 #include "lib/subtitle_content.h"
26 #include <wx/wx.h>
27 #include <boost/foreach.hpp>
28 #include <iostream>
29
30 using std::list;
31 using std::string;
32 using std::cout;
33 using boost::shared_ptr;
34
35 FontsDialog::FontsDialog (wxWindow* parent, shared_ptr<SubtitleContent> content)
36         : wxDialog (parent, wxID_ANY, _("Fonts"))
37         , _content (content)
38 {
39         _fonts = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxSize (550, 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 (_("Normal file"));
53                 ip.SetWidth (150);
54                 _fonts->InsertColumn (1, ip);
55         }
56
57         {
58                 wxListItem ip;
59                 ip.SetId (2);
60                 ip.SetText (_("Italic file"));
61                 ip.SetWidth (150);
62                 _fonts->InsertColumn (2, ip);
63         }
64
65         {
66                 wxListItem ip;
67                 ip.SetId (3);
68                 ip.SetText (_("Bold file"));
69                 ip.SetWidth (150);
70                 _fonts->InsertColumn (3, ip);
71         }
72
73         wxBoxSizer* sizer = new wxBoxSizer (wxHORIZONTAL);
74         sizer->Add (_fonts, 1, wxEXPAND | wxLEFT | wxRIGHT, DCPOMATIC_SIZER_X_GAP);
75
76         _edit = new wxButton (this, wxID_ANY, _("Edit..."));
77         sizer->Add (_edit, 0, wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
78
79         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
80         overall_sizer->Add (sizer, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_X_GAP);
81
82         wxSizer* buttons = CreateSeparatedButtonSizer (wxOK);
83         if (buttons) {
84                 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
85         }
86
87         SetSizerAndFit (overall_sizer);
88
89         _edit->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&FontsDialog::edit_clicked, this));
90         _fonts->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, boost::bind (&FontsDialog::selection_changed, this));
91         _fonts->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, boost::bind (&FontsDialog::selection_changed, this));
92
93         setup ();
94 }
95
96 void
97 FontsDialog::setup ()
98 {
99         shared_ptr<SubtitleContent> content = _content.lock ();
100         if (!content) {
101                 return;
102         }
103
104         _fonts->DeleteAllItems ();
105         size_t n = 0;
106         BOOST_FOREACH (shared_ptr<Font> i, content->fonts ()) {
107                 wxListItem item;
108                 item.SetId (n);
109                 _fonts->InsertItem (item);
110                 _fonts->SetItem (n, 0, std_to_wx (i->id ()));
111                 if (i->file(FontFiles::NORMAL)) {
112                         _fonts->SetItem (n, 1, i->file(FontFiles::NORMAL).get().leaf().string ());
113                 }
114                 ++n;
115         }
116
117         setup_sensitivity ();
118 }
119
120 void
121 FontsDialog::selection_changed ()
122 {
123         setup_sensitivity ();
124 }
125
126 void
127 FontsDialog::setup_sensitivity ()
128 {
129         int const item = _fonts->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
130         _edit->Enable (item != -1);
131 }
132
133 void
134 FontsDialog::edit_clicked ()
135 {
136         shared_ptr<SubtitleContent> content = _content.lock ();
137         if (!content) {
138                 return;
139         }
140
141         int const item = _fonts->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
142         string const id = wx_to_std (_fonts->GetItemText (item, 0));
143         shared_ptr<Font> font;
144         BOOST_FOREACH (shared_ptr<Font> i, content->fonts()) {
145                 if (i->id() == id) {
146                         font = i;
147                 }
148         }
149
150         if (!font) {
151                 return;
152         }
153
154         FontFilesDialog* d = new FontFilesDialog (this, font->files ());
155         if (d->ShowModal () == wxID_OK) {
156                 font->set_files (d->get ());
157         }
158         d->Destroy ();
159
160         setup ();
161 }