1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 // MARKER(update_precomp.py): autogen include statement, do not remove 29 #include "precompiled_basic.hxx" 30 #include <vcl/svapp.hxx> 31 #include <vcl/wrkwin.hxx> 32 #include <vcl/toolbox.hxx> 33 #include <vcl/msgbox.hxx> 34 #include <vcl/sound.hxx> 35 #include <basic/basmgr.hxx> 36 #include <basic/sbx.hxx> 37 #include <basic/sbmod.hxx> 38 #include <basic/basrdll.hxx> 39 40 //#include <sv.hxx> 41 //#include <basic.hxx> 42 //#include <sostor.hxx> 43 44 // Defines for ToolBox-Id's 45 #define TB_NEW 1 46 #define TB_OPENSTORAGE 2 47 #define TB_SAVESTORAGE 3 48 #define TB_ORG 4 49 #define TB_CREATELIB1 10 50 #define TB_CREATELIB2 11 51 #define TB_CREATELIB3 12 52 #define TB_LOADLIB1 20 53 #define TB_LOADLIB2 21 54 #define TB_LOADLIB3 22 55 #define TB_STORELIBX 30 56 #define TB_UNLOADX 31 57 #define TB_LOADX 32 58 #define TB_EXECX 33 59 #define TB_REMOVEX 34 60 #define TB_REMOVEDELX 35 61 62 #define TB_LIB0 40 63 #define TB_LIB1 41 64 #define TB_LIB2 42 65 #define TB_LIB3 43 66 67 const char* pLib1Str = "Lib1"; 68 const char* pLib2Str = "Lib2"; 69 const char* pLib3Str = "Lib3"; 70 71 // Test-Application 72 class TestApp : public Application 73 { 74 public: 75 virtual void Main( void ); 76 virtual void Main( int, char*[] ); 77 }; 78 79 // Test-Window with a ToolBox to choose a test from 80 // and the typically used virtual methods 81 class TestWindow : public WorkWindow 82 { 83 private: 84 ToolBox aToolBox; 85 BasicManager* pBasMgr; 86 87 void CheckError(); 88 sal_uInt16 nLibX; 89 DECL_LINK( BasicErrorHdl, StarBASIC * ); 90 91 92 public: 93 TestWindow(); 94 ~TestWindow(); 95 96 virtual void Paint( const Rectangle& ); 97 virtual void Resize(); 98 virtual void KeyInput( const KeyEvent& rKeyEvt ); 99 virtual void MouseMove( const MouseEvent& rMEvt ); 100 virtual void MouseButtonDown( const MouseEvent& rMEvt ); 101 virtual void MouseButtonUp( const MouseEvent& rMEvt ); 102 103 DECL_LINK( TBSelect, ToolBox * ); 104 void UpdateToolBox(); 105 void ShowInfo(); 106 }; 107 108 TestWindow::~TestWindow() 109 { 110 } 111 112 TestWindow::TestWindow() : 113 WorkWindow( NULL, WB_APP | WB_STDWORK | WB_3DLOOK | WB_CLIPCHILDREN ) , 114 aToolBox( this, WinBits( WB_BORDER | WB_3DLOOK | WB_SCROLL | WB_LINESPACING ) ) 115 { 116 nLibX = 0; 117 118 aToolBox.SetButtonType( BUTTON_TEXT ); 119 aToolBox.SetLineCount( 2 ); 120 aToolBox.SetPosPixel( Point( 0, 0 ) ); 121 aToolBox.SetSelectHdl( LINK( this, TestWindow, TBSelect ) ); 122 123 Font aFont; 124 aFont.SetName( "Helv" ); 125 aFont.SetSize( Size( 0, 6 ) ); 126 aFont.SetPitch( PITCH_VARIABLE ); 127 aFont.SetFamily( FAMILY_SWISS ); 128 aFont.SetTransparent( sal_True ); 129 aFont.SetAlign( ALIGN_TOP ); 130 aToolBox.SetFont( aFont ); 131 SetFont( aFont ); 132 133 aToolBox.InsertItem( TB_NEW, "New" ); 134 aToolBox.SetHelpText( TB_NEW, "New BasicManager" ); 135 aToolBox.InsertItem( TB_OPENSTORAGE, "Load" ); 136 aToolBox.SetHelpText( TB_OPENSTORAGE, "Load Storage D:\\MYSTORE.SVS" ); 137 aToolBox.InsertItem( TB_SAVESTORAGE, "Save" ); 138 aToolBox.SetHelpText( TB_SAVESTORAGE, "Save Storage D:\\MYSTORE.SVS" ); 139 140 aToolBox.InsertSeparator(); 141 142 aToolBox.InsertItem( TB_ORG, "Verwalten" ); 143 aToolBox.SetHelpText( TB_ORG, "Libaries verwalten" ); 144 145 aToolBox.InsertSeparator(); 146 147 aToolBox.InsertItem( TB_LIB0, "0" ); 148 aToolBox.SetHelpText( TB_LIB0, "Aktuelle Lib: STANDARD" ); 149 aToolBox.InsertItem( TB_LIB1, "1" ); 150 aToolBox.SetHelpText( TB_LIB1, "Aktuelle Lib: 1" ); 151 aToolBox.InsertItem( TB_LIB2, "2" ); 152 aToolBox.SetHelpText( TB_LIB2, "Aktuelle Lib: 2" ); 153 aToolBox.InsertItem( TB_LIB3, "3" ); 154 aToolBox.SetHelpText( TB_LIB3, "Aktuelle Lib: 3" ); 155 156 aToolBox.InsertBreak(); 157 aToolBox.InsertItem( TB_CREATELIB1, "CreateLib1" ); 158 aToolBox.SetHelpText( TB_CREATELIB1, "Create Libary LIB1" ); 159 aToolBox.InsertItem( TB_CREATELIB2, "CreateLib2" ); 160 aToolBox.SetHelpText( TB_CREATELIB2, "Create Libary LIB2" ); 161 aToolBox.InsertItem( TB_CREATELIB3, "CreateLib3" ); 162 aToolBox.SetHelpText( TB_CREATELIB3, "Create Libary LIB3" ); 163 164 aToolBox.InsertSeparator(); 165 aToolBox.InsertItem( TB_LOADLIB1, "LoadLib1" ); 166 aToolBox.SetHelpText( TB_LOADLIB1, "Load Libary LIB1" ); 167 aToolBox.InsertItem( TB_LOADLIB2, "LoadLib2" ); 168 aToolBox.SetHelpText( TB_LOADLIB2, "Load Libary LIB2" ); 169 aToolBox.InsertItem( TB_LOADLIB3, "LoadLib3" ); 170 aToolBox.SetHelpText( TB_LOADLIB3, "Load Libary LIB3" ); 171 172 aToolBox.InsertSeparator(); 173 aToolBox.InsertItem( TB_STORELIBX, "StoreLibX" ); 174 aToolBox.SetHelpText( TB_STORELIBX, "Store Libary LIBX" ); 175 aToolBox.InsertItem( TB_UNLOADX, "UnloadX" ); 176 aToolBox.SetHelpText( TB_UNLOADX, "Unload Libary LIBX" ); 177 aToolBox.InsertItem( TB_LOADX, "LoadX" ); 178 aToolBox.SetHelpText( TB_LOADX, "Load Libary LIBX" ); 179 aToolBox.InsertItem( TB_EXECX, "ExecX" ); 180 aToolBox.SetHelpText( TB_EXECX, "Execute 'Libary' LIBX" ); 181 aToolBox.InsertItem( TB_REMOVEX, "RemoveX" ); 182 aToolBox.SetHelpText( TB_REMOVEX, "Remove Libary LIBX" ); 183 aToolBox.InsertItem( TB_REMOVEDELX, "RemDelX" ); 184 aToolBox.SetHelpText( TB_REMOVEDELX, "Remove and delete Libary LIBX" ); 185 186 pBasMgr = 0; 187 188 Show(); 189 UpdateToolBox(); 190 aToolBox.Show(); 191 } 192 void TestWindow::ShowInfo() 193 { 194 Invalidate(); 195 Update(); 196 long nH = GetTextSize( "X" ).Height(); 197 if ( pBasMgr ) 198 { 199 Point aPos( 10, aToolBox.GetSizePixel().Height()+5 ); 200 for ( sal_uInt16 nLib = 0; nLib < pBasMgr->GetLibCount(); nLib++ ) 201 { 202 String aOutStr( nLib ); 203 aOutStr +=": "; 204 StarBASIC* pL = pBasMgr->GetLib( nLib ); 205 aOutStr += '['; 206 aOutStr += pBasMgr->GetLibName( nLib ); 207 aOutStr += "]<"; 208 if ( pL ) 209 aOutStr += pL->GetName(); 210 else 211 aOutStr += "NoLoaded"; 212 aOutStr += ">, Storage='"; 213 aOutStr += pBasMgr->GetLibStorageName( nLib ); 214 aOutStr += "', bLoaded="; 215 aOutStr += (sal_uInt16)pBasMgr->IsLibLoaded( nLib ); 216 DrawText( aPos, aOutStr ); 217 aPos.Y() += nH; 218 } 219 } 220 } 221 222 void TestWindow::UpdateToolBox() 223 { 224 // View of some buttons as checked or disabled if 225 // wished by tests 226 aToolBox.EnableItem( TB_ORG, (sal_Bool)(sal_uIntPtr)pBasMgr ); 227 228 aToolBox.EnableItem( TB_CREATELIB1, (sal_Bool)(sal_uIntPtr)pBasMgr ); 229 aToolBox.EnableItem( TB_CREATELIB2, (sal_Bool)(sal_uIntPtr)pBasMgr ); 230 aToolBox.EnableItem( TB_CREATELIB3, (sal_Bool)(sal_uIntPtr)pBasMgr ); 231 232 aToolBox.EnableItem( TB_LOADLIB1, (sal_Bool)(sal_uIntPtr)pBasMgr ); 233 aToolBox.EnableItem( TB_LOADLIB2, (sal_Bool)(sal_uIntPtr)pBasMgr ); 234 aToolBox.EnableItem( TB_LOADLIB3, (sal_Bool)(sal_uIntPtr)pBasMgr ); 235 236 aToolBox.EnableItem( TB_STORELIBX, (sal_Bool)(sal_uIntPtr)pBasMgr ); 237 aToolBox.EnableItem( TB_EXECX, (sal_Bool)(sal_uIntPtr)pBasMgr ); 238 aToolBox.EnableItem( TB_UNLOADX, (sal_Bool)(sal_uIntPtr)pBasMgr ); 239 aToolBox.EnableItem( TB_LOADX, (sal_Bool)(sal_uIntPtr)pBasMgr ); 240 aToolBox.EnableItem( TB_REMOVEX, (sal_Bool)(sal_uIntPtr)pBasMgr ); 241 aToolBox.EnableItem( TB_REMOVEDELX, (sal_Bool)(sal_uIntPtr)pBasMgr ); 242 243 aToolBox.CheckItem( TB_LIB0, nLibX == 0 ); 244 aToolBox.CheckItem( TB_LIB1, nLibX == 1 ); 245 aToolBox.CheckItem( TB_LIB2, nLibX == 2 ); 246 aToolBox.CheckItem( TB_LIB3, nLibX == 3 ); 247 } 248 249 IMPL_LINK( TestWindow, TBSelect, ToolBox *, p ) 250 { 251 sal_uInt16 nId = aToolBox.GetCurItemId(); 252 sal_Bool bChecked = aToolBox.IsItemChecked( nId ); 253 switch ( nId ) 254 { 255 case TB_NEW: 256 { 257 delete pBasMgr; 258 pBasMgr = new BasicManager( new StarBASIC ); 259 pBasMgr->SetStorageName( "d:\\mystore.svs" ); 260 } 261 break; 262 case TB_OPENSTORAGE: 263 { 264 delete pBasMgr; 265 SvStorageRef xStorage = new SvStorage( "d:\\mystore.svs", STREAM_READ | STREAM_SHARE_DENYWRITE ); 266 DBG_ASSERT( xStorage.Is(), "Kein Storage!" ); 267 pBasMgr = new BasicManager( *xStorage ); 268 } 269 break; 270 case TB_SAVESTORAGE: 271 { 272 if ( pBasMgr) 273 { 274 SvStorageRef xStorage = new SvStorage( "d:\\mystore.svs" ); 275 DBG_ASSERT( xStorage.Is(), "Kein Storage!" ); 276 pBasMgr->Store( *xStorage ); 277 } 278 } 279 break; 280 case TB_ORG: 281 { 282 if ( pBasMgr) 283 { 284 InfoBox( 0, "Organisieren..." ).Execute(); 285 } 286 } 287 break; 288 case TB_CREATELIB1: 289 { 290 if ( pBasMgr ) 291 { 292 sal_uInt16 nLib = pBasMgr->GetLibId( pBasMgr->CreateLib( pLib1Str ) ); 293 if ( nLib != LIB_NOTFOUND ) 294 { 295 pBasMgr->SetLibStorageName( nLib, "d:\\mystore.svs" ); 296 StarBASIC* pLib = pBasMgr->GetLib( pLib1Str ); 297 DBG_ASSERT( pLib, "Lib?!" ); 298 String aSource( "Sub SubInLib1Mod1\nprint\"XXX\"\nEnd Sub"); 299 SbModule* pM = pLib->MakeModule( "ModLib1", aSource ); 300 DBG_ASSERT( pM, "Modul?" ); 301 pLib->Compile( pM ); 302 } 303 else 304 InfoBox( 0, "CreateLibary fehlgeschlagen..." ).Execute(); 305 } 306 } 307 break; 308 case TB_CREATELIB2: 309 { 310 if ( pBasMgr ) 311 { 312 sal_uInt16 nLib = pBasMgr->GetLibId( pBasMgr->CreateLib( pLib2Str ) ); 313 if ( nLib != LIB_NOTFOUND ) 314 { 315 pBasMgr->SetLibStorageName( nLib, "d:\\mystore.svs" ); 316 StarBASIC* pLib = pBasMgr->GetLib( pLib2Str ); 317 DBG_ASSERT( pLib, "Lib?!" ); 318 SbModule* pM = pLib->MakeModule( "ModuleLib2", "Sub SubInLib2\n print \"Tralala\" \nEnd Sub\n" ); 319 pLib->Compile( pM ); 320 } 321 else 322 InfoBox( 0, "CreateLibary fehlgeschlagen..." ).Execute(); 323 } 324 } 325 break; 326 case TB_CREATELIB3: 327 { 328 if ( pBasMgr ) 329 { 330 // liegt in einem anderen Storage !!! 331 sal_uInt16 nLib = pBasMgr->GetLibId( pBasMgr->CreateLib( pLib3Str ) ); 332 if ( nLib != LIB_NOTFOUND ) 333 { 334 pBasMgr->SetLibStorageName( nLib, "d:\\mystore2.svs" ); 335 StarBASIC* pLib = pBasMgr->GetLib( pLib3Str ); 336 DBG_ASSERT( pLib, "Lib?!" ); 337 SbModule* pM = pLib->MakeModule( "ModuleLib2", "Sub XYZInLib3\n print \"?!\" \nEnd Sub\n" ); 338 pLib->Compile( pM ); 339 } 340 else 341 InfoBox( 0, "CreateLibary fehlgeschlagen..." ).Execute(); 342 } 343 } 344 break; 345 case TB_LOADLIB1: 346 { 347 if ( pBasMgr ) 348 { 349 SvStorageRef xStorage = new SvStorage( "d:\\mystore.svs" ); 350 if ( !pBasMgr->AddLib( *xStorage, pLib1Str, sal_False ) ) 351 Sound::Beep(); 352 } 353 } 354 break; 355 case TB_LOADLIB2: 356 { 357 if ( pBasMgr ) 358 { 359 SvStorageRef xStorage = new SvStorage( "d:\\mystore.svs" ); 360 if ( !pBasMgr->AddLib( *xStorage, pLib2Str, sal_False ) ) 361 Sound::Beep(); 362 } 363 } 364 break; 365 case TB_LOADLIB3: 366 { 367 if ( pBasMgr ) 368 { 369 // liegt in einem anderen Storage !!! 370 SvStorageRef xStorage = new SvStorage( "d:\\mystore2.svs" ); 371 if ( !pBasMgr->AddLib( *xStorage, pLib3Str, sal_False ) ) 372 Sound::Beep(); 373 } 374 } 375 break; 376 case TB_STORELIBX: 377 { 378 if ( pBasMgr ) 379 pBasMgr->StoreLib( nLibX ); 380 } 381 break; 382 case TB_UNLOADX: 383 { 384 if ( pBasMgr ) 385 pBasMgr->UnloadLib( nLibX ); 386 } 387 break; 388 case TB_LOADX: 389 { 390 if ( pBasMgr ) 391 pBasMgr->LoadLib( nLibX ); 392 } 393 break; 394 case TB_REMOVEX: 395 { 396 if ( pBasMgr ) 397 pBasMgr->RemoveLib( nLibX, sal_False ); 398 } 399 break; 400 case TB_REMOVEDELX: 401 { 402 if ( pBasMgr ) 403 pBasMgr->RemoveLib( nLibX, sal_True ); 404 } 405 break; 406 case TB_EXECX: 407 { 408 if ( pBasMgr ) 409 { 410 StarBASIC* pBasic = pBasMgr->GetLib( nLibX ); 411 if ( pBasic && pBasic->GetModules()->Count() ) 412 { 413 pBasic->SetErrorHdl( LINK( this, TestWindow, BasicErrorHdl ) ); 414 415 SbModule* pMod = (SbModule*)pBasic->GetModules()->Get( 0 ); 416 if ( pMod && pMod->GetMethods()->Count() ) 417 pMod->GetMethods()->Get(0)->GetInteger(); 418 } 419 } 420 } 421 break; 422 423 case TB_LIB0: nLibX = 0; 424 break; 425 case TB_LIB1: nLibX = 1; 426 break; 427 case TB_LIB2: nLibX = 2; 428 break; 429 case TB_LIB3: nLibX = 3; 430 break; 431 } 432 433 UpdateToolBox(); 434 CheckError(); 435 ShowInfo(); 436 return 0; 437 } 438 439 void TestWindow::CheckError() 440 { 441 if ( pBasMgr ) 442 { 443 BasicError* pError = pBasMgr->GetFirstError(); 444 while ( pError ) 445 { 446 String aErrorStr; 447 String aReasonStr; 448 switch ( pError->GetErrorId() ) 449 { 450 case BASERR_ID_STDLIBOPEN: 451 aErrorStr = "Standard-Lib konnte nicht geoffnet werden."; 452 break; 453 case BASERR_ID_STDLIBSAVE: 454 aErrorStr = "Standard-Lib konnte nicht gespeichert werden."; 455 break; 456 case BASERR_ID_LIBLOAD: 457 aErrorStr = "Lib konnte nicht geoffnet werden."; 458 break; 459 case BASERR_ID_LIBCREATE: 460 aErrorStr = "Lib konnte nicht erzeugt werden."; 461 break; 462 case BASERR_ID_LIBSAVE: 463 aErrorStr = "Lib konnte nicht gespeichert werden."; 464 break; 465 case BASERR_ID_MGROPEN: 466 aErrorStr = "Manager konnte nicht geladen werden."; 467 break; 468 case BASERR_ID_MGRSAVE: 469 aErrorStr = "Manager konnte nicht gespeichert werden."; 470 break; 471 case BASERR_ID_UNLOADLIB: 472 aErrorStr = "Libary konnte nicht entladen werden."; 473 break; 474 case BASERR_ID_REMOVELIB: 475 aErrorStr = "Libary konnte nicht entfernt werden."; 476 break; 477 default: 478 aErrorStr = "Unbekannter Fehler!"; 479 } 480 481 switch ( pError->GetReason() ) 482 { 483 case BASERR_REASON_OPENSTORAGE: 484 aReasonStr = "Der Storage konnte nicht geoeffnet werden"; 485 break; 486 case BASERR_REASON_OPENLIBSTORAGE: 487 aReasonStr = "Der Lib-Storage konnte nicht geoeffnet werden"; 488 break; 489 case BASERR_REASON_OPENMGRSTREAM: 490 aReasonStr = "Der Manager-Stream konnte nicht geoeffnet werden"; 491 break; 492 case BASERR_REASON_OPENLIBSTREAM: 493 aReasonStr = "Der Basic-Stream konnte nicht geoeffnet werden"; 494 break; 495 case BASERR_REASON_STDLIB: 496 aReasonStr = "STANDARD-Lib"; 497 break; 498 case BASERR_REASON_BASICLOADERROR: 499 aReasonStr = "Fehler beim Laden des Basics"; 500 default: 501 aReasonStr = " - "; 502 } 503 504 String aErr( aErrorStr ); 505 aErr += "\nGrund: "; 506 aErr += aReasonStr; 507 InfoBox( 0, aErr ).Execute(); 508 509 pError = pBasMgr->GetNextError(); 510 } 511 pBasMgr->ClearErrors(); 512 } 513 } 514 515 void __EXPORT TestWindow::Paint( const Rectangle& rRec ) 516 { 517 } 518 519 void __EXPORT TestWindow::Resize() 520 { 521 Size aTBSz = aToolBox.CalcWindowSizePixel(); 522 aToolBox.SetSizePixel( Size( GetOutputSizePixel().Width(), aTBSz.Height()) ); 523 Invalidate(); 524 ShowInfo(); 525 } 526 527 void __EXPORT TestWindow::KeyInput( const KeyEvent& rKEvt ) 528 { 529 char nCharCode = rKEvt.GetCharCode(); 530 sal_uInt16 nCode = rKEvt.GetKeyCode().GetCode(); 531 532 // Nur bei Alt-Return 533 if ( ( nCode == KEY_RETURN ) && rKEvt.GetKeyCode().IsMod2() ) 534 ; 535 else 536 WorkWindow::KeyInput( rKEvt ); 537 538 UpdateToolBox(); 539 } 540 541 void __EXPORT TestWindow::MouseMove( const MouseEvent& rMEvt ) 542 { 543 } 544 545 void __EXPORT TestWindow::MouseButtonDown( const MouseEvent& rMEvt ) 546 { 547 ShowInfo(); 548 } 549 550 void __EXPORT TestWindow::MouseButtonUp( const MouseEvent& rMEvt ) 551 { 552 UpdateToolBox(); 553 } 554 555 IMPL_LINK( TestWindow, BasicErrorHdl, StarBASIC *, pBasic ) 556 { 557 String aErrorText( pBasic->GetErrorText() ); 558 559 String aErrorTextPrefix; 560 if( pBasic->IsCompilerError() ) 561 { 562 aErrorTextPrefix = "Compilererror: "; 563 } 564 else 565 { 566 aErrorTextPrefix = "Runtimeerror: "; 567 aErrorTextPrefix += pBasic->GetErrorCode(); 568 aErrorTextPrefix += " "; 569 } 570 571 InfoBox( 0, String( aErrorTextPrefix + aErrorText ) ).Execute(); 572 return 0; 573 } 574 575 void __EXPORT TestApp::Main( void ) 576 { 577 Main( 0, NULL ); 578 } 579 580 void __EXPORT TestApp::Main( int, char*[] ) 581 { 582 BasicDLL aBasiDLL; 583 SvFactory::Init(); 584 EnableSVLook(); 585 TestWindow aWindow; 586 Execute(); 587 SvFactory::DeInit(); 588 } 589 590 591 TestApp aTestApp; 592