LibreOffice Module sw (master)  1
uivwimp.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 <config_features.h>
21 
22 #include <cmdid.h>
23 
24 #include <com/sun/star/scanner/XScannerManager2.hpp>
25 #include <com/sun/star/datatransfer/clipboard/XClipboard.hpp>
28 #include <vcl/weld.hxx>
29 #include <vcl/svapp.hxx>
30 #include <sfx2/viewfrm.hxx>
31 #include <sfx2/bindings.hxx>
32 #include <sfx2/docfile.hxx>
33 
34 #include <sfx2/docinsert.hxx>
35 #include <sfx2/request.hxx>
36 #include <uivwimp.hxx>
37 #include <unotxvw.hxx>
38 #include <unodispatch.hxx>
39 #include <swmodule.hxx>
40 #include <swdtflvr.hxx>
41 
42 #include <strings.hrc>
43 
44 using namespace ::com::sun::star;
45 using namespace ::com::sun::star::uno;
46 using namespace ::com::sun::star::scanner;
47 using namespace ::com::sun::star::lang;
49 
51  : pView(pShell)
52  , eShellMode(ShellMode::Text)
53  , m_nParam(0)
54  , m_bSelectObject(false)
55  , m_bEditingPositionSet(false)
56 {
59 }
60 
62 {
63  auto pInterceptor = comphelper::getFromUnoTunnel<SwXDispatchProviderInterceptor>(xDisProvInterceptor);
64  if(pInterceptor)
65  pInterceptor->Invalidate();
66  view::XSelectionSupplier* pTextView = mxXTextView.get();
67  static_cast<SwXTextView*>(pTextView)->Invalidate();
68  mxXTextView.clear();
69  if( mxScanEvtLstnr.is() )
70  mxScanEvtLstnr->ViewDestroyed();
71  if( mxClipEvtLstnr.is() )
72  {
73  mxClipEvtLstnr->AddRemoveListener( false );
74  mxClipEvtLstnr->ViewDestroyed();
75  }
76 #if HAVE_FEATURE_DBCONNECTIVITY
77  xConfigItem.reset();
78 #endif
79  m_pDocInserter.reset();
80  m_pRequest.reset();
81 }
82 
84 {
85  eShellMode = eSet;
86 }
87 
88 view::XSelectionSupplier* SwView_Impl::GetUNOObject()
89 {
90  return mxXTextView.get();
91 }
92 
94 {
95  view::XSelectionSupplier* pTextView = mxXTextView.get();
96  return static_cast<SwXTextView*>(pTextView);
97 }
98 
100 {
101  switch(rReq.GetSlot())
102  {
103  case SID_TWAIN_SELECT:
104  {
105  bool bDone = false;
106  Reference< XScannerManager2 > xScanMgr = SW_MOD()->GetScannerManager();
107 
108  if( xScanMgr.is() )
109  {
110  try
111  {
113  const Sequence< ScannerContext >
114  aContexts( xScanMgr->getAvailableScanners() );
115 
116  if( aContexts.hasElements() )
117  {
118  Reference< XEventListener > xLstner = &rListener;
119  ScannerContext aContext( aContexts.getConstArray()[ 0 ] );
120 
121  Reference<lang::XInitialization> xInit(xScanMgr, UNO_QUERY);
122  if (xInit.is())
123  {
124  // initialize dialog
125  weld::Window* pWindow = rReq.GetFrameWeld();
126  uno::Sequence<uno::Any> aSeq(comphelper::InitAnyPropertySequence(
127  {
128  {"ParentWindow", pWindow ? uno::Any(pWindow->GetXWindow()) : uno::Any(Reference<awt::XWindow>())}
129  }));
130  xInit->initialize( aSeq );
131  }
132 
133  bDone = xScanMgr->configureScannerAndScan( aContext, xLstner );
134  }
135  }
136  catch(...)
137  {
138  }
139 
140  }
141  if( bDone )
142  rReq.Done();
143  else
144  {
145  rReq.Ignore();
146  }
147  }
148  break;
149 
150  case SID_TWAIN_TRANSFER:
151  {
152  bool bDone = false;
153 
154  Reference< XScannerManager2 > xScanMgr = SW_MOD()->GetScannerManager();
155  if( xScanMgr.is() )
156  {
158  try
159  {
160  const Sequence< scanner::ScannerContext >aContexts( xScanMgr->getAvailableScanners() );
161  if( aContexts.hasElements() )
162  {
163  Reference< XEventListener > xLstner = &rListener;
164  xScanMgr->startScan( aContexts.getConstArray()[ 0 ], xLstner );
165  bDone = true;
166  }
167  }
168  catch(...)
169  {
170  }
171  }
172 
173  if( !bDone )
174  {
175  std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(rReq.GetFrameWeld(),
176  VclMessageType::Info, VclButtonsType::Ok,
177  SwResId(STR_SCAN_NOSOURCE)));
178  xBox->run();
179  rReq.Ignore();
180  }
181  else
182  {
183  rReq.Done();
184  SfxBindings& rBind = pView->GetViewFrame()->GetBindings();
185  rBind.Invalidate( SID_TWAIN_SELECT );
186  rBind.Invalidate( SID_TWAIN_TRANSFER );
187  }
188  }
189  break;
190  }
191 }
192 
194 {
195  if(!mxScanEvtLstnr.is())
197  return *mxScanEvtLstnr;
198 }
199 
201 {
202  if(!mxClipEvtLstnr.is())
203  {
205  mxClipEvtLstnr->AddRemoveListener( true );
206  }
207 }
208 
210 {
212  for (const auto& xTransferable: mxTransferables)
213  {
214  auto pTransferable = comphelper::getFromUnoTunnel<SwTransferable>(xTransferable.get());
215  if(pTransferable)
216  pTransferable->Invalidate();
217  }
218 }
219 
221 {
222  //prevent removing of the non-referenced SwTransferable
223  osl_atomic_increment(&rTransferable.m_refCount);
224  {
225  // Remove previously added, but no longer existing weak references.
226  mxTransferables.erase(std::remove_if(mxTransferables.begin(), mxTransferables.end(),
227  [](const css::uno::WeakReference<css::lang::XUnoTunnel>& rTunnel) {
228  uno::Reference<lang::XUnoTunnel> xTunnel(rTunnel.get(), uno::UNO_QUERY);
229  return !xTunnel.is();
230  }), mxTransferables.end());
231 
232  mxTransferables.emplace_back(&rTransferable);
233  }
234  osl_atomic_decrement(&rTransferable.m_refCount);
235 }
236 
238  const OUString& rFactory,
239  const Link<sfx2::FileDialogHelper*,void>& rEndDialogHdl,
240  const sal_uInt16 nSlotId
241 )
242 {
244  switch( nSlotId )
245  {
246  case SID_DOCUMENT_MERGE:
248  break;
249  case SID_DOCUMENT_COMPARE:
251  break;
252  default:
253  break;
254  }
255 
256  m_pDocInserter.reset(new ::sfx2::DocumentInserter(pView->GetFrameWeld(), rFactory, mode));
257  m_pDocInserter->StartExecuteModal( rEndDialogHdl );
258 }
259 
260 std::unique_ptr<SfxMedium> SwView_Impl::CreateMedium()
261 {
262  return m_pDocInserter->CreateMedium();
263 }
264 
265 void SwView_Impl::InitRequest( const SfxRequest& rRequest )
266 {
267  m_pRequest.reset(new SfxRequest( rRequest ));
268 }
269 
271 {
272 }
273 
274 void SAL_CALL SwScannerEventListener::disposing( const EventObject& /*rEventObject*/)
275 {
276 #if defined(_WIN32) || defined UNX
277  SolarMutexGuard aGuard;
278  if( pView )
280 #endif
281 }
282 
284 {
285 }
286 
287 void SAL_CALL SwClipboardChangeListener::disposing( const EventObject& /*rEventObject*/ )
288 {
289  SolarMutexGuard aGuard;
290  pView = nullptr; // so we don't touch the view if changedContents somehow fires afterwards
291 }
292 
293 void SAL_CALL SwClipboardChangeListener::changedContents( const css::datatransfer::clipboard::ClipboardEvent& rEventObject )
294 
295 {
296  const SolarMutexGuard aGuard;
297  if( !pView )
298  return;
299 
300  {
301  TransferableDataHelper aDataHelper( rEventObject.Contents );
302  SwWrtShell& rSh = pView->GetWrtShell();
303 
305  pView->m_bPasteState = aDataHelper.GetXTransferable().is() &&
306  SwTransferable::IsPaste( rSh, aDataHelper );
307 
308  pView->m_bPasteSpecialState = aDataHelper.GetXTransferable().is() &&
309  SwTransferable::IsPasteSpecial( rSh, aDataHelper );
310  }
311 
312  SfxBindings& rBind = pView->GetViewFrame()->GetBindings();
313  rBind.Invalidate( SID_PASTE );
314  rBind.Invalidate( SID_PASTE_SPECIAL );
315  rBind.Invalidate( SID_CLIPBOARD_FORMAT_ITEMS );
316 }
317 
319 {
320  pView->AddRemoveClipboardListener( Reference< XClipboardListener >( this ), bAdd );
321 }
322 
323 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SfxViewFrame * GetViewFrame() const
virtual ~SwScannerEventListener() override
Definition: uivwimp.cxx:270
SwXTextView * GetUNOObject_Impl()
Definition: uivwimp.cxx:93
virtual css::uno::Reference< css::awt::XWindow > GetXWindow()=0
void AddRemoveClipboardListener(const css::uno::Reference< css::datatransfer::clipboard::XClipboardListener > &, bool)
void InitRequest(const SfxRequest &rRequest)
Definition: uivwimp.cxx:265
void ExecuteScan(SfxRequest &rReq)
Definition: uivwimp.cxx:99
std::vector< css::uno::WeakReference< css::lang::XUnoTunnel > > mxTransferables
Definition: uivwimp.hxx:89
std::shared_ptr< SwMailMergeConfigItem > xConfigItem
Definition: uivwimp.hxx:103
static bool IsPaste(const SwWrtShell &, const TransferableDataHelper &)
Definition: swdtflvr.cxx:1384
void AddRemoveListener(bool bAdd)
Definition: uivwimp.cxx:318
void Done(bool bRemove=false)
void Invalidate(sal_uInt16 nId)
std::unique_ptr< sfx2::DocumentInserter > m_pDocInserter
Definition: uivwimp.hxx:105
void AddTransferable(SwTransferable &rTransferable)
Definition: uivwimp.cxx:220
void StartDocumentInserter(const OUString &rFactory, const Link< sfx2::FileDialogHelper *, void > &rEndDialogHdl, const sal_uInt16 nSlotId)
Definition: uivwimp.cxx:237
rtl::Reference< SwClipboardChangeListener > mxClipEvtLstnr
Definition: uivwimp.hxx:99
SwWrtShell & GetWrtShell() const
Definition: view.hxx:413
Used by the UI to modify the document model.
Definition: wrtsh.hxx:93
OUString SwResId(TranslateId aId)
Definition: swmodule.cxx:165
rtl::Reference< SwScannerEventListener > mxScanEvtLstnr
Definition: uivwimp.hxx:97
bool m_bPasteState
Definition: view.hxx:240
css::uno::Reference< css::frame::XDispatchProviderInterceptor > xDisProvInterceptor
Definition: uivwimp.hxx:87
static SotExchangeDest GetSotDestination(const SwWrtShell &rSh)
Definition: swdtflvr.cxx:2049
ShellMode
Definition: view.hxx:85
virtual void SAL_CALL disposing(const css::lang::EventObject &rEventObject) override
Definition: uivwimp.cxx:274
SotExchangeDest m_nLastPasteDestination
Definition: view.hxx:230
void SetShellMode(ShellMode eSet)
Definition: uivwimp.cxx:83
#define SW_MOD()
Definition: swmodule.hxx:256
css::uno::Sequence< css::uno::Any > InitAnyPropertySequence(::std::initializer_list< ::std::pair< OUString, css::uno::Any > > vInit)
css::uno::Reference< css::datatransfer::XTransferable > GetXTransferable() const
std::unique_ptr< SfxRequest > m_pRequest
Definition: uivwimp.hxx:106
virtual ~SwClipboardChangeListener() override
Definition: uivwimp.cxx:283
SfxBindings & GetBindings()
virtual void SAL_CALL disposing(const css::lang::EventObject &rEventObject) override
Definition: uivwimp.cxx:287
virtual void SAL_CALL changedContents(const css::datatransfer::clipboard::ClipboardEvent &rEventObject) override
Definition: uivwimp.cxx:293
static bool IsPasteSpecial(const SwWrtShell &rWrtShell, const TransferableDataHelper &)
Definition: swdtflvr.cxx:3331
void Invalidate()
Definition: unotxvw.cxx:101
void AddClipboardListener()
Definition: uivwimp.cxx:200
ConversionMode mode
weld::Window * GetFrameWeld() const
css::uno::Reference< css::view::XSelectionSupplier > mxXTextView
Definition: uivwimp.hxx:88
sal_uInt16 GetSlot() const
weld::Window * GetFrameWeld() const
bool m_bPasteSpecialState
Definition: view.hxx:240
css::view::XSelectionSupplier * GetUNOObject()
Definition: uivwimp.cxx:88
Sequence< sal_Int8 > aSeq
void Invalidate()
Definition: uivwimp.cxx:209
SwScannerEventListener & GetScannerEventListener()
Definition: uivwimp.cxx:193
SwView_Impl(SwView *pShell)
Definition: uivwimp.cxx:50
void ScannerEventHdl()
Definition: view.cxx:1756
ShellMode eShellMode
Definition: uivwimp.hxx:100
std::unique_ptr< SfxMedium > CreateMedium()
Definition: uivwimp.cxx:260
~SwView_Impl()
Definition: uivwimp.cxx:61
void Ignore()
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage, bool bMobile=false)
SwView * pView
Definition: uivwimp.hxx:95
Definition: view.hxx:144