/* Copyright 1996-2002 Simon Whiteside, All Rights Reserved 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 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Demo_Controller.cpp This file implements the class which controls the behaviour of the view in the demo. $Id: Demo_Controller.cpp.txt,v 1.1 2007/09/18 11:41:38 sdw Exp $ */ #include "Demo_Controller.h" #include "skRValueArray.h" #include skLITERAL(View); skLITERAL(Title); skLITERAL(X); skLITERAL(Y); skLITERAL(Height); skLITERAL(Controls); skLITERAL(Text); skLITERAL(Type); skLITERAL(Width); skLITERAL(Id); skLITERAL(Method); skLITERAL(reload); skLITERAL(setFocus); skLITERAL(run); skLITERAL(getText); skLITERAL(user); skLITERAL(close); //----------------------------------------------------------------- Controller::Controller(const skString& fileName,skInterpreter * interp) //----------------------------------------------------------------- // this class loads the demo script file, whose methods // are an extension of the class functionality : skXMLExecutable(fileName),m_View(0),m_FileName(fileName),m_Interpreter(interp) { init(); } //----------------------------------------------------------------- Controller::~Controller() //----------------------------------------------------------------- { delete m_View; } //----------------------------------------------------------------- skString Controller::getStringAttribute(DOM_Element elem,const skString& name) //----------------------------------------------------------------- { return toString(elem.getAttribute(fromString(name))); } //----------------------------------------------------------------- int Controller::getIntegerAttribute(DOM_Element elem,const skString& name) //----------------------------------------------------------------- { return toString(elem.getAttribute(fromString(name))).to(); } //----------------------------------------------------------------- void Controller::init() //----------------------------------------------------------------- { // load the view description from the file // and create the controls contained within the view definition DOM_Element code=getElement(); DOM_Element viewNode=findChild(code,s_View); if (viewNode.isNull()==false){ skString title=getStringAttribute(viewNode,s_Title); int x_pos=getIntegerAttribute(viewNode,s_X); int y_pos=getIntegerAttribute(viewNode,s_Y); int width=getIntegerAttribute(viewNode,s_Width); int height=getIntegerAttribute(viewNode,s_Height); m_View=new View(*this,title,x_pos,y_pos,width,height); // look for some controls DOM_Element controls=findChild(viewNode,s_Controls); if (controls.isNull()==false){ DOM_NodeList controlsList=controls.getChildNodes(); for (unsigned int i=0;iaddControl(type,id,text,x_pos,y_pos,width,height); } } } } skRValueArray args; skRValue ret; skExecutableContext context(m_Interpreter); method("init",args,ret,context); } //----------------------------------------------------------------- void Controller::buttonPressed(int id) //----------------------------------------------------------------- // this method is a virtual from ViewCallback, it is called when the user // presses a button { DOM_Element code=getElement(); // look for the node for this control, matching on id DOM_Element viewNode=findChild(code,s_View); if (viewNode.isNull()==false){ DOM_Element controls=findChild(viewNode,s_Controls); if (controls.isNull()==false){ DOM_NodeList controlsList=controls.getChildNodes(); for (unsigned int i=0;isetFocus(args[0].intValue()); bRet=true; }else if (IS_METHOD(s,s_close) && m_View){ // closes the view and the application m_View->close(); bRet=true; }else if (IS_METHOD(s,s_run) && m_View && args.entries()==1){ // run a command as a separate process m_View->run(args[0].str()); bRet=true; }else if (IS_METHOD(s,s_getText) && m_View && args.entries()==1){ // retrieves the text for a control ret=m_View->getText(args[0].intValue()); bRet=true; }else if (IS_METHOD(s,s_user) && m_View && args.entries()==1){ // shows a message box to the user m_View->user(args[0].str()); bRet=true; }else // we pass any other method up to the base class, which in turn looks inside // the demo script file for a matching method bRet=skXMLExecutable::method(s,args,ret,ctxt); return bRet; }