/* Copyright 1996-2000 Simon Whiteside, All Rights Reserved THIS CODE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. Demo_Controller.cpp This file implements the class which controls the behaviour of the view in the demo. $Id: Demo_Controller.cpp,v 1.1 2007/09/19 14:56:59 sdw Exp $ */ #include "Demo_Controller.h" #include "skTreeNode.h" #include "skRValueArray.h" //----------------------------------------------------------------- Controller::Controller(skString fileName) //----------------------------------------------------------------- // this class loads the demo script file, whose methods // are an extension of the class functionality : skScriptedExecutable(fileName),m_View(0),m_FileName(fileName) { Init(); } //----------------------------------------------------------------- Controller::~Controller() //----------------------------------------------------------------- { delete m_View; } //----------------------------------------------------------------- void Controller::Init() //----------------------------------------------------------------- { // load the view description from the file // and create the controls contained within the view definition if (m_Code){ skTreeNode * viewNode=m_Code->FindChild("View"); if (viewNode){ skString title=viewNode->FindChildData("Title",""); int x_pos=viewNode->FindChildIntData("X"); int y_pos=viewNode->FindChildIntData("Y"); int width=viewNode->FindChildIntData("Width"); int height=viewNode->FindChildIntData("Height"); m_View=new View(*this,title,x_pos,y_pos,width,height); // look for some controls skTreeNode * controls=viewNode->FindChild("Controls"); if (controls){ skTreeNodeListIterator iter(*controls); skTreeNode * control=0; while ((control=iter())){ skString type=control->FindChildData("Type",""); int id=control->FindChildIntData("Id"); skString text=control->FindChildData("Text",""); int x_pos=control->FindChildIntData("X"); int y_pos=control->FindChildIntData("Y"); int width=control->FindChildIntData("Width"); int height=control->FindChildIntData("Height"); m_View->AddControl(type,id,text,x_pos,y_pos,width,height); } } } skRValueArray args; skRValue ret; Method("Init",args,ret); } }//----------------------------------------------------------------- void Controller::ButtonPressed(int id) //----------------------------------------------------------------- // this method is a virtual from ViewCallback, it is called when the user // presses a button { if (m_Code){ // look for the node for this control, matching on id skTreeNode * viewNode=m_Code->FindChild("View"); if (viewNode){ skTreeNode * controls=viewNode->FindChild("Controls"); if (controls){ skTreeNodeListIterator iter(*controls); skTreeNode * control=0; while ((control=iter())){ int this_id=control->FindChildIntData("Id"); if (this_id==id){ skString method=control->FindChildData("Method",""); if (method.Length()){ // call a method on ourselves, if one is set up skRValueArray args; skRValue ret; Method(method,args,ret); } break; } } } } } } //----------------------------------------------------------------- Bool Controller::Method(skString s,skRValueArray& args,skRValue& ret) //----------------------------------------------------------------- // this is the function called by the Simkin interpreter when a method // is invoked on this object { Bool bRet=False; if (IS_METHOD(s,"Reload")){ // this code causes the script file to be re-read and // the view recreated skTreeNode * newCode=skTreeNode::Read(m_FileName); if (newCode){ delete m_View; m_View=0; delete m_Code; m_Code=newCode; }else{ m_View->User("You changed Demo.s so that it cannot be loaded, please check its syntax!"); } Init(); bRet=True; }else if (IS_METHOD(s,"SetFocus") && m_View && args.Entries()==1){ // sets focus to a control m_View->SetFocus(args[0]->IntValue()); bRet=True; }else if (IS_METHOD(s,"Close") && m_View){ // closes the view and the application m_View->Close(); bRet=True; }else if (IS_METHOD(s,"Run") && m_View && args.Entries()==1){ // runs a command as a separate program m_View->Run(args[0]->Str()); bRet=True; }else if (IS_METHOD(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,"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=skScriptedExecutable::Method(s,args,ret); return bRet; }