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