xref: /aoo41x/main/vcl/test/dndtest.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_vcl.hxx"
30 #include <vcl/event.hxx>
31 #include <vcl/svapp.hxx>
32 #include <vcl/wrkwin.hxx>
33 #include <vcl/msgbox.hxx>
34 #include <vcl/lstbox.hxx>
35 #include <comphelper/processfactory.hxx>
36 #include <cppuhelper/servicefactory.hxx>
37 #include <cppuhelper/implbase1.hxx>
38 #include <cppuhelper/implbase3.hxx>
39 #include <com/sun/star/lang/XComponent.hpp>
40 #include <com/sun/star/datatransfer/XTransferable.hpp>
41 #include <com/sun/star/datatransfer/clipboard/XClipboard.hpp>
42 #include <com/sun/star/datatransfer/dnd/XDropTarget.hpp>
43 #include <com/sun/star/datatransfer/dnd/XDragSourceListener.hpp>
44 #include <com/sun/star/datatransfer/dnd/XDropTargetListener.hpp>
45 #include <com/sun/star/datatransfer/dnd/XDragGestureRecognizer.hpp>
46 #include <com/sun/star/datatransfer/dnd/XDragGestureListener.hpp>
47 #include <vos/process.hxx>
48 
49 #include <stdio.h>
50 
51 using namespace ::rtl;
52 using namespace ::vos;
53 using namespace ::com::sun::star::io;
54 using namespace ::com::sun::star::uno;
55 using namespace ::com::sun::star::lang;
56 using namespace ::com::sun::star::datatransfer;
57 using namespace ::com::sun::star::datatransfer::clipboard;
58 using namespace ::com::sun::star::datatransfer::dnd;
59 
60 // -----------------------------------------------------------------------
61 
62 class MyApp : public Application
63 {
64 public:
65 	void		Main();
66 };
67 
68 MyApp aMyApp;
69 
70 // -----------------------------------------------------------------------
71 
72 class MyWin : public WorkWindow
73 {
74 public:
75 				MyWin( Window* pParent, WinBits nWinStyle );
76 
77 	void		MouseMove( const MouseEvent& rMEvt );
78 	void		MouseButtonDown( const MouseEvent& rMEvt );
79 	void		MouseButtonUp( const MouseEvent& rMEvt );
80 	void		KeyInput( const KeyEvent& rKEvt );
81 	void		KeyUp( const KeyEvent& rKEvt );
82 	void		Paint( const Rectangle& rRect );
83 	void		Resize();
84 };
85 
86 // -----------------------------------------------------------------------
87 
88 class MyDragAndDropListener: public ::cppu::WeakImplHelper3 < XDropTargetListener, XDragGestureListener, XDragSourceListener >
89 {
90 	Window * m_pWindow;
91 
92 public:
93 
94 	MyDragAndDropListener( Window * pWindow ) : m_pWindow( pWindow ) {};
95 
96 	virtual void SAL_CALL dragGestureRecognized( const DragGestureEvent& dge ) throw(RuntimeException);
97 	virtual void SAL_CALL drop( const DropTargetDropEvent& dtde ) throw(RuntimeException);
98 	virtual void SAL_CALL dragEnter( const DropTargetDragEnterEvent& dtde ) throw(RuntimeException);
99 	virtual void SAL_CALL dragExit( const DropTargetEvent& dte ) throw(RuntimeException);
100 	virtual void SAL_CALL dragOver( const DropTargetDragEvent& dtde ) throw(RuntimeException);
101 	virtual void SAL_CALL dropActionChanged( const DropTargetDragEvent& dtde ) throw(RuntimeException);
102     virtual void SAL_CALL dragDropEnd( const DragSourceDropEvent& dsde ) throw(RuntimeException);
103     virtual void SAL_CALL dragEnter( const DragSourceDragEvent& dsdee ) throw(RuntimeException);
104     virtual void SAL_CALL dragExit( const DragSourceEvent& dse ) throw(RuntimeException);
105     virtual void SAL_CALL dragOver( const DragSourceDragEvent& dsde ) throw(RuntimeException);
106     virtual void SAL_CALL dropActionChanged( const DragSourceDragEvent& dsde ) throw(RuntimeException);
107 	virtual void SAL_CALL disposing( const EventObject& eo ) throw(RuntimeException);
108 };
109 
110 // -----------------------------------------------------------------------
111 
112 class MyInfoBox : public InfoBox
113 {
114 
115 public:
116 
117 	MyInfoBox( Window* pParent );
118 };
119 
120 // -----------------------------------------------------------------------
121 
122 class MyListBox : public ListBox
123 {
124 
125 public:
126 
127 	MyListBox( Window* pParent );
128 };
129 
130 // -----------------------------------------------------------------------
131 
132 class StringTransferable : public ::cppu::WeakImplHelper1< XTransferable >
133 {
134 	const OUString         m_aData;
135 	Sequence< DataFlavor > m_aFlavorList;
136 
137 public:
138 	StringTransferable( const OUString& rString ) : m_aData( rString ), m_aFlavorList( 1 )
139 	{
140 		DataFlavor df;
141 
142 		df.MimeType = OUString::createFromAscii( "text/plain;charset=utf-16" );
143 		df.DataType = getCppuType( static_cast < OUString * > ( 0 ) );
144 
145 	 	m_aFlavorList[0] = df;
146 	};
147 
148 	virtual Any SAL_CALL getTransferData( const DataFlavor& aFlavor ) throw(UnsupportedFlavorException, IOException, RuntimeException);
149     virtual Sequence< DataFlavor > SAL_CALL getTransferDataFlavors(  ) throw(RuntimeException);
150 	virtual sal_Bool SAL_CALL isDataFlavorSupported( const DataFlavor& aFlavor ) throw(RuntimeException);
151 };
152 
153 
154 // -----------------------------------------------------------------------
155 
156 void MyApp::Main()
157 {
158 	OUString aRegistry;
159 	OStartupInfo aInfo;
160 
161 	for( sal_Int32 n = 0, nmax = aInfo.getCommandArgCount(); n < nmax; n++ )
162 	{
163 		OUString aArg;
164 
165 		aInfo.getCommandArg( n, aArg );
166 
167 		if( aArg.compareTo( OUString::createFromAscii( "-r" ), 2 ) == 0 )
168 		{
169 			if ( n + 1 < nmax )
170 				aInfo.getCommandArg( ++n, aRegistry );
171 		}
172 	}
173 
174 	Reference< XMultiServiceFactory > xServiceManager;
175 
176 	if( aRegistry.getLength() )
177 	{
178 		xServiceManager = ::cppu::createRegistryServiceFactory( aRegistry, sal_True );
179 
180 		if( xServiceManager.is() )
181 		{
182 			::comphelper::setProcessServiceFactory( xServiceManager );
183 		}
184 
185 		if( ! xServiceManager.is() )
186 			printf( "No servicemanager available.\n" );
187 		else
188 			printf( "Ok\n" );
189 
190 	}
191 	else
192 		fprintf( stderr, "Usage: %s -r full-path-to-applicat.rdb\n", "dnddemo" );
193 
194 
195 	MyWin aMainWin( NULL, WB_APP | WB_STDWORK );
196 	aMainWin.SetText( XubString( RTL_CONSTASCII_USTRINGPARAM( "Drag And Drop - Workbench" ) ) );
197 	aMainWin.Show();
198 
199 	// test the clipboard code
200 	Reference< XClipboard > xClipboard = aMainWin.GetClipboard();
201 	if( xClipboard.is() )
202 	{
203 		printf( "System clipboard available.\n" );
204 		xClipboard->getContents();
205 	}
206 	else
207 		fprintf( stderr, "System clipboard not available.\n" );
208 
209 	MyInfoBox aInfoBox( &aMainWin );
210 	aInfoBox.Execute();
211 
212 	MyListBox aListBox( &aMainWin );
213 	aListBox.SetPosSizePixel( 10, 10, 100, 100 );
214 	aListBox.InsertEntry( OUString::createFromAscii( "TestItem" ));
215 	aListBox.Show();
216 
217 	Execute();
218 
219 	Reference< XComponent > xComponent( xServiceManager, UNO_QUERY );
220 	if( xComponent.is() )
221 		xComponent->dispose();
222 
223 }
224 
225 // -----------------------------------------------------------------------
226 
227 MyWin::MyWin( Window* pParent, WinBits nWinStyle ) :
228 	WorkWindow( pParent, nWinStyle )
229 {
230 	Reference< XDropTargetListener > xListener = new MyDragAndDropListener( this );
231 
232 	Reference< XDropTarget > xDropTarget = GetDropTarget();
233 	if( xDropTarget.is() )
234 	{
235 		xDropTarget->addDropTargetListener( xListener );
236 		xDropTarget->setActive( sal_True );
237 	}
238 
239 	Reference< XDragGestureRecognizer > xRecognizer = GetDragGestureRecognizer();
240 	if( xRecognizer.is() )
241 		xRecognizer->addDragGestureListener( Reference< XDragGestureListener > ( xListener, UNO_QUERY ) );
242 }
243 
244 // -----------------------------------------------------------------------
245 
246 void MyWin::MouseMove( const MouseEvent& rMEvt )
247 {
248 	WorkWindow::MouseMove( rMEvt );
249 }
250 
251 // -----------------------------------------------------------------------
252 
253 void MyWin::MouseButtonDown( const MouseEvent& rMEvt )
254 {
255 	WorkWindow::MouseButtonDown( rMEvt );
256 }
257 
258 // -----------------------------------------------------------------------
259 
260 void MyWin::MouseButtonUp( const MouseEvent& rMEvt )
261 {
262 	WorkWindow::MouseButtonUp( rMEvt );
263 }
264 
265 // -----------------------------------------------------------------------
266 
267 void MyWin::KeyInput( const KeyEvent& rKEvt )
268 {
269 	WorkWindow::KeyInput( rKEvt );
270 }
271 
272 // -----------------------------------------------------------------------
273 
274 void MyWin::KeyUp( const KeyEvent& rKEvt )
275 {
276 	WorkWindow::KeyUp( rKEvt );
277 }
278 
279 // -----------------------------------------------------------------------
280 
281 void MyWin::Paint( const Rectangle& rRect )
282 {
283 	WorkWindow::Paint( rRect );
284 }
285 
286 // -----------------------------------------------------------------------
287 
288 void MyWin::Resize()
289 {
290 	WorkWindow::Resize();
291 }
292 
293 // -----------------------------------------------------------------------
294 
295 void SAL_CALL MyDragAndDropListener::dragGestureRecognized( const DragGestureEvent& dge ) throw(RuntimeException)
296 {
297 	printf( "XDragGestureListener::dragGestureRecognized called ( Window: %p, %"SAL_PRIdINT32", %"SAL_PRIdINT32" ).\n", m_pWindow, dge.DragOriginX, dge.DragOriginY );
298 
299 	Reference< XDragSource > xDragSource( dge.DragSource, UNO_QUERY );
300 	xDragSource->startDrag( dge, -1, 0, 0, new StringTransferable( OUString::createFromAscii( "TestString" ) ), this );
301 	printf( "XDragSource::startDrag returned.\n" );
302 }
303 
304 // -----------------------------------------------------------------------
305 
306 void SAL_CALL MyDragAndDropListener::drop( const DropTargetDropEvent& dtde ) throw(RuntimeException)
307 {
308 	printf( "XDropTargetListener::drop called ( Window: %p, %"SAL_PRIdINT32", %"SAL_PRIdINT32" ).\n", m_pWindow, dtde.LocationX, dtde.LocationY );
309 
310 	dtde.Context->dropComplete( sal_True );
311 }
312 
313 // -----------------------------------------------------------------------
314 
315 void SAL_CALL MyDragAndDropListener::dragEnter( const DropTargetDragEnterEvent& dtdee ) throw(RuntimeException)
316 {
317 	printf( "XDropTargetListener::dragEnter called ( Window: %p, %"SAL_PRIdINT32", %"SAL_PRIdINT32" ).\n", m_pWindow, dtdee.LocationX, dtdee.LocationY );
318 	dtdee.Context->acceptDrag( dtdee.DropAction );
319 }
320 
321 // -----------------------------------------------------------------------
322 
323 void SAL_CALL MyDragAndDropListener::dragExit( const DropTargetEvent& ) throw(RuntimeException)
324 {
325 	printf( "XDropTargetListener::dragExit called ( Window: %p ).\n", m_pWindow );
326 }
327 
328 // -----------------------------------------------------------------------
329 
330 void SAL_CALL MyDragAndDropListener::dragOver( const DropTargetDragEvent& dtde ) throw(RuntimeException)
331 {
332 	printf( "XDropTargetListener::dragOver called ( Window: %p, %"SAL_PRIdINT32", %"SAL_PRIdINT32" ).\n", m_pWindow, dtde.LocationX, dtde.LocationY );
333 	dtde.Context->acceptDrag( dtde.DropAction );
334 }
335 
336 // -----------------------------------------------------------------------
337 
338 void SAL_CALL MyDragAndDropListener::dropActionChanged( const DropTargetDragEvent& dtde ) throw(RuntimeException)
339 {
340 	printf( "XDropTargetListener::dropActionChanged called ( Window: %p, %"SAL_PRIdINT32", %"SAL_PRIdINT32" ).\n", m_pWindow, dtde.LocationX, dtde.LocationY );
341 	dtde.Context->acceptDrag( dtde.DropAction );
342 }
343 
344 // -----------------------------------------------------------------------
345 
346 void SAL_CALL MyDragAndDropListener::dragDropEnd( const DragSourceDropEvent& dsde ) throw(RuntimeException)
347 {
348 	printf( "XDragSourceListener::dropDropEnd called ( Window: %p, %s ).\n", m_pWindow, dsde.DropSuccess ? "sucess" : "failed" );
349 }
350 
351 // -----------------------------------------------------------------------
352 
353 void SAL_CALL MyDragAndDropListener::dragEnter( const DragSourceDragEvent& ) throw(RuntimeException)
354 {
355 	printf( "XDragSourceListener::dragEnter called ( Window: %p ).\n", m_pWindow );
356 }
357 
358 // -----------------------------------------------------------------------
359 
360 void SAL_CALL MyDragAndDropListener::dragExit( const DragSourceEvent& ) throw(RuntimeException)
361 {
362 	printf( "XDragSourceListener::dragExit called ( Window: %p ).\n", m_pWindow );
363 }
364 
365 // -----------------------------------------------------------------------
366 
367 void SAL_CALL MyDragAndDropListener::dragOver( const DragSourceDragEvent& ) throw(RuntimeException)
368 {
369 	printf( "XDragSourceListener::dragOver called ( Window: %p ).\n", m_pWindow );
370 }
371 
372 // -----------------------------------------------------------------------
373 
374 void SAL_CALL MyDragAndDropListener::dropActionChanged( const DragSourceDragEvent& ) throw(RuntimeException)
375 {
376 	printf( "XDragSourceListener::dropActionChanged called ( Window: %p ).\n", m_pWindow );
377 }
378 
379 // -----------------------------------------------------------------------
380 
381 void SAL_CALL MyDragAndDropListener::disposing( const EventObject& ) throw(RuntimeException)
382 {
383 	printf( "XEventListener::disposing called ( Window: %p ).\n", m_pWindow );
384 }
385 
386 // -----------------------------------------------------------------------
387 
388 MyInfoBox::MyInfoBox( Window* pParent ) : InfoBox( pParent,
389 	OUString::createFromAscii( "dragging over this box should result in another window id in the drag log." ) )
390 {
391 	Reference< XDropTargetListener > xListener = new MyDragAndDropListener( this );
392 
393 	Reference< XDropTarget > xDropTarget = GetDropTarget();
394 	if( xDropTarget.is() )
395 	{
396 		xDropTarget->addDropTargetListener( xListener );
397 		xDropTarget->setActive( sal_True );
398 	}
399 
400 	Reference< XDragGestureRecognizer > xRecognizer = GetDragGestureRecognizer();
401 	if( xRecognizer.is() )
402 		xRecognizer->addDragGestureListener( Reference< XDragGestureListener > ( xListener, UNO_QUERY ) );
403 };
404 
405 // -----------------------------------------------------------------------
406 
407 MyListBox::MyListBox( Window* pParent ) : ListBox( pParent )
408 {
409 	Reference< XDropTargetListener > xListener = new MyDragAndDropListener( this );
410 
411 	Reference< XDropTarget > xDropTarget = GetDropTarget();
412 	if( xDropTarget.is() )
413 	{
414 		xDropTarget->addDropTargetListener( xListener );
415 		xDropTarget->setActive( sal_True );
416 	}
417 
418 	Reference< XDragGestureRecognizer > xRecognizer = GetDragGestureRecognizer();
419 	if( xRecognizer.is() )
420 		xRecognizer->addDragGestureListener( Reference< XDragGestureListener > ( xListener, UNO_QUERY ) );
421 };
422 
423 // -----------------------------------------------------------------------
424 
425 Any SAL_CALL StringTransferable::getTransferData( const DataFlavor& )
426 	throw(UnsupportedFlavorException, IOException, RuntimeException)
427 {
428 	return makeAny( m_aData );
429 }
430 
431 // -----------------------------------------------------------------------
432 
433 Sequence< DataFlavor > SAL_CALL StringTransferable::getTransferDataFlavors(  )
434 	throw(RuntimeException)
435 {
436 	return m_aFlavorList;
437 }
438 
439 // -----------------------------------------------------------------------
440 
441 sal_Bool SAL_CALL StringTransferable::isDataFlavorSupported( const DataFlavor& )
442 	throw(RuntimeException)
443 {
444 	return sal_True;
445 }
446 
447 
448