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