Update sensitivity of Set File button correctly.
[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 "lib/font.h"
21 #include "lib/subtitle_content.h"
22 #include "fonts_dialog.h"
23 #include "wx_util.h"
24 #include <wx/wx.h>
25
26 using std::list;
27 using std::string;
28 using boost::shared_ptr;
29
30 FontsDialog::FontsDialog (wxWindow* parent, shared_ptr<SubtitleContent> content)
31         : wxDialog (parent, wxID_ANY, _("Fonts"))
32         , _content (content)
33 {
34         _fonts = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxSize (400, 200), wxLC_REPORT | wxLC_SINGLE_SEL);
35
36         {
37                 wxListItem ip;
38                 ip.SetId (0);
39                 ip.SetText (_("ID"));
40                 ip.SetWidth (100);
41                 _fonts->InsertColumn (0, ip);
42         }
43         
44         {
45                 wxListItem ip;
46                 ip.SetId (1);
47                 ip.SetText (_("Font file"));
48                 ip.SetWidth (300);
49                 _fonts->InsertColumn (1, ip);
50         }
51
52         wxBoxSizer* sizer = new wxBoxSizer (wxHORIZONTAL);
53         sizer->Add (_fonts, 1, wxEXPAND);
54
55         {
56                 wxSizer* s = new wxBoxSizer (wxVERTICAL);
57                 _set_file = new wxButton (this, wxID_ANY, _("Set file..."));
58                 s->Add (_set_file, 0, wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
59                 sizer->Add (s, 0, wxLEFT, DCPOMATIC_SIZER_X_GAP);
60         }
61
62         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
63         overall_sizer->Add (sizer, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_X_GAP);
64
65         wxSizer* buttons = CreateSeparatedButtonSizer (wxOK);
66         if (buttons) {
67                 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
68         }
69
70         SetSizerAndFit (overall_sizer);
71
72         _set_file->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&FontsDialog::set_file_clicked, this));
73         _fonts->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, boost::bind (&FontsDialog::selection_changed, this));
74         _fonts->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, boost::bind (&FontsDialog::selection_changed, this));
75
76         setup ();
77         update_sensitivity ();
78 }
79
80 void
81 FontsDialog::setup ()
82 {
83         shared_ptr<SubtitleContent> content = _content.lock ();
84         if (!content) {
85                 return;
86         }
87         
88         _fonts->DeleteAllItems ();
89         list<shared_ptr<Font> > fonts = content->fonts ();
90         size_t n = 0;
91         for (list<shared_ptr<Font> >::const_iterator i = fonts.begin(); i != fonts.end(); ++i) {
92                 wxListItem item;
93                 item.SetId (n);
94                 _fonts->InsertItem (item);
95                 _fonts->SetItem (n, 0, std_to_wx ((*i)->id));
96                 if ((*i)->file) {
97                         _fonts->SetItem (n, 1, (*i)->file.get().leaf().string ());
98                 }
99                 ++n;
100         }
101 }
102
103 void
104 FontsDialog::set_file_clicked ()
105 {
106         shared_ptr<SubtitleContent> content = _content.lock ();
107         if (!content) {
108                 return;
109         }
110
111         int item = _fonts->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
112         if (item == -1) {
113                 return;
114         }
115         
116         /* The wxFD_CHANGE_DIR here prevents a `could not set working directory' error 123 on Windows when using
117            non-Latin filenames or paths.
118         */
119         wxFileDialog* d = new wxFileDialog (this, _("Choose a font file"), wxT (""), wxT (""), wxT ("*.ttf"), wxFD_CHANGE_DIR);
120         int const r = d->ShowModal ();
121
122         if (r != wxID_OK) {
123                 d->Destroy ();
124                 return;
125         }
126
127         string id = wx_to_std (_fonts->GetItemText (item, 0));
128
129         list<shared_ptr<Font> > fonts = content->fonts ();
130         for (list<shared_ptr<Font> >::iterator i = fonts.begin(); i != fonts.end(); ++i) {
131                 if ((*i)->id == id) {
132                         (*i)->file = wx_to_std (d->GetPath ());
133                 }
134         }
135
136         d->Destroy ();
137
138         setup ();
139 }
140
141 void
142 FontsDialog::selection_changed ()
143 {
144         update_sensitivity ();
145 }
146
147 void
148 FontsDialog::update_sensitivity ()
149 {
150         int const item = _fonts->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
151         _set_file->Enable (item != -1);
152 }