More automated renaming.
[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 "font_files_dialog.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 boost::shared_ptr;
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 (_("Normal file"));
56                 ip.SetWidth (150);
57                 _fonts->InsertColumn (1, ip);
58         }
59
60         {
61                 wxListItem ip;
62                 ip.SetId (2);
63                 ip.SetText (_("Italic file"));
64                 ip.SetWidth (150);
65                 _fonts->InsertColumn (2, ip);
66         }
67
68         {
69                 wxListItem ip;
70                 ip.SetId (3);
71                 ip.SetText (_("Bold file"));
72                 ip.SetWidth (150);
73                 _fonts->InsertColumn (3, ip);
74         }
75
76         wxBoxSizer* sizer = new wxBoxSizer (wxHORIZONTAL);
77         sizer->Add (_fonts, 1, wxEXPAND | wxLEFT | wxRIGHT, DCPOMATIC_SIZER_X_GAP);
78
79         _edit = new wxButton (this, wxID_ANY, _("Edit..."));
80         sizer->Add (_edit, 0, wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
81
82         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
83         overall_sizer->Add (sizer, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_X_GAP);
84
85         wxSizer* buttons = CreateSeparatedButtonSizer (wxOK);
86         if (buttons) {
87                 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
88         }
89
90         SetSizerAndFit (overall_sizer);
91
92         _edit->Bind (wxEVT_BUTTON, boost::bind (&FontsDialog::edit_clicked, this));
93         _fonts->Bind (wxEVT_LIST_ITEM_SELECTED, boost::bind (&FontsDialog::selection_changed, this));
94         _fonts->Bind (wxEVT_LIST_ITEM_DESELECTED, boost::bind (&FontsDialog::selection_changed, this));
95
96         setup ();
97 }
98
99 void
100 FontsDialog::setup ()
101 {
102         shared_ptr<Content> content = _content.lock ();
103         shared_ptr<TextContent> caption = _caption.lock ();
104         if (!content || !caption) {
105                 return;
106         }
107
108         _fonts->DeleteAllItems ();
109         size_t n = 0;
110         BOOST_FOREACH (shared_ptr<Font> i, caption->fonts ()) {
111                 wxListItem item;
112                 item.SetId (n);
113                 _fonts->InsertItem (item);
114                 _fonts->SetItem (n, 0, std_to_wx (i->id ()));
115                 for (int j = 0; j < FontFiles::VARIANTS; ++j) {
116                         if (i->file(static_cast<FontFiles::Variant>(j))) {
117                                 _fonts->SetItem (n, j + 1, i->file(static_cast<FontFiles::Variant>(j)).get().leaf().string ());
118                         }
119                 }
120                 ++n;
121         }
122
123         setup_sensitivity ();
124 }
125
126 void
127 FontsDialog::selection_changed ()
128 {
129         setup_sensitivity ();
130 }
131
132 void
133 FontsDialog::setup_sensitivity ()
134 {
135         int const item = _fonts->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
136         _edit->Enable (item != -1);
137 }
138
139 void
140 FontsDialog::edit_clicked ()
141 {
142         shared_ptr<Content> content = _content.lock ();
143         shared_ptr<TextContent> caption = _caption.lock ();
144         if (!content || !caption) {
145                 return;
146         }
147
148         int const item = _fonts->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
149         string const id = wx_to_std (_fonts->GetItemText (item, 0));
150         shared_ptr<Font> font;
151         BOOST_FOREACH (shared_ptr<Font> i, caption->fonts()) {
152                 if (i->id() == id) {
153                         font = i;
154                 }
155         }
156
157         if (!font) {
158                 return;
159         }
160
161         FontFilesDialog* d = new FontFilesDialog (this, font->files ());
162         if (d->ShowModal () == wxID_OK) {
163                 font->set_files (d->get ());
164         }
165         d->Destroy ();
166
167         setup ();
168 }