LibreOffice Module sw (master)  1
drformsh.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 #include <svx/hlnkitem.hxx>
22 #include <svx/svdview.hxx>
23 #include <svl/whiter.hxx>
24 #include <sfx2/request.hxx>
25 #include <sfx2/objface.hxx>
26 #include <sfx2/app.hxx>
27 #include <sfx2/viewfrm.hxx>
28 #include <sfx2/dispatch.hxx>
29 #include <vcl/EnumContext.hxx>
30 #include <svl/srchitem.hxx>
31 #include <svx/fmglob.hxx>
32 #include <svx/svdouno.hxx>
33 #include <com/sun/star/form/FormButtonType.hpp>
34 #include <com/sun/star/beans/XPropertySet.hpp>
35 #include <com/sun/star/beans/XPropertySetInfo.hpp>
36 #include <sfx2/htmlmode.hxx>
37 #include <tools/urlobj.hxx>
38 
39 #include <viewopt.hxx>
40 #include <swmodule.hxx>
41 #include <wrtsh.hxx>
42 #include <cmdid.h>
43 #include <globals.hrc>
44 #include <drwbassh.hxx>
45 #include <drformsh.hxx>
46 #include <svl/urihelper.hxx>
47 #include <view.hxx>
48 #include <sfx2/docfile.hxx>
49 #include <docsh.hxx>
50 
51 #define ShellClass_SwDrawFormShell
52 #include <sfx2/msg.hxx>
53 #include <swslots.hxx>
54 
55 #include <unomid.h>
56 
57 using namespace ::com::sun::star;
58 
60 
61 void SwDrawFormShell::InitInterface_Impl()
62 {
63  GetStaticInterface()->RegisterPopupMenu("form");
64 
65  GetStaticInterface()->RegisterObjectBar(SFX_OBJECTBAR_OBJECT, SfxVisibilityFlags::Invisible, ToolbarId::Text_Toolbox_Sw);
66 }
67 
68 
70 {
71  SwWrtShell &rSh = GetShell();
72  const SfxPoolItem* pItem = nullptr;
73  const SfxItemSet *pArgs = rReq.GetArgs();
74 
75  switch ( rReq.GetSlot() )
76  {
77  case SID_HYPERLINK_SETLINK:
78  {
79  if(pArgs)
80  pArgs->GetItemState(SID_HYPERLINK_SETLINK, false, &pItem);
81  if(pItem)
82  {
83  SdrView *pSdrView = rSh.GetDrawView();
84  const SvxHyperlinkItem& rHLinkItem = *static_cast<const SvxHyperlinkItem *>(pItem);
85  bool bConvertToText = rHLinkItem.GetInsertMode() == HLINK_DEFAULT ||
86  rHLinkItem.GetInsertMode() == HLINK_FIELD;
87  const SdrMarkList& rMarkList = pSdrView->GetMarkedObjectList();
88  if (rMarkList.GetMark(0))
89  {
90  SdrUnoObj* pUnoCtrl = dynamic_cast<SdrUnoObj*>( rMarkList.GetMark(0)->GetMarkedSdrObj() );
91  if (pUnoCtrl && SdrInventor::FmForm == pUnoCtrl->GetObjInventor())
92  {
93  if(bConvertToText)
94  {
95  //remove object -> results in destruction of this!
96  SwView& rTempView = GetView();
97  rTempView.GetViewFrame()->GetDispatcher()->Execute(SID_DELETE, SfxCallMode::SYNCHRON );
98  rTempView.StopShellTimer();
99  //issue a new command to insert the link
100  rTempView.GetViewFrame()->GetDispatcher()->ExecuteList(
101  SID_HYPERLINK_SETLINK, SfxCallMode::ASYNCHRON,
102  { &rHLinkItem });
103  }
104  else
105  {
106  const uno::Reference< awt::XControlModel >& xControlModel = pUnoCtrl->GetUnoControlModel();
107 
108  OSL_ENSURE( xControlModel.is(), "UNO-Control without Model" );
109  if( !xControlModel.is() )
110  return;
111 
112  uno::Reference< beans::XPropertySet > xPropSet(xControlModel, uno::UNO_QUERY);
113 
114  // Can we set a URL to the object?
115  OUString sTargetURL( "TargetURL" );
116  uno::Reference< beans::XPropertySetInfo > xPropInfoSet = xPropSet->getPropertySetInfo();
117  if( xPropInfoSet->hasPropertyByName( sTargetURL ))
118  {
119  beans::Property aProp = xPropInfoSet->getPropertyByName( sTargetURL );
120  if( !aProp.Name.isEmpty() )
121  {
122  uno::Any aTmp;
123  // Yes!
124  OUString sLabel("Label");
125  if( xPropInfoSet->hasPropertyByName(sLabel) )
126  {
127  aTmp <<= rHLinkItem.GetName();
128  xPropSet->setPropertyValue(sLabel, aTmp );
129  }
130 
131  SfxMedium* pMedium = GetView().GetDocShell()->GetMedium();
132  INetURLObject aAbs;
133  if( pMedium )
134  aAbs = pMedium->GetURLObject();
135  aTmp <<= URIHelper::SmartRel2Abs(aAbs, rHLinkItem.GetURL());
136  xPropSet->setPropertyValue( sTargetURL, aTmp );
137 
138  if( !rHLinkItem.GetTargetFrame().isEmpty() )
139  {
140  aTmp <<= rHLinkItem.GetTargetFrame();
141  xPropSet->setPropertyValue( "TargetFrame", aTmp );
142  }
143 
144  aTmp <<= form::FormButtonType_URL;
145  xPropSet->setPropertyValue( "ButtonType", aTmp );
146  }
147  }
148  }
149  }
150  }
151  }
152  }
153  break;
154 
155  default:
156  OSL_ENSURE(false, "wrong dispatcher");
157  return;
158  }
159 }
160 
162 {
163  SwWrtShell &rSh = GetShell();
164  SfxWhichIter aIter( rSet );
165  sal_uInt16 nWhich = aIter.FirstWhich();
166 
167  while( nWhich )
168  {
169  switch( nWhich )
170  {
171  case SID_HYPERLINK_GETLINK:
172  {
173  SdrView* pSdrView = rSh.GetDrawViewWithValidMarkList();
174  const SdrMarkList& rMarkList = pSdrView->GetMarkedObjectList();
175  SvxHyperlinkItem aHLinkItem;
176  if (rMarkList.GetMark(0))
177  {
178  SdrUnoObj* pUnoCtrl = dynamic_cast<SdrUnoObj*>( rMarkList.GetMark(0)->GetMarkedSdrObj() );
179  if (pUnoCtrl && SdrInventor::FmForm == pUnoCtrl->GetObjInventor())
180  {
181  const uno::Reference< awt::XControlModel >& xControlModel = pUnoCtrl->GetUnoControlModel();
182 
183  OSL_ENSURE( xControlModel.is(), "UNO-Control without Model" );
184  if( !xControlModel.is() )
185  return;
186 
187  uno::Reference< beans::XPropertySet > xPropSet(xControlModel, uno::UNO_QUERY);
188 
189  uno::Any aTmp;
190  uno::Reference< beans::XPropertySetInfo > xInfo = xPropSet->getPropertySetInfo();
191  if(xInfo->hasPropertyByName( "ButtonType" ))
192  {
193  form::FormButtonType eButtonType = form::FormButtonType_URL;
194  aTmp = xPropSet->getPropertyValue( "ButtonType" );
195  if( aTmp >>= eButtonType )
196  {
197  // Label
198  if(xInfo->hasPropertyByName( "Label" ))
199  {
200  aTmp = xPropSet->getPropertyValue( "Label" );
201  OUString sTmp;
202  if( (aTmp >>= sTmp) && !sTmp.isEmpty())
203  {
204  aHLinkItem.SetName(sTmp);
205  }
206  }
207 
208  // URL
209  if(xInfo->hasPropertyByName( "TargetURL" ))
210  {
211  aTmp = xPropSet->getPropertyValue( "TargetURL" );
212  OUString sTmp;
213  if( (aTmp >>= sTmp) && !sTmp.isEmpty())
214  {
215  aHLinkItem.SetURL(sTmp);
216  }
217  }
218 
219  // Target
220  if(xInfo->hasPropertyByName( "TargetFrame" ))
221  {
222  aTmp = xPropSet->getPropertyValue( "TargetFrame" );
223  OUString sTmp;
224  if( (aTmp >>= sTmp) && !sTmp.isEmpty())
225  {
226  aHLinkItem.SetTargetFrame(sTmp);
227  }
228  }
229  aHLinkItem.SetInsertMode(HLINK_BUTTON);
230  }
231  }
232  }
233  }
234  sal_uInt16 nHtmlMode = ::GetHtmlMode(GetView().GetDocShell());
235  aHLinkItem.SetInsertMode(static_cast<SvxLinkInsertMode>(aHLinkItem.GetInsertMode() |
236  ((nHtmlMode & HTMLMODE_ON) != 0 ? HLINK_HTMLMODE : 0)));
237 
238  rSet.Put(aHLinkItem);
239  }
240  break;
241  }
242  nWhich = aIter.NextWhich();
243  }
244 }
245 
247  SwDrawBaseShell(_rView)
248 {
249  GetShell().NoEdit();
250  SetName("DrawForm");
252 }
253 
255 {
256 }
257 
258 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void SetURL(const OUString &rURL)
SfxViewFrame * GetViewFrame() const
void StopShellTimer()
Definition: view.cxx:1769
const SfxPoolItem * ExecuteList(sal_uInt16 nSlot, SfxCallMode nCall, std::initializer_list< SfxPoolItem const * > args, std::initializer_list< SfxPoolItem const * > internalargs=std::initializer_list< SfxPoolItem const * >())
SwView & GetView()
Definition: basesh.hxx:57
#define SFX_IMPL_INTERFACE(Class, SuperClass)
SdrView * GetDrawView()
Definition: vnew.cxx:376
const OUString & GetTargetFrame() const
SfxDispatcher * GetDispatcher()
SwWrtShell & GetShell()
Definition: basesh.cxx:2623
sal_uInt16 FirstWhich()
SwDrawFormShell(SwView &rView)
Definition: drformsh.cxx:246
SdrMark * GetMark(size_t nNum) const
const SfxItemSet * GetArgs() const
void NoEdit(bool bHideCursor=true)
Definition: wrtsh1.cxx:160
sal_uInt16 NextWhich()
Used by the UI to modify the document model.
Definition: wrtsh.hxx:88
void SetTargetFrame(const OUString &rTarget)
HLINK_HTMLMODE
void Execute(SfxRequest const &)
Definition: drformsh.cxx:69
SdrObject * GetMarkedSdrObj() const
const OUString & GetURL() const
SfxItemState GetItemState(sal_uInt16 nWhich, bool bSrchInParent=true, const SfxPoolItem **ppItem=nullptr) const
HLINK_BUTTON
const SdrMarkList & GetMarkedObjectList() const
HTMLMODE_ON
sal_uInt16 GetHtmlMode(const SwDocShell *pShell)
Definition: viewopt.cxx:340
HLINK_FIELD
void SetName(const OUString &rName)
void SetName(const OUString &rName)
SwDocShell * GetDocShell()
Definition: view.cxx:1127
virtual SdrInventor GetObjInventor() const
#define SFX_OBJECTBAR_OBJECT
const SfxPoolItem * Put(const SfxPoolItem &rItem, sal_uInt16 nWhich)
sal_uInt16 GetSlot() const
static const OUString & GetContextName(const Context eContext)
HLINK_DEFAULT
SdrView * GetDrawViewWithValidMarkList()
Definition: vnew.cxx:381
const INetURLObject & GetURLObject() const
void SetInsertMode(SvxLinkInsertMode eNew)
const css::uno::Reference< css::awt::XControlModel > & GetUnoControlModel() const
void GetState(SfxItemSet &)
Definition: drformsh.cxx:161
const OUString & GetName() const
SvxLinkInsertMode GetInsertMode() const
const SfxPoolItem * Execute(sal_uInt16 nSlot, SfxCallMode nCall=SfxCallMode::SLOT, const SfxPoolItem **pArgs=nullptr, sal_uInt16 nModi=0, const SfxPoolItem **pInternalArgs=nullptr)
virtual ~SwDrawFormShell() override
Definition: drformsh.cxx:254
void SetContextName(const OUString &rsContextName)
Definition: view.hxx:146
SVL_DLLPUBLIC OUString SmartRel2Abs(INetURLObject const &rTheBaseURIRef, OUString const &rTheRelURIRef, Link< OUString *, bool > const &rMaybeFileHdl=Link< OUString *, bool >(), bool bCheckFileExists=true, bool bIgnoreFragment=false, INetURLObject::EncodeMechanism eEncodeMechanism=INetURLObject::EncodeMechanism::WasEncoded, INetURLObject::DecodeMechanism eDecodeMechanism=INetURLObject::DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8, FSysStyle eStyle=FSysStyle::Detect)
SfxMedium * GetMedium() const