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_basctl.hxx"
30 
31 #include "docsignature.hxx"
32 
33 #define SI_NOCONTROL
34 #define SI_NOSBXCONTROLS
35 
36 #include <ide_pch.hxx>
37 #include <basic/sbx.hxx>
38 #include "basicrenderable.hxx"
39 
40 #include <com/sun/star/frame/XTitle.hpp>
41 
42 #include <vcl/sound.hxx>
43 #include <basidesh.hxx>
44 #include <basidesh.hrc>
45 #include <baside2.hxx>
46 #include <basdoc.hxx>
47 #include <basobj.hxx>
48 #include <svtools/texteng.hxx>
49 #include <svtools/textview.hxx>
50 #include <svtools/xtextedt.hxx>
51 #include <tools/diagnose_ex.h>
52 #include <sfx2/sfxdefs.hxx>
53 #include <sfx2/signaturestate.hxx>
54 #include <com/sun/star/container/XNameContainer.hpp>
55 #include <com/sun/star/container/XNamed.hpp>
56 #include <com/sun/star/lang/XServiceInfo.hpp>
57 
58 using namespace ::com::sun::star;
59 using namespace ::com::sun::star::uno;
60 namespace css = ::com::sun::star;
61 
62 IMPL_LINK_INLINE_START( BasicIDEShell, ObjectDialogCancelHdl, ObjectCatalog *, EMPTYARG )
63 {
64 	ShowObjectDialog( sal_False, sal_True );
65 	return 0;
66 }
67 IMPL_LINK_INLINE_END( BasicIDEShell, ObjectDialogCancelHdl, ObjectCatalog *, EMPTYARG )
68 
69 /*
70 IMPL_LINK( BasicIDEShell, ObjectDialogInsertHdl, ObjectCatalog *, pObjCat )
71 {
72 	if ( !pCurWin )
73 		return 0;
74 
75 	if ( pCurWin->IsA( TYPE( ModulWindow ) ) )
76 	{
77 		ModulWindow* pEditWin = (ModulWindow*)pCurWin;
78 		pEditWin->InsertFromObjectCatalog( pObjCat );
79 	}
80 	else
81 		Sound::Beep();
82 
83 	return 0;
84 }
85 */
86 
87 Reference< view::XRenderable > BasicIDEShell::GetRenderable()
88 {
89     return Reference< view::XRenderable >( new basicide::BasicRenderable( pCurWin ) );
90 }
91 
92 #if 0
93 sal_uInt16 __EXPORT BasicIDEShell::Print( SfxProgress &rProgress, sal_Bool bIsAPI, PrintDialog *pPrintDialog )
94 {
95 	if ( pCurWin )
96 	{
97 		SfxPrinter* pPrinter = GetPrinter( sal_True );
98 		if ( pPrinter )
99 		{
100 			SfxViewShell::Print( rProgress, bIsAPI, pPrintDialog );
101 			pCurWin->PrintData( pPrinter );
102 		}
103 	}
104 	return 0;
105 }
106 #endif
107 
108 sal_Bool BasicIDEShell::HasSelection( sal_Bool /* bText */ ) const
109 {
110 	sal_Bool bSel = sal_False;
111 	if ( pCurWin && pCurWin->ISA( ModulWindow ) )
112 	{
113 		TextView* pEditView = ((ModulWindow*)pCurWin)->GetEditView();
114 		if ( pEditView && pEditView->HasSelection() )
115 			bSel = sal_True;
116 	}
117 	return bSel;
118 }
119 
120 String BasicIDEShell::GetSelectionText( sal_Bool bWholeWord )
121 {
122 	String aText;
123 	if ( pCurWin && pCurWin->ISA( ModulWindow ) )
124 	{
125 		TextView* pEditView = ((ModulWindow*)pCurWin)->GetEditView();
126 		if ( pEditView )
127 		{
128 			if ( bWholeWord && !pEditView->HasSelection() )
129 			{
130 				// String aStrCurrentDelimiters = pEngine->GetWordDelimiters();
131 				// pEngine->SetWordDelimiters( " .,;\"'" );
132 				aText = pEditView->GetTextEngine()->GetWord( pEditView->GetSelection().GetEnd() );
133 				// pEngine->SetWordDelimiters( aStrCurrentDelimiters );
134 			}
135 			else
136 			{
137 				TextSelection aSel = pEditView->GetSelection();
138 				if ( !bWholeWord || ( aSel.GetStart().GetPara() == aSel.GetEnd().GetPara() ) )
139 					aText = pEditView->GetSelected();
140 			}
141 		}
142 	}
143 	return aText;
144 }
145 
146 SfxPrinter* __EXPORT BasicIDEShell::GetPrinter( sal_Bool bCreate )
147 {
148 	if ( pCurWin ) // && pCurWin->ISA( ModulWindow ) )
149 	{
150 		BasicDocShell* pDocShell = (BasicDocShell*)GetViewFrame()->GetObjectShell();
151 		DBG_ASSERT( pDocShell, "DocShell ?!" );
152 		return pDocShell->GetPrinter( bCreate );
153 	}
154 	return 0;
155 }
156 
157 sal_uInt16 __EXPORT BasicIDEShell::SetPrinter( SfxPrinter *pNewPrinter, sal_uInt16 nDiffFlags, bool )
158 {
159 	(void)nDiffFlags;
160 	BasicDocShell* pDocShell = (BasicDocShell*)GetViewFrame()->GetObjectShell();
161 	DBG_ASSERT( pDocShell, "DocShell ?!" );
162 	pDocShell->SetPrinter( pNewPrinter );
163 	return 0;
164 }
165 
166 void BasicIDEShell::SetMDITitle()
167 {
168 	String aTitle;
169 
170     if ( m_aCurLibName.Len() )
171     {
172         LibraryLocation eLocation = m_aCurDocument.getLibraryLocation( m_aCurLibName );
173         aTitle = m_aCurDocument.getTitle( eLocation );
174         aTitle += '.';
175         aTitle += m_aCurLibName;
176 	}
177     else
178     {
179         aTitle = String( IDEResId( RID_STR_ALL ) );
180     }
181 
182     ::basctl::DocumentSignature aCurSignature( m_aCurDocument );
183     if ( aCurSignature.getScriptingSignatureState() == SIGNATURESTATE_SIGNATURES_OK )
184     {
185         aTitle += String::CreateFromAscii( " " );
186         aTitle += String( IDEResId( RID_STR_SIGNED ) );
187         aTitle += String::CreateFromAscii( " " );
188     }
189 
190     SfxViewFrame* pViewFrame = GetViewFrame();
191     if ( pViewFrame )
192     {
193         SfxObjectShell* pShell = pViewFrame->GetObjectShell();
194         if ( pShell && aTitle != pShell->GetTitle( SFX_TITLE_CAPTION ) )
195         {
196             pShell->SetTitle( aTitle );
197             pShell->SetModified( sal_False );
198         }
199 
200         css::uno::Reference< css::frame::XController > xController = GetController ();
201         css::uno::Reference< css::frame::XTitle >      xTitle      (xController, css::uno::UNO_QUERY);
202         if (xTitle.is ())
203             xTitle->setTitle (aTitle);
204     }
205 }
206 
207 void BasicIDEShell::DestroyModulWindowLayout()
208 {
209 	delete pModulLayout;
210 	pModulLayout = 0;
211 }
212 
213 
214 void BasicIDEShell::UpdateModulWindowLayout( bool bBasicStopped )
215 {
216 	if ( pModulLayout )
217 	{
218 		pModulLayout->GetStackWindow().UpdateCalls();
219 		pModulLayout->GetWatchWindow().UpdateWatches( bBasicStopped );
220 	}
221 }
222 
223 void BasicIDEShell::CreateModulWindowLayout()
224 {
225 	pModulLayout = new ModulWindowLayout( &GetViewFrame()->GetWindow() );
226 }
227 
228 ModulWindow* BasicIDEShell::CreateBasWin( const ScriptDocument& rDocument, const String& rLibName, const String& rModName )
229 {
230 	bCreatingWindow = sal_True;
231 
232 	sal_uLong nKey = 0;
233 	ModulWindow* pWin = 0;
234 
235     String aLibName( rLibName );
236     String aModName( rModName );
237 
238     if ( !aLibName.Len() )
239         aLibName = String::CreateFromAscii( "Standard" );
240 
241     uno::Reference< container::XNameContainer > xLib = rDocument.getOrCreateLibrary( E_SCRIPTS, aLibName );
242 
243 	if ( !aModName.Len() )
244         aModName = rDocument.createObjectName( E_SCRIPTS, aLibName );
245 
246 	// Vielleicht gibt es ein suspendiertes?
247 	pWin = FindBasWin( rDocument, aLibName, aModName, sal_False, sal_True );
248 
249 	if ( !pWin )
250 	{
251 		::rtl::OUString aModule;
252         bool bSuccess = false;
253         if ( rDocument.hasModule( aLibName, aModName ) )
254             bSuccess = rDocument.getModule( aLibName, aModName, aModule );
255 		else
256             bSuccess = rDocument.createModule( aLibName, aModName, sal_True, aModule );
257 
258         if ( bSuccess )
259         {
260 			pWin = FindBasWin( rDocument, aLibName, aModName, sal_False, sal_True );
261 			if( !pWin )
262 			{
263 				// new module window
264 				pWin = new ModulWindow( pModulLayout, rDocument, aLibName, aModName, aModule );
265 				nKey = InsertWindowInTable( pWin );
266 			}
267 			else // we've gotten called recursively ( via listener from createModule above ), get outta here
268 				return pWin;
269 		}
270 	}
271     else
272 	{
273 		pWin->SetStatus( pWin->GetStatus() & ~BASWIN_SUSPENDED );
274 		IDEBaseWindow* pTmp = aIDEWindowTable.First();
275 		while ( pTmp && !nKey )
276 		{
277 			if ( pTmp == pWin )
278 				nKey = aIDEWindowTable.GetCurKey();
279 			pTmp = aIDEWindowTable.Next();
280 		}
281 		DBG_ASSERT( nKey, "CreateBasWin: Kein Key- Fenster nicht gefunden!" );
282 	}
283 	if( nKey && xLib.is() && rDocument.isInVBAMode() )
284 	{
285 		// display a nice friendly name in the ObjectModule tab,
286 		// combining the objectname and module name, e.g. Sheet1 ( Financials )
287 		String sObjName;
288 		ModuleInfoHelper::getObjectName( xLib, rModName, sObjName );
289 		if( sObjName.Len() )
290 		{
291 			aModName.AppendAscii(" (").Append(sObjName).AppendAscii(")");
292 		}
293 	}
294 	pTabBar->InsertPage( (sal_uInt16)nKey, aModName );
295 	pTabBar->Sort();
296 	pWin->GrabScrollBars( &aHScrollBar, &aVScrollBar );
297 	if ( !pCurWin )
298 		SetCurWindow( pWin, sal_False, sal_False );
299 
300 	bCreatingWindow = sal_False;
301 	return pWin;
302 }
303 
304 ModulWindow* BasicIDEShell::FindBasWin( const ScriptDocument& rDocument, const String& rLibName, const String& rModName, sal_Bool bCreateIfNotExist, sal_Bool bFindSuspended )
305 {
306 	ModulWindow* pModWin = 0;
307 	IDEBaseWindow* pWin = aIDEWindowTable.First();
308 	while ( pWin && !pModWin )
309 	{
310 		if ( ( !pWin->IsSuspended() || bFindSuspended ) && pWin->IsA( TYPE( ModulWindow ) ) )
311 		{
312 			if ( !rLibName.Len() )	// nur irgendeins finden...
313 				pModWin = (ModulWindow*)pWin;
314             else if ( pWin->IsDocument( rDocument ) && pWin->GetLibName() == rLibName && pWin->GetName() == rModName )
315 				pModWin = (ModulWindow*)pWin;
316 		}
317 		pWin = aIDEWindowTable.Next();
318 	}
319 	if ( !pModWin && bCreateIfNotExist )
320         pModWin = CreateBasWin( rDocument, rLibName, rModName );
321 
322 	return pModWin;
323 }
324 
325 void __EXPORT BasicIDEShell::Move()
326 {
327 	if ( pCurWin && pCurWin->ISA( ModulWindow ) )
328 		((ModulWindow*)pCurWin)->FrameWindowMoved();
329 }
330 
331 void __EXPORT BasicIDEShell::ShowCursor( FASTBOOL bOn )
332 {
333 	if ( pCurWin && pCurWin->ISA( ModulWindow ) )
334 		((ModulWindow*)pCurWin)->ShowCursor( (sal_Bool)bOn );
335 }
336 
337 // Hack for #101048
338 sal_Int32 getBasicIDEShellCount( void );
339 
340 // Nur wenn Basicfenster oben:
341 void __EXPORT BasicIDEShell::ExecuteBasic( SfxRequest& rReq )
342 {
343 	if ( !pCurWin || !pCurWin->IsA( TYPE( ModulWindow ) ) )
344 		return;
345 
346 	pCurWin->ExecuteCommand( rReq );
347     sal_Int32 nCount = getBasicIDEShellCount();
348     if( nCount )
349 	    CheckWindows();
350 }
351 
352