1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: mgrtest.cxx,v $
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>
44 //#include <basic.hxx>
45 //#include <sostor.hxx>
47 // Defines for ToolBox-Id's
49 #define TB_OPENSTORAGE 2
50 #define TB_SAVESTORAGE 3
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
63 #define TB_REMOVEDELX 35
70 const char* pLib1Str
= "Lib1";
71 const char* pLib2Str
= "Lib2";
72 const char* pLib3Str
= "Lib3";
75 class TestApp
: public Application
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
88 BasicManager
* pBasMgr
;
92 DECL_LINK( BasicErrorHdl
, StarBASIC
* );
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();
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
) )
121 aToolBox
.SetButtonType( BUTTON_TEXT
);
122 aToolBox
.SetLineCount( 2 );
123 aToolBox
.SetPosPixel( Point( 0, 0 ) );
124 aToolBox
.SetSelectHdl( LINK( this, TestWindow
, TBSelect
) );
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
);
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" );
195 void TestWindow::ShowInfo()
199 long nH
= GetTextSize( "X" ).Height();
202 Point
aPos( 10, aToolBox
.GetSizePixel().Height()+5 );
203 for ( USHORT nLib
= 0; nLib
< pBasMgr
->GetLibCount(); nLib
++ )
205 String
aOutStr( nLib
);
207 StarBASIC
* pL
= pBasMgr
->GetLib( nLib
);
209 aOutStr
+= pBasMgr
->GetLibName( nLib
);
212 aOutStr
+= pL
->GetName();
214 aOutStr
+= "NoLoaded";
215 aOutStr
+= ">, Storage='";
216 aOutStr
+= pBasMgr
->GetLibStorageName( nLib
);
217 aOutStr
+= "', bLoaded=";
218 aOutStr
+= (USHORT
)pBasMgr
->IsLibLoaded( nLib
);
219 DrawText( aPos
, aOutStr
);
225 void TestWindow::UpdateToolBox()
227 // View of some buttons as checked or disabled if
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
);
261 pBasMgr
= new BasicManager( new StarBASIC
);
262 pBasMgr
->SetStorageName( "d:\\mystore.svs" );
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
);
277 SvStorageRef xStorage
= new SvStorage( "d:\\mystore.svs" );
278 DBG_ASSERT( xStorage
.Is(), "Kein Storage!" );
279 pBasMgr
->Store( *xStorage
);
287 InfoBox( 0, "Organisieren..." ).Execute();
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?" );
307 InfoBox( 0, "CreateLibary fehlgeschlagen..." ).Execute();
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" );
325 InfoBox( 0, "CreateLibary fehlgeschlagen..." ).Execute();
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" );
344 InfoBox( 0, "CreateLibary fehlgeschlagen..." ).Execute();
352 SvStorageRef xStorage
= new SvStorage( "d:\\mystore.svs" );
353 if ( !pBasMgr
->AddLib( *xStorage
, pLib1Str
, FALSE
) )
362 SvStorageRef xStorage
= new SvStorage( "d:\\mystore.svs" );
363 if ( !pBasMgr
->AddLib( *xStorage
, pLib2Str
, FALSE
) )
372 // liegt in einem anderen Storage !!!
373 SvStorageRef xStorage
= new SvStorage( "d:\\mystore2.svs" );
374 if ( !pBasMgr
->AddLib( *xStorage
, pLib3Str
, FALSE
) )
382 pBasMgr
->StoreLib( nLibX
);
388 pBasMgr
->UnloadLib( nLibX
);
394 pBasMgr
->LoadLib( nLibX
);
400 pBasMgr
->RemoveLib( nLibX
, FALSE
);
406 pBasMgr
->RemoveLib( nLibX
, TRUE
);
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();
426 case TB_LIB0
: nLibX
= 0;
428 case TB_LIB1
: nLibX
= 1;
430 case TB_LIB2
: nLibX
= 2;
432 case TB_LIB3
: nLibX
= 3;
442 void TestWindow::CheckError()
446 BasicError
* pError
= pBasMgr
->GetFirstError();
451 switch ( pError
->GetErrorId() )
453 case BASERR_ID_STDLIBOPEN
:
454 aErrorStr
= "Standard-Lib konnte nicht geoffnet werden.";
456 case BASERR_ID_STDLIBSAVE
:
457 aErrorStr
= "Standard-Lib konnte nicht gespeichert werden.";
459 case BASERR_ID_LIBLOAD
:
460 aErrorStr
= "Lib konnte nicht geoffnet werden.";
462 case BASERR_ID_LIBCREATE
:
463 aErrorStr
= "Lib konnte nicht erzeugt werden.";
465 case BASERR_ID_LIBSAVE
:
466 aErrorStr
= "Lib konnte nicht gespeichert werden.";
468 case BASERR_ID_MGROPEN
:
469 aErrorStr
= "Manager konnte nicht geladen werden.";
471 case BASERR_ID_MGRSAVE
:
472 aErrorStr
= "Manager konnte nicht gespeichert werden.";
474 case BASERR_ID_UNLOADLIB
:
475 aErrorStr
= "Libary konnte nicht entladen werden.";
477 case BASERR_ID_REMOVELIB
:
478 aErrorStr
= "Libary konnte nicht entfernt werden.";
481 aErrorStr
= "Unbekannter Fehler!";
484 switch ( pError
->GetReason() )
486 case BASERR_REASON_OPENSTORAGE
:
487 aReasonStr
= "Der Storage konnte nicht geoeffnet werden";
489 case BASERR_REASON_OPENLIBSTORAGE
:
490 aReasonStr
= "Der Lib-Storage konnte nicht geoeffnet werden";
492 case BASERR_REASON_OPENMGRSTREAM
:
493 aReasonStr
= "Der Manager-Stream konnte nicht geoeffnet werden";
495 case BASERR_REASON_OPENLIBSTREAM
:
496 aReasonStr
= "Der Basic-Stream konnte nicht geoeffnet werden";
498 case BASERR_REASON_STDLIB
:
499 aReasonStr
= "STANDARD-Lib";
501 case BASERR_REASON_BASICLOADERROR
:
502 aReasonStr
= "Fehler beim Laden des Basics";
507 String
aErr( aErrorStr
);
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()) );
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() )
539 WorkWindow::KeyInput( rKEvt
);
544 void __EXPORT
TestWindow::MouseMove( const MouseEvent
& rMEvt
)
548 void __EXPORT
TestWindow::MouseButtonDown( const MouseEvent
& rMEvt
)
553 void __EXPORT
TestWindow::MouseButtonUp( const MouseEvent
& rMEvt
)
558 IMPL_LINK( TestWindow
, BasicErrorHdl
, StarBASIC
*, pBasic
)
560 String
aErrorText( pBasic
->GetErrorText() );
562 String aErrorTextPrefix
;
563 if( pBasic
->IsCompilerError() )
565 aErrorTextPrefix
= "Compilererror: ";
569 aErrorTextPrefix
= "Runtimeerror: ";
570 aErrorTextPrefix
+= pBasic
->GetErrorCode();
571 aErrorTextPrefix
+= " ";
574 InfoBox( 0, String( aErrorTextPrefix
+ aErrorText
) ).Execute();
578 void __EXPORT
TestApp::Main( void )
583 void __EXPORT
TestApp::Main( int, char*[] )