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