LibreOffice Module sw (master)  1
docbasic.cxx
Go to the documentation of this file.
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 <hintids.hxx>
21 
22 #include <rtl/ustring.hxx>
23 #include <sal/log.hxx>
24 #include <vcl/imap.hxx>
25 #include <vcl/imapobj.hxx>
26 #include <basic/sbx.hxx>
27 #include <frmfmt.hxx>
28 #include <fmtinfmt.hxx>
29 #include <fmturl.hxx>
30 #include <frmatr.hxx>
31 #include <docary.hxx>
32 #include <doc.hxx>
33 #include <docsh.hxx>
34 #include <swevent.hxx>
35 #include <frameformats.hxx>
36 #include <memory>
37 
38 using namespace ::com::sun::star::uno;
39 
40 static Sequence<Any> *lcl_docbasic_convertArgs( SbxArray& rArgs )
41 {
42  Sequence<Any> *pRet = nullptr;
43 
44  sal_uInt32 nCount = rArgs.Count32();
45  if( nCount > 1 )
46  {
47  nCount--;
48  pRet = new Sequence<Any>( nCount );
49  Any *pUnoArgs = pRet->getArray();
50  for( sal_uInt32 i=0; i<nCount; i++ )
51  {
52  SbxVariable *pVar = rArgs.Get32( i+1 );
53  switch( pVar->GetType() )
54  {
55  case SbxSTRING:
56  pUnoArgs[i] <<= pVar->GetOUString();
57  break;
58  case SbxCHAR:
59  pUnoArgs[i] <<= static_cast<sal_Int16>(pVar->GetChar()) ;
60  break;
61  case SbxUSHORT:
62  pUnoArgs[i] <<= static_cast<sal_Int16>(pVar->GetUShort());
63  break;
64  case SbxLONG:
65  pUnoArgs[i] <<= pVar->GetLong();
66  break;
67  default:
68  pUnoArgs[i].clear();
69  break;
70  }
71  }
72  }
73 
74  return pRet;
75 }
76 
77 void SwDoc::ExecMacro( const SvxMacro& rMacro, OUString* pRet, SbxArray* pArgs )
78 {
79  switch( rMacro.GetScriptType() )
80  {
81  case STARBASIC:
82  {
83  SbxBaseRef aRef;
84  SbxValue* pRetValue = new SbxValue;
85  aRef = pRetValue;
86  mpDocShell->CallBasic( rMacro.GetMacName(),
87  rMacro.GetLibName(),
88  pArgs, pRet ? pRetValue : nullptr );
89 
90  if( pRet && SbxNULL < pRetValue->GetType() &&
91  SbxVOID != pRetValue->GetType() )
92  {
93  // valid value, so set it
94  *pRet = pRetValue->GetOUString();
95  }
96  }
97  break;
98  case JAVASCRIPT:
99  // ignore JavaScript calls
100  break;
101  case EXTENDED_STYPE:
102  {
103  std::unique_ptr<Sequence<Any> > pUnoArgs;
104  if( pArgs )
105  {
106  // better to rename the local function to lcl_translateBasic2Uno and
107  // a much shorter routine can be found in sfx2/source/doc/objmisc.cxx
108  pUnoArgs.reset(lcl_docbasic_convertArgs( *pArgs ));
109  }
110 
111  if (!pUnoArgs)
112  {
113  pUnoArgs.reset(new Sequence< Any > (0));
114  }
115 
116  // TODO - return value is not handled
117  Any aRet;
118  Sequence< sal_Int16 > aOutArgsIndex;
119  Sequence< Any > aOutArgs;
120 
121  SAL_INFO("sw", "SwDoc::ExecMacro URL is " << rMacro.GetMacName() );
122 
124  rMacro.GetMacName(), *pUnoArgs, aRet, aOutArgsIndex, aOutArgs);
125 
126  break;
127  }
128  }
129 }
130 
131 sal_uInt16 SwDoc::CallEvent( SvMacroItemId nEvent, const SwCallMouseEvent& rCallEvent,
132  bool bCheckPtr )
133 {
134  if( !mpDocShell ) // we can't do that without a DocShell!
135  return 0;
136 
137  sal_uInt16 nRet = 0;
138  const SvxMacroTableDtor* pTable = nullptr;
139  switch( rCallEvent.eType )
140  {
142  if( bCheckPtr )
143  {
144  for (const SfxPoolItem* pItem : GetAttrPool().GetItemSurrogates(RES_TXTATR_INETFMT))
145  {
146  auto pFormatItem = dynamic_cast<const SwFormatINetFormat*>(pItem);
147  if( pFormatItem && rCallEvent.PTR.pINetAttr == pFormatItem )
148  {
149  bCheckPtr = false; // misuse as a flag
150  break;
151  }
152  }
153  }
154  if( !bCheckPtr )
155  pTable = rCallEvent.PTR.pINetAttr->GetMacroTable();
156  break;
157 
159  case EVENT_OBJECT_IMAGE:
160  {
161  const SwFrameFormat* pFormat = rCallEvent.PTR.pFormat;
162  if( bCheckPtr )
163  {
164  if (GetSpzFrameFormats()->IsAlive(pFormat))
165  bCheckPtr = false; // misuse as a flag
166  else
167  // this shouldn't be possible now that SwCallMouseEvent
168  // listens for dying format?
169  assert(false);
170  }
171  if( !bCheckPtr )
172  pTable = &pFormat->GetMacro().GetMacroTable();
173  }
174  break;
175 
177  {
178  const IMapObject* pIMapObj = rCallEvent.PTR.IMAP.pIMapObj;
179  if( bCheckPtr )
180  {
181  const SwFrameFormat* pFormat = rCallEvent.PTR.IMAP.pFormat;
182  if (GetSpzFrameFormats()->IsAlive(pFormat))
183  {
184  const ImageMap* pIMap = pFormat->GetURL().GetMap();
185  if (pIMap)
186  {
187  for( size_t nPos = pIMap->GetIMapObjectCount(); nPos; )
188  if( pIMapObj == pIMap->GetIMapObject( --nPos ))
189  {
190  bCheckPtr = false; // misuse as a flag
191  break;
192  }
193  }
194  }
195  }
196  if( !bCheckPtr )
197  pTable = &pIMapObj->GetMacroTable();
198  }
199  break;
200  default:
201  break;
202  }
203 
204  if( pTable )
205  {
206  nRet = 0x1;
207  if( pTable->IsKeyValid( nEvent ) )
208  {
209  const SvxMacro& rMacro = *pTable->Get( nEvent );
210  if( STARBASIC == rMacro.GetScriptType() )
211  {
212  nRet += ERRCODE_NONE == mpDocShell->CallBasic( rMacro.GetMacName(),
213  rMacro.GetLibName(), nullptr ) ? 1 : 0;
214  }
215  else if( EXTENDED_STYPE == rMacro.GetScriptType() )
216  {
217  std::unique_ptr<Sequence<Any> > pUnoArgs(new Sequence<Any>());
218 
219  Any aRet;
220  Sequence< sal_Int16 > aOutArgsIndex;
221  Sequence< Any > aOutArgs;
222 
223  SAL_INFO("sw", "SwDoc::CallEvent URL is " << rMacro.GetMacName() );
224 
225  nRet += ERRCODE_NONE == mpDocShell->CallXScript(
226  rMacro.GetMacName(), *pUnoArgs,aRet, aOutArgsIndex, aOutArgs) ? 1 : 0;
227  }
228  // JavaScript calls are ignored
229  }
230  }
231  return nRet;
232 }
233 
234 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Unicode GetChar() const
EXTENDED_STYPE
ErrCode CallBasic(const OUString &rMacro, const OUString &rBasicName, SbxArray *pArgs, SbxValue *pRet=nullptr)
SbxVOID
OUString GetOUString() const
sal_Int32 GetLong() const
SvMacroItemId
size_t GetIMapObjectCount() const
const SwFrameFormats * GetSpzFrameFormats() const
Definition: doc.hxx:739
sal_uInt16 CallEvent(SvMacroItemId nEvent, const SwCallMouseEvent &rCallEvent, bool bChkPtr=false)
Definition: docbasic.cxx:131
const OUString & GetLibName() const
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
int nCount
const SvxMacroTableDtor * GetMacroTable() const
Definition: fmtinfmt.hxx:132
virtual SbxDataType GetType() const override
SwDocShell * mpDocShell
Definition: doc.hxx:256
const SvxMacroItem & GetMacro(bool=true) const
Definition: frmatr.hxx:92
sal_uInt32 Count32() const
SbxCHAR
constexpr TypedWhichId< SwFormatINetFormat > RES_TXTATR_INETFMT(51)
const SwFrameFormat * pFormat
Definition: swevent.hxx:63
SbxLONG
SwCallEventObjectType eType
Definition: swevent.hxx:59
Style of a layout element.
Definition: frmfmt.hxx:57
int i
const SvxMacroTableDtor & GetMacroTable() const
const SwFormatURL & GetURL(bool=true) const
Definition: fmturl.hxx:78
sal_uInt16 GetUShort() const
ErrCode CallXScript(const OUString &rScriptURL, const css::uno::Sequence< css::uno::Any > &aParams, css::uno::Any &aRet, css::uno::Sequence< sal_Int16 > &aOutParamIndex, css::uno::Sequence< css::uno::Any > &aOutParam, bool bRaiseError=true, const css::uno::Any *aCaller=nullptr)
JAVASCRIPT
const ImageMap * GetMap() const
Definition: fmturl.hxx:68
bool IsKeyValid(SvMacroItemId nEvent) const
const SvxMacroTableDtor & GetMacroTable() const
virtual SbxDataType GetType() const override
const SvxMacro * Get(SvMacroItemId nEvent) const
SbxSTRING
#define ERRCODE_NONE
static Sequence< Any > * lcl_docbasic_convertArgs(SbxArray &rArgs)
Definition: docbasic.cxx:40
#define SAL_INFO(area, stream)
SbxUSHORT
void ExecMacro(const SvxMacro &rMacro, OUString *pRet, SbxArray *pArgs)
Definition: docbasic.cxx:77
ScriptType GetScriptType() const
IMapObject * GetIMapObject(size_t nPos) const
union SwCallMouseEvent::@27 PTR
struct SwCallMouseEvent::@27::@28 IMAP
SbxVariable * Get32(sal_uInt32)
const SwFormatINetFormat * pINetAttr
Definition: swevent.hxx:66
STARBASIC
const OUString & GetMacName() const
sal_uInt16 nPos
const SwAttrPool & GetAttrPool() const
Definition: doc.hxx:1315