#include "precompiled.h" /* * wxJavaScript - validator.cpp * * Copyright (c) 2002-2007 Franky Braem and the wxJavaScript project * * Project Info: http://www.wxjavascript.net or http://wxjs.sourceforge.net * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, * USA. * * $Id: validator.cpp 784 2007-06-25 18:34:22Z fbraem $ */ // validator.cpp #ifndef WX_PRECOMP #include #endif #include "../../common/main.h" #include "../../common/jsutil.h" #include "../control/window.h" #include "validate.h" using namespace wxjs; using namespace wxjs::gui; Validator::Validator() : wxValidator() { } Validator::Validator(const Validator ©) : wxValidator() { JavaScriptClientData *data = dynamic_cast(copy.GetClientObject()); if ( data != NULL ) { SetClientObject(new JavaScriptClientData(*data)); data->Protect(false); } } bool Validator::TransferToWindow() { JavaScriptClientData *data = dynamic_cast(GetClientObject()); if ( data == NULL ) return true; JSContext *cx = data->GetContext(); JSObject *obj = data->GetObject(); jsval fval; if ( GetFunctionProperty(cx, obj, "transferToWindow", &fval) == JS_TRUE ) { jsval rval; if ( JS_CallFunctionValue(cx, obj, fval, 0, NULL, &rval) == JS_FALSE ) { if ( JS_IsExceptionPending(cx) ) { JS_ReportPendingException(cx); } return false; } else { return JSVAL_IS_BOOLEAN(rval) ? JSVAL_TO_BOOLEAN(rval) == JS_TRUE : false; } } return true; } bool Validator::TransferFromWindow() { JavaScriptClientData *data = dynamic_cast(GetClientObject()); if ( data == NULL ) return true; JSContext *cx = data->GetContext(); JSObject *obj = data->GetObject(); jsval fval; if ( GetFunctionProperty(cx, obj, "transferFromWindow", &fval) == JS_TRUE ) { jsval rval; if ( JS_CallFunctionValue(cx, obj, fval, 0, NULL, &rval) == JS_FALSE ) { if ( JS_IsExceptionPending(cx) ) { JS_ReportPendingException(cx); } return false; } else { return JSVAL_IS_BOOLEAN(rval) ? JSVAL_TO_BOOLEAN(rval) == JS_TRUE : false; } } return true; } bool Validator::Validate(wxWindow *parent) { JavaScriptClientData *data = dynamic_cast(GetClientObject()); if ( data == NULL ) return true; JSContext *cx = data->GetContext(); JSObject *obj = data->GetObject(); JavaScriptClientData *parentData = NULL; if ( parent != NULL ) { parentData = dynamic_cast(parent->GetClientObject()); } jsval fval; if ( GetFunctionProperty(cx, obj, "validate", &fval) == JS_TRUE ) { jsval argv[] = { parentData == NULL ? JSVAL_VOID : OBJECT_TO_JSVAL(parentData->GetObject()) }; jsval rval; if ( JS_CallFunctionValue(cx, obj, fval, 1, argv, &rval) == JS_FALSE ) { if ( JS_IsExceptionPending(cx) ) { JS_ReportPendingException(cx); } return false; } else { return JSVAL_IS_BOOLEAN(rval) ? JSVAL_TO_BOOLEAN(rval) == JS_TRUE : false; } } return false; } /*** * misc/validator * gui * * Use wxValidator to create your own validators. * */ WXJS_INIT_CLASS(Validator, "wxValidator", 0) /*** * * * * Assign a function to this property that transfers the content of the window * to a variable. You must return true on success, false on failure. * * * Assign a function to this property that transfers the variable to the window. * You must return true on success, false on failure. * * * Assign a function to this property that checks the content of the associated window. The function * can have one argument: the parent of the associated window. This function should return false * when the content is invalid, true when it is valid. * * * The window associated with this validator. * * */ WXJS_BEGIN_PROPERTY_MAP(Validator) WXJS_PROPERTY(P_WINDOW, "window") WXJS_PROPERTY(P_BELL_ON_ERROR, "bellOnError") WXJS_END_PROPERTY_MAP() bool Validator::GetProperty(wxValidator *p, JSContext *cx, JSObject *obj, int id, jsval *vp) { switch (id) { case P_BELL_ON_ERROR: *vp = ToJS(cx, p->IsSilent()); break; case P_WINDOW: { wxWindow *win = p->GetWindow(); if ( win != NULL ) { JavaScriptClientData *data = dynamic_cast(win->GetClientObject()); *vp = data == NULL ? JSVAL_VOID : OBJECT_TO_JSVAL(data->GetObject()); } break; } } return true; } bool Validator::SetProperty(wxValidator *p, JSContext *cx, JSObject *obj, int id, jsval *vp) { switch (id) { case P_BELL_ON_ERROR: { bool bell; if ( FromJS(cx, *vp, bell) ) p->SetBellOnError(bell); break; } case P_WINDOW: { wxWindow *win = Window::GetPrivate(cx, *vp); if ( win != NULL ) p->SetWindow(win); } break; } return true; } /** * * * * Constructs a new wxValidator object * * */ wxValidator *Validator::Construct(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, bool constructing) { Validator *v = new Validator(); v->SetClientObject(new JavaScriptClientData(cx, obj, false, true)); return v; }