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