Update ooo320-m1
[ooovba.git] / basic / source / app / appbased.cxx
blob657b44e55aa5ac90cb834180f37359c1ce72cab0
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: appbased.cxx,v $
10 * $Revision: 1.13.20.1 $
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"
34 #ifndef _MSGBOX_HXX //autogen
35 #include <vcl/msgbox.hxx>
36 #endif
37 #include <basic/sbx.hxx>
38 #include <svtools/texteng.hxx>
39 #include <svtools/textview.hxx>
40 #include <basic/sbmeth.hxx>
41 #include <svtools/stringtransfer.hxx>
43 #ifndef _BASIC_TTRESHLP_HXX
44 #include <basic/ttstrhlp.hxx>
45 #endif
47 #include "basic.hrc"
48 #include "status.hxx"
49 #include "appbased.hxx"
50 #include "brkpnts.hxx"
51 #include <basic/testtool.hxx> // defines for Syntaxhighlighting
52 #include "basrid.hxx"
55 TYPEINIT1(AppBasEd,AppEdit);
56 AppBasEd::AppBasEd( BasicFrame* pParent, SbModule* p )
57 : AppEdit( pParent )
58 , pBreakpoints( NULL )
60 pBreakpoints = new BreakpointWindow( this );
61 pBreakpoints->SetFont( ((TextEdit*)pDataEdit)->GetTextEditImp().pTextEngine->GetFont() );
63 pBreakpoints->Show();
66 ((TextEdit*)pDataEdit)->GetTextEditImp().pTextView->SetAutoIndentMode( TRUE );
67 ((TextEdit*)pDataEdit)->GetTextEditImp().pTextEngine->SetMaxTextLen( STRING_MAXLEN );
68 // ((TextEdit*)pDataEdit)->GetTextEditImp().pTextEngine->SetWordDelimiters( CUniString(" ,.;:(){}[]\"'+-*/<>^\\") );
69 ((TextEdit*)pDataEdit)->GetTextEditImp().SyntaxHighlight( TRUE );
70 ((TextEdit*)pDataEdit)->SaveAsUTF8( TRUE );
72 String aEmpty;
74 pMod = p;
75 if( !pMod )
77 String aModName = *pNoName;
78 aModName += String::CreateFromInt32( nCount );
79 pMod = pFrame->Basic().MakeModule( aModName, aEmpty );
81 bCompiled = pMod->IsCompiled();
83 pBreakpoints->SetModule( pMod );
85 // Define icon:
86 // pIcon = new Icon( ResId( RID_WORKICON ) );
87 // if( pIcon ) SetIcon( *pIcon );
89 SetText( pMod->GetName() );
90 pDataEdit->SetText( pMod->GetSource() );
92 // If a module was given, load the source from harddisk
93 if ( p )
94 LoadSource();
96 // Dispatch event AFTER loading the sourcecode
97 ((TextEdit*)pDataEdit)->SetBreakpointWindow( pBreakpoints );
99 // Touch compile flag
100 pDataEdit->SetModifyHdl( LINK( this, AppBasEd, EditChange ) );
104 AppBasEd::~AppBasEd()
106 pBreakpoints->SaveBreakpoints( GetText() );
107 delete pBreakpoints;
108 pMod->SetName( CUniString("--").Append( pMod->GetName() ) );
111 void AppBasEd::Notify( SfxBroadcaster&, const SfxHint& rHint )
113 const SfxSimpleHint* p = PTR_CAST(SfxSimpleHint,&rHint);
114 if( p )
116 ULONG nHintId = p->GetId();
117 if( nHintId == SBX_HINT_LANGUAGE_EXTENSION_LOADED )
119 ((TextEdit*)pDataEdit)->GetTextEditImp().InvalidateSyntaxHighlight();
124 FileType AppBasEd::GetFileType()
126 return FT_BASIC_SOURCE;
129 IMPL_LINK_INLINE_START( AppBasEd, EditChange, void *, p )
131 (void) p; /* avoid warning about unused parameter */
132 bCompiled = FALSE;
133 return TRUE;
135 IMPL_LINK_INLINE_END( AppBasEd, EditChange, void *, p )
137 // Set up the menu
138 long AppBasEd::InitMenu( Menu* pMenu )
140 AppEdit::InitMenu (pMenu );
141 BOOL bRunning = pFrame->Basic().IsRunning();
142 pMenu->EnableItem( RID_RUNCOMPILE, !bCompiled && !bRunning );
143 return TRUE;
146 long AppBasEd::DeInitMenu( Menu* pMenu )
148 AppEdit::DeInitMenu (pMenu );
149 pMenu->EnableItem( RID_RUNCOMPILE );
150 return TRUE;
153 // Menu Handler
154 void AppBasEd::Command( const CommandEvent& rCEvt )
156 switch( rCEvt.GetCommand() ) {
157 case RID_TOGLEBRKPNT:
158 ((TextEdit*)pDataEdit)->GetBreakpointWindow()->ToggleBreakpoint( pDataEdit->GetLineNr() );
159 break;
160 default:
161 AppEdit::Command( rCEvt );
165 void AppBasEd::Resize()
167 if( pDataEdit )
169 AppEdit::Resize();
171 // Insert breakpoint window
172 Size aEditSize = pDataEdit->GetSizePixel();
173 Point aEditPos = pDataEdit->GetPosPixel();
175 pBreakpoints->SetPosPixel( aEditPos );
177 aEditPos.X() += BREAKPOINTSWIDTH;
178 pDataEdit->SetPosPixel( aEditPos );
179 aEditSize.Width() -= BREAKPOINTSWIDTH;
180 pDataEdit->SetSizePixel( aEditSize );
182 aEditSize.Width() = BREAKPOINTSWIDTH;
183 pBreakpoints->SetSizePixel( aEditSize );
187 void AppBasEd::PostLoad()
189 pMod->SetName( GetText() );
190 pMod->Clear();
191 pMod->SetSource( pDataEdit->GetText() );
192 bCompiled = FALSE; // because the code might have changed in the meantime
193 AppEdit::PostLoad();
195 pBreakpoints->LoadBreakpoints( GetText() );
198 USHORT AppBasEd::ImplSave()
200 pBreakpoints->SaveBreakpoints( GetText() );
201 return AppEdit::ImplSave();
204 void AppBasEd::Reload()
206 TextSelection aSelMemo = pDataEdit->GetSelection();
207 LoadSource();
208 pDataEdit->SetSelection( aSelMemo );
211 // Reload source code file after change
212 void AppBasEd::LoadSource()
214 BOOL bErr;
216 // if( pDataEdit->GetText().Len() != 0 ) return;
217 String aName = pMod->GetName();
218 bErr = !pDataEdit->Load( aName );
219 pBreakpoints->LoadBreakpoints( GetText() );
220 if( bErr )
221 ErrorBox( this, SttResId( IDS_READERROR ) ).Execute();
222 else
223 UpdateFileInfo( HAS_BEEN_LOADED );
224 bCompiled = FALSE; // because the code might have changed in the meantime
227 // Save as (new name)
228 void AppBasEd::PostSaveAs()
230 pMod->SetName( GetText() );
231 AppEdit::PostSaveAs();
234 // Compile
235 BOOL AppBasEd::Compile()
237 if( !pDataEdit->HasText() || bCompiled )
238 return TRUE;
239 pMod->SetSource( pDataEdit->GetText() );
240 BOOL bRes = FALSE;
241 if( pFrame->Basic().Compile( pMod ) )
243 bRes = TRUE;
244 if( pFrame->bDisas )
245 Disassemble();
246 TextSelection aSel( pDataEdit->GetSelection() );
247 String aString;
248 pFrame->pStatus->Message( aString );
249 if( aSel.HasRange() )
250 aSel.GetStart() = aSel.GetEnd(), pDataEdit->SetSelection( aSel );
252 pBreakpoints->SetBPsInModule();
254 else
256 BasicError* pErr = pFrame->Basic().aErrors.First();
257 if( pErr ) pErr->Show();
259 return bCompiled = bRes;
262 void AppBasEd::Disassemble()
264 String aText;
265 if( pFrame->Basic().Disassemble( pMod, aText ) )
266 ::svt::OStringTransfer::CopyString( aText, this );
269 void AppBasEd::Run()
271 pFrame->Basic().Reset();
272 SbxArray* pAllModules = pFrame->Basic().GetModules();
273 for (USHORT i = 0; i < pAllModules->Count(); i++)
275 if ( (pAllModules->Get(i)->GetName()).Copy(0,2).CompareToAscii( "--" ) == COMPARE_EQUAL )
277 // Little hack to get around basic
278 SbxVariableRef pRMod = pAllModules->Get(i);
279 pFrame->Basic().Remove(pRMod);
280 i--;
284 SbMethod* pMain = (SbMethod*) pMod->Find( CUniString("Main"), SbxCLASS_METHOD );
285 if( pMain )
287 pMain->SetDebugFlags( pFrame->nFlags );
288 // Triggers a call!
289 pFrame->SetAppMode( String( SttResId( IDS_APPMODE_RUN ) ) );
290 pMain->Run();
291 if (aBasicApp.pFrame)
293 BasicError* pErr = aBasicApp.pFrame->Basic().aErrors.First();
294 if( pErr )
295 pErr->Show();
296 aBasicApp.pFrame->SetAppMode( String() );
298 pMain->SetDebugFlags( 0 );