mirror of https://github.com/odrling/Aegisub
Use validators for reading values from all lua controls
Fixes some issues with control values not being read back correctly. Originally committed to SVN as r6533.
This commit is contained in:
parent
95fe56d41b
commit
110c2358a6
|
@ -192,14 +192,12 @@ namespace Automation4 {
|
|||
|
||||
ExportFilter::ExportFilter(wxString const& name, wxString const& description, int priority)
|
||||
: AssExportFilter(name, description, priority)
|
||||
, config_dialog(0)
|
||||
{
|
||||
AssExportFilterChain::Register(this);
|
||||
}
|
||||
|
||||
ExportFilter::~ExportFilter()
|
||||
{
|
||||
delete config_dialog;
|
||||
AssExportFilterChain::Unregister(this);
|
||||
}
|
||||
|
||||
|
@ -209,12 +207,9 @@ namespace Automation4 {
|
|||
}
|
||||
|
||||
wxWindow* ExportFilter::GetConfigDialogWindow(wxWindow *parent, agi::Context *c) {
|
||||
if (config_dialog) {
|
||||
delete config_dialog;
|
||||
config_dialog = 0;
|
||||
}
|
||||
config_dialog.reset(GenerateConfigDialog(parent, c));
|
||||
|
||||
if ((config_dialog = GenerateConfigDialog(parent, c))) {
|
||||
if (config_dialog) {
|
||||
wxString val = c->ass->GetScriptInfo(GetScriptSettingsIdentifier());
|
||||
if (!val.empty())
|
||||
config_dialog->Unserialise(val);
|
||||
|
@ -226,8 +221,6 @@ namespace Automation4 {
|
|||
|
||||
void ExportFilter::LoadSettings(bool is_default, agi::Context *c) {
|
||||
if (config_dialog) {
|
||||
config_dialog->ReadBack();
|
||||
|
||||
wxString val = config_dialog->Serialise();
|
||||
if (!val.empty())
|
||||
c->ass->SetScriptInfo(GetScriptSettingsIdentifier(), val);
|
||||
|
@ -280,14 +273,15 @@ namespace Automation4 {
|
|||
{
|
||||
std::pair<ScriptDialog*, wxSemaphore*> payload = evt.GetPayload<std::pair<ScriptDialog*, wxSemaphore*> >();
|
||||
|
||||
wxDialog w(impl.get(), -1, impl->GetTitle()); // container dialog box
|
||||
wxDialog w; // container dialog box
|
||||
w.SetExtraStyle(wxWS_EX_VALIDATE_RECURSIVELY);
|
||||
w.Create(impl.get(), -1, impl->GetTitle());
|
||||
wxBoxSizer *s = new wxBoxSizer(wxHORIZONTAL); // sizer for putting contents in
|
||||
wxWindow *ww = payload.first->CreateWindow(&w); // generate actual dialog contents
|
||||
s->Add(ww, 0, wxALL, 5); // add contents to dialog
|
||||
w.SetSizerAndFit(s);
|
||||
w.CenterOnParent();
|
||||
w.ShowModal();
|
||||
payload.first->ReadBack();
|
||||
|
||||
// Tell the calling thread it can wake up now
|
||||
payload.second->Post();
|
||||
|
|
|
@ -84,7 +84,7 @@ namespace Automation4 {
|
|||
class ScriptDialog;
|
||||
|
||||
class ExportFilter : public AssExportFilter {
|
||||
ScriptDialog *config_dialog;
|
||||
agi::scoped_ptr<ScriptDialog> config_dialog;
|
||||
|
||||
/// subclasses should implement this, producing a new ScriptDialog
|
||||
virtual ScriptDialog* GenerateConfigDialog(wxWindow *parent, agi::Context *c) = 0;
|
||||
|
@ -112,10 +112,6 @@ namespace Automation4 {
|
|||
/// Create a window with the given parent
|
||||
virtual wxWindow *CreateWindow(wxWindow *parent) = 0;
|
||||
|
||||
/// Read the values of the controls in this dialog and cache them in
|
||||
/// some way so that they can be accessed from a background thread
|
||||
virtual void ReadBack() = 0;
|
||||
|
||||
/// Serialize the values of the controls in this dialog to a string
|
||||
/// suitable for storage in the subtitle script
|
||||
virtual wxString Serialise() { return ""; }
|
||||
|
|
|
@ -172,9 +172,6 @@ namespace Automation4 {
|
|||
/// Get the default flags to use when inserting this control into a sizer
|
||||
virtual int GetSizerFlags() const { return wxEXPAND; }
|
||||
|
||||
/// Read the current value of the control. Must not touch Lua as this is called on the GUI thread.
|
||||
virtual void ControlReadBack() = 0;
|
||||
|
||||
/// Push the current value of the control onto the lua stack. Must not
|
||||
/// touch the GUI as this may be called on a background thread.
|
||||
virtual void LuaReadBack(lua_State *L) = 0;
|
||||
|
@ -208,6 +205,8 @@ namespace Automation4 {
|
|||
/// Id of the button pushed (once a button has been pushed)
|
||||
int button_pushed;
|
||||
|
||||
wxWindow *window;
|
||||
|
||||
void OnButtonPush(wxCommandEvent &evt);
|
||||
|
||||
public:
|
||||
|
@ -222,7 +221,6 @@ namespace Automation4 {
|
|||
wxWindow* CreateWindow(wxWindow *parent);
|
||||
wxString Serialise();
|
||||
void Unserialise(const wxString &serialised);
|
||||
void ReadBack();
|
||||
};
|
||||
|
||||
class LuaFeature {
|
||||
|
|
|
@ -58,6 +58,7 @@
|
|||
#endif
|
||||
|
||||
#include <cfloat>
|
||||
#include <wx/valgen.h>
|
||||
|
||||
#include <libaegisub/log.h>
|
||||
|
||||
|
@ -159,8 +160,6 @@ namespace Automation4 {
|
|||
|
||||
int GetSizerFlags() const { return wxALIGN_CENTRE_VERTICAL | wxALIGN_LEFT; }
|
||||
|
||||
void ControlReadBack() { }
|
||||
|
||||
void LuaReadBack(lua_State *L)
|
||||
{
|
||||
// Label doesn't produce output, so let it be nil
|
||||
|
@ -200,15 +199,11 @@ namespace Automation4 {
|
|||
wxControl *Create(wxWindow *parent)
|
||||
{
|
||||
cw = new wxTextCtrl(parent, -1, text);
|
||||
cw->SetValidator(wxGenericValidator(&text));
|
||||
cw->SetToolTip(hint);
|
||||
return cw;
|
||||
}
|
||||
|
||||
void ControlReadBack()
|
||||
{
|
||||
text = cw->GetValue();
|
||||
}
|
||||
|
||||
void LuaReadBack(lua_State *L)
|
||||
{
|
||||
lua_pushstring(L, text.utf8_str());
|
||||
|
@ -218,9 +213,21 @@ namespace Automation4 {
|
|||
/// A color-picker button
|
||||
class Color : public LuaDialogControl {
|
||||
wxString text;
|
||||
ColourButton *cw;
|
||||
public:
|
||||
|
||||
struct ColorValidator : public wxValidator {
|
||||
wxString *text;
|
||||
ColorValidator(wxString *text) : text(text) { }
|
||||
wxValidator *Clone() const { return new ColorValidator(text); }
|
||||
bool Validate(wxWindow*) { return true; }
|
||||
bool TransferToWindow() { return true; }
|
||||
|
||||
bool TransferFromWindow()
|
||||
{
|
||||
*text = static_cast<ColourButton*>(GetWindow())->GetColour().GetAsString(wxC2S_HTML_SYNTAX);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
public:
|
||||
Color(lua_State *L)
|
||||
: LuaDialogControl(L)
|
||||
, text(get_field(L, "value"))
|
||||
|
@ -243,16 +250,12 @@ namespace Automation4 {
|
|||
{
|
||||
AssColor colour;
|
||||
colour.Parse(text);
|
||||
cw = new ColourButton(parent, -1, wxSize(50*width,10*height), colour.GetWXColor());
|
||||
wxControl *cw = new ColourButton(parent, -1, wxSize(50*width,10*height), colour.GetWXColor());
|
||||
cw->SetValidator(ColorValidator(&text));
|
||||
cw->SetToolTip(hint);
|
||||
return cw;
|
||||
}
|
||||
|
||||
void ControlReadBack()
|
||||
{
|
||||
text = cw->GetColour().GetAsString(wxC2S_HTML_SYNTAX);
|
||||
}
|
||||
|
||||
void LuaReadBack(lua_State *L)
|
||||
{
|
||||
lua_pushstring(L, text.utf8_str());
|
||||
|
@ -270,7 +273,7 @@ namespace Automation4 {
|
|||
// Same serialisation interface as single-line edit
|
||||
wxControl *Create(wxWindow *parent)
|
||||
{
|
||||
cw = new wxTextCtrl(parent, -1, text, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE);
|
||||
cw = new wxTextCtrl(parent, -1, text, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE, wxGenericValidator(&text));
|
||||
cw->SetMinSize(wxSize(0, 30));
|
||||
cw->SetToolTip(hint);
|
||||
return cw;
|
||||
|
@ -314,15 +317,11 @@ namespace Automation4 {
|
|||
wxControl *Create(wxWindow *parent)
|
||||
{
|
||||
cw = new wxSpinCtrl(parent, -1, wxString::Format("%d", value), wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, min, max, value);
|
||||
cw->SetValidator(wxGenericValidator(&value));
|
||||
cw->SetToolTip(hint);
|
||||
return cw;
|
||||
}
|
||||
|
||||
void ControlReadBack()
|
||||
{
|
||||
value = cw->GetValue();
|
||||
}
|
||||
|
||||
void LuaReadBack(lua_State *L)
|
||||
{
|
||||
lua_pushinteger(L, value);
|
||||
|
@ -338,6 +337,20 @@ namespace Automation4 {
|
|||
double step;
|
||||
wxSpinCtrlDouble *scd;
|
||||
|
||||
struct DoubleValidator : public wxValidator {
|
||||
double *value;
|
||||
DoubleValidator(double *value) : value(value) { }
|
||||
wxValidator *Clone() const { return new DoubleValidator(value); }
|
||||
bool Validate(wxWindow*) { return true; }
|
||||
bool TransferToWindow() { return true; }
|
||||
|
||||
bool TransferFromWindow()
|
||||
{
|
||||
*value = static_cast<wxSpinCtrlDouble*>(GetWindow())->GetValue();
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
public:
|
||||
FloatEdit(lua_State *L)
|
||||
: Edit(L)
|
||||
|
@ -373,6 +386,7 @@ namespace Automation4 {
|
|||
scd = new wxSpinCtrlDouble(parent, -1,
|
||||
wxString::Format("%g", value), wxDefaultPosition,
|
||||
wxDefaultSize, wxSP_ARROW_KEYS, min, max, value, step);
|
||||
scd->SetValidator(DoubleValidator(&value));
|
||||
scd->SetToolTip(hint);
|
||||
return scd;
|
||||
}
|
||||
|
@ -385,16 +399,6 @@ namespace Automation4 {
|
|||
return cw;
|
||||
}
|
||||
|
||||
void ControlReadBack()
|
||||
{
|
||||
if (scd)
|
||||
value = scd->GetValue();
|
||||
else
|
||||
cw->TransferDataFromWindow();
|
||||
|
||||
text = SerialiseValue();
|
||||
}
|
||||
|
||||
void LuaReadBack(lua_State *L)
|
||||
{
|
||||
lua_pushnumber(L, value);
|
||||
|
@ -406,8 +410,8 @@ namespace Automation4 {
|
|||
wxArrayString items;
|
||||
wxString value;
|
||||
wxComboBox *cw;
|
||||
public:
|
||||
|
||||
public:
|
||||
Dropdown(lua_State *L)
|
||||
: LuaDialogControl(L)
|
||||
, value(get_field(L, "value"))
|
||||
|
@ -430,16 +434,11 @@ namespace Automation4 {
|
|||
|
||||
wxControl *Create(wxWindow *parent)
|
||||
{
|
||||
cw = new wxComboBox(parent, -1, value, wxDefaultPosition, wxDefaultSize, items, wxCB_READONLY);
|
||||
cw = new wxComboBox(parent, -1, value, wxDefaultPosition, wxDefaultSize, items, wxCB_READONLY, wxGenericValidator(&value));
|
||||
cw->SetToolTip(hint);
|
||||
return cw;
|
||||
}
|
||||
|
||||
void ControlReadBack()
|
||||
{
|
||||
value = cw->GetValue();
|
||||
}
|
||||
|
||||
void LuaReadBack(lua_State *L)
|
||||
{
|
||||
lua_pushstring(L, value.utf8_str());
|
||||
|
@ -475,16 +474,12 @@ namespace Automation4 {
|
|||
wxControl *Create(wxWindow *parent)
|
||||
{
|
||||
cw = new wxCheckBox(parent, -1, label);
|
||||
cw->SetValidator(wxGenericValidator(&value));
|
||||
cw->SetToolTip(hint);
|
||||
cw->SetValue(value);
|
||||
return cw;
|
||||
}
|
||||
|
||||
void ControlReadBack()
|
||||
{
|
||||
value = cw->GetValue();
|
||||
}
|
||||
|
||||
void LuaReadBack(lua_State *L)
|
||||
{
|
||||
lua_pushboolean(L, value);
|
||||
|
@ -560,12 +555,12 @@ namespace Automation4 {
|
|||
|
||||
wxWindow* LuaDialog::CreateWindow(wxWindow *parent)
|
||||
{
|
||||
wxWindow *w = new wxPanel(parent);
|
||||
window = new wxPanel(parent);
|
||||
wxGridBagSizer *s = new wxGridBagSizer(4, 4);
|
||||
|
||||
for (size_t i = 0; i < controls.size(); ++i) {
|
||||
LuaDialogControl *c = controls[i];
|
||||
s->Add(c->Create(w), wxGBPosition(c->y, c->x), wxGBSpan(c->height, c->width), c->GetSizerFlags());
|
||||
s->Add(c->Create(window), wxGBPosition(c->y, c->x), wxGBSpan(c->height, c->width), c->GetSizerFlags());
|
||||
}
|
||||
|
||||
if (use_buttons) {
|
||||
|
@ -575,26 +570,26 @@ namespace Automation4 {
|
|||
for (size_t i = 0; i < buttons.size(); ++i) {
|
||||
LOG_D("automation/lua/dialog") << "button '" << STD_STR(buttons[i]) << "' gets id " << 1001+(wxWindowID)i;
|
||||
|
||||
bs->Add(new wxButton(w, 1001+(wxWindowID)i, buttons[i]));
|
||||
bs->Add(new wxButton(window, 1001+(wxWindowID)i, buttons[i]));
|
||||
}
|
||||
} else {
|
||||
LOG_D("automation/lua/dialog") << "creating default buttons";
|
||||
bs->Add(new wxButton(w, wxID_OK));
|
||||
bs->Add(new wxButton(w, wxID_CANCEL));
|
||||
bs->Add(new wxButton(window, wxID_OK));
|
||||
bs->Add(new wxButton(window, wxID_CANCEL));
|
||||
}
|
||||
bs->Realize();
|
||||
|
||||
w->Bind(wxEVT_COMMAND_BUTTON_CLICKED, &LuaDialog::OnButtonPush, this);
|
||||
window->Bind(wxEVT_COMMAND_BUTTON_CLICKED, &LuaDialog::OnButtonPush, this);
|
||||
|
||||
wxBoxSizer *ms = new wxBoxSizer(wxVERTICAL);
|
||||
ms->Add(s, 0, wxBOTTOM, 5);
|
||||
ms->Add(bs);
|
||||
w->SetSizerAndFit(ms);
|
||||
window->SetSizerAndFit(ms);
|
||||
} else {
|
||||
w->SetSizerAndFit(s);
|
||||
window->SetSizerAndFit(s);
|
||||
}
|
||||
|
||||
return w;
|
||||
return window;
|
||||
}
|
||||
|
||||
int LuaDialog::LuaReadBack(lua_State *L)
|
||||
|
@ -670,13 +665,6 @@ namespace Automation4 {
|
|||
}
|
||||
}
|
||||
|
||||
void LuaDialog::ReadBack()
|
||||
{
|
||||
for (size_t i = 0; i < controls.size(); ++i) {
|
||||
controls[i]->ControlReadBack();
|
||||
}
|
||||
}
|
||||
|
||||
void LuaDialog::OnButtonPush(wxCommandEvent &evt)
|
||||
{
|
||||
// Let button_pushed == 0 mean "cancelled", such that pushing Cancel or closing the dialog
|
||||
|
|
|
@ -53,10 +53,12 @@
|
|||
#include "help_button.h"
|
||||
|
||||
DialogExport::DialogExport(agi::Context *c)
|
||||
: wxDialog(c->parent, -1, _("Export"), wxDefaultPosition, wxSize(200, 100), wxCAPTION | wxCLOSE_BOX, "Export")
|
||||
: wxDialog(c->parent, -1, _("Export"), wxDefaultPosition, wxSize(200, 100), wxCAPTION | wxCLOSE_BOX)
|
||||
, c(c)
|
||||
, exporter(new AssExporter(c))
|
||||
{
|
||||
SetExtraStyle(wxWS_EX_VALIDATE_RECURSIVELY);
|
||||
|
||||
wxArrayString filters = exporter->GetAllFilterNames();
|
||||
filter_list = new wxCheckListBox(this, -1, wxDefaultPosition, wxSize(200, 100), filters);
|
||||
filter_list->Bind(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, &DialogExport::OnCheck, this);
|
||||
|
@ -109,8 +111,8 @@ DialogExport::DialogExport(agi::Context *c)
|
|||
|
||||
wxStdDialogButtonSizer *btn_sizer = CreateStdDialogButtonSizer(wxOK | wxCANCEL | wxHELP);
|
||||
btn_sizer->GetAffirmativeButton()->SetLabelText(_("Export..."));
|
||||
btn_sizer->GetAffirmativeButton()->Bind(wxEVT_COMMAND_BUTTON_CLICKED, &DialogExport::OnProcess, this);
|
||||
btn_sizer->GetHelpButton()->Bind(wxEVT_COMMAND_BUTTON_CLICKED, std::tr1::bind(&HelpButton::OpenPage, "Export"));
|
||||
Bind(wxEVT_COMMAND_BUTTON_CLICKED, &DialogExport::OnProcess, this, wxID_OK);
|
||||
Bind(wxEVT_COMMAND_BUTTON_CLICKED, std::tr1::bind(&HelpButton::OpenPage, "Export"), wxID_HELP);
|
||||
|
||||
wxSizer *horz_sizer = new wxBoxSizer(wxHORIZONTAL);
|
||||
opt_sizer = new wxBoxSizer(wxVERTICAL);
|
||||
|
@ -137,6 +139,8 @@ DialogExport::~DialogExport() {
|
|||
}
|
||||
|
||||
void DialogExport::OnProcess(wxCommandEvent &) {
|
||||
if (!TransferDataFromWindow()) return;
|
||||
|
||||
wxString filename = wxFileSelector(_("Export subtitles file"), "", "", "", AssFile::GetWildcardList(2), wxFD_SAVE | wxFD_OVERWRITE_PROMPT, this);
|
||||
if (filename.empty()) return;
|
||||
|
||||
|
|
Loading…
Reference in New Issue