Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sw / source / uibase / uiview / uivwimp.cxx
blob06871734d844b8b6f4828f4c089076e7db591af4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <config_features.h>
21 #include <config_fuzzers.h>
23 #include <cmdid.h>
25 #include <com/sun/star/scanner/XScannerManager2.hpp>
26 #include <com/sun/star/datatransfer/clipboard/XClipboard.hpp>
27 #include <comphelper/propertysequence.hxx>
28 #include <comphelper/servicehelper.hxx>
29 #include <vcl/weld.hxx>
30 #include <vcl/svapp.hxx>
31 #include <sfx2/viewfrm.hxx>
32 #include <sfx2/bindings.hxx>
33 #include <sfx2/docfile.hxx>
35 #include <sfx2/docinsert.hxx>
36 #include <sfx2/request.hxx>
37 #include <uivwimp.hxx>
38 #include <unotxvw.hxx>
39 #include <unodispatch.hxx>
40 #include <swmodule.hxx>
41 #include <swdtflvr.hxx>
43 #include <strings.hrc>
45 using namespace ::com::sun::star;
46 using namespace ::com::sun::star::uno;
47 using namespace ::com::sun::star::scanner;
48 using namespace ::com::sun::star::lang;
49 using namespace ::com::sun::star::datatransfer::clipboard;
51 SwView_Impl::SwView_Impl(SwView* pShell)
52 : m_pView(pShell)
53 , m_eShellMode(ShellMode::Text)
54 , m_nParam(0)
55 , m_bSelectObject(false)
56 , m_bEditingPositionSet(false)
58 mxXTextView = new SwXTextView(m_pView);
59 m_xDispatchProviderInterceptor = new SwXDispatchProviderInterceptor(*m_pView);
62 SwView_Impl::~SwView_Impl()
64 if(m_xDispatchProviderInterceptor)
65 m_xDispatchProviderInterceptor->Invalidate();
66 mxXTextView->Invalidate();
67 mxXTextView.clear();
68 if( mxScanEvtLstnr.is() )
69 mxScanEvtLstnr->ViewDestroyed();
70 if( mxClipEvtLstnr.is() )
72 mxClipEvtLstnr->AddRemoveListener( false );
73 mxClipEvtLstnr->ViewDestroyed();
75 #if HAVE_FEATURE_DBCONNECTIVITY && !ENABLE_FUZZERS
76 m_xConfigItem.reset();
77 #endif
78 m_pDocInserter.reset();
79 m_pRequest.reset();
82 void SwView_Impl::SetShellMode(ShellMode eSet)
84 m_eShellMode = eSet;
87 view::XSelectionSupplier* SwView_Impl::GetUNOObject()
89 return mxXTextView.get();
92 SwXTextView* SwView_Impl::GetUNOObject_Impl()
94 return mxXTextView.get();
97 void SwView_Impl::ExecuteScan( SfxRequest& rReq )
99 switch(rReq.GetSlot())
101 case SID_TWAIN_SELECT:
103 bool bDone = false;
104 Reference< XScannerManager2 > xScanMgr = SW_MOD()->GetScannerManager();
106 if( xScanMgr.is() )
110 SwScannerEventListener& rListener = GetScannerEventListener();
111 const Sequence< ScannerContext >
112 aContexts( xScanMgr->getAvailableScanners() );
114 if( aContexts.hasElements() )
116 Reference< XEventListener > xLstner = &rListener;
117 ScannerContext aContext( aContexts.getConstArray()[ 0 ] );
119 Reference<lang::XInitialization> xInit(xScanMgr, UNO_QUERY);
120 if (xInit.is())
122 // initialize dialog
123 weld::Window* pWindow = rReq.GetFrameWeld();
124 uno::Sequence<uno::Any> aSeq(comphelper::InitAnyPropertySequence(
126 {"ParentWindow", pWindow ? uno::Any(pWindow->GetXWindow()) : uno::Any(Reference<awt::XWindow>())}
127 }));
128 xInit->initialize( aSeq );
131 bDone = xScanMgr->configureScannerAndScan( aContext, xLstner );
134 catch(...)
139 if( bDone )
140 rReq.Done();
141 else
143 rReq.Ignore();
146 break;
148 case SID_TWAIN_TRANSFER:
150 bool bDone = false;
152 Reference< XScannerManager2 > xScanMgr = SW_MOD()->GetScannerManager();
153 if( xScanMgr.is() )
155 SwScannerEventListener& rListener = GetScannerEventListener();
158 const Sequence< scanner::ScannerContext >aContexts( xScanMgr->getAvailableScanners() );
159 if( aContexts.hasElements() )
161 Reference< XEventListener > xLstner = &rListener;
162 xScanMgr->startScan( aContexts.getConstArray()[ 0 ], xLstner );
163 bDone = true;
166 catch(...)
171 if( !bDone )
173 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(rReq.GetFrameWeld(),
174 VclMessageType::Info, VclButtonsType::Ok,
175 SwResId(STR_SCAN_NOSOURCE)));
176 xBox->run();
177 rReq.Ignore();
179 else
181 rReq.Done();
182 SfxBindings& rBind = m_pView->GetViewFrame().GetBindings();
183 rBind.Invalidate( SID_TWAIN_SELECT );
184 rBind.Invalidate( SID_TWAIN_TRANSFER );
187 break;
191 SwScannerEventListener& SwView_Impl::GetScannerEventListener()
193 if(!mxScanEvtLstnr.is())
194 mxScanEvtLstnr = new SwScannerEventListener(*m_pView);
195 return *mxScanEvtLstnr;
198 void SwView_Impl::AddClipboardListener()
200 if(!mxClipEvtLstnr.is())
202 mxClipEvtLstnr = new SwClipboardChangeListener( *m_pView );
203 mxClipEvtLstnr->AddRemoveListener( true );
207 void SwView_Impl::Invalidate()
209 GetUNOObject_Impl()->Invalidate();
210 for (const auto& xTransferable: mxTransferables)
212 rtl::Reference<SwTransferable> pTransferable = xTransferable.get();
213 if(pTransferable)
214 pTransferable->Invalidate();
218 void SwView_Impl::AddTransferable(SwTransferable& rTransferable)
220 //prevent removing of the non-referenced SwTransferable
221 osl_atomic_increment(&rTransferable.m_refCount);
223 // Remove previously added, but no longer existing weak references.
224 mxTransferables.erase(std::remove_if(mxTransferables.begin(), mxTransferables.end(),
225 [](const unotools::WeakReference<SwTransferable>& rTunnel) {
226 return !rTunnel.get();
227 }), mxTransferables.end());
229 mxTransferables.emplace_back(&rTransferable);
231 osl_atomic_decrement(&rTransferable.m_refCount);
234 void SwView_Impl::StartDocumentInserter(
235 const OUString& rFactory,
236 const Link<sfx2::FileDialogHelper*,void>& rEndDialogHdl,
237 const sal_uInt16 nSlotId
240 sfx2::DocumentInserter::Mode mode {sfx2::DocumentInserter::Mode::Insert};
241 switch( nSlotId )
243 case SID_DOCUMENT_MERGE:
244 mode = sfx2::DocumentInserter::Mode::Merge;
245 break;
246 case SID_DOCUMENT_COMPARE:
247 mode = sfx2::DocumentInserter::Mode::Compare;
248 break;
249 default:
250 break;
253 m_pDocInserter.reset(new ::sfx2::DocumentInserter(m_pView->GetFrameWeld(), rFactory, mode));
254 m_pDocInserter->StartExecuteModal( rEndDialogHdl );
257 std::unique_ptr<SfxMedium> SwView_Impl::CreateMedium()
259 return m_pDocInserter->CreateMedium();
262 void SwView_Impl::InitRequest( const SfxRequest& rRequest )
264 m_pRequest.reset(new SfxRequest( rRequest ));
267 SwScannerEventListener::~SwScannerEventListener()
271 void SAL_CALL SwScannerEventListener::disposing( const EventObject& /*rEventObject*/)
273 #if defined(_WIN32) || defined UNX
274 SolarMutexGuard aGuard;
275 if( m_pView )
276 m_pView->ScannerEventHdl();
277 #endif
280 SwClipboardChangeListener::~SwClipboardChangeListener()
284 void SAL_CALL SwClipboardChangeListener::disposing( const EventObject& /*rEventObject*/ )
286 SolarMutexGuard aGuard;
287 m_pView = nullptr; // so we don't touch the view if changedContents somehow fires afterwards
290 void SAL_CALL SwClipboardChangeListener::changedContents( const css::datatransfer::clipboard::ClipboardEvent& rEventObject )
293 const SolarMutexGuard aGuard;
294 if( !m_pView )
295 return;
298 TransferableDataHelper aDataHelper( rEventObject.Contents );
299 SwWrtShell& rSh = m_pView->GetWrtShell();
301 m_pView->m_nLastPasteDestination = SwTransferable::GetSotDestination( rSh );
302 m_pView->m_bPasteState = aDataHelper.GetXTransferable().is() &&
303 SwTransferable::IsPaste( rSh, aDataHelper );
305 m_pView->m_bPasteSpecialState = aDataHelper.GetXTransferable().is() &&
306 SwTransferable::IsPasteSpecial( rSh, aDataHelper );
309 SfxBindings& rBind = m_pView->GetViewFrame().GetBindings();
310 rBind.Invalidate( SID_PASTE );
311 rBind.Invalidate( SID_PASTE_SPECIAL );
312 rBind.Invalidate( SID_CLIPBOARD_FORMAT_ITEMS );
315 void SwClipboardChangeListener::AddRemoveListener( bool bAdd )
317 m_pView->AddRemoveClipboardListener( Reference< XClipboardListener >( this ), bAdd );
320 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */