LCOV - code coverage report
Current view: top level - libreoffice/basctl/source/basicide - macrodlg.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 467 0.0 %
Date: 2012-12-17 Functions: 0 25 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <memory>
      21             : #include <vcl/msgbox.hxx>
      22             : 
      23             : #include <macrodlg.hxx>
      24             : #include <macrodlg.hrc>
      25             : #include <basidesh.hrc>
      26             : #include <basidesh.hxx>
      27             : #include <baside2.hrc>      // ID's for Images
      28             : #include <basobj.hxx>
      29             : #include <baside3.hxx>
      30             : 
      31             : #include <iderdll.hxx>
      32             : #include <iderdll2.hxx>
      33             : #include <iderid.hxx>
      34             : 
      35             : #include <moduldlg.hxx>
      36             : #include <basic/basmgr.hxx>
      37             : #include <basic/sbmeth.hxx>
      38             : #include <basic/sbmod.hxx>
      39             : #include <basic/sbx.hxx>
      40             : 
      41             : #include <bastypes.hxx>
      42             : #include <sbxitem.hxx>
      43             : #include <sfx2/app.hxx>
      44             : #include <sfx2/dispatch.hxx>
      45             : #include <sfx2/minfitem.hxx>
      46             : #include <sfx2/request.hxx>
      47             : 
      48             : #include <com/sun/star/script/XLibraryContainer2.hpp>
      49             : #include <com/sun/star/document/MacroExecMode.hpp>
      50             : 
      51             : #include <map>
      52             : 
      53             : namespace basctl
      54             : {
      55             : 
      56             : using ::std::map;
      57             : using ::std::pair;
      58             : 
      59             : using namespace ::com::sun::star;
      60             : using namespace ::com::sun::star::uno;
      61             : 
      62           0 : MacroChooser::MacroChooser( Window* pParnt, bool bCreateEntries )
      63             :     : SfxModalDialog(pParnt, "BasicMacroDialog", "modules/BasicIDE/ui/basicmacrodialog.ui")
      64             :     , bNewDelIsDel(true)
      65             :     // the Sfx doesn't aske the BasicManger whether modified or not
      66             :     // => start saving in case of a change without a into the BasicIDE.
      67             :     , bForceStoreBasic(false)
      68           0 :     , nMode(All)
      69             : {
      70           0 :     get(m_pMacroNameEdit, "macronameedit");
      71           0 :     get(m_pMacroFromTxT, "macrofromft");
      72           0 :     get(m_pMacrosSaveInTxt, "macrotoft");
      73           0 :     get(m_pBasicBox, "libraries");
      74           0 :     get(m_pMacrosInTxt, "existingmacrosft");
      75           0 :     m_aMacrosInTxtBaseStr = m_pMacrosInTxt->GetText();
      76           0 :     get(m_pMacroBox, "macros");
      77           0 :     get(m_pRunButton, "run");
      78           0 :     get(m_pCloseButton, "close");
      79           0 :     get(m_pAssignButton, "assign");
      80           0 :     get(m_pEditButton, "edit");
      81           0 :     get(m_pDelButton, "delete");
      82           0 :     get(m_pOrganizeButton, "organize");
      83           0 :     get(m_pNewLibButton, "newlibrary");
      84           0 :     get(m_pNewModButton, "newmodule");
      85             : 
      86           0 :     m_pMacroBox->SetSelectionMode( SINGLE_SELECTION );
      87           0 :     m_pMacroBox->SetHighlightRange(); // select over the whole width
      88             : 
      89           0 :     m_pRunButton->SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
      90           0 :     m_pCloseButton->SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
      91           0 :     m_pAssignButton->SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
      92           0 :     m_pEditButton->SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
      93           0 :     m_pDelButton->SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
      94           0 :     m_pOrganizeButton->SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
      95             : 
      96             :     // Buttons only for MacroChooser::Recording
      97           0 :     m_pNewLibButton->SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
      98           0 :     m_pNewModButton->SetClickHdl( LINK( this, MacroChooser, ButtonHdl ) );
      99           0 :     m_pNewLibButton->Hide();       // default
     100           0 :     m_pNewModButton->Hide();       // default
     101           0 :     m_pMacrosSaveInTxt->Hide();    // default
     102             : 
     103           0 :     m_pMacrosInTxt->SetStyle( WB_NOMULTILINE | WB_PATHELLIPSIS );
     104             : 
     105           0 :     m_pMacroNameEdit->SetModifyHdl( LINK( this, MacroChooser, EditModifyHdl ) );
     106             : 
     107           0 :     m_pBasicBox->SetSelectHdl( LINK( this, MacroChooser, BasicSelectHdl ) );
     108             : 
     109           0 :     m_pMacroBox->SetDoubleClickHdl( LINK( this, MacroChooser, MacroDoubleClickHdl ) );
     110           0 :     m_pMacroBox->SetSelectHdl( LINK( this, MacroChooser, MacroSelectHdl ) );
     111             : 
     112           0 :     m_pBasicBox->SetMode( BROWSEMODE_MODULES );
     113             :     m_pBasicBox->SetStyle( WB_TABSTOP | WB_BORDER |
     114             :                         WB_HASLINES | WB_HASLINESATROOT |
     115             :                         WB_HASBUTTONS | WB_HASBUTTONSATROOT |
     116           0 :                         WB_HSCROLL );
     117             : 
     118           0 :     if (SfxDispatcher* pDispatcher = GetDispatcher())
     119           0 :         pDispatcher->Execute( SID_BASICIDE_STOREALLMODULESOURCES );
     120             : 
     121           0 :     if ( bCreateEntries )
     122           0 :         m_pBasicBox->ScanAllEntries();
     123           0 : }
     124             : 
     125           0 : MacroChooser::~MacroChooser()
     126             : {
     127           0 :     if ( bForceStoreBasic )
     128           0 :         SFX_APP()->SaveBasicAndDialogContainer();
     129           0 : }
     130             : 
     131           0 : void MacroChooser::StoreMacroDescription()
     132             : {
     133           0 :     EntryDescriptor aDesc = m_pBasicBox->GetEntryDescriptor(m_pBasicBox->FirstSelected());
     134           0 :     String aMethodName;
     135           0 :     SvTreeListEntry* pEntry = m_pMacroBox->FirstSelected();
     136           0 :     if ( pEntry )
     137           0 :         aMethodName = m_pMacroBox->GetEntryText( pEntry );
     138             :     else
     139           0 :         aMethodName = m_pMacroNameEdit->GetText();
     140           0 :     if ( aMethodName.Len() )
     141             :     {
     142           0 :         aDesc.SetMethodName( aMethodName );
     143           0 :         aDesc.SetType( OBJ_TYPE_METHOD );
     144             :     }
     145             : 
     146           0 :     if (ExtraData* pData = basctl::GetExtraData())
     147           0 :         pData->SetLastEntryDescriptor( aDesc );
     148           0 : }
     149             : 
     150           0 : void MacroChooser::RestoreMacroDescription()
     151             : {
     152           0 :     EntryDescriptor aDesc;
     153           0 :     if (Shell* pShell = GetShell())
     154             :     {
     155           0 :         if (BaseWindow* pCurWin = pShell->GetCurWindow())
     156           0 :             aDesc = pCurWin->CreateEntryDescriptor();
     157             :     }
     158             :     else
     159             :     {
     160           0 :         if (ExtraData* pData = basctl::GetExtraData())
     161           0 :             aDesc = pData->GetLastEntryDescriptor();
     162             :     }
     163             : 
     164           0 :     m_pBasicBox->SetCurrentEntry( aDesc );
     165             : 
     166           0 :     String aLastMacro( aDesc.GetMethodName() );
     167           0 :     if ( aLastMacro.Len() )
     168             :     {
     169             :         // find entry in macro box
     170           0 :         SvTreeListEntry* pEntry = 0;
     171           0 :         sal_uLong nPos = 0;
     172           0 :         SvTreeListEntry* pE = m_pMacroBox->GetEntry( nPos );
     173           0 :         while ( pE )
     174             :         {
     175           0 :             if ( m_pMacroBox->GetEntryText( pE ) == aLastMacro )
     176             :             {
     177           0 :                 pEntry = pE;
     178           0 :                 break;
     179             :             }
     180           0 :             pE = m_pMacroBox->GetEntry( ++nPos );
     181             :         }
     182             : 
     183           0 :         if ( pEntry )
     184           0 :             m_pMacroBox->SetCurEntry( pEntry );
     185             :         else
     186             :         {
     187           0 :             m_pMacroNameEdit->SetText( aLastMacro );
     188           0 :             m_pMacroNameEdit->SetSelection( Selection( 0, 0 ) );
     189             :         }
     190           0 :     }
     191           0 : }
     192             : 
     193           0 : short MacroChooser::Execute()
     194             : {
     195           0 :     RestoreMacroDescription();
     196           0 :     m_pRunButton->GrabFocus();
     197             : 
     198             :     // #104198 Check if "wrong" document is active
     199           0 :     SvTreeListEntry* pSelectedEntry = m_pBasicBox->GetCurEntry();
     200           0 :     EntryDescriptor aDesc( m_pBasicBox->GetEntryDescriptor( pSelectedEntry ) );
     201           0 :     const ScriptDocument& rSelectedDoc( aDesc.GetDocument() );
     202             : 
     203             :     // App Basic is always ok, so only check if shell was found
     204           0 :     if( rSelectedDoc.isDocument() && !rSelectedDoc.isActive() )
     205             :     {
     206             :         // Search for the right entry
     207           0 :         sal_uLong nRootPos = 0;
     208           0 :         SvTreeListEntry* pRootEntry = m_pBasicBox->GetEntry( nRootPos );
     209           0 :         while( pRootEntry )
     210             :         {
     211           0 :             EntryDescriptor aCmpDesc( m_pBasicBox->GetEntryDescriptor( pRootEntry ) );
     212           0 :             const ScriptDocument& rCmpDoc( aCmpDesc.GetDocument() );
     213           0 :             if ( rCmpDoc.isDocument() && rCmpDoc.isActive() )
     214             :             {
     215           0 :                 SvTreeListEntry* pEntry = pRootEntry;
     216           0 :                 SvTreeListEntry* pLastValid = pEntry;
     217           0 :                 while ( pEntry )
     218             :                 {
     219           0 :                     pLastValid = pEntry;
     220           0 :                     pEntry = m_pBasicBox->FirstChild( pEntry );
     221             :                 }
     222           0 :                 if( pLastValid )
     223           0 :                     m_pBasicBox->SetCurEntry( pLastValid );
     224             :             }
     225           0 :             pRootEntry = m_pBasicBox->GetEntry( ++nRootPos );
     226           0 :         }
     227             :     }
     228             : 
     229           0 :     CheckButtons();
     230           0 :     UpdateFields();
     231             : 
     232           0 :     if ( StarBASIC::IsRunning() )
     233           0 :         m_pCloseButton->GrabFocus();
     234             : 
     235           0 :     Window* pPrevDlgParent = Application::GetDefDialogParent();
     236           0 :     Application::SetDefDialogParent( this );
     237           0 :     short nRet = ModalDialog::Execute();
     238             :     // #57314# If the BasicIDE has been activated, don't reset the DefModalDialogParent to the inactive document.
     239           0 :     if ( Application::GetDefDialogParent() == this )
     240           0 :         Application::SetDefDialogParent( pPrevDlgParent );
     241           0 :     return nRet;
     242             : }
     243             : 
     244             : 
     245           0 : void MacroChooser::EnableButton( Button& rButton, bool bEnable )
     246             : {
     247           0 :     if ( bEnable )
     248             :     {
     249           0 :         if (nMode == ChooseOnly || nMode == Recording)
     250           0 :             rButton.Enable(&rButton == m_pRunButton);
     251             :         else
     252           0 :             rButton.Enable();
     253             :     }
     254             :     else
     255           0 :         rButton.Disable();
     256           0 : }
     257             : 
     258             : 
     259             : 
     260             : 
     261           0 : SbMethod* MacroChooser::GetMacro()
     262             : {
     263           0 :     SbMethod* pMethod = 0;
     264           0 :     SbModule* pModule = m_pBasicBox->FindModule( m_pBasicBox->GetCurEntry() );
     265           0 :     if ( pModule )
     266             :     {
     267           0 :         SvTreeListEntry* pEntry = m_pMacroBox->FirstSelected();
     268           0 :         if ( pEntry )
     269             :         {
     270           0 :             String aMacroName( m_pMacroBox->GetEntryText( pEntry ) );
     271           0 :             pMethod = (SbMethod*)pModule->GetMethods()->Find( aMacroName, SbxCLASS_METHOD );
     272             :         }
     273             :     }
     274           0 :     return pMethod;
     275             : }
     276             : 
     277             : 
     278             : 
     279           0 : void MacroChooser::DeleteMacro()
     280             : {
     281           0 :     SbMethod* pMethod = GetMacro();
     282             :     DBG_ASSERT( pMethod, "DeleteMacro: Kein Macro !" );
     283           0 :     if ( pMethod && QueryDelMacro( pMethod->GetName(), this ) )
     284             :     {
     285           0 :         if (SfxDispatcher* pDispatcher = GetDispatcher())
     286           0 :             pDispatcher->Execute( SID_BASICIDE_STOREALLMODULESOURCES );
     287             : 
     288             :         // mark current doc as modified:
     289           0 :         StarBASIC* pBasic = FindBasic(pMethod);
     290             :         DBG_ASSERT( pBasic, "Basic?!" );
     291           0 :         BasicManager* pBasMgr = FindBasicManager( pBasic );
     292             :         DBG_ASSERT( pBasMgr, "BasMgr?" );
     293           0 :         ScriptDocument aDocument( ScriptDocument::getDocumentForBasicManager( pBasMgr ) );
     294           0 :         if ( aDocument.isDocument() )
     295             :         {
     296           0 :             aDocument.setDocumentModified();
     297           0 :             if (SfxBindings* pBindings = GetBindingsPtr())
     298           0 :                 pBindings->Invalidate( SID_SAVEDOC );
     299             :         }
     300             : 
     301           0 :         SbModule* pModule = pMethod->GetModule();
     302             :         DBG_ASSERT( pModule, "DeleteMacro: Kein Modul?!" );
     303           0 :         OUString aSource( pModule->GetSource32() );
     304             :         sal_uInt16 nStart, nEnd;
     305           0 :         pMethod->GetLineRange( nStart, nEnd );
     306           0 :         pModule->GetMethods()->Remove( pMethod );
     307           0 :         CutLines( aSource, nStart-1, nEnd-nStart+1, true );
     308           0 :         pModule->SetSource32( aSource );
     309             : 
     310             :         // update module in library
     311           0 :         String aLibName = pBasic->GetName();
     312           0 :         String aModName = pModule->GetName();
     313           0 :         OSL_VERIFY( aDocument.updateModule( aLibName, aModName, aSource ) );
     314             : 
     315           0 :         SvTreeListEntry* pEntry = m_pMacroBox->FirstSelected();
     316             :         DBG_ASSERT( pEntry, "DeleteMacro: Entry ?!" );
     317           0 :         m_pMacroBox->GetModel()->Remove( pEntry );
     318           0 :         bForceStoreBasic = true;
     319             :     }
     320           0 : }
     321             : 
     322           0 : SbMethod* MacroChooser::CreateMacro()
     323             : {
     324           0 :     SbMethod* pMethod = 0;
     325           0 :     SvTreeListEntry* pCurEntry = m_pBasicBox->GetCurEntry();
     326           0 :     EntryDescriptor aDesc = m_pBasicBox->GetEntryDescriptor(pCurEntry);
     327           0 :     ScriptDocument aDocument( aDesc.GetDocument() );
     328             :     OSL_ENSURE( aDocument.isAlive(), "MacroChooser::CreateMacro: no document!" );
     329           0 :     if ( !aDocument.isAlive() )
     330           0 :         return NULL;
     331             : 
     332           0 :     String aLibName( aDesc.GetLibName() );
     333             : 
     334           0 :     if ( !aLibName.Len() )
     335           0 :         aLibName = "Standard" ;
     336             : 
     337           0 :     aDocument.getOrCreateLibrary( E_SCRIPTS, aLibName );
     338             : 
     339           0 :     OUString aOULibName( aLibName );
     340           0 :     Reference< script::XLibraryContainer > xModLibContainer( aDocument.getLibraryContainer( E_SCRIPTS ) );
     341           0 :     if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) && !xModLibContainer->isLibraryLoaded( aOULibName ) )
     342           0 :         xModLibContainer->loadLibrary( aOULibName );
     343           0 :     Reference< script::XLibraryContainer > xDlgLibContainer( aDocument.getLibraryContainer( E_DIALOGS ) );
     344           0 :     if ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aOULibName ) && !xDlgLibContainer->isLibraryLoaded( aOULibName ) )
     345           0 :         xDlgLibContainer->loadLibrary( aOULibName );
     346             : 
     347           0 :     BasicManager* pBasMgr = aDocument.getBasicManager();
     348           0 :     StarBASIC* pBasic = pBasMgr ? pBasMgr->GetLib( aLibName ) : 0;
     349           0 :     if ( pBasic )
     350             :     {
     351           0 :         SbModule* pModule = 0;
     352           0 :         String aModName( aDesc.GetName() );
     353           0 :         if ( aModName.Len() )
     354             :         {
     355             :             // extract the module name from the string like "Sheet1 (Example1)"
     356           0 :             if( aDesc.GetLibSubName() == IDE_RESSTR(RID_STR_DOCUMENT_OBJECTS) )
     357             :             {
     358           0 :                 sal_uInt16 nIndex = 0;
     359           0 :                 aModName = aModName.GetToken( 0, ' ', nIndex );
     360             :             }
     361           0 :             pModule = pBasic->FindModule( aModName );
     362             :         }
     363           0 :         else if ( pBasic->GetModules()->Count() )
     364           0 :             pModule = (SbModule*)pBasic->GetModules()->Get( 0 );
     365             : 
     366           0 :         if ( !pModule )
     367             :         {
     368             :             pModule = createModImpl( static_cast<Window*>( this ),
     369           0 :                 aDocument, *m_pBasicBox, aLibName, aModName );
     370             :         }
     371             : 
     372           0 :         String aSubName = m_pMacroNameEdit->GetText();
     373             :         DBG_ASSERT( !pModule || !pModule->GetMethods()->Find( aSubName, SbxCLASS_METHOD ), "Macro existiert schon!" );
     374           0 :         pMethod = pModule ? basctl::CreateMacro( pModule, aSubName ) : NULL;
     375             :     }
     376             : 
     377           0 :     return pMethod;
     378             : }
     379             : 
     380           0 : void MacroChooser::SaveSetCurEntry( SvTreeListBox& rBox, SvTreeListEntry* pEntry )
     381             : {
     382             :     // the edit would be killed by the highlight otherwise:
     383             : 
     384           0 :     String aSaveText( m_pMacroNameEdit->GetText() );
     385           0 :     Selection aCurSel( m_pMacroNameEdit->GetSelection() );
     386             : 
     387           0 :     rBox.SetCurEntry( pEntry );
     388           0 :     m_pMacroNameEdit->SetText( aSaveText );
     389           0 :     m_pMacroNameEdit->SetSelection( aCurSel );
     390           0 : }
     391             : 
     392           0 : void MacroChooser::CheckButtons()
     393             : {
     394           0 :     SvTreeListEntry* pCurEntry = m_pBasicBox->GetCurEntry();
     395           0 :     EntryDescriptor aDesc = m_pBasicBox->GetEntryDescriptor(pCurEntry);
     396           0 :     SvTreeListEntry* pMacroEntry = m_pMacroBox->FirstSelected();
     397           0 :     SbMethod* pMethod = GetMacro();
     398             : 
     399             :     // check, if corresponding libraries are readonly
     400           0 :     bool bReadOnly = false;
     401           0 :     sal_uInt16 nDepth = pCurEntry ? m_pBasicBox->GetModel()->GetDepth( pCurEntry ) : 0;
     402           0 :     if ( nDepth == 1 || nDepth == 2 )
     403             :     {
     404           0 :         ScriptDocument aDocument( aDesc.GetDocument() );
     405           0 :         OUString aOULibName( aDesc.GetLibName() );
     406           0 :         Reference< script::XLibraryContainer2 > xModLibContainer( aDocument.getLibraryContainer( E_SCRIPTS ), UNO_QUERY );
     407           0 :         Reference< script::XLibraryContainer2 > xDlgLibContainer( aDocument.getLibraryContainer( E_DIALOGS ), UNO_QUERY );
     408           0 :         if ( ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) && xModLibContainer->isLibraryReadOnly( aOULibName ) ) ||
     409           0 :                 ( xDlgLibContainer.is() && xDlgLibContainer->hasByName( aOULibName ) && xDlgLibContainer->isLibraryReadOnly( aOULibName ) ) )
     410             :         {
     411           0 :             bReadOnly = true;
     412           0 :         }
     413             :     }
     414             : 
     415           0 :     if (nMode != Recording)
     416             :     {
     417             :         // Run...
     418           0 :         bool bEnable = pMethod ? true : false;
     419           0 :         if (nMode != ChooseOnly && StarBASIC::IsRunning())
     420           0 :             bEnable = false;
     421           0 :         EnableButton(*m_pRunButton, bEnable);
     422             :     }
     423             : 
     424             :     // organising still possible?
     425             : 
     426             :     // Assign...
     427           0 :     EnableButton(*m_pAssignButton, pMethod ? true : false);
     428             : 
     429             :     // Edit...
     430           0 :     EnableButton(*m_pEditButton, pMacroEntry ? true : false);
     431             : 
     432             :     // Organizer...
     433           0 :     EnableButton(*m_pOrganizeButton, !StarBASIC::IsRunning() && nMode == All);
     434             : 
     435             :     // m_pDelButton->...
     436           0 :     bool bProtected = m_pBasicBox->IsEntryProtected( pCurEntry );
     437           0 :     bool bShare = ( aDesc.GetLocation() == LIBRARY_LOCATION_SHARE );
     438           0 :     EnableButton(*m_pDelButton, !StarBASIC::IsRunning() && nMode == All && !bProtected && !bReadOnly && !bShare);
     439           0 :     bool bPrev = bNewDelIsDel;
     440           0 :     bNewDelIsDel = pMethod ? true : false;
     441           0 :     if (bPrev != bNewDelIsDel && nMode == All)
     442             :     {
     443           0 :         String aBtnText( bNewDelIsDel ? IDEResId( RID_STR_BTNDEL) : IDEResId( RID_STR_BTNNEW ) );
     444           0 :         m_pDelButton->SetText( aBtnText );
     445             :     }
     446             : 
     447           0 :     if (nMode == Recording)
     448             :     {
     449             :         // save button
     450           0 :         m_pRunButton->Enable(!bProtected && !bReadOnly && !bShare);
     451             :         // new library button
     452           0 :         m_pNewLibButton->Enable(!bShare);
     453             :         // new module button
     454           0 :         m_pNewModButton->Enable(!bProtected && !bReadOnly && !bShare);
     455           0 :     }
     456           0 : }
     457             : 
     458             : 
     459             : 
     460           0 : IMPL_LINK_NOARG_INLINE_START(MacroChooser, MacroDoubleClickHdl)
     461             : {
     462           0 :     StoreMacroDescription();
     463           0 :     if (nMode == Recording)
     464             :     {
     465           0 :         SbMethod* pMethod = GetMacro();
     466           0 :         if ( pMethod && !QueryReplaceMacro( pMethod->GetName(), this ) )
     467           0 :             return 0;
     468             :     }
     469             : 
     470           0 :     EndDialog(Macro_OkRun);
     471           0 :     return 0;
     472             : }
     473           0 : IMPL_LINK_NOARG_INLINE_END(MacroChooser, MacroDoubleClickHdl)
     474             : 
     475           0 : IMPL_LINK( MacroChooser, MacroSelectHdl, SvTreeListBox *, pBox )
     476             : {
     477             :     // Is also called if deselected!
     478             :     // Two function calls in every SelectHdl because
     479             :     // there's no separate DeselectHDL.
     480             :     // So find out if select or deselect:
     481           0 :     if ( pBox->IsSelected( pBox->GetHdlEntry() ) )
     482             :     {
     483           0 :         UpdateFields();
     484           0 :         CheckButtons();
     485             :     }
     486           0 :     return 0;
     487             : }
     488             : 
     489           0 : IMPL_LINK( MacroChooser, BasicSelectHdl, SvTreeListBox *, pBox )
     490             : {
     491             :     // Is also called if deselected!
     492             :     // Two function calls in every SelectHdl because
     493             :     // there's no separate DeselectHDL.
     494             :     // So find out if select or deselect:
     495           0 :     if ( !pBox->IsSelected( pBox->GetHdlEntry() ) )
     496           0 :         return 0;
     497             : 
     498           0 :     SbModule* pModule = m_pBasicBox->FindModule( m_pBasicBox->GetCurEntry() );
     499             : 
     500           0 :     m_pMacroBox->Clear();
     501           0 :     if ( pModule )
     502             :     {
     503           0 :         String aStr = m_aMacrosInTxtBaseStr;
     504           0 :         aStr += " " ;
     505           0 :         aStr += pModule->GetName();
     506             : 
     507           0 :         m_pMacrosInTxt->SetText( aStr );
     508             : 
     509             :         // The macros should be called in the same order that they
     510             :         // are written down in the module.
     511             : 
     512           0 :         map< sal_uInt16, SbMethod* > aMacros;
     513           0 :         size_t nMacroCount = pModule->GetMethods()->Count();
     514           0 :         for ( size_t iMeth = 0; iMeth  < nMacroCount; iMeth++ )
     515             :         {
     516           0 :             SbMethod* pMethod = (SbMethod*)pModule->GetMethods()->Get( iMeth );
     517           0 :             if( pMethod->IsHidden() )
     518           0 :                 continue;
     519             :             DBG_ASSERT( pMethod, "Methode nicht gefunden! (NULL)" );
     520             :             sal_uInt16 nStart, nEnd;
     521           0 :             pMethod->GetLineRange( nStart, nEnd );
     522           0 :             aMacros.insert( map< sal_uInt16, SbMethod*>::value_type( nStart, pMethod ) );
     523             :         }
     524             : 
     525           0 :         m_pMacroBox->SetUpdateMode(false);
     526           0 :         for ( map< sal_uInt16, SbMethod* >::iterator it = aMacros.begin(); it != aMacros.end(); ++it )
     527           0 :             m_pMacroBox->InsertEntry( (*it).second->GetName() );
     528           0 :         m_pMacroBox->SetUpdateMode(true);
     529             : 
     530           0 :         if ( m_pMacroBox->GetEntryCount() )
     531             :         {
     532           0 :             SvTreeListEntry* pEntry = m_pMacroBox->GetEntry( 0 );
     533             :             DBG_ASSERT( pEntry, "Entry ?!" );
     534           0 :             m_pMacroBox->SetCurEntry( pEntry );
     535           0 :         }
     536             :     }
     537             : 
     538           0 :     UpdateFields();
     539           0 :     CheckButtons();
     540           0 :     return 0;
     541             : }
     542             : 
     543             : 
     544             : 
     545           0 : IMPL_LINK( MacroChooser, EditModifyHdl, Edit *, pEdit )
     546             : {
     547             :     (void)pEdit;
     548             : 
     549             :     // select the module in which the macro is put at Neu (new),
     550             :     // if BasicManager or Lib is selecting
     551           0 :     SvTreeListEntry* pCurEntry = m_pBasicBox->GetCurEntry();
     552           0 :     if ( pCurEntry )
     553             :     {
     554           0 :         sal_uInt16 nDepth = m_pBasicBox->GetModel()->GetDepth( pCurEntry );
     555           0 :         if ( ( nDepth == 1 ) && ( m_pBasicBox->IsEntryProtected( pCurEntry ) ) )
     556             :         {
     557             :             // then put to the respective Std-Lib...
     558           0 :             SvTreeListEntry* pManagerEntry = m_pBasicBox->GetModel()->GetParent( pCurEntry );
     559           0 :             pCurEntry = m_pBasicBox->GetModel()->FirstChild( pManagerEntry );
     560             :         }
     561           0 :         if ( nDepth < 2 )
     562             :         {
     563           0 :             SvTreeListEntry* pNewEntry = pCurEntry;
     564           0 :             while ( pCurEntry && ( nDepth < 2 ) )
     565             :             {
     566           0 :                 pCurEntry = m_pBasicBox->FirstChild( pCurEntry );
     567           0 :                 if ( pCurEntry )
     568             :                 {
     569           0 :                     pNewEntry = pCurEntry;
     570           0 :                     nDepth = m_pBasicBox->GetModel()->GetDepth( pCurEntry );
     571             :                 }
     572             :             }
     573           0 :             SaveSetCurEntry( *m_pBasicBox, pNewEntry );
     574             :         }
     575           0 :         if ( m_pMacroBox->GetEntryCount() )
     576             :         {
     577           0 :             String aEdtText( m_pMacroNameEdit->GetText() );
     578           0 :             bool bFound = false;
     579           0 :             for ( sal_uInt16 n = 0; n < m_pMacroBox->GetEntryCount(); n++ )
     580             :             {
     581           0 :                 SvTreeListEntry* pEntry = m_pMacroBox->GetEntry( n );
     582             :                 DBG_ASSERT( pEntry, "Entry ?!" );
     583           0 :                 if ( m_pMacroBox->GetEntryText( pEntry ).CompareIgnoreCaseToAscii( aEdtText ) == COMPARE_EQUAL )
     584             :                 {
     585           0 :                     SaveSetCurEntry(*m_pMacroBox, pEntry);
     586           0 :                     bFound = true;
     587           0 :                     break;
     588             :                 }
     589             :             }
     590           0 :             if ( !bFound )
     591             :             {
     592           0 :                 SvTreeListEntry* pEntry = m_pMacroBox->FirstSelected();
     593             :                 // if the entry exists ->Select ->Desription...
     594           0 :                 if ( pEntry )
     595           0 :                     m_pMacroBox->Select( pEntry, false );
     596           0 :             }
     597             :         }
     598             :     }
     599             : 
     600           0 :     CheckButtons();
     601           0 :     return 0;
     602             : }
     603             : 
     604             : 
     605             : 
     606           0 : IMPL_LINK( MacroChooser, ButtonHdl, Button *, pButton )
     607             : {
     608             :     // apart from New/Record the Description is done by LoseFocus
     609           0 :     if (pButton == m_pRunButton)
     610             :     {
     611           0 :         StoreMacroDescription();
     612             : 
     613             :         // #116444# check security settings before macro execution
     614           0 :         if (nMode == All)
     615             :         {
     616           0 :             SbMethod* pMethod = GetMacro();
     617           0 :             SbModule* pModule = pMethod ? pMethod->GetModule() : NULL;
     618           0 :             StarBASIC* pBasic = pModule ? (StarBASIC*)pModule->GetParent() : NULL;
     619           0 :             BasicManager* pBasMgr = pBasic ? FindBasicManager(pBasic) : NULL;
     620           0 :             if ( pBasMgr )
     621             :             {
     622           0 :                 ScriptDocument aDocument( ScriptDocument::getDocumentForBasicManager( pBasMgr ) );
     623           0 :                 if ( aDocument.isDocument() && !aDocument.allowMacros() )
     624             :                 {
     625           0 :                     WarningBox( this, WB_OK, String( IDEResId( RID_STR_CANNOTRUNMACRO ) ) ).Execute();
     626           0 :                     return 0;
     627           0 :                 }
     628             :             }
     629             :         }
     630           0 :         else if (nMode == Recording )
     631             :         {
     632           0 :             if ( !IsValidSbxName(m_pMacroNameEdit->GetText()) )
     633             :             {
     634           0 :                 ErrorBox( this, WB_OK | WB_DEF_OK, String( IDEResId( RID_STR_BADSBXNAME ) ) ).Execute();
     635           0 :                 m_pMacroNameEdit->SetSelection( Selection( 0, m_pMacroNameEdit->GetText().Len() ) );
     636           0 :                 m_pMacroNameEdit->GrabFocus();
     637           0 :                 return 0;
     638             :             }
     639             : 
     640           0 :             SbMethod* pMethod = GetMacro();
     641           0 :             if ( pMethod && !QueryReplaceMacro( pMethod->GetName(), this ) )
     642           0 :                 return 0;
     643             :         }
     644             : 
     645           0 :         EndDialog(Macro_OkRun);
     646             :     }
     647           0 :     else if (pButton == m_pCloseButton)
     648             :     {
     649           0 :         StoreMacroDescription();
     650           0 :         EndDialog(Macro_Close);
     651             :     }
     652           0 :     else if ((pButton == m_pEditButton) || (pButton == m_pDelButton))
     653             :     {
     654           0 :         SvTreeListEntry* pCurEntry = m_pBasicBox->GetCurEntry();
     655           0 :         EntryDescriptor aDesc = m_pBasicBox->GetEntryDescriptor(pCurEntry);
     656           0 :         ScriptDocument aDocument( aDesc.GetDocument() );
     657             :         DBG_ASSERT( aDocument.isAlive(), "MacroChooser::ButtonHdl: no document, or document is dead!" );
     658           0 :         if ( !aDocument.isAlive() )
     659           0 :             return 0;
     660           0 :         BasicManager* pBasMgr = aDocument.getBasicManager();
     661           0 :         String aLib( aDesc.GetLibName() );
     662           0 :         String aMod( aDesc.GetName() );
     663             :         // extract the module name from the string like "Sheet1 (Example1)"
     664           0 :         if( aDesc.GetLibSubName() == IDE_RESSTR(RID_STR_DOCUMENT_OBJECTS) )
     665             :         {
     666           0 :             sal_uInt16 nIndex = 0;
     667           0 :             aMod = aMod.GetToken( 0, ' ', nIndex );
     668             :         }
     669           0 :         String aSub( aDesc.GetMethodName() );
     670           0 :         SfxMacroInfoItem aInfoItem( SID_BASICIDE_ARG_MACROINFO, pBasMgr, aLib, aMod, aSub, String() );
     671           0 :         if (pButton == m_pEditButton)
     672             :         {
     673           0 :             SvTreeListEntry* pEntry = m_pMacroBox->FirstSelected();
     674           0 :             if ( pEntry )
     675           0 :                 aInfoItem.SetMethod( m_pMacroBox->GetEntryText( pEntry ) );
     676           0 :             StoreMacroDescription();
     677           0 :             SfxAllItemSet aArgs( SFX_APP()->GetPool() );
     678           0 :             SfxRequest aRequest( SID_BASICIDE_APPEAR, SFX_CALLMODE_SYNCHRON, aArgs );
     679           0 :             SFX_APP()->ExecuteSlot( aRequest );
     680             : 
     681           0 :             if (SfxDispatcher* pDispatcher = GetDispatcher())
     682           0 :                 pDispatcher->Execute( SID_BASICIDE_EDITMACRO, SFX_CALLMODE_ASYNCHRON, &aInfoItem, 0L );
     683           0 :             EndDialog(Macro_Edit);
     684             :         }
     685             :         else
     686             :         {
     687           0 :             if ( bNewDelIsDel )
     688             :             {
     689           0 :                 DeleteMacro();
     690           0 :                 if (SfxDispatcher* pDispatcher = GetDispatcher())
     691             :                     pDispatcher->Execute( SID_BASICIDE_UPDATEMODULESOURCE,
     692           0 :                                           SFX_CALLMODE_SYNCHRON, &aInfoItem, 0L );
     693           0 :                 CheckButtons();
     694           0 :                 UpdateFields();
     695             :                 //if ( m_pMacroBox->GetCurEntry() )    // OV-Bug ?
     696             :                 //  m_pMacroBox->Select( m_pMacroBox->GetCurEntry() );
     697             :             }
     698             :             else
     699             :             {
     700           0 :                 if ( !IsValidSbxName(m_pMacroNameEdit->GetText()) )
     701             :                 {
     702           0 :                     ErrorBox( this, WB_OK | WB_DEF_OK, String( IDEResId( RID_STR_BADSBXNAME ) ) ).Execute();
     703           0 :                     m_pMacroNameEdit->SetSelection( Selection( 0, m_pMacroNameEdit->GetText().Len() ) );
     704           0 :                     m_pMacroNameEdit->GrabFocus();
     705           0 :                     return 1;
     706             :                 }
     707           0 :                 SbMethod* pMethod = CreateMacro();
     708           0 :                 if ( pMethod )
     709             :                 {
     710           0 :                     aInfoItem.SetMethod( pMethod->GetName() );
     711           0 :                     aInfoItem.SetModule( pMethod->GetModule()->GetName() );
     712           0 :                     aInfoItem.SetLib( pMethod->GetModule()->GetParent()->GetName() );
     713           0 :                     SfxAllItemSet aArgs( SFX_APP()->GetPool() );
     714           0 :                     SfxRequest aRequest( SID_BASICIDE_APPEAR, SFX_CALLMODE_SYNCHRON, aArgs );
     715           0 :                     SFX_APP()->ExecuteSlot( aRequest );
     716             : 
     717           0 :                     if (SfxDispatcher* pDispatcher = GetDispatcher())
     718           0 :                         pDispatcher->Execute( SID_BASICIDE_EDITMACRO, SFX_CALLMODE_ASYNCHRON, &aInfoItem, 0L );
     719           0 :                     StoreMacroDescription();
     720           0 :                     EndDialog(Macro_New);
     721             :                 }
     722             :             }
     723           0 :         }
     724             :     }
     725           0 :     else if (pButton == m_pAssignButton)
     726             :     {
     727           0 :         SvTreeListEntry* pCurEntry = m_pBasicBox->GetCurEntry();
     728           0 :         EntryDescriptor aDesc = m_pBasicBox->GetEntryDescriptor(pCurEntry);
     729           0 :         ScriptDocument aDocument( aDesc.GetDocument() );
     730             :         DBG_ASSERT( aDocument.isAlive(), "MacroChooser::ButtonHdl: no document, or document is dead!" );
     731           0 :         if ( !aDocument.isAlive() )
     732           0 :             return 0;
     733           0 :         BasicManager* pBasMgr = aDocument.getBasicManager();
     734           0 :         String aLib( aDesc.GetLibName() );
     735           0 :         String aMod( aDesc.GetName() );
     736           0 :         String aSub( m_pMacroNameEdit->GetText() );
     737           0 :         SbMethod* pMethod = GetMacro();
     738             :         DBG_ASSERT( pBasMgr, "BasMgr?" );
     739             :         DBG_ASSERT( pMethod, "Method?" );
     740           0 :         String aComment( GetInfo( pMethod ) );
     741           0 :         SfxMacroInfoItem aItem( SID_MACROINFO, pBasMgr, aLib, aMod, aSub, aComment );
     742           0 :         SfxAllItemSet Args( SFX_APP()->GetPool() );
     743           0 :         SfxRequest aRequest( SID_CONFIG, SFX_CALLMODE_SYNCHRON, Args );
     744           0 :         aRequest.AppendItem( aItem );
     745           0 :         SFX_APP()->ExecuteSlot( aRequest );
     746             :     }
     747           0 :     else if (pButton == m_pNewLibButton)
     748             :     {
     749           0 :         SvTreeListEntry* pCurEntry = m_pBasicBox->GetCurEntry();
     750           0 :         EntryDescriptor aDesc = m_pBasicBox->GetEntryDescriptor(pCurEntry);
     751           0 :         ScriptDocument aDocument( aDesc.GetDocument() );
     752           0 :         createLibImpl( static_cast<Window*>( this ), aDocument, NULL, m_pBasicBox );
     753             :     }
     754           0 :     else if (pButton == m_pNewModButton)
     755             :     {
     756           0 :         SvTreeListEntry* pCurEntry = m_pBasicBox->GetCurEntry();
     757           0 :         EntryDescriptor aDesc = m_pBasicBox->GetEntryDescriptor(pCurEntry);
     758           0 :         ScriptDocument aDocument( aDesc.GetDocument() );
     759           0 :         String aLibName( aDesc.GetLibName() );
     760           0 :         String aModName;
     761             :         createModImpl( static_cast<Window*>( this ), aDocument,
     762           0 :             *m_pBasicBox, aLibName, aModName, true );
     763             :     }
     764           0 :     else if (pButton == m_pOrganizeButton)
     765             :     {
     766           0 :         StoreMacroDescription();
     767             : 
     768           0 :         EntryDescriptor aDesc = m_pBasicBox->GetEntryDescriptor(m_pBasicBox->FirstSelected());
     769           0 :         OrganizeDialog* pDlg = new OrganizeDialog( this, 0, aDesc );
     770           0 :         sal_uInt16 nRet = pDlg->Execute();
     771           0 :         delete pDlg;
     772             : 
     773           0 :         if ( nRet ) // not only closed
     774             :         {
     775           0 :             EndDialog(Macro_Edit);
     776           0 :             return 0;
     777             :         }
     778             : 
     779           0 :         Shell* pShell = GetShell();
     780           0 :         if ( pShell && pShell->IsAppBasicModified() )
     781           0 :             bForceStoreBasic = true;
     782             : 
     783           0 :         m_pBasicBox->UpdateEntries();
     784             :     }
     785           0 :     return 0;
     786             : }
     787             : 
     788             : 
     789             : 
     790           0 : void MacroChooser::UpdateFields()
     791             : {
     792           0 :     SvTreeListEntry*    pMacroEntry = m_pMacroBox->GetCurEntry();
     793           0 :     String          aEmptyStr;
     794             : 
     795           0 :     m_pMacroNameEdit->SetText( aEmptyStr );
     796           0 :     if ( pMacroEntry )
     797           0 :         m_pMacroNameEdit->SetText( m_pMacroBox->GetEntryText( pMacroEntry ) );
     798           0 : }
     799             : 
     800           0 : void MacroChooser::SetMode (Mode nM)
     801             : {
     802           0 :     nMode = nM;
     803           0 :     switch (nMode)
     804             :     {
     805             :         case All:
     806             :         {
     807           0 :             m_pRunButton->SetText( String( IDEResId( RID_STR_RUN ) ) );
     808           0 :             EnableButton(*m_pDelButton, true);
     809           0 :             EnableButton(*m_pOrganizeButton, true);
     810           0 :             break;
     811             :         }
     812             : 
     813             :         case ChooseOnly:
     814             :         {
     815           0 :             m_pRunButton->SetText( String( IDEResId( RID_STR_CHOOSE ) ) );
     816           0 :             EnableButton(*m_pDelButton, false);
     817           0 :             EnableButton(*m_pOrganizeButton, false);
     818           0 :             break;
     819             :         }
     820             : 
     821             :         case Recording:
     822             :         {
     823           0 :             m_pRunButton->SetText( String( IDEResId( RID_STR_RECORD ) ) );
     824           0 :             EnableButton(*m_pDelButton, false);
     825           0 :             EnableButton(*m_pOrganizeButton, false);
     826             : 
     827           0 :             m_pAssignButton->Hide();
     828           0 :             m_pEditButton->Hide();
     829           0 :             m_pDelButton->Hide();
     830           0 :             m_pOrganizeButton->Hide();
     831           0 :             m_pMacroFromTxT->Hide();
     832             : 
     833           0 :             m_pNewLibButton->Show();
     834           0 :             m_pNewModButton->Show();
     835           0 :             m_pMacrosSaveInTxt->Show();
     836             : 
     837           0 :             break;
     838             :         }
     839             :     }
     840           0 :     CheckButtons();
     841           0 : }
     842             : 
     843           0 : String MacroChooser::GetInfo( SbxVariable* pVar )
     844             : {
     845           0 :     String aComment;
     846           0 :     SbxInfoRef xInfo = pVar->GetInfo();
     847           0 :     if ( xInfo.Is() )
     848           0 :         aComment = xInfo->GetComment();
     849           0 :     return aComment;
     850             : }
     851             : 
     852             : 
     853             : } // namespace basctl
     854             : 
     855             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10