3 #include <wx/control.h>
4 #include <wx/combobox.h>
5 #include <wx/radiobut.h>
7 #include "core/subtitles.hpp"
8 #include "core/instance.hpp"
9 #include "core/moviedata.hpp"
10 #include "core/dispatch.hpp"
11 #include "library/string.hpp"
13 #include "platform/wxwidgets/platform.hpp"
27 class wxeditor_subtitles
: public wxFrame
30 wxeditor_subtitles(wxWindow
* parent
, emulator_instance
& _inst
);
31 ~wxeditor_subtitles() throw();
32 bool ShouldPreventAppExit() const;
33 void on_change(wxCommandEvent
& e
);
34 void on_add(wxCommandEvent
& e
);
35 void on_edit(wxCommandEvent
& e
);
36 void on_delete(wxCommandEvent
& e
);
37 void on_close(wxCommandEvent
& e
);
38 void on_wclose(wxCloseEvent
& e
);
41 emulator_instance
& inst
;
49 std::map
<int, subdata
> subtexts
;
50 struct dispatch::target
<> subchange
;
56 class wxeditor_subtitles_subtitle
: public wxDialog
59 wxeditor_subtitles_subtitle(wxWindow
* parent
, subdata d
);
60 void on_change(wxCommandEvent
& e
);
61 void on_cancel(wxCommandEvent
& e
);
62 void on_ok(wxCommandEvent
& e
);
72 wxeditor_subtitles_subtitle::wxeditor_subtitles_subtitle(wxWindow
* parent
, subdata d
)
73 : wxDialog(parent
, wxID_ANY
, wxT("lsnes: Edit subtitle"), wxDefaultPosition
, wxSize(-1, -1))
77 wxFlexGridSizer
* top_s
= new wxFlexGridSizer(2, 1, 0, 0);
80 wxFlexGridSizer
* data_s
= new wxFlexGridSizer(3, 2, 0, 0);
81 data_s
->Add(new wxStaticText(this, wxID_ANY
, wxT("First frame:")));
82 data_s
->Add(first
= new wxTextCtrl(this, wxID_ANY
, wxT(""), wxDefaultPosition
, wxSize(200, -1)));
83 data_s
->Add(new wxStaticText(this, wxID_ANY
, wxT("Last frame:")));
84 data_s
->Add(last
= new wxTextCtrl(this, wxID_ANY
, wxT(""), wxDefaultPosition
, wxSize(200, -1)));
85 data_s
->Add(new wxStaticText(this, wxID_ANY
, wxT("Text:")));
86 data_s
->Add(text
= new wxTextCtrl(this, wxID_ANY
, wxT(""), wxDefaultPosition
, wxSize(400, -1)));
87 top_s
->Add(data_s
, 1, wxGROW
);
89 first
->Connect(wxEVT_COMMAND_TEXT_UPDATED
,
90 wxCommandEventHandler(wxeditor_subtitles_subtitle::on_change
), NULL
, this);
91 last
->Connect(wxEVT_COMMAND_TEXT_UPDATED
,
92 wxCommandEventHandler(wxeditor_subtitles_subtitle::on_change
), NULL
, this);
93 text
->Connect(wxEVT_COMMAND_TEXT_UPDATED
,
94 wxCommandEventHandler(wxeditor_subtitles_subtitle::on_change
), NULL
, this);
96 wxBoxSizer
* pbutton_s
= new wxBoxSizer(wxHORIZONTAL
);
97 pbutton_s
->AddStretchSpacer();
98 pbutton_s
->Add(ok
= new wxButton(this, wxID_ANY
, wxT("OK")), 0, wxGROW
);
99 pbutton_s
->Add(cancel
= new wxButton(this, wxID_ANY
, wxT("Cancel")), 0, wxGROW
);
100 ok
->Connect(wxEVT_COMMAND_BUTTON_CLICKED
, wxCommandEventHandler(wxeditor_subtitles_subtitle::on_ok
),
102 cancel
->Connect(wxEVT_COMMAND_BUTTON_CLICKED
,
103 wxCommandEventHandler(wxeditor_subtitles_subtitle::on_cancel
), NULL
, this);
104 top_s
->Add(pbutton_s
, 0, wxGROW
);
106 pbutton_s
->SetSizeHints(this);
107 top_s
->SetSizeHints(this);
109 first
->SetValue(towxstring((stringfmt() << d
.first
).str()));
110 last
->SetValue(towxstring((stringfmt() << d
.last
).str()));
111 text
->SetValue(towxstring(d
.text
));
115 void wxeditor_subtitles_subtitle::on_change(wxCommandEvent
& e
)
119 std::string _first
= tostdstring(first
->GetValue());
120 std::string _last
= tostdstring(last
->GetValue());
121 std::string _text
= tostdstring(text
->GetValue());
122 valid
= valid
&& regex_match("[0-9]{1,19}", _first
);
123 valid
= valid
&& regex_match("[0-9]{1,19}", _last
);
124 valid
= valid
&& (_text
!= "");
128 void wxeditor_subtitles_subtitle::on_cancel(wxCommandEvent
& e
)
131 EndModal(wxID_CANCEL
);
134 void wxeditor_subtitles_subtitle::on_ok(wxCommandEvent
& e
)
140 subdata
wxeditor_subtitles_subtitle::get_result()
144 d
.first
= parse_value
<uint64_t>(tostdstring(first
->GetValue()));
145 d
.last
= parse_value
<uint64_t>(tostdstring(last
->GetValue()));
146 d
.text
= tostdstring(text
->GetValue());
150 bool edit_subtext(wxWindow
* w
, struct subdata
& d
)
154 wxeditor_subtitles_subtitle
* editor
= NULL
;
156 editor
= new wxeditor_subtitles_subtitle(w
, d
);
157 int ret
= editor
->ShowModal();
159 d
= editor
->get_result();
172 wxeditor_subtitles::wxeditor_subtitles(wxWindow
* parent
, emulator_instance
& _inst
)
173 : wxFrame(NULL
, wxID_ANY
, wxT("lsnes: Edit subtitles"), wxDefaultPosition
, wxSize(-1, -1)), inst(_inst
)
178 wxFlexGridSizer
* top_s
= new wxFlexGridSizer(2, 1, 0, 0);
181 //TODO: Apppropriate controls.
182 top_s
->Add(subs
= new wxListBox(this, wxID_ANY
, wxDefaultPosition
, wxSize(300, 400), 0, NULL
,
183 wxLB_SINGLE
), 1, wxGROW
);
184 subs
->Connect(wxEVT_COMMAND_LISTBOX_SELECTED
,
185 wxCommandEventHandler(wxeditor_subtitles::on_change
), NULL
, this);
187 wxBoxSizer
* pbutton_s
= new wxBoxSizer(wxHORIZONTAL
);
188 pbutton_s
->AddStretchSpacer();
189 pbutton_s
->Add(add
= new wxButton(this, wxID_ANY
, wxT("Add")), 0, wxGROW
);
190 pbutton_s
->Add(edit
= new wxButton(this, wxID_ANY
, wxT("Edit")), 0, wxGROW
);
191 pbutton_s
->Add(_delete
= new wxButton(this, wxID_ANY
, wxT("Delete")), 0, wxGROW
);
192 pbutton_s
->Add(close
= new wxButton(this, wxID_ANY
, wxT("Close")), 0, wxGROW
);
193 add
->Connect(wxEVT_COMMAND_BUTTON_CLICKED
, wxCommandEventHandler(wxeditor_subtitles::on_add
), NULL
, this);
194 edit
->Connect(wxEVT_COMMAND_BUTTON_CLICKED
, wxCommandEventHandler(wxeditor_subtitles::on_edit
), NULL
, this);
195 _delete
->Connect(wxEVT_COMMAND_BUTTON_CLICKED
, wxCommandEventHandler(wxeditor_subtitles::on_delete
), NULL
,
197 close
->Connect(wxEVT_COMMAND_BUTTON_CLICKED
, wxCommandEventHandler(wxeditor_subtitles::on_close
), NULL
, this);
198 top_s
->Add(pbutton_s
, 0, wxGROW
);
200 pbutton_s
->SetSizeHints(this);
201 top_s
->SetSizeHints(this);
203 subchange
.set(inst
.dispatch
->subtitle_change
, [this]() { runuifun([this]() -> void {
204 this->refresh(); }); });
208 wxeditor_subtitles::~wxeditor_subtitles() throw()
212 bool wxeditor_subtitles::ShouldPreventAppExit() const
217 void wxeditor_subtitles::on_close(wxCommandEvent
& e
)
224 void wxeditor_subtitles::on_wclose(wxCloseEvent
& e
)
229 void wxeditor_subtitles::refresh()
234 std::map
<std::pair
<uint64_t, uint64_t>, std::string
> _subtitles
;
235 inst
.iqueue
->run([&_subtitles
]() -> void {
236 auto keys
= CORE().subtitles
->get_all();
238 _subtitles
[i
] = CORE().subtitles
->get(i
.first
, i
.second
);
240 int sel
= subs
->GetSelection();
241 bool found
= (subtexts
.count(sel
) != 0);
242 subdata matching
= subtexts
[sel
];
246 for(auto i
: _subtitles
) {
248 newdata
.first
= i
.first
.first
;
249 newdata
.last
= i
.first
.second
;
250 newdata
.text
= i
.second
;
251 subtexts
[num
++] = newdata
;
252 std::string s
= (stringfmt() << i
.first
.first
<< "-" << i
.first
.second
<< ": " << i
.second
).str();
253 subs
->Append(towxstring(s
));
255 for(size_t i
= 0; i
< subs
->GetCount(); i
++)
256 if(subtexts
[i
].first
== matching
.first
&& subtexts
[i
].last
== matching
.last
)
257 subs
->SetSelection(i
);
258 if(subs
->GetSelection() == wxNOT_FOUND
&& sel
< (ssize_t
)subs
->GetCount())
259 subs
->SetSelection(sel
);
260 sel
= subs
->GetSelection();
261 found
= (subtexts
.count(sel
) != 0);
263 _delete
->Enable(found
);
266 void wxeditor_subtitles::on_change(wxCommandEvent
& e
)
271 int sel
= subs
->GetSelection();
272 bool found
= (subtexts
.count(sel
) != 0);
274 _delete
->Enable(found
);
277 void wxeditor_subtitles::on_add(wxCommandEvent
& e
)
286 if(edit_subtext(this, t
))
287 inst
.subtitles
->set(t
.first
, t
.last
, t
.text
);
290 void wxeditor_subtitles::on_edit(wxCommandEvent
& e
)
295 int sel
= subs
->GetSelection();
296 if(!subtexts
.count(sel
))
298 auto t
= subtexts
[sel
];
300 if(edit_subtext(this, t
)) {
301 inst
.subtitles
->set(old
.first
, old
.last
, "");
302 inst
.subtitles
->set(t
.first
, t
.last
, t
.text
);
306 void wxeditor_subtitles::on_delete(wxCommandEvent
& e
)
311 int sel
= subs
->GetSelection();
312 if(!subtexts
.count(sel
))
314 auto t
= subtexts
[sel
];
315 inst
.subtitles
->set(t
.first
, t
.last
, "");
318 void wxeditor_subtitles_display(wxWindow
* parent
, emulator_instance
& inst
)
323 show_message_ok(parent
, "No movie", "Can't edit subtitles of nonexistent movie", wxICON_EXCLAMATION
);
327 editor
= new wxeditor_subtitles(parent
, inst
);