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