Extract ColorValidator from Automation's dialog stuff

This commit is contained in:
Thomas Goyne 2013-06-11 16:32:59 -07:00
parent 72d4577d7d
commit 77dd026555
2 changed files with 19 additions and 20 deletions

View File

@ -194,43 +194,29 @@ namespace Automation4 {
/// A color-picker button /// A color-picker button
class Color : public LuaDialogControl { class Color : public LuaDialogControl {
std::string text; agi::Color color;
bool alpha; bool alpha;
struct ColorValidator : public wxValidator {
std::string *text;
ColorValidator(std::string *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())->GetColor().GetHexFormatted();
return true;
}
};
public: public:
Color(lua_State *L, bool alpha) Color(lua_State *L, bool alpha)
: LuaDialogControl(L) : LuaDialogControl(L)
, text(get_field(L, "value")) , color(get_field(L, "value"))
, alpha(alpha) , alpha(alpha)
{ {
} }
bool CanSerialiseValue() const { return true; } bool CanSerialiseValue() const { return true; }
std::string SerialiseValue() const { return inline_string_encode(text); } std::string SerialiseValue() const { return inline_string_encode(color.GetHexFormatted()); }
void UnserialiseValue(const std::string &serialised) { text = inline_string_decode(serialised); } void UnserialiseValue(const std::string &serialised) { color = inline_string_decode(serialised); }
wxControl *Create(wxWindow *parent) { wxControl *Create(wxWindow *parent) {
agi::Color colour(text); wxControl *cw = new ColourButton(parent, wxSize(50*width,10*height), alpha, color, ColorValidator(&color));
wxControl *cw = new ColourButton(parent, wxSize(50*width,10*height), alpha, colour, ColorValidator(&text));
cw->SetToolTip(to_wx(hint)); cw->SetToolTip(to_wx(hint));
return cw; return cw;
} }
void LuaReadBack(lua_State *L) { void LuaReadBack(lua_State *L) {
lua_pushstring(L, text.c_str()); lua_pushstring(L, color.GetHexFormatted().c_str());
} }
}; };

View File

@ -42,3 +42,16 @@ public:
/// Get the currently selected color /// Get the currently selected color
agi::Color GetColor() { return colour; } agi::Color GetColor() { return colour; }
}; };
struct ColorValidator : public wxValidator {
agi::Color *color;
ColorValidator(agi::Color *color) : color(color) { }
wxValidator *Clone() const { return new ColorValidator(color); }
bool Validate(wxWindow*) { return true; }
bool TransferToWindow() { return true; }
bool TransferFromWindow() {
*color = static_cast<ColourButton*>(GetWindow())->GetColor();
return true;
}
};