tdf#148168 set built-in gtk search box to search "document name" column
[LibreOffice.git] / vcl / qt5 / QtDragAndDrop.cxx
blobe05993718753bfd0b3d6f439752acd4fe5b2ea51
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 */
11 #include <com/sun/star/awt/MouseButton.hpp>
12 #include <com/sun/star/datatransfer/DataFlavor.hpp>
13 #include <com/sun/star/datatransfer/dnd/DNDConstants.hpp>
14 #include <cppuhelper/supportsservice.hxx>
15 #include <sal/log.hxx>
17 #include <QtDragAndDrop.hxx>
18 #include <QtFrame.hxx>
19 #include <QtTransferable.hxx>
20 #include <QtWidget.hxx>
22 #include <QtGui/QDrag>
24 using namespace com::sun::star;
26 QtDragSource::~QtDragSource() {}
28 void QtDragSource::deinitialize() { m_pFrame = nullptr; }
30 sal_Bool QtDragSource::isDragImageSupported() { return true; }
32 sal_Int32 QtDragSource::getDefaultCursor(sal_Int8) { return 0; }
34 void QtDragSource::initialize(const css::uno::Sequence<css::uno::Any>& rArguments)
36 if (rArguments.getLength() < 2)
38 throw uno::RuntimeException("DragSource::initialize: Cannot install window event handler",
39 static_cast<OWeakObject*>(this));
42 sal_IntPtr nFrame = 0;
43 rArguments.getConstArray()[1] >>= nFrame;
45 if (!nFrame)
47 throw uno::RuntimeException("DragSource::initialize: missing SalFrame",
48 static_cast<OWeakObject*>(this));
51 m_pFrame = reinterpret_cast<QtFrame*>(nFrame);
52 m_pFrame->registerDragSource(this);
55 void QtDragSource::startDrag(
56 const datatransfer::dnd::DragGestureEvent& /*rEvent*/, sal_Int8 sourceActions,
57 sal_Int32 /*cursor*/, sal_Int32 /*image*/,
58 const css::uno::Reference<css::datatransfer::XTransferable>& rTrans,
59 const css::uno::Reference<css::datatransfer::dnd::XDragSourceListener>& rListener)
61 m_xListener = rListener;
63 if (m_pFrame)
65 QDrag* drag = new QDrag(m_pFrame->GetQWidget());
66 drag->setMimeData(new QtMimeData(rTrans));
67 // just a reminder that exec starts a nested event loop, so everything after
68 // this call is just executed, after D'n'D has finished!
69 drag->exec(toQtDropActions(sourceActions), getPreferredDropAction(sourceActions));
72 // the drop will eventually call fire_dragEnd, which will clear the listener.
73 // if D'n'D ends without success, we just get a leave event without any indicator,
74 // but the event loop will be terminated, so we have to try to inform the source of
75 // a failure in any way.
76 fire_dragEnd(datatransfer::dnd::DNDConstants::ACTION_NONE, false);
79 void QtDragSource::fire_dragEnd(sal_Int8 nAction, bool bDropSuccessful)
81 if (!m_xListener.is())
82 return;
84 datatransfer::dnd::DragSourceDropEvent aEv;
85 aEv.DropAction = nAction;
86 aEv.DropSuccess = bDropSuccessful;
88 auto xListener = m_xListener;
89 m_xListener.clear();
90 xListener->dragDropEnd(aEv);
93 OUString SAL_CALL QtDragSource::getImplementationName()
95 return "com.sun.star.datatransfer.dnd.VclQtDragSource";
98 sal_Bool SAL_CALL QtDragSource::supportsService(OUString const& ServiceName)
100 return cppu::supportsService(this, ServiceName);
103 css::uno::Sequence<OUString> SAL_CALL QtDragSource::getSupportedServiceNames()
105 return { "com.sun.star.datatransfer.dnd.QtDragSource" };
108 QtDropTarget::QtDropTarget()
109 : WeakComponentImplHelper(m_aMutex)
110 , m_pFrame(nullptr)
111 , m_bActive(false)
112 , m_nDefaultActions(0)
116 OUString SAL_CALL QtDropTarget::getImplementationName()
118 return "com.sun.star.datatransfer.dnd.VclQtDropTarget";
121 sal_Bool SAL_CALL QtDropTarget::supportsService(OUString const& ServiceName)
123 return cppu::supportsService(this, ServiceName);
126 css::uno::Sequence<OUString> SAL_CALL QtDropTarget::getSupportedServiceNames()
128 return { "com.sun.star.datatransfer.dnd.QtDropTarget" };
131 QtDropTarget::~QtDropTarget() {}
133 void QtDropTarget::deinitialize()
135 m_pFrame = nullptr;
136 m_bActive = false;
139 void QtDropTarget::initialize(const uno::Sequence<uno::Any>& rArguments)
141 if (rArguments.getLength() < 2)
143 throw uno::RuntimeException("DropTarget::initialize: Cannot install window event handler",
144 static_cast<OWeakObject*>(this));
147 sal_IntPtr nFrame = 0;
148 rArguments.getConstArray()[1] >>= nFrame;
150 if (!nFrame)
152 throw uno::RuntimeException("DropTarget::initialize: missing SalFrame",
153 static_cast<OWeakObject*>(this));
156 m_nDropAction = datatransfer::dnd::DNDConstants::ACTION_NONE;
158 m_pFrame = reinterpret_cast<QtFrame*>(nFrame);
159 m_pFrame->registerDropTarget(this);
160 m_bActive = true;
163 void QtDropTarget::addDropTargetListener(
164 const uno::Reference<css::datatransfer::dnd::XDropTargetListener>& xListener)
166 ::osl::Guard<::osl::Mutex> aGuard(m_aMutex);
168 m_aListeners.push_back(xListener);
171 void QtDropTarget::removeDropTargetListener(
172 const uno::Reference<css::datatransfer::dnd::XDropTargetListener>& xListener)
174 ::osl::Guard<::osl::Mutex> aGuard(m_aMutex);
176 m_aListeners.erase(std::remove(m_aListeners.begin(), m_aListeners.end(), xListener),
177 m_aListeners.end());
180 sal_Bool QtDropTarget::isActive() { return m_bActive; }
182 void QtDropTarget::setActive(sal_Bool bActive) { m_bActive = bActive; }
184 sal_Int8 QtDropTarget::getDefaultActions() { return m_nDefaultActions; }
186 void QtDropTarget::setDefaultActions(sal_Int8 nDefaultActions)
188 m_nDefaultActions = nDefaultActions;
191 void QtDropTarget::fire_dragEnter(const css::datatransfer::dnd::DropTargetDragEnterEvent& dtde)
193 osl::ClearableGuard<::osl::Mutex> aGuard(m_aMutex);
194 std::vector<css::uno::Reference<css::datatransfer::dnd::XDropTargetListener>> aListeners(
195 m_aListeners);
196 aGuard.clear();
198 for (auto const& listener : aListeners)
200 listener->dragEnter(dtde);
204 void QtDropTarget::fire_dragOver(const css::datatransfer::dnd::DropTargetDragEnterEvent& dtde)
206 osl::ClearableGuard<::osl::Mutex> aGuard(m_aMutex);
207 std::vector<css::uno::Reference<css::datatransfer::dnd::XDropTargetListener>> aListeners(
208 m_aListeners);
209 aGuard.clear();
211 for (auto const& listener : aListeners)
212 listener->dragOver(dtde);
215 void QtDropTarget::fire_drop(const css::datatransfer::dnd::DropTargetDropEvent& dtde)
217 m_bDropSuccessful = true;
219 osl::ClearableGuard<osl::Mutex> aGuard(m_aMutex);
220 std::vector<css::uno::Reference<css::datatransfer::dnd::XDropTargetListener>> aListeners(
221 m_aListeners);
222 aGuard.clear();
224 for (auto const& listener : aListeners)
225 listener->drop(dtde);
228 void QtDropTarget::fire_dragExit(const css::datatransfer::dnd::DropTargetEvent& dte)
230 osl::ClearableGuard<::osl::Mutex> aGuard(m_aMutex);
231 std::vector<css::uno::Reference<css::datatransfer::dnd::XDropTargetListener>> aListeners(
232 m_aListeners);
233 aGuard.clear();
235 for (auto const& listener : aListeners)
236 listener->dragExit(dte);
239 void QtDropTarget::acceptDrag(sal_Int8 dragOperation) { m_nDropAction = dragOperation; }
241 void QtDropTarget::rejectDrag() { m_nDropAction = 0; }
243 void QtDropTarget::acceptDrop(sal_Int8 dropOperation) { m_nDropAction = dropOperation; }
245 void QtDropTarget::rejectDrop() { m_nDropAction = 0; }
247 void QtDropTarget::dropComplete(sal_Bool success)
249 m_bDropSuccessful = (m_bDropSuccessful && success);
252 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */