1*31598a22SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*31598a22SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*31598a22SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*31598a22SAndrew Rist  * distributed with this work for additional information
6*31598a22SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*31598a22SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*31598a22SAndrew Rist  * "License"); you may not use this file except in compliance
9*31598a22SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*31598a22SAndrew Rist  *
11*31598a22SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*31598a22SAndrew Rist  *
13*31598a22SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*31598a22SAndrew Rist  * software distributed under the License is distributed on an
15*31598a22SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*31598a22SAndrew Rist  * KIND, either express or implied.  See the License for the
17*31598a22SAndrew Rist  * specific language governing permissions and limitations
18*31598a22SAndrew Rist  * under the License.
19*31598a22SAndrew Rist  *
20*31598a22SAndrew Rist  *************************************************************/
21*31598a22SAndrew Rist 
22*31598a22SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_basctl.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <memory>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <ide_pch.hxx>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <macrodlg.hxx>
33cdf0e10cSrcweir #include <macrodlg.hrc>
34cdf0e10cSrcweir #include <basidesh.hrc>
35cdf0e10cSrcweir #include <basidesh.hxx>
36cdf0e10cSrcweir #include <baside2.hrc>		// ID's fuer Imagese
37cdf0e10cSrcweir #include <basobj.hxx>
38cdf0e10cSrcweir #include <baside3.hxx>
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #include <iderdll.hxx>
41cdf0e10cSrcweir #include <iderdll2.hxx>
42cdf0e10cSrcweir #include <iderid.hxx>
43cdf0e10cSrcweir 
44cdf0e10cSrcweir #include <moduldlg.hxx>
45cdf0e10cSrcweir #include <basic/sbx.hxx>
46cdf0e10cSrcweir 
47cdf0e10cSrcweir #include <bastypes.hxx>
48cdf0e10cSrcweir #include <sbxitem.hxx>
49cdf0e10cSrcweir #include <sfx2/minfitem.hxx>
50cdf0e10cSrcweir 
51cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SCRIPT_XLIBRYARYCONTAINER2_HPP_
52cdf0e10cSrcweir #include <com/sun/star/script/XLibraryContainer2.hpp>
53cdf0e10cSrcweir #endif
54cdf0e10cSrcweir #include <com/sun/star/document/MacroExecMode.hpp>
55cdf0e10cSrcweir 
56cdf0e10cSrcweir using namespace ::com::sun::star;
57cdf0e10cSrcweir using namespace ::com::sun::star::uno;
58cdf0e10cSrcweir 
59cdf0e10cSrcweir 
DECLARE_LIST(MacroList,SbMethod *)60cdf0e10cSrcweir DECLARE_LIST( MacroList, SbMethod* )
61cdf0e10cSrcweir 
62cdf0e10cSrcweir MacroChooser::MacroChooser( Window* pParnt, sal_Bool bCreateEntries ) :
63cdf0e10cSrcweir 		SfxModalDialog( 	pParnt,	IDEResId( RID_MACROCHOOSER ) ),
64cdf0e10cSrcweir 		aMacroNameTxt(		this,	IDEResId( RID_TXT_MACRONAME ) ),
65cdf0e10cSrcweir 		aMacroNameEdit(		this,	IDEResId( RID_ED_MACRONAME ) ),
66cdf0e10cSrcweir 		aMacroFromTxT(		this,	IDEResId( RID_TXT_MACROFROM ) ),
67cdf0e10cSrcweir 		aMacrosSaveInTxt(	this,	IDEResId( RID_TXT_SAVEMACRO ) ),
68cdf0e10cSrcweir 		aBasicBox(			this,	IDEResId( RID_CTRL_LIB ) ),
69cdf0e10cSrcweir         aMacrosInTxt(		this,	IDEResId( RID_TXT_MACROSIN ) ),
70cdf0e10cSrcweir 		aMacroBox(			this,	IDEResId( RID_CTRL_MACRO ) ),
71cdf0e10cSrcweir         aRunButton(			this,	IDEResId( RID_PB_RUN ) ),
72cdf0e10cSrcweir 		aCloseButton(	    this,	IDEResId( RID_PB_CLOSE ) ),
73cdf0e10cSrcweir 		aAssignButton(		this,	IDEResId( RID_PB_ASSIGN ) ),
74cdf0e10cSrcweir 		aEditButton(		this,	IDEResId( RID_PB_EDIT ) ),
75cdf0e10cSrcweir 		aNewDelButton(		this,	IDEResId( RID_PB_DEL ) ),
76cdf0e10cSrcweir 		aOrganizeButton(	this,	IDEResId( RID_PB_ORG ) ),
77cdf0e10cSrcweir 		aHelpButton(		this,	IDEResId( RID_PB_HELP ) ),
78cdf0e10cSrcweir 		aNewLibButton(	    this,	IDEResId( RID_PB_NEWLIB ) ),
79cdf0e10cSrcweir 		aNewModButton(	    this,	IDEResId( RID_PB_NEWMOD ) )
80cdf0e10cSrcweir {
81cdf0e10cSrcweir 	FreeResource();
82cdf0e10cSrcweir 
83cdf0e10cSrcweir 	nMode = MACROCHOOSER_ALL;
84cdf0e10cSrcweir 	bNewDelIsDel = sal_True;
85cdf0e10cSrcweir 
86cdf0e10cSrcweir 	// Der Sfx fragt den BasicManager nicht, ob modified
87cdf0e10cSrcweir 	// => Speichern anschmeissen, wenn Aenderung, aber kein Sprung in
88cdf0e10cSrcweir 	// die BasicIDE.
89cdf0e10cSrcweir 	bForceStoreBasic = sal_False;
90cdf0e10cSrcweir 
91cdf0e10cSrcweir 	aMacrosInTxtBaseStr = aMacrosInTxt.GetText();
92cdf0e10cSrcweir 
93cdf0e10cSrcweir 	aMacroBox.SetSelectionMode( SINGLE_SELECTION );
94cdf0e10cSrcweir 	aMacroBox.SetHighlightRange(); // ueber ganze Breite selektieren
95cdf0e10cSrcweir 
96cdf0e10cSrcweir 	aRunButton.SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
97cdf0e10cSrcweir 	aCloseButton.SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
98cdf0e10cSrcweir 	aAssignButton.SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
99cdf0e10cSrcweir 	aEditButton.SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
100cdf0e10cSrcweir 	aNewDelButton.SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
101cdf0e10cSrcweir 	aOrganizeButton.SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
102cdf0e10cSrcweir 
103cdf0e10cSrcweir 	// Buttons only for MACROCHOOSER_RECORDING
104cdf0e10cSrcweir 	aNewLibButton.SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
105cdf0e10cSrcweir 	aNewModButton.SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
106cdf0e10cSrcweir 	aNewLibButton.Hide();		// default
107cdf0e10cSrcweir 	aNewModButton.Hide();		// default
108cdf0e10cSrcweir 	aMacrosSaveInTxt.Hide();	// default
109cdf0e10cSrcweir 
110cdf0e10cSrcweir     aMacrosInTxt.SetStyle( WB_NOMULTILINE | WB_PATHELLIPSIS );
111cdf0e10cSrcweir 
112cdf0e10cSrcweir 	aMacroNameEdit.SetModifyHdl( LINK( this, MacroChooser, EditModifyHdl ) );
113cdf0e10cSrcweir 
114cdf0e10cSrcweir 	aBasicBox.SetSelectHdl( LINK( this, MacroChooser, BasicSelectHdl ) );
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 	aMacroBox.SetDoubleClickHdl( LINK( this, MacroChooser, MacroDoubleClickHdl ) );
117cdf0e10cSrcweir 	aMacroBox.SetSelectHdl( LINK( this, MacroChooser, MacroSelectHdl ) );
118cdf0e10cSrcweir 
119cdf0e10cSrcweir 	aBasicBox.SetMode( BROWSEMODE_MODULES );
120cdf0e10cSrcweir     aBasicBox.SetStyle( WB_TABSTOP | WB_BORDER |
121cdf0e10cSrcweir                         WB_HASLINES | WB_HASLINESATROOT |
122cdf0e10cSrcweir                         WB_HASBUTTONS | WB_HASBUTTONSATROOT |
123cdf0e10cSrcweir                         WB_HSCROLL );
124cdf0e10cSrcweir 
125cdf0e10cSrcweir     BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
126cdf0e10cSrcweir     SfxViewFrame* pViewFrame = pIDEShell ? pIDEShell->GetViewFrame() : NULL;
127cdf0e10cSrcweir 	SfxDispatcher* pDispatcher = pViewFrame ? pViewFrame->GetDispatcher() : NULL;
128cdf0e10cSrcweir     if( pDispatcher )
129cdf0e10cSrcweir 	{
130cdf0e10cSrcweir 		pDispatcher->Execute( SID_BASICIDE_STOREALLMODULESOURCES );
131cdf0e10cSrcweir 	}
132cdf0e10cSrcweir 
133cdf0e10cSrcweir 	if ( bCreateEntries )
134cdf0e10cSrcweir 		aBasicBox.ScanAllEntries();
135cdf0e10cSrcweir }
136cdf0e10cSrcweir 
~MacroChooser()137cdf0e10cSrcweir MacroChooser::~MacroChooser()
138cdf0e10cSrcweir {
139cdf0e10cSrcweir 	if ( bForceStoreBasic )
140cdf0e10cSrcweir         SFX_APP()->SaveBasicAndDialogContainer();
141cdf0e10cSrcweir }
142cdf0e10cSrcweir 
StoreMacroDescription()143cdf0e10cSrcweir void MacroChooser::StoreMacroDescription()
144cdf0e10cSrcweir {
145cdf0e10cSrcweir     BasicEntryDescriptor aDesc( aBasicBox.GetEntryDescriptor( aBasicBox.FirstSelected() ) );
146cdf0e10cSrcweir     String aMethodName;
147cdf0e10cSrcweir     SvLBoxEntry* pEntry = aMacroBox.FirstSelected();
148cdf0e10cSrcweir     if ( pEntry )
149cdf0e10cSrcweir         aMethodName = aMacroBox.GetEntryText( pEntry );
150cdf0e10cSrcweir     else
151cdf0e10cSrcweir         aMethodName = aMacroNameEdit.GetText();
152cdf0e10cSrcweir     if ( aMethodName.Len() )
153cdf0e10cSrcweir     {
154cdf0e10cSrcweir         aDesc.SetMethodName( aMethodName );
155cdf0e10cSrcweir         aDesc.SetType( OBJ_TYPE_METHOD );
156cdf0e10cSrcweir     }
157cdf0e10cSrcweir 
158cdf0e10cSrcweir     BasicIDEData* pData = IDE_DLL()->GetExtraData();
159cdf0e10cSrcweir     if ( pData )
160cdf0e10cSrcweir         pData->SetLastEntryDescriptor( aDesc );
161cdf0e10cSrcweir }
162cdf0e10cSrcweir 
RestoreMacroDescription()163cdf0e10cSrcweir void MacroChooser::RestoreMacroDescription()
164cdf0e10cSrcweir {
165cdf0e10cSrcweir     BasicEntryDescriptor aDesc;
166cdf0e10cSrcweir     BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
167cdf0e10cSrcweir     if ( pIDEShell )
168cdf0e10cSrcweir     {
169cdf0e10cSrcweir         IDEBaseWindow* pCurWin = pIDEShell->GetCurWindow();
170cdf0e10cSrcweir         if ( pCurWin )
171cdf0e10cSrcweir             aDesc = pCurWin->CreateEntryDescriptor();
172cdf0e10cSrcweir     }
173cdf0e10cSrcweir     else
174cdf0e10cSrcweir     {
175cdf0e10cSrcweir         BasicIDEData* pData = IDE_DLL()->GetExtraData();
176cdf0e10cSrcweir         if ( pData )
177cdf0e10cSrcweir             aDesc = pData->GetLastEntryDescriptor();
178cdf0e10cSrcweir     }
179cdf0e10cSrcweir 
180cdf0e10cSrcweir     aBasicBox.SetCurrentEntry( aDesc );
181cdf0e10cSrcweir 
182cdf0e10cSrcweir 	String aLastMacro( aDesc.GetMethodName() );
183cdf0e10cSrcweir 	if ( aLastMacro.Len() )
184cdf0e10cSrcweir 	{
185cdf0e10cSrcweir         // find entry in macro box
186cdf0e10cSrcweir         SvLBoxEntry* pEntry = 0;
187cdf0e10cSrcweir         sal_uLong nPos = 0;
188cdf0e10cSrcweir         SvLBoxEntry* pE = aMacroBox.GetEntry( nPos );
189cdf0e10cSrcweir         while ( pE )
190cdf0e10cSrcweir         {
191cdf0e10cSrcweir             if ( aMacroBox.GetEntryText( pE ) == aLastMacro )
192cdf0e10cSrcweir             {
193cdf0e10cSrcweir                 pEntry = pE;
194cdf0e10cSrcweir                 break;
195cdf0e10cSrcweir             }
196cdf0e10cSrcweir             pE = aMacroBox.GetEntry( ++nPos );
197cdf0e10cSrcweir         }
198cdf0e10cSrcweir 
199cdf0e10cSrcweir 		if ( pEntry )
200cdf0e10cSrcweir 			aMacroBox.SetCurEntry( pEntry );
201cdf0e10cSrcweir 		else
202cdf0e10cSrcweir 		{
203cdf0e10cSrcweir 			aMacroNameEdit.SetText( aLastMacro );
204cdf0e10cSrcweir 			aMacroNameEdit.SetSelection( Selection( 0, 0 ) );
205cdf0e10cSrcweir 		}
206cdf0e10cSrcweir 	}
207cdf0e10cSrcweir }
208cdf0e10cSrcweir 
Execute()209cdf0e10cSrcweir short __EXPORT MacroChooser::Execute()
210cdf0e10cSrcweir {
211cdf0e10cSrcweir     RestoreMacroDescription();
212cdf0e10cSrcweir     aRunButton.GrabFocus();
213cdf0e10cSrcweir 
214cdf0e10cSrcweir     // #104198 Check if "wrong" document is active
215cdf0e10cSrcweir 	SvLBoxEntry* pSelectedEntry = aBasicBox.GetCurEntry();
216cdf0e10cSrcweir     BasicEntryDescriptor aDesc( aBasicBox.GetEntryDescriptor( pSelectedEntry ) );
217cdf0e10cSrcweir     const ScriptDocument& rSelectedDoc( aDesc.GetDocument() );
218cdf0e10cSrcweir 
219cdf0e10cSrcweir     // App Basic is always ok, so only check if shell was found
220cdf0e10cSrcweir     if( rSelectedDoc.isDocument() && !rSelectedDoc.isActive() )
221cdf0e10cSrcweir     {
222cdf0e10cSrcweir         // Search for the right entry
223cdf0e10cSrcweir 	    sal_uLong nRootPos = 0;
224cdf0e10cSrcweir 	    SvLBoxEntry* pRootEntry = aBasicBox.GetEntry( nRootPos );
225cdf0e10cSrcweir 	    while( pRootEntry )
226cdf0e10cSrcweir 	    {
227cdf0e10cSrcweir             BasicEntryDescriptor aCmpDesc( aBasicBox.GetEntryDescriptor( pRootEntry ) );
228cdf0e10cSrcweir             const ScriptDocument& rCmpDoc( aCmpDesc.GetDocument() );
229cdf0e10cSrcweir             if ( rCmpDoc.isDocument() && rCmpDoc.isActive() )
230cdf0e10cSrcweir             {
231cdf0e10cSrcweir                 SvLBoxEntry* pEntry = pRootEntry;
232cdf0e10cSrcweir                 SvLBoxEntry* pLastValid = pEntry;
233cdf0e10cSrcweir 		        while ( pEntry )
234cdf0e10cSrcweir 		        {
235cdf0e10cSrcweir 			        pLastValid = pEntry;
236cdf0e10cSrcweir 			        pEntry = aBasicBox.FirstChild( pEntry );
237cdf0e10cSrcweir 		        }
238cdf0e10cSrcweir 		        if( pLastValid )
239cdf0e10cSrcweir         			aBasicBox.SetCurEntry( pLastValid );
240cdf0e10cSrcweir             }
241cdf0e10cSrcweir 		    pRootEntry = aBasicBox.GetEntry( ++nRootPos );
242cdf0e10cSrcweir 	    }
243cdf0e10cSrcweir     }
244cdf0e10cSrcweir 
245cdf0e10cSrcweir 	CheckButtons();
246cdf0e10cSrcweir 	UpdateFields();
247cdf0e10cSrcweir 
248cdf0e10cSrcweir 	if ( StarBASIC::IsRunning() )
249cdf0e10cSrcweir 		aCloseButton.GrabFocus();
250cdf0e10cSrcweir 
251cdf0e10cSrcweir 	Window* pPrevDlgParent = Application::GetDefDialogParent();
252cdf0e10cSrcweir 	Application::SetDefDialogParent( this );
253cdf0e10cSrcweir 	short nRet = ModalDialog::Execute();
254cdf0e10cSrcweir 	// #57314# Wenn die BasicIDE aktiviert wurde, dann nicht den DefModalDialogParent auf das inaktive Dokument zuruecksetzen.
255cdf0e10cSrcweir 	if ( Application::GetDefDialogParent() == this )
256cdf0e10cSrcweir 		Application::SetDefDialogParent( pPrevDlgParent );
257cdf0e10cSrcweir 	return nRet;
258cdf0e10cSrcweir }
259cdf0e10cSrcweir 
260cdf0e10cSrcweir 
EnableButton(Button & rButton,sal_Bool bEnable)261cdf0e10cSrcweir void MacroChooser::EnableButton( Button& rButton, sal_Bool bEnable )
262cdf0e10cSrcweir {
263cdf0e10cSrcweir 	if ( bEnable )
264cdf0e10cSrcweir 	{
265cdf0e10cSrcweir         if ( nMode == MACROCHOOSER_CHOOSEONLY || nMode == MACROCHOOSER_RECORDING )
266cdf0e10cSrcweir 		{
267cdf0e10cSrcweir 			// Nur der RunButton kann enabled werden
268cdf0e10cSrcweir 			if ( &rButton == &aRunButton )
269cdf0e10cSrcweir 				rButton.Enable();
270cdf0e10cSrcweir 			else
271cdf0e10cSrcweir 				rButton.Disable();
272cdf0e10cSrcweir 		}
273cdf0e10cSrcweir 		else
274cdf0e10cSrcweir 			rButton.Enable();
275cdf0e10cSrcweir 	}
276cdf0e10cSrcweir 	else
277cdf0e10cSrcweir 		rButton.Disable();
278cdf0e10cSrcweir }
279cdf0e10cSrcweir 
280cdf0e10cSrcweir 
281cdf0e10cSrcweir 
282cdf0e10cSrcweir 
GetMacro()283cdf0e10cSrcweir SbMethod* MacroChooser::GetMacro()
284cdf0e10cSrcweir {
285cdf0e10cSrcweir 	SbMethod* pMethod = 0;
286cdf0e10cSrcweir 	SbModule* pModule = aBasicBox.FindModule( aBasicBox.GetCurEntry() );
287cdf0e10cSrcweir 	if ( pModule )
288cdf0e10cSrcweir 	{
289cdf0e10cSrcweir 		SvLBoxEntry* pEntry = aMacroBox.FirstSelected();
290cdf0e10cSrcweir 		if ( pEntry )
291cdf0e10cSrcweir 		{
292cdf0e10cSrcweir 			String aMacroName( aMacroBox.GetEntryText( pEntry ) );
293cdf0e10cSrcweir 			pMethod = (SbMethod*)pModule->GetMethods()->Find( aMacroName, SbxCLASS_METHOD );
294cdf0e10cSrcweir 		}
295cdf0e10cSrcweir 	}
296cdf0e10cSrcweir 	return pMethod;
297cdf0e10cSrcweir }
298cdf0e10cSrcweir 
299cdf0e10cSrcweir 
300cdf0e10cSrcweir 
DeleteMacro()301cdf0e10cSrcweir void MacroChooser::DeleteMacro()
302cdf0e10cSrcweir {
303cdf0e10cSrcweir 	SbMethod* pMethod = GetMacro();
304cdf0e10cSrcweir 	DBG_ASSERT( pMethod, "DeleteMacro: Kein Macro !" );
305cdf0e10cSrcweir 	if ( pMethod && QueryDelMacro( pMethod->GetName(), this ) )
306cdf0e10cSrcweir 	{
307cdf0e10cSrcweir         BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
308cdf0e10cSrcweir         SfxViewFrame* pViewFrame = pIDEShell ? pIDEShell->GetViewFrame() : NULL;
309cdf0e10cSrcweir 	    SfxDispatcher* pDispatcher = pViewFrame ? pViewFrame->GetDispatcher() : NULL;
310cdf0e10cSrcweir         if( pDispatcher )
311cdf0e10cSrcweir 		{
312cdf0e10cSrcweir 			pDispatcher->Execute( SID_BASICIDE_STOREALLMODULESOURCES );
313cdf0e10cSrcweir 		}
314cdf0e10cSrcweir 
315cdf0e10cSrcweir 		// Aktuelles Doc als geaendert markieren:
316cdf0e10cSrcweir 		StarBASIC* pBasic = BasicIDE::FindBasic( pMethod );
317cdf0e10cSrcweir 		DBG_ASSERT( pBasic, "Basic?!" );
318cdf0e10cSrcweir 		BasicManager* pBasMgr = BasicIDE::FindBasicManager( pBasic );
319cdf0e10cSrcweir 		DBG_ASSERT( pBasMgr, "BasMgr?" );
320cdf0e10cSrcweir         ScriptDocument aDocument( ScriptDocument::getDocumentForBasicManager( pBasMgr ) );
321cdf0e10cSrcweir         if ( aDocument.isDocument() )    // Muss ja nicht aus einem Document kommen...
322cdf0e10cSrcweir 		{
323cdf0e10cSrcweir 			aDocument.setDocumentModified();
324cdf0e10cSrcweir             SfxBindings* pBindings = BasicIDE::GetBindingsPtr();
325cdf0e10cSrcweir             if ( pBindings )
326cdf0e10cSrcweir                 pBindings->Invalidate( SID_SAVEDOC );
327cdf0e10cSrcweir 		}
328cdf0e10cSrcweir 
329cdf0e10cSrcweir 		SbModule* pModule = pMethod->GetModule();
330cdf0e10cSrcweir 		DBG_ASSERT( pModule, "DeleteMacro: Kein Modul?!" );
331cdf0e10cSrcweir         ::rtl::OUString aSource( pModule->GetSource32() );
332cdf0e10cSrcweir 		sal_uInt16 nStart, nEnd;
333cdf0e10cSrcweir 		pMethod->GetLineRange( nStart, nEnd );
334cdf0e10cSrcweir 		pModule->GetMethods()->Remove( pMethod );
335cdf0e10cSrcweir 		CutLines( aSource, nStart-1, nEnd-nStart+1, sal_True );
336cdf0e10cSrcweir 		pModule->SetSource32( aSource );
337cdf0e10cSrcweir 
338cdf0e10cSrcweir         // update module in library
339cdf0e10cSrcweir         String aLibName = pBasic->GetName();
340cdf0e10cSrcweir 	    String aModName = pModule->GetName();
341cdf0e10cSrcweir         OSL_VERIFY( aDocument.updateModule( aLibName, aModName, aSource ) );
342cdf0e10cSrcweir 
343cdf0e10cSrcweir         SvLBoxEntry* pEntry = aMacroBox.FirstSelected();
344cdf0e10cSrcweir 		DBG_ASSERT( pEntry, "DeleteMacro: Entry ?!" );
345cdf0e10cSrcweir 		aMacroBox.GetModel()->Remove( pEntry );
346cdf0e10cSrcweir 		bForceStoreBasic = sal_True;
347cdf0e10cSrcweir 	}
348cdf0e10cSrcweir }
349cdf0e10cSrcweir 
CreateMacro()350cdf0e10cSrcweir SbMethod* MacroChooser::CreateMacro()
351cdf0e10cSrcweir {
352cdf0e10cSrcweir     SbMethod* pMethod = 0;
353cdf0e10cSrcweir     SvLBoxEntry* pCurEntry = aBasicBox.GetCurEntry();
354cdf0e10cSrcweir     BasicEntryDescriptor aDesc( aBasicBox.GetEntryDescriptor( pCurEntry ) );
355cdf0e10cSrcweir     ScriptDocument aDocument( aDesc.GetDocument() );
356cdf0e10cSrcweir     OSL_ENSURE( aDocument.isAlive(), "MacroChooser::CreateMacro: no document!" );
357cdf0e10cSrcweir     if ( !aDocument.isAlive() )
358cdf0e10cSrcweir         return NULL;
359cdf0e10cSrcweir 
360cdf0e10cSrcweir     String aLibName( aDesc.GetLibName() );
361cdf0e10cSrcweir 
362cdf0e10cSrcweir     if ( !aLibName.Len() )
363cdf0e10cSrcweir         aLibName = String::CreateFromAscii( "Standard" );
364cdf0e10cSrcweir 
365cdf0e10cSrcweir     aDocument.getOrCreateLibrary( E_SCRIPTS, aLibName );
366cdf0e10cSrcweir 
367cdf0e10cSrcweir     ::rtl::OUString aOULibName( aLibName );
368cdf0e10cSrcweir     Reference< script::XLibraryContainer > xModLibContainer( aDocument.getLibraryContainer( E_SCRIPTS ) );
369cdf0e10cSrcweir     if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) && !xModLibContainer->isLibraryLoaded( aOULibName ) )
370cdf0e10cSrcweir         xModLibContainer->loadLibrary( aOULibName );
371cdf0e10cSrcweir     Reference< script::XLibraryContainer > xDlgLibContainer( aDocument.getLibraryContainer( E_DIALOGS ) );
372cdf0e10cSrcweir     if ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aOULibName ) && !xDlgLibContainer->isLibraryLoaded( aOULibName ) )
373cdf0e10cSrcweir         xDlgLibContainer->loadLibrary( aOULibName );
374cdf0e10cSrcweir 
375cdf0e10cSrcweir     BasicManager* pBasMgr = aDocument.getBasicManager();
376cdf0e10cSrcweir     StarBASIC* pBasic = pBasMgr ? pBasMgr->GetLib( aLibName ) : 0;
377cdf0e10cSrcweir     if ( pBasic )
378cdf0e10cSrcweir     {
379cdf0e10cSrcweir         SbModule* pModule = 0;
380cdf0e10cSrcweir         String aModName( aDesc.GetName() );
381cdf0e10cSrcweir         if ( aModName.Len() )
382cdf0e10cSrcweir         {
383cdf0e10cSrcweir             // extract the module name from the string like "Sheet1 (Example1)"
384cdf0e10cSrcweir             if( aDesc.GetLibSubName().Equals( String( IDEResId( RID_STR_DOCUMENT_OBJECTS ) ) ) )
385cdf0e10cSrcweir             {
386cdf0e10cSrcweir                 sal_uInt16 nIndex = 0;
387cdf0e10cSrcweir                 aModName = aModName.GetToken( 0, ' ', nIndex );
388cdf0e10cSrcweir             }
389cdf0e10cSrcweir 	        pModule = pBasic->FindModule( aModName );
390cdf0e10cSrcweir         }
391cdf0e10cSrcweir         else if ( pBasic->GetModules()->Count() )
392cdf0e10cSrcweir 	        pModule = (SbModule*)pBasic->GetModules()->Get( 0 );
393cdf0e10cSrcweir 
394cdf0e10cSrcweir         if ( !pModule )
395cdf0e10cSrcweir         {
396cdf0e10cSrcweir 	        pModule = createModImpl( static_cast<Window*>( this ),
397cdf0e10cSrcweir 		        aDocument, aBasicBox, aLibName, aModName );
398cdf0e10cSrcweir         }
399cdf0e10cSrcweir 
400cdf0e10cSrcweir         String aSubName = aMacroNameEdit.GetText();
401cdf0e10cSrcweir         DBG_ASSERT( !pModule || !pModule->GetMethods()->Find( aSubName, SbxCLASS_METHOD ), "Macro existiert schon!" );
402cdf0e10cSrcweir         pMethod = pModule ? BasicIDE::CreateMacro( pModule, aSubName ) : NULL;
403cdf0e10cSrcweir     }
404cdf0e10cSrcweir 
405cdf0e10cSrcweir 	return pMethod;
406cdf0e10cSrcweir }
407cdf0e10cSrcweir 
SaveSetCurEntry(SvTreeListBox & rBox,SvLBoxEntry * pEntry)408cdf0e10cSrcweir void MacroChooser::SaveSetCurEntry( SvTreeListBox& rBox, SvLBoxEntry* pEntry )
409cdf0e10cSrcweir {
410cdf0e10cSrcweir 	// Durch das Highlight wird das Edit sonst platt gemacht:
411cdf0e10cSrcweir 
412cdf0e10cSrcweir 	String aSaveText( aMacroNameEdit.GetText() );
413cdf0e10cSrcweir 	Selection aCurSel( aMacroNameEdit.GetSelection() );
414cdf0e10cSrcweir 
415cdf0e10cSrcweir 	rBox.SetCurEntry( pEntry );
416cdf0e10cSrcweir 	aMacroNameEdit.SetText( aSaveText );
417cdf0e10cSrcweir 	aMacroNameEdit.SetSelection( aCurSel );
418cdf0e10cSrcweir }
419cdf0e10cSrcweir 
CheckButtons()420cdf0e10cSrcweir void MacroChooser::CheckButtons()
421cdf0e10cSrcweir {
422cdf0e10cSrcweir 	SvLBoxEntry* pCurEntry = aBasicBox.GetCurEntry();
423cdf0e10cSrcweir     BasicEntryDescriptor aDesc( aBasicBox.GetEntryDescriptor( pCurEntry ) );
424cdf0e10cSrcweir 	SvLBoxEntry* pMacroEntry = aMacroBox.FirstSelected();
425cdf0e10cSrcweir 	SbMethod* pMethod = GetMacro();
426cdf0e10cSrcweir 
427cdf0e10cSrcweir     // check, if corresponding libraries are readonly
428cdf0e10cSrcweir     sal_Bool bReadOnly = sal_False;
429cdf0e10cSrcweir     sal_uInt16 nDepth = pCurEntry ? aBasicBox.GetModel()->GetDepth( pCurEntry ) : 0;
430cdf0e10cSrcweir     if ( nDepth == 1 || nDepth == 2 )
431cdf0e10cSrcweir     {
432cdf0e10cSrcweir         ScriptDocument aDocument( aDesc.GetDocument() );
433cdf0e10cSrcweir         ::rtl::OUString aOULibName( aDesc.GetLibName() );
434cdf0e10cSrcweir         Reference< script::XLibraryContainer2 > xModLibContainer( aDocument.getLibraryContainer( E_SCRIPTS ), UNO_QUERY );
435cdf0e10cSrcweir         Reference< script::XLibraryContainer2 > xDlgLibContainer( aDocument.getLibraryContainer( E_DIALOGS ), UNO_QUERY );
436cdf0e10cSrcweir         if ( ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) && xModLibContainer->isLibraryReadOnly( aOULibName ) ) ||
437cdf0e10cSrcweir                 ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aOULibName ) && xDlgLibContainer->isLibraryReadOnly( aOULibName ) ) )
438cdf0e10cSrcweir         {
439cdf0e10cSrcweir             bReadOnly = sal_True;
440cdf0e10cSrcweir         }
441cdf0e10cSrcweir     }
442cdf0e10cSrcweir 
443cdf0e10cSrcweir     if ( nMode != MACROCHOOSER_RECORDING )
444cdf0e10cSrcweir     {
445cdf0e10cSrcweir         // Run...
446cdf0e10cSrcweir         sal_Bool bEnable = pMethod ? sal_True : sal_False;
447cdf0e10cSrcweir         if ( ( nMode != MACROCHOOSER_CHOOSEONLY ) && StarBASIC::IsRunning() )
448cdf0e10cSrcweir             bEnable = sal_False;
449cdf0e10cSrcweir         EnableButton( aRunButton, bEnable );
450cdf0e10cSrcweir     }
451cdf0e10cSrcweir 
452cdf0e10cSrcweir 	// Organisieren immer moeglich ?
453cdf0e10cSrcweir 
454cdf0e10cSrcweir 	// Assign...
455cdf0e10cSrcweir 	EnableButton( aAssignButton, pMethod ? sal_True : sal_False );
456cdf0e10cSrcweir 
457cdf0e10cSrcweir 	// Edit...
458cdf0e10cSrcweir 	EnableButton( aEditButton, pMacroEntry ? sal_True : sal_False );
459cdf0e10cSrcweir 
460cdf0e10cSrcweir 	// aOrganizeButton
461cdf0e10cSrcweir 	EnableButton( aOrganizeButton, !StarBASIC::IsRunning() && ( nMode == MACROCHOOSER_ALL ));
462cdf0e10cSrcweir 
463cdf0e10cSrcweir 	// aNewDelButton....
464cdf0e10cSrcweir     bool bProtected = aBasicBox.IsEntryProtected( pCurEntry );
465cdf0e10cSrcweir     bool bShare = ( aDesc.GetLocation() == LIBRARY_LOCATION_SHARE );
466cdf0e10cSrcweir 	EnableButton( aNewDelButton,
467cdf0e10cSrcweir 		!StarBASIC::IsRunning() && ( nMode == MACROCHOOSER_ALL ) && !bProtected && !bReadOnly && !bShare );
468cdf0e10cSrcweir 	sal_Bool bPrev = bNewDelIsDel;
469cdf0e10cSrcweir 	bNewDelIsDel = pMethod ? sal_True : sal_False;
470cdf0e10cSrcweir     if ( ( bPrev != bNewDelIsDel ) && ( nMode == MACROCHOOSER_ALL ) )
471cdf0e10cSrcweir 	{
472cdf0e10cSrcweir 		String aBtnText( bNewDelIsDel ? IDEResId( RID_STR_BTNDEL) : IDEResId( RID_STR_BTNNEW ) );
473cdf0e10cSrcweir 		aNewDelButton.SetText( aBtnText );
474cdf0e10cSrcweir 	}
475cdf0e10cSrcweir 
476cdf0e10cSrcweir     if ( nMode == MACROCHOOSER_RECORDING )
477cdf0e10cSrcweir     {
478cdf0e10cSrcweir         // save button
479cdf0e10cSrcweir         if ( !bProtected && !bReadOnly && !bShare )
480cdf0e10cSrcweir             aRunButton.Enable();
481cdf0e10cSrcweir         else
482cdf0e10cSrcweir             aRunButton.Disable();
483cdf0e10cSrcweir 
484cdf0e10cSrcweir         // new library button
485cdf0e10cSrcweir         if ( !bShare )
486cdf0e10cSrcweir             aNewLibButton.Enable();
487cdf0e10cSrcweir         else
488cdf0e10cSrcweir             aNewLibButton.Disable();
489cdf0e10cSrcweir 
490cdf0e10cSrcweir         // new module button
491cdf0e10cSrcweir         if ( !bProtected && !bReadOnly && !bShare )
492cdf0e10cSrcweir             aNewModButton.Enable();
493cdf0e10cSrcweir         else
494cdf0e10cSrcweir             aNewModButton.Disable();
495cdf0e10cSrcweir     }
496cdf0e10cSrcweir }
497cdf0e10cSrcweir 
498cdf0e10cSrcweir 
499cdf0e10cSrcweir 
IMPL_LINK_INLINE_START(MacroChooser,MacroDoubleClickHdl,SvTreeListBox *,EMPTYARG)500cdf0e10cSrcweir IMPL_LINK_INLINE_START( MacroChooser, MacroDoubleClickHdl, SvTreeListBox *, EMPTYARG )
501cdf0e10cSrcweir {
502cdf0e10cSrcweir 	StoreMacroDescription();
503cdf0e10cSrcweir     if ( nMode == MACROCHOOSER_RECORDING )
504cdf0e10cSrcweir     {
505cdf0e10cSrcweir         SbMethod* pMethod = GetMacro();
506cdf0e10cSrcweir         if ( pMethod && !QueryReplaceMacro( pMethod->GetName(), this ) )
507cdf0e10cSrcweir             return 0;
508cdf0e10cSrcweir     }
509cdf0e10cSrcweir 
510cdf0e10cSrcweir 	EndDialog( MACRO_OK_RUN );
511cdf0e10cSrcweir 	return 0;
512cdf0e10cSrcweir }
IMPL_LINK_INLINE_END(MacroChooser,MacroDoubleClickHdl,SvTreeListBox *,EMPTYARG)513cdf0e10cSrcweir IMPL_LINK_INLINE_END( MacroChooser, MacroDoubleClickHdl, SvTreeListBox *, EMPTYARG )
514cdf0e10cSrcweir 
515cdf0e10cSrcweir IMPL_LINK( MacroChooser, MacroSelectHdl, SvTreeListBox *, pBox )
516cdf0e10cSrcweir {
517cdf0e10cSrcweir 	// Wird auch gerufen, wenn Deselektiert!
518cdf0e10cSrcweir 	// 2 Funktionsaufrufe in jedem SelectHdl, nur weil Olli
519cdf0e10cSrcweir 	// keinen separatren DeselctHdl einfuehren wollte:
520cdf0e10cSrcweir 	// Also: Feststellen, ob Select oder Deselect:
521cdf0e10cSrcweir 	if ( pBox->IsSelected( pBox->GetHdlEntry() ) )
522cdf0e10cSrcweir 	{
523cdf0e10cSrcweir 		UpdateFields();
524cdf0e10cSrcweir 		CheckButtons();
525cdf0e10cSrcweir 	}
526cdf0e10cSrcweir 	return 0;
527cdf0e10cSrcweir }
528cdf0e10cSrcweir 
IMPL_LINK(MacroChooser,BasicSelectHdl,SvTreeListBox *,pBox)529cdf0e10cSrcweir IMPL_LINK( MacroChooser, BasicSelectHdl, SvTreeListBox *, pBox )
530cdf0e10cSrcweir {
531cdf0e10cSrcweir 	static String aSpaceStr = String::CreateFromAscii(" ");
532cdf0e10cSrcweir 
533cdf0e10cSrcweir 	// Wird auch gerufen, wenn Deselektiert!
534cdf0e10cSrcweir 	// 2 Funktionsaufrufe in jedem SelectHdl, nur weil Olli
535cdf0e10cSrcweir 	// keinen separatren DeselctHdl einfuehren wollte:
536cdf0e10cSrcweir 	// Also: Feststellen, ob Select oder Deselect:
537cdf0e10cSrcweir 	if ( !pBox->IsSelected( pBox->GetHdlEntry() ) )
538cdf0e10cSrcweir 		return 0;
539cdf0e10cSrcweir 
540cdf0e10cSrcweir 	SbModule* pModule = aBasicBox.FindModule( aBasicBox.GetCurEntry() );
541cdf0e10cSrcweir 
542cdf0e10cSrcweir 	aMacroBox.Clear();
543cdf0e10cSrcweir 	if ( pModule )
544cdf0e10cSrcweir 	{
545cdf0e10cSrcweir 		String aStr = aMacrosInTxtBaseStr;
546cdf0e10cSrcweir 		aStr += aSpaceStr;
547cdf0e10cSrcweir 		aStr += pModule->GetName();
548cdf0e10cSrcweir 
549cdf0e10cSrcweir 		aMacrosInTxt.SetText( aStr );
550cdf0e10cSrcweir 
551cdf0e10cSrcweir 		// Die Macros sollen in der Reihenfolge angezeigt werden,
552cdf0e10cSrcweir 		// wie sie im Modul stehen.
553cdf0e10cSrcweir 		MacroList aMacros;
554cdf0e10cSrcweir 		sal_uInt16 nMacroCount = pModule->GetMethods()->Count();
555cdf0e10cSrcweir 		sal_uInt16 nRealMacroCount = 0;
556cdf0e10cSrcweir 		sal_uInt16 iMeth;
557cdf0e10cSrcweir 		for ( iMeth = 0; iMeth  < nMacroCount; iMeth++ )
558cdf0e10cSrcweir 		{
559cdf0e10cSrcweir 			SbMethod* pMethod = (SbMethod*)pModule->GetMethods()->Get( iMeth );
560cdf0e10cSrcweir 			if( pMethod->IsHidden() )
561cdf0e10cSrcweir 				continue;
562cdf0e10cSrcweir 			++nRealMacroCount;
563cdf0e10cSrcweir 			DBG_ASSERT( pMethod, "Methode nicht gefunden! (NULL)" );
564cdf0e10cSrcweir 			sal_uLong nPos = LIST_APPEND;
565cdf0e10cSrcweir 			// Eventuell weiter vorne ?
566cdf0e10cSrcweir 			sal_uInt16 nStart, nEnd;
567cdf0e10cSrcweir 			pMethod->GetLineRange( nStart, nEnd );
568cdf0e10cSrcweir 			for ( sal_uLong n = 0; n < aMacros.Count(); n++ )
569cdf0e10cSrcweir 			{
570cdf0e10cSrcweir 				sal_uInt16 nS, nE;
571cdf0e10cSrcweir 				SbMethod* pM = aMacros.GetObject( n );
572cdf0e10cSrcweir 				DBG_ASSERT( pM, "Macro nicht in Liste ?!" );
573cdf0e10cSrcweir 				pM->GetLineRange( nS, nE );
574cdf0e10cSrcweir 				if ( nS > nStart )
575cdf0e10cSrcweir 				{
576cdf0e10cSrcweir 					nPos = n;
577cdf0e10cSrcweir 					break;
578cdf0e10cSrcweir 				}
579cdf0e10cSrcweir 			}
580cdf0e10cSrcweir 			aMacros.Insert( pMethod, nPos );
581cdf0e10cSrcweir 		}
582cdf0e10cSrcweir 
583cdf0e10cSrcweir 		aMacroBox.SetUpdateMode( sal_False );
584cdf0e10cSrcweir 		for ( iMeth = 0; iMeth < nRealMacroCount; iMeth++ )
585cdf0e10cSrcweir 			aMacroBox.InsertEntry( aMacros.GetObject( iMeth )->GetName() );
586cdf0e10cSrcweir 		aMacroBox.SetUpdateMode( sal_True );
587cdf0e10cSrcweir 
588cdf0e10cSrcweir 		if ( aMacroBox.GetEntryCount() )
589cdf0e10cSrcweir 		{
590cdf0e10cSrcweir 			SvLBoxEntry* pEntry = aMacroBox.GetEntry( 0 );
591cdf0e10cSrcweir 			DBG_ASSERT( pEntry, "Entry ?!" );
592cdf0e10cSrcweir 			aMacroBox.SetCurEntry( pEntry );
593cdf0e10cSrcweir 		}
594cdf0e10cSrcweir 	}
595cdf0e10cSrcweir 
596cdf0e10cSrcweir 	UpdateFields();
597cdf0e10cSrcweir 	CheckButtons();
598cdf0e10cSrcweir 	return 0;
599cdf0e10cSrcweir }
600cdf0e10cSrcweir 
601cdf0e10cSrcweir 
602cdf0e10cSrcweir 
IMPL_LINK(MacroChooser,EditModifyHdl,Edit *,pEdit)603cdf0e10cSrcweir IMPL_LINK( MacroChooser, EditModifyHdl, Edit *, pEdit )
604cdf0e10cSrcweir {
605cdf0e10cSrcweir 	(void)pEdit;
606cdf0e10cSrcweir 
607cdf0e10cSrcweir 	// Das Modul, in dem bei Neu das Macro landet, selektieren,
608cdf0e10cSrcweir 	// wenn BasicManager oder Lib selektiert.
609cdf0e10cSrcweir 	SvLBoxEntry* pCurEntry = aBasicBox.GetCurEntry();
610cdf0e10cSrcweir 	if ( pCurEntry )
611cdf0e10cSrcweir 	{
612cdf0e10cSrcweir 		sal_uInt16 nDepth = aBasicBox.GetModel()->GetDepth( pCurEntry );
613cdf0e10cSrcweir 		if ( ( nDepth == 1 ) && ( aBasicBox.IsEntryProtected( pCurEntry ) ) )
614cdf0e10cSrcweir 		{
615cdf0e10cSrcweir 			// Dann auf die entsprechende Std-Lib stellen...
616cdf0e10cSrcweir 			SvLBoxEntry* pManagerEntry = aBasicBox.GetModel()->GetParent( pCurEntry );
617cdf0e10cSrcweir 			pCurEntry = aBasicBox.GetModel()->FirstChild( pManagerEntry );
618cdf0e10cSrcweir 		}
619cdf0e10cSrcweir 		if ( nDepth < 2 )
620cdf0e10cSrcweir 		{
621cdf0e10cSrcweir 			SvLBoxEntry* pNewEntry = pCurEntry;
622cdf0e10cSrcweir 			while ( pCurEntry && ( nDepth < 2 ) )
623cdf0e10cSrcweir 			{
624cdf0e10cSrcweir 				pCurEntry = aBasicBox.FirstChild( pCurEntry );
625cdf0e10cSrcweir 				if ( pCurEntry )
626cdf0e10cSrcweir 				{
627cdf0e10cSrcweir 					pNewEntry = pCurEntry;
628cdf0e10cSrcweir 					nDepth = aBasicBox.GetModel()->GetDepth( pCurEntry );
629cdf0e10cSrcweir 				}
630cdf0e10cSrcweir 			}
631cdf0e10cSrcweir 			SaveSetCurEntry( aBasicBox, pNewEntry );
632cdf0e10cSrcweir 		}
633cdf0e10cSrcweir 		if ( aMacroBox.GetEntryCount() )
634cdf0e10cSrcweir 		{
635cdf0e10cSrcweir 			String aEdtText( aMacroNameEdit.GetText() );
636cdf0e10cSrcweir 			sal_Bool bFound = sal_False;
637cdf0e10cSrcweir 			for ( sal_uInt16 n = 0; n < aMacroBox.GetEntryCount(); n++ )
638cdf0e10cSrcweir 			{
639cdf0e10cSrcweir 				SvLBoxEntry* pEntry = aMacroBox.GetEntry( n );
640cdf0e10cSrcweir 				DBG_ASSERT( pEntry, "Entry ?!" );
641cdf0e10cSrcweir 				if ( aMacroBox.GetEntryText( pEntry ).CompareIgnoreCaseToAscii( aEdtText ) == COMPARE_EQUAL )
642cdf0e10cSrcweir 				{
643cdf0e10cSrcweir 					SaveSetCurEntry( aMacroBox, pEntry );
644cdf0e10cSrcweir 					bFound = sal_True;
645cdf0e10cSrcweir 					break;
646cdf0e10cSrcweir 				}
647cdf0e10cSrcweir 			}
648cdf0e10cSrcweir 			if ( !bFound )
649cdf0e10cSrcweir 			{
650cdf0e10cSrcweir 				SvLBoxEntry* pEntry = aMacroBox.FirstSelected();
651cdf0e10cSrcweir 				// Wenn es den Eintrag gibt ->Select ->Desription...
652cdf0e10cSrcweir 				if ( pEntry )
653cdf0e10cSrcweir 					aMacroBox.Select( pEntry, sal_False );
654cdf0e10cSrcweir 			}
655cdf0e10cSrcweir 		}
656cdf0e10cSrcweir 	}
657cdf0e10cSrcweir 
658cdf0e10cSrcweir 	CheckButtons();
659cdf0e10cSrcweir 	return 0;
660cdf0e10cSrcweir }
661cdf0e10cSrcweir 
662cdf0e10cSrcweir 
663cdf0e10cSrcweir 
IMPL_LINK(MacroChooser,ButtonHdl,Button *,pButton)664cdf0e10cSrcweir IMPL_LINK( MacroChooser, ButtonHdl, Button *, pButton )
665cdf0e10cSrcweir {
666cdf0e10cSrcweir 	// ausser bei New/Record wird die Description durch LoseFocus uebernommen.
667cdf0e10cSrcweir 	if ( pButton == &aRunButton )
668cdf0e10cSrcweir 	{
669cdf0e10cSrcweir 		StoreMacroDescription();
670cdf0e10cSrcweir 
671cdf0e10cSrcweir         // #116444# check security settings before macro execution
672cdf0e10cSrcweir         if ( nMode == MACROCHOOSER_ALL )
673cdf0e10cSrcweir         {
674cdf0e10cSrcweir             SbMethod* pMethod = GetMacro();
675cdf0e10cSrcweir             SbModule* pModule = pMethod ? pMethod->GetModule() : NULL;
676cdf0e10cSrcweir             StarBASIC* pBasic = pModule ? (StarBASIC*)pModule->GetParent() : NULL;
677cdf0e10cSrcweir             BasicManager* pBasMgr = pBasic ? BasicIDE::FindBasicManager( pBasic ) : NULL;
678cdf0e10cSrcweir             if ( pBasMgr )
679cdf0e10cSrcweir             {
680cdf0e10cSrcweir                 ScriptDocument aDocument( ScriptDocument::getDocumentForBasicManager( pBasMgr ) );
681cdf0e10cSrcweir                 if ( aDocument.isDocument() && !aDocument.allowMacros() )
682cdf0e10cSrcweir                 {
683cdf0e10cSrcweir                     WarningBox( this, WB_OK, String( IDEResId( RID_STR_CANNOTRUNMACRO ) ) ).Execute();
684cdf0e10cSrcweir                     return 0;
685cdf0e10cSrcweir                 }
686cdf0e10cSrcweir             }
687cdf0e10cSrcweir         }
688cdf0e10cSrcweir         else if ( nMode == MACROCHOOSER_RECORDING )
689cdf0e10cSrcweir         {
690cdf0e10cSrcweir 			sal_Bool bValid = BasicIDE::IsValidSbxName( aMacroNameEdit.GetText() );
691cdf0e10cSrcweir 			if ( !bValid )
692cdf0e10cSrcweir 			{
693cdf0e10cSrcweir 				ErrorBox( this, WB_OK | WB_DEF_OK, String( IDEResId( RID_STR_BADSBXNAME ) ) ).Execute();
694cdf0e10cSrcweir 				aMacroNameEdit.SetSelection( Selection( 0, aMacroNameEdit.GetText().Len() ) );
695cdf0e10cSrcweir 				aMacroNameEdit.GrabFocus();
696cdf0e10cSrcweir 				return 0;
697cdf0e10cSrcweir 			}
698cdf0e10cSrcweir 
699cdf0e10cSrcweir             SbMethod* pMethod = GetMacro();
700cdf0e10cSrcweir             if ( pMethod && !QueryReplaceMacro( pMethod->GetName(), this ) )
701cdf0e10cSrcweir                 return 0;
702cdf0e10cSrcweir         }
703cdf0e10cSrcweir 
704cdf0e10cSrcweir         EndDialog( MACRO_OK_RUN );
705cdf0e10cSrcweir 	}
706cdf0e10cSrcweir 	else if ( pButton == &aCloseButton )
707cdf0e10cSrcweir 	{
708cdf0e10cSrcweir         StoreMacroDescription();
709cdf0e10cSrcweir 		EndDialog( MACRO_CLOSE );
710cdf0e10cSrcweir 	}
711cdf0e10cSrcweir 	else if ( ( pButton == &aEditButton ) || ( pButton == &aNewDelButton ) )
712cdf0e10cSrcweir 	{
713cdf0e10cSrcweir         SvLBoxEntry* pCurEntry = aBasicBox.GetCurEntry();
714cdf0e10cSrcweir         BasicEntryDescriptor aDesc( aBasicBox.GetEntryDescriptor( pCurEntry ) );
715cdf0e10cSrcweir         ScriptDocument aDocument( aDesc.GetDocument() );
716cdf0e10cSrcweir         DBG_ASSERT( aDocument.isAlive(), "MacroChooser::ButtonHdl: no document, or document is dead!" );
717cdf0e10cSrcweir         if ( !aDocument.isAlive() )
718cdf0e10cSrcweir             return 0;
719cdf0e10cSrcweir         BasicManager* pBasMgr = aDocument.getBasicManager();
720cdf0e10cSrcweir         String aLib( aDesc.GetLibName() );
721cdf0e10cSrcweir         String aMod( aDesc.GetName() );
722cdf0e10cSrcweir         // extract the module name from the string like "Sheet1 (Example1)"
723cdf0e10cSrcweir         if( aDesc.GetLibSubName().Equals( String( IDEResId( RID_STR_DOCUMENT_OBJECTS ) ) ) )
724cdf0e10cSrcweir         {
725cdf0e10cSrcweir             sal_uInt16 nIndex = 0;
726cdf0e10cSrcweir             aMod = aMod.GetToken( 0, ' ', nIndex );
727cdf0e10cSrcweir         }
728cdf0e10cSrcweir         String aSub( aDesc.GetMethodName() );
729cdf0e10cSrcweir 		SfxMacroInfoItem aInfoItem( SID_BASICIDE_ARG_MACROINFO, pBasMgr, aLib, aMod, aSub, String() );
730cdf0e10cSrcweir 		if ( pButton == &aEditButton )
731cdf0e10cSrcweir 		{
732cdf0e10cSrcweir 			SvLBoxEntry* pEntry = aMacroBox.FirstSelected();
733cdf0e10cSrcweir 			if ( pEntry )
734cdf0e10cSrcweir 				aInfoItem.SetMethod( aMacroBox.GetEntryText( pEntry ) );
735cdf0e10cSrcweir 			StoreMacroDescription();
736cdf0e10cSrcweir             SfxAllItemSet aArgs( SFX_APP()->GetPool() );
737cdf0e10cSrcweir             SfxRequest aRequest( SID_BASICIDE_APPEAR, SFX_CALLMODE_SYNCHRON, aArgs );
738cdf0e10cSrcweir             SFX_APP()->ExecuteSlot( aRequest );
739cdf0e10cSrcweir 
740cdf0e10cSrcweir             BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
741cdf0e10cSrcweir             SfxViewFrame* pViewFrame = pIDEShell ? pIDEShell->GetViewFrame() : NULL;
742cdf0e10cSrcweir             SfxDispatcher* pDispatcher = pViewFrame ? pViewFrame->GetDispatcher() : NULL;
743cdf0e10cSrcweir 			if( pDispatcher )
744cdf0e10cSrcweir                 pDispatcher->Execute( SID_BASICIDE_EDITMACRO, SFX_CALLMODE_ASYNCHRON, &aInfoItem, 0L );
745cdf0e10cSrcweir 			EndDialog( MACRO_EDIT );
746cdf0e10cSrcweir 		}
747cdf0e10cSrcweir 		else
748cdf0e10cSrcweir 		{
749cdf0e10cSrcweir 			if ( bNewDelIsDel )
750cdf0e10cSrcweir 			{
751cdf0e10cSrcweir 				DeleteMacro();
752cdf0e10cSrcweir 				BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
753cdf0e10cSrcweir 				SfxViewFrame* pViewFrame = pIDEShell ? pIDEShell->GetViewFrame() : NULL;
754cdf0e10cSrcweir                 SfxDispatcher* pDispatcher = pViewFrame ? pViewFrame->GetDispatcher() : NULL;
755cdf0e10cSrcweir 				if( pDispatcher )
756cdf0e10cSrcweir 				{
757cdf0e10cSrcweir 					pDispatcher->Execute( SID_BASICIDE_UPDATEMODULESOURCE,
758cdf0e10cSrcweir 										  SFX_CALLMODE_SYNCHRON, &aInfoItem, 0L );
759cdf0e10cSrcweir 				}
760cdf0e10cSrcweir 				CheckButtons();
761cdf0e10cSrcweir 				UpdateFields();
762cdf0e10cSrcweir                 //if ( aMacroBox.GetCurEntry() )	// OV-Bug ?
763cdf0e10cSrcweir 				//	aMacroBox.Select( aMacroBox.GetCurEntry() );
764cdf0e10cSrcweir 			}
765cdf0e10cSrcweir 			else
766cdf0e10cSrcweir 			{
767cdf0e10cSrcweir 				sal_Bool bValid = BasicIDE::IsValidSbxName( aMacroNameEdit.GetText() );
768cdf0e10cSrcweir 				if ( !bValid )
769cdf0e10cSrcweir 				{
770cdf0e10cSrcweir 					ErrorBox( this, WB_OK | WB_DEF_OK, String( IDEResId( RID_STR_BADSBXNAME ) ) ).Execute();
771cdf0e10cSrcweir 					aMacroNameEdit.SetSelection( Selection( 0, aMacroNameEdit.GetText().Len() ) );
772cdf0e10cSrcweir 					aMacroNameEdit.GrabFocus();
773cdf0e10cSrcweir 					return 1;
774cdf0e10cSrcweir 				}
775cdf0e10cSrcweir 				SbMethod* pMethod = CreateMacro();
776cdf0e10cSrcweir 				if ( pMethod )
777cdf0e10cSrcweir 				{
778cdf0e10cSrcweir 					aInfoItem.SetMethod( pMethod->GetName() );
779cdf0e10cSrcweir 					aInfoItem.SetModule( pMethod->GetModule()->GetName() );
780cdf0e10cSrcweir 					aInfoItem.SetLib( pMethod->GetModule()->GetParent()->GetName() );
781cdf0e10cSrcweir                     SfxAllItemSet aArgs( SFX_APP()->GetPool() );
782cdf0e10cSrcweir                     SfxRequest aRequest( SID_BASICIDE_APPEAR, SFX_CALLMODE_SYNCHRON, aArgs );
783cdf0e10cSrcweir                     SFX_APP()->ExecuteSlot( aRequest );
784cdf0e10cSrcweir 
785cdf0e10cSrcweir                     BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
786cdf0e10cSrcweir                     SfxViewFrame* pViewFrame = pIDEShell ? pIDEShell->GetViewFrame() : NULL;
787cdf0e10cSrcweir                     SfxDispatcher* pDispatcher = pViewFrame ? pViewFrame->GetDispatcher() : NULL;
788cdf0e10cSrcweir                     if ( pDispatcher )
789cdf0e10cSrcweir                         pDispatcher->Execute( SID_BASICIDE_EDITMACRO, SFX_CALLMODE_ASYNCHRON, &aInfoItem, 0L );
790cdf0e10cSrcweir 					StoreMacroDescription();
791cdf0e10cSrcweir 					EndDialog( MACRO_NEW );
792cdf0e10cSrcweir 				}
793cdf0e10cSrcweir 			}
794cdf0e10cSrcweir 		}
795cdf0e10cSrcweir 	}
796cdf0e10cSrcweir 
797cdf0e10cSrcweir 	else if ( pButton == &aAssignButton )
798cdf0e10cSrcweir 	{
799cdf0e10cSrcweir         SvLBoxEntry* pCurEntry = aBasicBox.GetCurEntry();
800cdf0e10cSrcweir         BasicEntryDescriptor aDesc( aBasicBox.GetEntryDescriptor( pCurEntry ) );
801cdf0e10cSrcweir         ScriptDocument aDocument( aDesc.GetDocument() );
802cdf0e10cSrcweir         DBG_ASSERT( aDocument.isAlive(), "MacroChooser::ButtonHdl: no document, or document is dead!" );
803cdf0e10cSrcweir         if ( !aDocument.isAlive() )
804cdf0e10cSrcweir             return 0;
805cdf0e10cSrcweir         BasicManager* pBasMgr = aDocument.getBasicManager();
806cdf0e10cSrcweir         String aLib( aDesc.GetLibName() );
807cdf0e10cSrcweir         String aMod( aDesc.GetName() );
808cdf0e10cSrcweir 		String aSub( aMacroNameEdit.GetText() );
809cdf0e10cSrcweir 		SbMethod* pMethod = GetMacro();
810cdf0e10cSrcweir 		DBG_ASSERT( pBasMgr, "BasMgr?" );
811cdf0e10cSrcweir 		DBG_ASSERT( pMethod, "Method?" );
812cdf0e10cSrcweir 		String aComment( GetInfo( pMethod ) );
813cdf0e10cSrcweir 		SfxMacroInfoItem aItem( SID_MACROINFO, pBasMgr, aLib, aMod, aSub, aComment );
814cdf0e10cSrcweir         SfxAllItemSet Args( SFX_APP()->GetPool() );
815cdf0e10cSrcweir         SfxRequest aRequest( SID_CONFIG, SFX_CALLMODE_SYNCHRON, Args );
816cdf0e10cSrcweir         aRequest.AppendItem( aItem );
817cdf0e10cSrcweir         SFX_APP()->ExecuteSlot( aRequest );
818cdf0e10cSrcweir 	}
819cdf0e10cSrcweir 	else if ( pButton == &aNewLibButton )
820cdf0e10cSrcweir 	{
821cdf0e10cSrcweir         SvLBoxEntry* pCurEntry = aBasicBox.GetCurEntry();
822cdf0e10cSrcweir         BasicEntryDescriptor aDesc( aBasicBox.GetEntryDescriptor( pCurEntry ) );
823cdf0e10cSrcweir         ScriptDocument aDocument( aDesc.GetDocument() );
824cdf0e10cSrcweir 		createLibImpl( static_cast<Window*>( this ), aDocument, NULL, &aBasicBox );
825cdf0e10cSrcweir 	}
826cdf0e10cSrcweir 	else if ( pButton == &aNewModButton )
827cdf0e10cSrcweir 	{
828cdf0e10cSrcweir         SvLBoxEntry* pCurEntry = aBasicBox.GetCurEntry();
829cdf0e10cSrcweir         BasicEntryDescriptor aDesc( aBasicBox.GetEntryDescriptor( pCurEntry ) );
830cdf0e10cSrcweir         ScriptDocument aDocument( aDesc.GetDocument() );
831cdf0e10cSrcweir         String aLibName( aDesc.GetLibName() );
832cdf0e10cSrcweir 		String aModName;
833cdf0e10cSrcweir 		createModImpl( static_cast<Window*>( this ), aDocument,
834cdf0e10cSrcweir 			aBasicBox, aLibName, aModName, true );
835cdf0e10cSrcweir 	}
836cdf0e10cSrcweir 	else if ( pButton == &aOrganizeButton )
837cdf0e10cSrcweir 	{
838cdf0e10cSrcweir 		StoreMacroDescription();
839cdf0e10cSrcweir 
840cdf0e10cSrcweir         BasicEntryDescriptor aDesc( aBasicBox.GetEntryDescriptor( aBasicBox.FirstSelected() ) );
841cdf0e10cSrcweir 		OrganizeDialog* pDlg = new OrganizeDialog( this, 0, aDesc );
842cdf0e10cSrcweir 		sal_uInt16 nRet = pDlg->Execute();
843cdf0e10cSrcweir 		delete pDlg;
844cdf0e10cSrcweir 
845cdf0e10cSrcweir 		if ( nRet )	// Nicht einfach nur geschlossen
846cdf0e10cSrcweir 		{
847cdf0e10cSrcweir 			EndDialog( MACRO_EDIT );
848cdf0e10cSrcweir 			return 0;
849cdf0e10cSrcweir 		}
850cdf0e10cSrcweir 
851cdf0e10cSrcweir         BasicIDEShell* pIDEShell = IDE_DLL()->GetShell();
852cdf0e10cSrcweir         if ( pIDEShell && pIDEShell->IsAppBasicModified() )
853cdf0e10cSrcweir 			bForceStoreBasic = sal_True;
854cdf0e10cSrcweir 
855cdf0e10cSrcweir         aBasicBox.UpdateEntries();
856cdf0e10cSrcweir 	}
857cdf0e10cSrcweir 	return 0;
858cdf0e10cSrcweir }
859cdf0e10cSrcweir 
860cdf0e10cSrcweir 
861cdf0e10cSrcweir 
UpdateFields()862cdf0e10cSrcweir void MacroChooser::UpdateFields()
863cdf0e10cSrcweir {
864cdf0e10cSrcweir 	SvLBoxEntry*	pMacroEntry = aMacroBox.GetCurEntry();
865cdf0e10cSrcweir 	String			aEmptyStr;
866cdf0e10cSrcweir 
867cdf0e10cSrcweir 	aMacroNameEdit.SetText( aEmptyStr );
868cdf0e10cSrcweir 	if ( pMacroEntry )
869cdf0e10cSrcweir 		aMacroNameEdit.SetText( aMacroBox.GetEntryText( pMacroEntry ) );
870cdf0e10cSrcweir }
871cdf0e10cSrcweir 
SetMode(sal_uInt16 nM)872cdf0e10cSrcweir void MacroChooser::SetMode( sal_uInt16 nM )
873cdf0e10cSrcweir {
874cdf0e10cSrcweir 	nMode = nM;
875cdf0e10cSrcweir 	if ( nMode == MACROCHOOSER_ALL )
876cdf0e10cSrcweir 	{
877cdf0e10cSrcweir 		aRunButton.SetText( String( IDEResId( RID_STR_RUN ) ) );
878cdf0e10cSrcweir 		EnableButton( aNewDelButton, sal_True );
879cdf0e10cSrcweir 		EnableButton( aOrganizeButton, sal_True );
880cdf0e10cSrcweir 	}
881cdf0e10cSrcweir 	else if ( nMode == MACROCHOOSER_CHOOSEONLY )
882cdf0e10cSrcweir 	{
883cdf0e10cSrcweir 		aRunButton.SetText( String( IDEResId( RID_STR_CHOOSE ) ) );
884cdf0e10cSrcweir 		EnableButton( aNewDelButton, sal_False );
885cdf0e10cSrcweir 		EnableButton( aOrganizeButton, sal_False );
886cdf0e10cSrcweir 	}
887cdf0e10cSrcweir     else if ( nMode == MACROCHOOSER_RECORDING )
888cdf0e10cSrcweir 	{
889cdf0e10cSrcweir         aRunButton.SetText( String( IDEResId( RID_STR_RECORD ) ) );
890cdf0e10cSrcweir 		EnableButton( aNewDelButton, sal_False );
891cdf0e10cSrcweir 		EnableButton( aOrganizeButton, sal_False );
892cdf0e10cSrcweir 
893cdf0e10cSrcweir 		aAssignButton.Hide();
894cdf0e10cSrcweir 		aEditButton.Hide();
895cdf0e10cSrcweir 		aNewDelButton.Hide();
896cdf0e10cSrcweir 		aOrganizeButton.Hide();
897cdf0e10cSrcweir 		aMacroFromTxT.Hide();
898cdf0e10cSrcweir 
899cdf0e10cSrcweir 		aNewLibButton.Show();
900cdf0e10cSrcweir 		aNewModButton.Show();
901cdf0e10cSrcweir 		aMacrosSaveInTxt.Show();
902cdf0e10cSrcweir 
903cdf0e10cSrcweir 		Point aHelpPos = aHelpButton.GetPosPixel();
904cdf0e10cSrcweir 		Point aHelpPosLogic = PixelToLogic( aHelpPos, MapMode(MAP_APPFONT) );
905cdf0e10cSrcweir 		aHelpPosLogic.Y() -= 34;
906cdf0e10cSrcweir 		aHelpPos = LogicToPixel( aHelpPosLogic, MapMode(MAP_APPFONT) );
907cdf0e10cSrcweir 		aHelpButton.SetPosPixel( aHelpPos );
908cdf0e10cSrcweir 	}
909cdf0e10cSrcweir 	CheckButtons();
910cdf0e10cSrcweir }
911cdf0e10cSrcweir 
GetInfo(SbxVariable * pVar)912cdf0e10cSrcweir String MacroChooser::GetInfo( SbxVariable* pVar )
913cdf0e10cSrcweir {
914cdf0e10cSrcweir 	String aComment;
915cdf0e10cSrcweir 	SbxInfoRef xInfo = pVar->GetInfo();
916cdf0e10cSrcweir 	if ( xInfo.Is() )
917cdf0e10cSrcweir 		aComment = xInfo->GetComment();
918cdf0e10cSrcweir 	return aComment;
919cdf0e10cSrcweir }
920cdf0e10cSrcweir 
921