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