build fix
[LibreOffice.git] / vcl / qa / cppunit / dndtest.cxx
blob66cc9a12c3d783220255badc9f690f19ba3932db
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 <unotest/filters-test.hxx>
21 #include <test/bootstrapfixture.hxx>
23 #include <vcl/event.hxx>
24 #include <vcl/svapp.hxx>
25 #include <vcl/wrkwin.hxx>
26 #include <vcl/msgbox.hxx>
27 #include <vcl/lstbox.hxx>
29 #include <cppuhelper/implbase.hxx>
31 #include <com/sun/star/lang/XComponent.hpp>
32 #include <com/sun/star/datatransfer/XTransferable.hpp>
33 #include <com/sun/star/datatransfer/clipboard/XClipboard.hpp>
34 #include <com/sun/star/datatransfer/dnd/XDropTarget.hpp>
35 #include <com/sun/star/datatransfer/dnd/XDragSourceListener.hpp>
36 #include <com/sun/star/datatransfer/dnd/XDropTargetListener.hpp>
37 #include <com/sun/star/datatransfer/dnd/XDragGestureRecognizer.hpp>
38 #include <com/sun/star/datatransfer/dnd/XDragGestureListener.hpp>
40 using namespace ::com::sun::star::io;
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::lang;
43 using namespace ::com::sun::star::datatransfer;
44 using namespace ::com::sun::star::datatransfer::clipboard;
45 using namespace ::com::sun::star::datatransfer::dnd;
47 class MyWin : public WorkWindow
49 public:
50 MyWin( vcl::Window* pParent, WinBits nWinStyle );
52 void MouseMove( const MouseEvent& rMEvt );
53 void MouseButtonDown( const MouseEvent& rMEvt );
54 void MouseButtonUp( const MouseEvent& rMEvt );
55 void KeyInput( const KeyEvent& rKEvt );
56 void KeyUp( const KeyEvent& rKEvt );
57 void Paint( const Rectangle& rRect );
58 void Resize();
61 class MyDragAndDropListener: public ::cppu::WeakImplHelper < XDropTargetListener, XDragGestureListener, XDragSourceListener >
63 vcl::Window * m_pWindow;
65 public:
67 explicit MyDragAndDropListener( vcl::Window * pWindow ) : m_pWindow( pWindow ) {};
69 virtual void SAL_CALL dragGestureRecognized( const DragGestureEvent& dge ) throw(RuntimeException);
70 virtual void SAL_CALL drop( const DropTargetDropEvent& dtde ) throw(RuntimeException);
71 virtual void SAL_CALL dragEnter( const DropTargetDragEnterEvent& dtde ) throw(RuntimeException);
72 virtual void SAL_CALL dragExit( const DropTargetEvent& dte ) throw(RuntimeException);
73 virtual void SAL_CALL dragOver( const DropTargetDragEvent& dtde ) throw(RuntimeException);
74 virtual void SAL_CALL dropActionChanged( const DropTargetDragEvent& dtde ) throw(RuntimeException);
75 virtual void SAL_CALL dragDropEnd( const DragSourceDropEvent& dsde ) throw(RuntimeException);
76 virtual void SAL_CALL dragEnter( const DragSourceDragEvent& dsdee ) throw(RuntimeException);
77 virtual void SAL_CALL dragExit( const DragSourceEvent& dse ) throw(RuntimeException);
78 virtual void SAL_CALL dragOver( const DragSourceDragEvent& dsde ) throw(RuntimeException);
79 virtual void SAL_CALL dropActionChanged( const DragSourceDragEvent& dsde ) throw(RuntimeException);
80 virtual void SAL_CALL disposing( const EventObject& eo ) throw(RuntimeException);
83 class MyInfoBox : public InfoBox
86 public:
88 explicit MyInfoBox( vcl::Window* pParent );
91 class MyListBox : public ListBox
94 public:
96 explicit MyListBox( vcl::Window* pParent );
99 class StringTransferable : public ::cppu::WeakImplHelper< XTransferable >
101 const OUString m_aData;
102 Sequence< DataFlavor > m_aFlavorList;
104 public:
105 explicit StringTransferable( const OUString& rString ) : m_aData( rString ), m_aFlavorList( 1 )
107 DataFlavor df;
109 df.MimeType = "text/plain;charset=utf-16";
110 df.DataType = cppu::UnoType<OUString>::get();
112 m_aFlavorList[0] = df;
115 virtual Any SAL_CALL getTransferData( const DataFlavor& aFlavor ) throw(UnsupportedFlavorException, IOException, RuntimeException);
116 virtual Sequence< DataFlavor > SAL_CALL getTransferDataFlavors( ) throw(RuntimeException);
117 virtual bool SAL_CALL isDataFlavorSupported( const DataFlavor& aFlavor ) throw(RuntimeException);
120 class VclDnDTest : public test::BootstrapFixture
122 public:
123 VclDnDTest() : BootstrapFixture(true, false) {}
125 /// Play with drag and drop
126 void testDnD();
128 CPPUNIT_TEST_SUITE(VclDnDTest);
129 CPPUNIT_TEST(testDnD);
130 CPPUNIT_TEST_SUITE_END();
133 void VclDnDTest::testDnD()
135 MyWin aMainWin( NULL, WB_APP | WB_STDWORK );
136 aMainWin.SetText( OUString( "Drag And Drop - Workbench" ) );
137 aMainWin.Show();
139 // test the clipboard code
140 Reference< XClipboard > xClipboard = aMainWin.GetClipboard();
141 CPPUNIT_ASSERT_MESSAGE("System clipboard not available",
142 xClipboard.is());
144 MyInfoBox aInfoBox( &aMainWin );
145 aInfoBox.Execute();
147 MyListBox aListBox( &aMainWin );
148 aListBox.setPosSizePixel( 10, 10, 100, 100 );
149 aListBox.InsertEntry( OUString("TestItem"));
150 aListBox.Show();
153 MyWin::MyWin( vcl::Window* pParent, WinBits nWinStyle ) :
154 WorkWindow( pParent, nWinStyle )
156 Reference< XDropTargetListener > xListener = new MyDragAndDropListener( this );
158 Reference< XDropTarget > xDropTarget = GetDropTarget();
159 if( xDropTarget.is() )
161 xDropTarget->addDropTargetListener( xListener );
162 xDropTarget->setActive( true );
165 Reference< XDragGestureRecognizer > xRecognizer = GetDragGestureRecognizer();
166 if( xRecognizer.is() )
167 xRecognizer->addDragGestureListener( Reference< XDragGestureListener > ( xListener, UNO_QUERY ) );
170 void MyWin::MouseMove( const MouseEvent& rMEvt )
172 WorkWindow::MouseMove( rMEvt );
175 void MyWin::MouseButtonDown( const MouseEvent& rMEvt )
177 WorkWindow::MouseButtonDown( rMEvt );
180 void MyWin::MouseButtonUp( const MouseEvent& rMEvt )
182 WorkWindow::MouseButtonUp( rMEvt );
185 void MyWin::KeyInput( const KeyEvent& rKEvt )
187 WorkWindow::KeyInput( rKEvt );
190 void MyWin::KeyUp( const KeyEvent& rKEvt )
192 WorkWindow::KeyUp( rKEvt );
195 void MyWin::Paint( const Rectangle& rRect )
197 WorkWindow::Paint( rRect );
200 void MyWin::Resize()
202 WorkWindow::Resize();
205 void SAL_CALL MyDragAndDropListener::dragGestureRecognized( const DragGestureEvent& dge ) throw(RuntimeException)
207 Reference< XDragSource > xDragSource( dge.DragSource, UNO_QUERY );
208 xDragSource->startDrag( dge, -1, 0, 0, new StringTransferable( OUString("TestString") ), this );
211 void SAL_CALL MyDragAndDropListener::drop( const DropTargetDropEvent& dtde ) throw(RuntimeException)
213 dtde.Context->dropComplete( true );
216 void SAL_CALL MyDragAndDropListener::dragEnter( const DropTargetDragEnterEvent& dtdee ) throw(RuntimeException)
218 dtdee.Context->acceptDrag( dtdee.DropAction );
221 void SAL_CALL MyDragAndDropListener::dragExit( const DropTargetEvent& ) throw(RuntimeException)
225 void SAL_CALL MyDragAndDropListener::dragOver( const DropTargetDragEvent& dtde ) throw(RuntimeException)
227 dtde.Context->acceptDrag( dtde.DropAction );
230 void SAL_CALL MyDragAndDropListener::dropActionChanged( const DropTargetDragEvent& dtde ) throw(RuntimeException)
232 dtde.Context->acceptDrag( dtde.DropAction );
235 void SAL_CALL MyDragAndDropListener::dragDropEnd( const DragSourceDropEvent& ) throw(RuntimeException)
239 void SAL_CALL MyDragAndDropListener::dragEnter( const DragSourceDragEvent& ) throw(RuntimeException)
243 void SAL_CALL MyDragAndDropListener::dragExit( const DragSourceEvent& ) throw(RuntimeException)
247 void SAL_CALL MyDragAndDropListener::dragOver( const DragSourceDragEvent& ) throw(RuntimeException)
251 void SAL_CALL MyDragAndDropListener::dropActionChanged( const DragSourceDragEvent& ) throw(RuntimeException)
255 void SAL_CALL MyDragAndDropListener::disposing( const EventObject& ) throw(RuntimeException)
259 MyInfoBox::MyInfoBox( vcl::Window* pParent ) : InfoBox( pParent,
260 OUString("dragging over this box should result in another window id in the drag log.") )
262 Reference< XDropTargetListener > xListener = new MyDragAndDropListener( this );
264 Reference< XDropTarget > xDropTarget = GetDropTarget();
265 if( xDropTarget.is() )
267 xDropTarget->addDropTargetListener( xListener );
268 xDropTarget->setActive( true );
271 Reference< XDragGestureRecognizer > xRecognizer = GetDragGestureRecognizer();
272 if( xRecognizer.is() )
273 xRecognizer->addDragGestureListener( Reference< XDragGestureListener > ( xListener, UNO_QUERY ) );
276 MyListBox::MyListBox( vcl::Window* pParent ) : ListBox( pParent )
278 Reference< XDropTargetListener > xListener = new MyDragAndDropListener( this );
280 Reference< XDropTarget > xDropTarget = GetDropTarget();
281 if( xDropTarget.is() )
283 xDropTarget->addDropTargetListener( xListener );
284 xDropTarget->setActive( true );
287 Reference< XDragGestureRecognizer > xRecognizer = GetDragGestureRecognizer();
288 if( xRecognizer.is() )
289 xRecognizer->addDragGestureListener( Reference< XDragGestureListener > ( xListener, UNO_QUERY ) );
292 Any SAL_CALL StringTransferable::getTransferData( const DataFlavor& )
293 throw(UnsupportedFlavorException, IOException, RuntimeException)
295 return makeAny( m_aData );
298 Sequence< DataFlavor > SAL_CALL StringTransferable::getTransferDataFlavors( )
299 throw(RuntimeException)
301 return m_aFlavorList;
304 bool SAL_CALL StringTransferable::isDataFlavorSupported( const DataFlavor& )
305 throw(RuntimeException)
307 return true;
310 CPPUNIT_TEST_SUITE_REGISTRATION(VclDnDTest);
312 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */