update dev300-m58
[ooovba.git] / vcl / test / dndtest.cxx
blob114e9c8a6e31b650d7a61490f75c1fe3dfdc930b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: dndtest.cxx,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_vcl.hxx"
33 #include <vcl/event.hxx>
34 #include <vcl/svapp.hxx>
35 #include <vcl/wrkwin.hxx>
36 #include <vcl/msgbox.hxx>
37 #include <vcl/lstbox.hxx>
38 #include <comphelper/processfactory.hxx>
39 #include <cppuhelper/servicefactory.hxx>
40 #include <cppuhelper/implbase1.hxx>
41 #include <cppuhelper/implbase3.hxx>
42 #include <com/sun/star/lang/XComponent.hpp>
43 #include <com/sun/star/datatransfer/XTransferable.hpp>
44 #include <com/sun/star/datatransfer/clipboard/XClipboard.hpp>
45 #include <com/sun/star/datatransfer/dnd/XDropTarget.hpp>
46 #include <com/sun/star/datatransfer/dnd/XDragSourceListener.hpp>
47 #include <com/sun/star/datatransfer/dnd/XDropTargetListener.hpp>
48 #include <com/sun/star/datatransfer/dnd/XDragGestureRecognizer.hpp>
49 #include <com/sun/star/datatransfer/dnd/XDragGestureListener.hpp>
50 #include <vos/process.hxx>
52 #include <stdio.h>
54 using namespace ::rtl;
55 using namespace ::vos;
56 using namespace ::com::sun::star::io;
57 using namespace ::com::sun::star::uno;
58 using namespace ::com::sun::star::lang;
59 using namespace ::com::sun::star::datatransfer;
60 using namespace ::com::sun::star::datatransfer::clipboard;
61 using namespace ::com::sun::star::datatransfer::dnd;
63 // -----------------------------------------------------------------------
65 class MyApp : public Application
67 public:
68 void Main();
71 MyApp aMyApp;
73 // -----------------------------------------------------------------------
75 class MyWin : public WorkWindow
77 public:
78 MyWin( Window* pParent, WinBits nWinStyle );
80 void MouseMove( const MouseEvent& rMEvt );
81 void MouseButtonDown( const MouseEvent& rMEvt );
82 void MouseButtonUp( const MouseEvent& rMEvt );
83 void KeyInput( const KeyEvent& rKEvt );
84 void KeyUp( const KeyEvent& rKEvt );
85 void Paint( const Rectangle& rRect );
86 void Resize();
89 // -----------------------------------------------------------------------
91 class MyDragAndDropListener: public ::cppu::WeakImplHelper3 < XDropTargetListener, XDragGestureListener, XDragSourceListener >
93 Window * m_pWindow;
95 public:
97 MyDragAndDropListener( Window * pWindow ) : m_pWindow( pWindow ) {};
99 virtual void SAL_CALL dragGestureRecognized( const DragGestureEvent& dge ) throw(RuntimeException);
100 virtual void SAL_CALL drop( const DropTargetDropEvent& dtde ) throw(RuntimeException);
101 virtual void SAL_CALL dragEnter( const DropTargetDragEnterEvent& dtde ) throw(RuntimeException);
102 virtual void SAL_CALL dragExit( const DropTargetEvent& dte ) throw(RuntimeException);
103 virtual void SAL_CALL dragOver( const DropTargetDragEvent& dtde ) throw(RuntimeException);
104 virtual void SAL_CALL dropActionChanged( const DropTargetDragEvent& dtde ) throw(RuntimeException);
105 virtual void SAL_CALL dragDropEnd( const DragSourceDropEvent& dsde ) throw(RuntimeException);
106 virtual void SAL_CALL dragEnter( const DragSourceDragEvent& dsdee ) throw(RuntimeException);
107 virtual void SAL_CALL dragExit( const DragSourceEvent& dse ) throw(RuntimeException);
108 virtual void SAL_CALL dragOver( const DragSourceDragEvent& dsde ) throw(RuntimeException);
109 virtual void SAL_CALL dropActionChanged( const DragSourceDragEvent& dsde ) throw(RuntimeException);
110 virtual void SAL_CALL disposing( const EventObject& eo ) throw(RuntimeException);
113 // -----------------------------------------------------------------------
115 class MyInfoBox : public InfoBox
118 public:
120 MyInfoBox( Window* pParent );
123 // -----------------------------------------------------------------------
125 class MyListBox : public ListBox
128 public:
130 MyListBox( Window* pParent );
133 // -----------------------------------------------------------------------
135 class StringTransferable : public ::cppu::WeakImplHelper1< XTransferable >
137 const OUString m_aData;
138 Sequence< DataFlavor > m_aFlavorList;
140 public:
141 StringTransferable( const OUString& rString ) : m_aData( rString ), m_aFlavorList( 1 )
143 DataFlavor df;
145 df.MimeType = OUString::createFromAscii( "text/plain;charset=utf-16" );
146 df.DataType = getCppuType( static_cast < OUString * > ( 0 ) );
148 m_aFlavorList[0] = df;
151 virtual Any SAL_CALL getTransferData( const DataFlavor& aFlavor ) throw(UnsupportedFlavorException, IOException, RuntimeException);
152 virtual Sequence< DataFlavor > SAL_CALL getTransferDataFlavors( ) throw(RuntimeException);
153 virtual sal_Bool SAL_CALL isDataFlavorSupported( const DataFlavor& aFlavor ) throw(RuntimeException);
157 // -----------------------------------------------------------------------
159 void MyApp::Main()
161 OUString aRegistry;
162 OStartupInfo aInfo;
164 for( sal_Int32 n = 0, nmax = aInfo.getCommandArgCount(); n < nmax; n++ )
166 OUString aArg;
168 aInfo.getCommandArg( n, aArg );
170 if( aArg.compareTo( OUString::createFromAscii( "-r" ), 2 ) == 0 )
172 if ( n + 1 < nmax )
173 aInfo.getCommandArg( ++n, aRegistry );
177 Reference< XMultiServiceFactory > xServiceManager;
179 if( aRegistry.getLength() )
181 xServiceManager = ::cppu::createRegistryServiceFactory( aRegistry, sal_True );
183 if( xServiceManager.is() )
185 ::comphelper::setProcessServiceFactory( xServiceManager );
188 if( ! xServiceManager.is() )
189 printf( "No servicemanager available.\n" );
190 else
191 printf( "Ok\n" );
194 else
195 fprintf( stderr, "Usage: %s -r full-path-to-applicat.rdb\n", "dnddemo" );
198 MyWin aMainWin( NULL, WB_APP | WB_STDWORK );
199 aMainWin.SetText( XubString( RTL_CONSTASCII_USTRINGPARAM( "Drag And Drop - Workbench" ) ) );
200 aMainWin.Show();
202 // test the clipboard code
203 Reference< XClipboard > xClipboard = aMainWin.GetClipboard();
204 if( xClipboard.is() )
206 printf( "System clipboard available.\n" );
207 xClipboard->getContents();
209 else
210 fprintf( stderr, "System clipboard not available.\n" );
212 MyInfoBox aInfoBox( &aMainWin );
213 aInfoBox.Execute();
215 MyListBox aListBox( &aMainWin );
216 aListBox.SetPosSizePixel( 10, 10, 100, 100 );
217 aListBox.InsertEntry( OUString::createFromAscii( "TestItem" ));
218 aListBox.Show();
220 Execute();
222 Reference< XComponent > xComponent( xServiceManager, UNO_QUERY );
223 if( xComponent.is() )
224 xComponent->dispose();
228 // -----------------------------------------------------------------------
230 MyWin::MyWin( Window* pParent, WinBits nWinStyle ) :
231 WorkWindow( pParent, nWinStyle )
233 Reference< XDropTargetListener > xListener = new MyDragAndDropListener( this );
235 Reference< XDropTarget > xDropTarget = GetDropTarget();
236 if( xDropTarget.is() )
238 xDropTarget->addDropTargetListener( xListener );
239 xDropTarget->setActive( sal_True );
242 Reference< XDragGestureRecognizer > xRecognizer = GetDragGestureRecognizer();
243 if( xRecognizer.is() )
244 xRecognizer->addDragGestureListener( Reference< XDragGestureListener > ( xListener, UNO_QUERY ) );
247 // -----------------------------------------------------------------------
249 void MyWin::MouseMove( const MouseEvent& rMEvt )
251 WorkWindow::MouseMove( rMEvt );
254 // -----------------------------------------------------------------------
256 void MyWin::MouseButtonDown( const MouseEvent& rMEvt )
258 WorkWindow::MouseButtonDown( rMEvt );
261 // -----------------------------------------------------------------------
263 void MyWin::MouseButtonUp( const MouseEvent& rMEvt )
265 WorkWindow::MouseButtonUp( rMEvt );
268 // -----------------------------------------------------------------------
270 void MyWin::KeyInput( const KeyEvent& rKEvt )
272 WorkWindow::KeyInput( rKEvt );
275 // -----------------------------------------------------------------------
277 void MyWin::KeyUp( const KeyEvent& rKEvt )
279 WorkWindow::KeyUp( rKEvt );
282 // -----------------------------------------------------------------------
284 void MyWin::Paint( const Rectangle& rRect )
286 WorkWindow::Paint( rRect );
289 // -----------------------------------------------------------------------
291 void MyWin::Resize()
293 WorkWindow::Resize();
296 // -----------------------------------------------------------------------
298 void SAL_CALL MyDragAndDropListener::dragGestureRecognized( const DragGestureEvent& dge ) throw(RuntimeException)
300 printf( "XDragGestureListener::dragGestureRecognized called ( Window: %p, %"SAL_PRIdINT32", %"SAL_PRIdINT32" ).\n", m_pWindow, dge.DragOriginX, dge.DragOriginY );
302 Reference< XDragSource > xDragSource( dge.DragSource, UNO_QUERY );
303 xDragSource->startDrag( dge, -1, 0, 0, new StringTransferable( OUString::createFromAscii( "TestString" ) ), this );
304 printf( "XDragSource::startDrag returned.\n" );
307 // -----------------------------------------------------------------------
309 void SAL_CALL MyDragAndDropListener::drop( const DropTargetDropEvent& dtde ) throw(RuntimeException)
311 printf( "XDropTargetListener::drop called ( Window: %p, %"SAL_PRIdINT32", %"SAL_PRIdINT32" ).\n", m_pWindow, dtde.LocationX, dtde.LocationY );
313 dtde.Context->dropComplete( sal_True );
316 // -----------------------------------------------------------------------
318 void SAL_CALL MyDragAndDropListener::dragEnter( const DropTargetDragEnterEvent& dtdee ) throw(RuntimeException)
320 printf( "XDropTargetListener::dragEnter called ( Window: %p, %"SAL_PRIdINT32", %"SAL_PRIdINT32" ).\n", m_pWindow, dtdee.LocationX, dtdee.LocationY );
321 dtdee.Context->acceptDrag( dtdee.DropAction );
324 // -----------------------------------------------------------------------
326 void SAL_CALL MyDragAndDropListener::dragExit( const DropTargetEvent& ) throw(RuntimeException)
328 printf( "XDropTargetListener::dragExit called ( Window: %p ).\n", m_pWindow );
331 // -----------------------------------------------------------------------
333 void SAL_CALL MyDragAndDropListener::dragOver( const DropTargetDragEvent& dtde ) throw(RuntimeException)
335 printf( "XDropTargetListener::dragOver called ( Window: %p, %"SAL_PRIdINT32", %"SAL_PRIdINT32" ).\n", m_pWindow, dtde.LocationX, dtde.LocationY );
336 dtde.Context->acceptDrag( dtde.DropAction );
339 // -----------------------------------------------------------------------
341 void SAL_CALL MyDragAndDropListener::dropActionChanged( const DropTargetDragEvent& dtde ) throw(RuntimeException)
343 printf( "XDropTargetListener::dropActionChanged called ( Window: %p, %"SAL_PRIdINT32", %"SAL_PRIdINT32" ).\n", m_pWindow, dtde.LocationX, dtde.LocationY );
344 dtde.Context->acceptDrag( dtde.DropAction );
347 // -----------------------------------------------------------------------
349 void SAL_CALL MyDragAndDropListener::dragDropEnd( const DragSourceDropEvent& dsde ) throw(RuntimeException)
351 printf( "XDragSourceListener::dropDropEnd called ( Window: %p, %s ).\n", m_pWindow, dsde.DropSuccess ? "sucess" : "failed" );
354 // -----------------------------------------------------------------------
356 void SAL_CALL MyDragAndDropListener::dragEnter( const DragSourceDragEvent& ) throw(RuntimeException)
358 printf( "XDragSourceListener::dragEnter called ( Window: %p ).\n", m_pWindow );
361 // -----------------------------------------------------------------------
363 void SAL_CALL MyDragAndDropListener::dragExit( const DragSourceEvent& ) throw(RuntimeException)
365 printf( "XDragSourceListener::dragExit called ( Window: %p ).\n", m_pWindow );
368 // -----------------------------------------------------------------------
370 void SAL_CALL MyDragAndDropListener::dragOver( const DragSourceDragEvent& ) throw(RuntimeException)
372 printf( "XDragSourceListener::dragOver called ( Window: %p ).\n", m_pWindow );
375 // -----------------------------------------------------------------------
377 void SAL_CALL MyDragAndDropListener::dropActionChanged( const DragSourceDragEvent& ) throw(RuntimeException)
379 printf( "XDragSourceListener::dropActionChanged called ( Window: %p ).\n", m_pWindow );
382 // -----------------------------------------------------------------------
384 void SAL_CALL MyDragAndDropListener::disposing( const EventObject& ) throw(RuntimeException)
386 printf( "XEventListener::disposing called ( Window: %p ).\n", m_pWindow );
389 // -----------------------------------------------------------------------
391 MyInfoBox::MyInfoBox( Window* pParent ) : InfoBox( pParent,
392 OUString::createFromAscii( "dragging over this box should result in another window id in the drag log." ) )
394 Reference< XDropTargetListener > xListener = new MyDragAndDropListener( this );
396 Reference< XDropTarget > xDropTarget = GetDropTarget();
397 if( xDropTarget.is() )
399 xDropTarget->addDropTargetListener( xListener );
400 xDropTarget->setActive( sal_True );
403 Reference< XDragGestureRecognizer > xRecognizer = GetDragGestureRecognizer();
404 if( xRecognizer.is() )
405 xRecognizer->addDragGestureListener( Reference< XDragGestureListener > ( xListener, UNO_QUERY ) );
408 // -----------------------------------------------------------------------
410 MyListBox::MyListBox( Window* pParent ) : ListBox( pParent )
412 Reference< XDropTargetListener > xListener = new MyDragAndDropListener( this );
414 Reference< XDropTarget > xDropTarget = GetDropTarget();
415 if( xDropTarget.is() )
417 xDropTarget->addDropTargetListener( xListener );
418 xDropTarget->setActive( sal_True );
421 Reference< XDragGestureRecognizer > xRecognizer = GetDragGestureRecognizer();
422 if( xRecognizer.is() )
423 xRecognizer->addDragGestureListener( Reference< XDragGestureListener > ( xListener, UNO_QUERY ) );
426 // -----------------------------------------------------------------------
428 Any SAL_CALL StringTransferable::getTransferData( const DataFlavor& )
429 throw(UnsupportedFlavorException, IOException, RuntimeException)
431 return makeAny( m_aData );
434 // -----------------------------------------------------------------------
436 Sequence< DataFlavor > SAL_CALL StringTransferable::getTransferDataFlavors( )
437 throw(RuntimeException)
439 return m_aFlavorList;
442 // -----------------------------------------------------------------------
444 sal_Bool SAL_CALL StringTransferable::isDataFlavorSupported( const DataFlavor& )
445 throw(RuntimeException)
447 return sal_True;