1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_basic.hxx" 30*cdf0e10cSrcweir #include <tools/errcode.hxx> 31*cdf0e10cSrcweir #include <basic/sbxobj.hxx> 32*cdf0e10cSrcweir #include <basic/sbx.hxx> 33*cdf0e10cSrcweir #ifndef __SBX_SBXVARIABLE_HXX //autogen 34*cdf0e10cSrcweir #include <basic/sbxvar.hxx> 35*cdf0e10cSrcweir #endif 36*cdf0e10cSrcweir #ifndef _MSGBOX_HXX //autogen 37*cdf0e10cSrcweir #include <vcl/msgbox.hxx> 38*cdf0e10cSrcweir #endif 39*cdf0e10cSrcweir 40*cdf0e10cSrcweir #include "object.hxx" 41*cdf0e10cSrcweir #include "collelem.hxx" 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir // Das Sample-Objekt hat folgende Elemente: 44*cdf0e10cSrcweir // 1) Properties: 45*cdf0e10cSrcweir // Name der Name 46*cdf0e10cSrcweir // Value ein double-Wert, beide bereits als Default drin 47*cdf0e10cSrcweir // 2) Methoden: 48*cdf0e10cSrcweir // Create Erzeugen eines neuen Unterelements 49*cdf0e10cSrcweir // Display Ausgabe eines Textes 50*cdf0e10cSrcweir // Square Argument * Argument 51*cdf0e10cSrcweir // Event Aufruf eines Basic-Eventhandlers 52*cdf0e10cSrcweir // 3) Unterobjekte: 53*cdf0e10cSrcweir // Per Create() kann ein neues Unterelement eingerichtet werden, 54*cdf0e10cSrcweir // das indiziert werden kann, falls mehrere Objekte gleichen Namens 55*cdf0e10cSrcweir // existieren. 56*cdf0e10cSrcweir // Diese Implementation ist ein Beispiel fuer eine tabellengesteuerte 57*cdf0e10cSrcweir // Version, die sehr viele Elemente enthalten kann. Die Elemente werden 58*cdf0e10cSrcweir // je nach Bedarf aus der Tabelle in das Objekt uebernommen. 59*cdf0e10cSrcweir // Die Collection findet sich in COLLECTN.*, die in der Collection 60*cdf0e10cSrcweir // enthaltenen Objekte in COLLELEM.* 61*cdf0e10cSrcweir 62*cdf0e10cSrcweir // Das Sample-Objekt wird in ..\app\mybasic.cxx wie folgt in StarBASIC 63*cdf0e10cSrcweir // eingebaut: 64*cdf0e10cSrcweir 65*cdf0e10cSrcweir // MyBasic::MyBasic() : StarBASIC() 66*cdf0e10cSrcweir // { 67*cdf0e10cSrcweir // AddFactory( new SampleObjectFac() ); 68*cdf0e10cSrcweir // } 69*cdf0e10cSrcweir 70*cdf0e10cSrcweir // Das nArgs-Feld eines Tabelleneintrags ist wie folgt verschluesselt: 71*cdf0e10cSrcweir 72*cdf0e10cSrcweir #define _ARGSMASK 0x00FF // Bis zu 255 Argumente 73*cdf0e10cSrcweir #define _RWMASK 0x0F00 // Maske fuer R/W-Bits 74*cdf0e10cSrcweir #define _TYPEMASK 0xF000 // Maske fuer den Typ des Eintrags 75*cdf0e10cSrcweir 76*cdf0e10cSrcweir #define _READ 0x0100 // kann gelesen werden 77*cdf0e10cSrcweir #define _BWRITE 0x0200 // kann as Lvalue verwendet werden 78*cdf0e10cSrcweir #define _LVALUE _BWRITE // kann as Lvalue verwendet werden 79*cdf0e10cSrcweir #define _READWRITE 0x0300 // beides 80*cdf0e10cSrcweir #define _OPT 0x0400 // sal_True: optionaler Parameter 81*cdf0e10cSrcweir #define _METHOD 0x1000 // Masken-Bit fuer eine Methode 82*cdf0e10cSrcweir #define _PROPERTY 0x2000 // Masken-Bit fuer eine Property 83*cdf0e10cSrcweir #define _COLL 0x4000 // Masken-Bit fuer eine Collection 84*cdf0e10cSrcweir // Kombination von oberen Bits: 85*cdf0e10cSrcweir #define _FUNCTION 0x1100 // Maske fuer Function 86*cdf0e10cSrcweir #define _LFUNCTION 0x1300 // Maske fuer Function, die auch als Lvalue geht 87*cdf0e10cSrcweir #define _ROPROP 0x2100 // Maske Read Only-Property 88*cdf0e10cSrcweir #define _WOPROP 0x2200 // Maske Write Only-Property 89*cdf0e10cSrcweir #define _RWPROP 0x2300 // Maske Read/Write-Property 90*cdf0e10cSrcweir #define _COLLPROP 0x4100 // Maske Read-Collection-Element 91*cdf0e10cSrcweir 92*cdf0e10cSrcweir #define COLLNAME "Elements" // Name der Collection, hier mal hart verdrahtet 93*cdf0e10cSrcweir 94*cdf0e10cSrcweir SampleObject::Methods SampleObject::aMethods[] = { 95*cdf0e10cSrcweir // Eine Sample-Methode (der Returnwert ist SbxNULL) 96*cdf0e10cSrcweir { "Display", SbxEMPTY, &SampleObject::Display, 1 | _FUNCTION }, 97*cdf0e10cSrcweir // Ein Named Parameter 98*cdf0e10cSrcweir { "message", SbxSTRING, NULL, 0 }, 99*cdf0e10cSrcweir // Eine Sample-Funktion 100*cdf0e10cSrcweir { "Square", SbxDOUBLE, &SampleObject::Square, 1 | _FUNCTION }, 101*cdf0e10cSrcweir // Ein Named Parameter 102*cdf0e10cSrcweir { "value", SbxDOUBLE, NULL, 0 }, 103*cdf0e10cSrcweir // Basic-Callback 104*cdf0e10cSrcweir { "Event", SbxEMPTY, &SampleObject::Event, 1 | _FUNCTION }, 105*cdf0e10cSrcweir // Ein Named Parameter 106*cdf0e10cSrcweir { "event", SbxSTRING, NULL, 0 }, 107*cdf0e10cSrcweir // Element erzeugen 108*cdf0e10cSrcweir { "Create", SbxEMPTY, &SampleObject::Create, 1 | _FUNCTION }, 109*cdf0e10cSrcweir // Ein Named Parameter 110*cdf0e10cSrcweir { "name", SbxSTRING, NULL, 0 }, 111*cdf0e10cSrcweir 112*cdf0e10cSrcweir { NULL, SbxNULL, NULL, -1 }}; // Tabellenende 113*cdf0e10cSrcweir 114*cdf0e10cSrcweir SampleObject::SampleObject( const String& rClass ) : SbxObject( rClass ) 115*cdf0e10cSrcweir { 116*cdf0e10cSrcweir SetName( String( RTL_CONSTASCII_USTRINGPARAM("Sample") ) ); 117*cdf0e10cSrcweir PutDouble( 1.0 ); // Startwert fuer Value 118*cdf0e10cSrcweir } 119*cdf0e10cSrcweir 120*cdf0e10cSrcweir // Suche nach einem Element: 121*cdf0e10cSrcweir // Hier wird linear durch die Methodentabelle gegangen, bis eine 122*cdf0e10cSrcweir // passende Methode gefunden wurde. 123*cdf0e10cSrcweir // Wenn die Methode/Property nicht gefunden wurde, nur NULL ohne 124*cdf0e10cSrcweir // Fehlercode zurueckliefern, da so auch eine ganze Chain von 125*cdf0e10cSrcweir // Objekten nach der Methode/Property befragt werden kann. 126*cdf0e10cSrcweir 127*cdf0e10cSrcweir SbxVariable* SampleObject::Find( const String& rName, SbxClassType t ) 128*cdf0e10cSrcweir { 129*cdf0e10cSrcweir // Ist das Element bereits vorhanden? 130*cdf0e10cSrcweir SbxVariable* pRes = SbxObject::Find( rName, t ); 131*cdf0e10cSrcweir if( !pRes && t != SbxCLASS_OBJECT ) 132*cdf0e10cSrcweir { 133*cdf0e10cSrcweir // sonst suchen 134*cdf0e10cSrcweir Methods* p = aMethods; 135*cdf0e10cSrcweir short nIndex = 0; 136*cdf0e10cSrcweir sal_Bool bFound = sal_False; 137*cdf0e10cSrcweir while( p->nArgs != -1 ) 138*cdf0e10cSrcweir { 139*cdf0e10cSrcweir if( rName.EqualsIgnoreCaseAscii( p->pName ) ) 140*cdf0e10cSrcweir { 141*cdf0e10cSrcweir bFound = sal_True; break; 142*cdf0e10cSrcweir } 143*cdf0e10cSrcweir nIndex += ( p->nArgs & _ARGSMASK ) + 1; 144*cdf0e10cSrcweir p = aMethods + nIndex; 145*cdf0e10cSrcweir } 146*cdf0e10cSrcweir if( bFound ) 147*cdf0e10cSrcweir { 148*cdf0e10cSrcweir // Args-Felder isolieren: 149*cdf0e10cSrcweir short nAccess = ( p->nArgs & _RWMASK ) >> 8; 150*cdf0e10cSrcweir short nType = ( p->nArgs & _TYPEMASK ); 151*cdf0e10cSrcweir String aName_ = String::CreateFromAscii( p->pName ); 152*cdf0e10cSrcweir SbxClassType eCT = SbxCLASS_OBJECT; 153*cdf0e10cSrcweir if( nType & _PROPERTY ) 154*cdf0e10cSrcweir eCT = SbxCLASS_PROPERTY; 155*cdf0e10cSrcweir else if( nType & _METHOD ) 156*cdf0e10cSrcweir eCT = SbxCLASS_METHOD; 157*cdf0e10cSrcweir pRes = Make( aName_, eCT, p->eType ); 158*cdf0e10cSrcweir // Wir setzen den Array-Index + 1, da ja noch andere 159*cdf0e10cSrcweir // Standard-Properties existieren, die auch aktiviert 160*cdf0e10cSrcweir // werden muessen. 161*cdf0e10cSrcweir pRes->SetUserData( nIndex + 1 ); 162*cdf0e10cSrcweir pRes->SetFlags( nAccess ); 163*cdf0e10cSrcweir } 164*cdf0e10cSrcweir } 165*cdf0e10cSrcweir return pRes; 166*cdf0e10cSrcweir } 167*cdf0e10cSrcweir 168*cdf0e10cSrcweir // Aktivierung eines Elements oder Anfordern eines Infoblocks 169*cdf0e10cSrcweir 170*cdf0e10cSrcweir void SampleObject::SFX_NOTIFY( SfxBroadcaster& rBC, const TypeId& rBCT, 171*cdf0e10cSrcweir const SfxHint& rHint, const TypeId& rHT ) 172*cdf0e10cSrcweir { 173*cdf0e10cSrcweir const SbxHint* pHint = PTR_CAST(SbxHint,&rHint); 174*cdf0e10cSrcweir if( pHint ) 175*cdf0e10cSrcweir { 176*cdf0e10cSrcweir SbxVariable* pVar = pHint->GetVar(); 177*cdf0e10cSrcweir SbxArray* pPar_ = pVar->GetParameters(); 178*cdf0e10cSrcweir sal_uInt16 nIndex = (sal_uInt16) pVar->GetUserData(); 179*cdf0e10cSrcweir // kein Index: weiterreichen! 180*cdf0e10cSrcweir if( nIndex ) 181*cdf0e10cSrcweir { 182*cdf0e10cSrcweir sal_uIntPtr t = pHint->GetId(); 183*cdf0e10cSrcweir if( t == SBX_HINT_INFOWANTED ) 184*cdf0e10cSrcweir pVar->SetInfo( GetInfo( (short) pVar->GetUserData() ) ); 185*cdf0e10cSrcweir else 186*cdf0e10cSrcweir { 187*cdf0e10cSrcweir sal_Bool bWrite = sal_False; 188*cdf0e10cSrcweir if( t == SBX_HINT_DATACHANGED ) 189*cdf0e10cSrcweir bWrite = sal_True; 190*cdf0e10cSrcweir if( t == SBX_HINT_DATAWANTED || bWrite ) 191*cdf0e10cSrcweir { 192*cdf0e10cSrcweir // Parameter-Test fuer Methoden: 193*cdf0e10cSrcweir sal_uInt16 nPar = aMethods[ --nIndex ].nArgs & 0x00FF; 194*cdf0e10cSrcweir // Element 0 ist der Returnwert 195*cdf0e10cSrcweir if( ( !pPar_ && nPar ) 196*cdf0e10cSrcweir || ( pPar_->Count() != nPar+1 ) ) 197*cdf0e10cSrcweir SetError( SbxERR_WRONG_ARGS ); 198*cdf0e10cSrcweir // Alles klar, man kann den Call ausfuehren 199*cdf0e10cSrcweir else 200*cdf0e10cSrcweir { 201*cdf0e10cSrcweir (this->*(aMethods[ nIndex ].pFunc))( pVar, pPar_, bWrite ); 202*cdf0e10cSrcweir } 203*cdf0e10cSrcweir } 204*cdf0e10cSrcweir } 205*cdf0e10cSrcweir } 206*cdf0e10cSrcweir SbxObject::SFX_NOTIFY( rBC, rBCT, rHint, rHT ); 207*cdf0e10cSrcweir } 208*cdf0e10cSrcweir } 209*cdf0e10cSrcweir 210*cdf0e10cSrcweir // Zusammenbau der Infostruktur fuer einzelne Elemente 211*cdf0e10cSrcweir 212*cdf0e10cSrcweir SbxInfo* SampleObject::GetInfo( short nIdx ) 213*cdf0e10cSrcweir { 214*cdf0e10cSrcweir Methods* p = &aMethods[ nIdx ]; 215*cdf0e10cSrcweir // Wenn mal eine Hilfedatei zur Verfuegung steht: 216*cdf0e10cSrcweir // SbxInfo* pInfo_ = new SbxInfo( Hilfedateiname, p->nHelpId ); 217*cdf0e10cSrcweir SbxInfo* pInfo_ = new SbxInfo; 218*cdf0e10cSrcweir short nPar = p->nArgs & _ARGSMASK; 219*cdf0e10cSrcweir for( short i = 0; i < nPar; i++ ) 220*cdf0e10cSrcweir { 221*cdf0e10cSrcweir p++; 222*cdf0e10cSrcweir String aName_ = String::CreateFromAscii( p->pName ); 223*cdf0e10cSrcweir sal_uInt16 nFlags_ = ( p->nArgs >> 8 ) & 0x03; 224*cdf0e10cSrcweir if( p->nArgs & _OPT ) 225*cdf0e10cSrcweir nFlags_ |= SBX_OPTIONAL; 226*cdf0e10cSrcweir pInfo_->AddParam( aName_, p->eType, nFlags_ ); 227*cdf0e10cSrcweir } 228*cdf0e10cSrcweir return pInfo_; 229*cdf0e10cSrcweir } 230*cdf0e10cSrcweir 231*cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////// 232*cdf0e10cSrcweir 233*cdf0e10cSrcweir // Properties und Methoden legen beim Get (bPut = sal_False) den Returnwert 234*cdf0e10cSrcweir // im Element 0 des Argv ab; beim Put (bPut = sal_True) wird der Wert aus 235*cdf0e10cSrcweir // Element 0 gespeichert. 236*cdf0e10cSrcweir 237*cdf0e10cSrcweir // Die Methoden: 238*cdf0e10cSrcweir 239*cdf0e10cSrcweir void SampleObject::Display( SbxVariable*, SbxArray* pPar_, sal_Bool ) 240*cdf0e10cSrcweir { 241*cdf0e10cSrcweir // GetString() loest u.U. auch einen Error aus! 242*cdf0e10cSrcweir String s( pPar_->Get( 1 )->GetString() ); 243*cdf0e10cSrcweir if( !IsError() ) 244*cdf0e10cSrcweir InfoBox( NULL, s ).Execute(); 245*cdf0e10cSrcweir } 246*cdf0e10cSrcweir 247*cdf0e10cSrcweir void SampleObject::Square( SbxVariable* pVar, SbxArray* pPar_, sal_Bool ) 248*cdf0e10cSrcweir { 249*cdf0e10cSrcweir double n = pPar_->Get( 1 )->GetDouble(); 250*cdf0e10cSrcweir pVar->PutDouble( n * n ); 251*cdf0e10cSrcweir } 252*cdf0e10cSrcweir 253*cdf0e10cSrcweir // Callback nach BASIC: 254*cdf0e10cSrcweir 255*cdf0e10cSrcweir void SampleObject::Event( SbxVariable*, SbxArray* pPar_, sal_Bool ) 256*cdf0e10cSrcweir { 257*cdf0e10cSrcweir Call( pPar_->Get( 1 )->GetString(), NULL ); 258*cdf0e10cSrcweir } 259*cdf0e10cSrcweir 260*cdf0e10cSrcweir // Neues Element anlegen 261*cdf0e10cSrcweir 262*cdf0e10cSrcweir void SampleObject::Create( SbxVariable* pVar, SbxArray* pPar_, sal_Bool ) 263*cdf0e10cSrcweir { 264*cdf0e10cSrcweir pVar->PutObject( 265*cdf0e10cSrcweir MakeObject( pPar_->Get( 1 )->GetString(), String( RTL_CONSTASCII_USTRINGPARAM("SampleElement") ) ) ); 266*cdf0e10cSrcweir } 267*cdf0e10cSrcweir 268*cdf0e10cSrcweir // Die Factory legt unsere beiden Objekte an. 269*cdf0e10cSrcweir 270*cdf0e10cSrcweir SbxObject* SampleObjectFac::CreateObject( const String& rClass ) 271*cdf0e10cSrcweir { 272*cdf0e10cSrcweir if( rClass.EqualsIgnoreCaseAscii( "SampleObject" ) ) 273*cdf0e10cSrcweir return new SampleObject( rClass ); 274*cdf0e10cSrcweir if( rClass.EqualsIgnoreCaseAscii( "SampleElement" ) ) 275*cdf0e10cSrcweir return new SampleElement( rClass ); 276*cdf0e10cSrcweir return NULL; 277*cdf0e10cSrcweir } 278*cdf0e10cSrcweir 279