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