1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <vcl/event.hxx>
21 #include <vcl/svapp.hxx>
22 #include <vcl/wrkwin.hxx>
23 #include <vcl/msgbox.hxx>
24 #include <vcl/lstbox.hxx>
25 #include <comphelper/processfactory.hxx>
26 #include <cppuhelper/servicefactory.hxx>
27 #include <cppuhelper/implbase1.hxx>
28 #include <cppuhelper/implbase3.hxx>
29 #include <com/sun/star/lang/XComponent.hpp>
30 #include <com/sun/star/datatransfer/XTransferable.hpp>
31 #include <com/sun/star/datatransfer/clipboard/XClipboard.hpp>
32 #include <com/sun/star/datatransfer/dnd/XDropTarget.hpp>
33 #include <com/sun/star/datatransfer/dnd/XDragSourceListener.hpp>
34 #include <com/sun/star/datatransfer/dnd/XDropTargetListener.hpp>
35 #include <com/sun/star/datatransfer/dnd/XDragGestureRecognizer.hpp>
36 #include <com/sun/star/datatransfer/dnd/XDragGestureListener.hpp>
37 #include <osl/process.h>
41 using namespace ::rtl
;
42 using namespace ::com::sun::star::io
;
43 using namespace ::com::sun::star::uno
;
44 using namespace ::com::sun::star::lang
;
45 using namespace ::com::sun::star::datatransfer
;
46 using namespace ::com::sun::star::datatransfer::clipboard
;
47 using namespace ::com::sun::star::datatransfer::dnd
;
49 // -----------------------------------------------------------------------
51 class MyApp
: public Application
59 // -----------------------------------------------------------------------
61 class MyWin
: public WorkWindow
64 MyWin( Window
* pParent
, WinBits nWinStyle
);
66 void MouseMove( const MouseEvent
& rMEvt
);
67 void MouseButtonDown( const MouseEvent
& rMEvt
);
68 void MouseButtonUp( const MouseEvent
& rMEvt
);
69 void KeyInput( const KeyEvent
& rKEvt
);
70 void KeyUp( const KeyEvent
& rKEvt
);
71 void Paint( const Rectangle
& rRect
);
75 // -----------------------------------------------------------------------
77 class MyDragAndDropListener
: public ::cppu::WeakImplHelper3
< XDropTargetListener
, XDragGestureListener
, XDragSourceListener
>
83 MyDragAndDropListener( Window
* pWindow
) : m_pWindow( pWindow
) {};
85 virtual void SAL_CALL
dragGestureRecognized( const DragGestureEvent
& dge
) throw(RuntimeException
);
86 virtual void SAL_CALL
drop( const DropTargetDropEvent
& dtde
) throw(RuntimeException
);
87 virtual void SAL_CALL
dragEnter( const DropTargetDragEnterEvent
& dtde
) throw(RuntimeException
);
88 virtual void SAL_CALL
dragExit( const DropTargetEvent
& dte
) throw(RuntimeException
);
89 virtual void SAL_CALL
dragOver( const DropTargetDragEvent
& dtde
) throw(RuntimeException
);
90 virtual void SAL_CALL
dropActionChanged( const DropTargetDragEvent
& dtde
) throw(RuntimeException
);
91 virtual void SAL_CALL
dragDropEnd( const DragSourceDropEvent
& dsde
) throw(RuntimeException
);
92 virtual void SAL_CALL
dragEnter( const DragSourceDragEvent
& dsdee
) throw(RuntimeException
);
93 virtual void SAL_CALL
dragExit( const DragSourceEvent
& dse
) throw(RuntimeException
);
94 virtual void SAL_CALL
dragOver( const DragSourceDragEvent
& dsde
) throw(RuntimeException
);
95 virtual void SAL_CALL
dropActionChanged( const DragSourceDragEvent
& dsde
) throw(RuntimeException
);
96 virtual void SAL_CALL
disposing( const EventObject
& eo
) throw(RuntimeException
);
99 // -----------------------------------------------------------------------
101 class MyInfoBox
: public InfoBox
106 MyInfoBox( Window
* pParent
);
109 // -----------------------------------------------------------------------
111 class MyListBox
: public ListBox
116 MyListBox( Window
* pParent
);
119 // -----------------------------------------------------------------------
121 class StringTransferable
: public ::cppu::WeakImplHelper1
< XTransferable
>
123 const OUString m_aData
;
124 Sequence
< DataFlavor
> m_aFlavorList
;
127 StringTransferable( const OUString
& rString
) : m_aData( rString
), m_aFlavorList( 1 )
131 df
.MimeType
= OUString("text/plain;charset=utf-16");
132 df
.DataType
= getCppuType( static_cast < OUString
* > ( 0 ) );
134 m_aFlavorList
[0] = df
;
137 virtual Any SAL_CALL
getTransferData( const DataFlavor
& aFlavor
) throw(UnsupportedFlavorException
, IOException
, RuntimeException
);
138 virtual Sequence
< DataFlavor
> SAL_CALL
getTransferDataFlavors( ) throw(RuntimeException
);
139 virtual sal_Bool SAL_CALL
isDataFlavorSupported( const DataFlavor
& aFlavor
) throw(RuntimeException
);
143 // -----------------------------------------------------------------------
149 for( sal_Int32 n
= 0, nmax
= osl_getCommandArgCount(); n
< nmax
; n
++ )
153 osl_getCommandArg( n
, &aArg
.pData
);
155 if( aArg
.startsWith( "-r" ) )
158 osl_getCommandArg( ++n
, &aRegistry
.pData
);
162 Reference
< XMultiServiceFactory
> xServiceManager
;
164 if( aRegistry
.getLength() )
166 xServiceManager
= ::cppu::createRegistryServiceFactory( aRegistry
, sal_True
);
168 if( xServiceManager
.is() )
170 ::comphelper::setProcessServiceFactory( xServiceManager
);
173 if( ! xServiceManager
.is() )
174 printf( "No servicemanager available.\n" );
180 fprintf( stderr
, "Usage: %s -r full-path-to-applicat.rdb\n", "dnddemo" );
183 MyWin
aMainWin( NULL
, WB_APP
| WB_STDWORK
);
184 aMainWin
.SetText( OUString( "Drag And Drop - Workbench" ) );
187 // test the clipboard code
188 Reference
< XClipboard
> xClipboard
= aMainWin
.GetClipboard();
189 if( xClipboard
.is() )
191 printf( "System clipboard available.\n" );
192 xClipboard
->getContents();
195 fprintf( stderr
, "System clipboard not available.\n" );
197 MyInfoBox
aInfoBox( &aMainWin
);
200 MyListBox
aListBox( &aMainWin
);
201 aListBox
.SetPosSizePixel( 10, 10, 100, 100 );
202 aListBox
.InsertEntry( OUString("TestItem"));
207 Reference
< XComponent
> xComponent( xServiceManager
, UNO_QUERY
);
208 if( xComponent
.is() )
209 xComponent
->dispose();
213 // -----------------------------------------------------------------------
215 MyWin::MyWin( Window
* pParent
, WinBits nWinStyle
) :
216 WorkWindow( pParent
, nWinStyle
)
218 Reference
< XDropTargetListener
> xListener
= new MyDragAndDropListener( this );
220 Reference
< XDropTarget
> xDropTarget
= GetDropTarget();
221 if( xDropTarget
.is() )
223 xDropTarget
->addDropTargetListener( xListener
);
224 xDropTarget
->setActive( sal_True
);
227 Reference
< XDragGestureRecognizer
> xRecognizer
= GetDragGestureRecognizer();
228 if( xRecognizer
.is() )
229 xRecognizer
->addDragGestureListener( Reference
< XDragGestureListener
> ( xListener
, UNO_QUERY
) );
232 // -----------------------------------------------------------------------
234 void MyWin::MouseMove( const MouseEvent
& rMEvt
)
236 WorkWindow::MouseMove( rMEvt
);
239 // -----------------------------------------------------------------------
241 void MyWin::MouseButtonDown( const MouseEvent
& rMEvt
)
243 WorkWindow::MouseButtonDown( rMEvt
);
246 // -----------------------------------------------------------------------
248 void MyWin::MouseButtonUp( const MouseEvent
& rMEvt
)
250 WorkWindow::MouseButtonUp( rMEvt
);
253 // -----------------------------------------------------------------------
255 void MyWin::KeyInput( const KeyEvent
& rKEvt
)
257 WorkWindow::KeyInput( rKEvt
);
260 // -----------------------------------------------------------------------
262 void MyWin::KeyUp( const KeyEvent
& rKEvt
)
264 WorkWindow::KeyUp( rKEvt
);
267 // -----------------------------------------------------------------------
269 void MyWin::Paint( const Rectangle
& rRect
)
271 WorkWindow::Paint( rRect
);
274 // -----------------------------------------------------------------------
278 WorkWindow::Resize();
281 // -----------------------------------------------------------------------
283 void SAL_CALL
MyDragAndDropListener::dragGestureRecognized( const DragGestureEvent
& dge
) throw(RuntimeException
)
285 printf( "XDragGestureListener::dragGestureRecognized called ( Window: %p, %" SAL_PRIdINT32
", %" SAL_PRIdINT32
" ).\n", m_pWindow
, dge
.DragOriginX
, dge
.DragOriginY
);
287 Reference
< XDragSource
> xDragSource( dge
.DragSource
, UNO_QUERY
);
288 xDragSource
->startDrag( dge
, -1, 0, 0, new StringTransferable( OUString("TestString") ), this );
289 printf( "XDragSource::startDrag returned.\n" );
292 // -----------------------------------------------------------------------
294 void SAL_CALL
MyDragAndDropListener::drop( const DropTargetDropEvent
& dtde
) throw(RuntimeException
)
296 printf( "XDropTargetListener::drop called ( Window: %p, %" SAL_PRIdINT32
", %" SAL_PRIdINT32
" ).\n", m_pWindow
, dtde
.LocationX
, dtde
.LocationY
);
298 dtde
.Context
->dropComplete( sal_True
);
301 // -----------------------------------------------------------------------
303 void SAL_CALL
MyDragAndDropListener::dragEnter( const DropTargetDragEnterEvent
& dtdee
) throw(RuntimeException
)
305 printf( "XDropTargetListener::dragEnter called ( Window: %p, %" SAL_PRIdINT32
", %" SAL_PRIdINT32
" ).\n", m_pWindow
, dtdee
.LocationX
, dtdee
.LocationY
);
306 dtdee
.Context
->acceptDrag( dtdee
.DropAction
);
309 // -----------------------------------------------------------------------
311 void SAL_CALL
MyDragAndDropListener::dragExit( const DropTargetEvent
& ) throw(RuntimeException
)
313 printf( "XDropTargetListener::dragExit called ( Window: %p ).\n", m_pWindow
);
316 // -----------------------------------------------------------------------
318 void SAL_CALL
MyDragAndDropListener::dragOver( const DropTargetDragEvent
& dtde
) throw(RuntimeException
)
320 printf( "XDropTargetListener::dragOver called ( Window: %p, %" SAL_PRIdINT32
", %" SAL_PRIdINT32
" ).\n", m_pWindow
, dtde
.LocationX
, dtde
.LocationY
);
321 dtde
.Context
->acceptDrag( dtde
.DropAction
);
324 // -----------------------------------------------------------------------
326 void SAL_CALL
MyDragAndDropListener::dropActionChanged( const DropTargetDragEvent
& dtde
) throw(RuntimeException
)
328 printf( "XDropTargetListener::dropActionChanged called ( Window: %p, %" SAL_PRIdINT32
", %" SAL_PRIdINT32
" ).\n", m_pWindow
, dtde
.LocationX
, dtde
.LocationY
);
329 dtde
.Context
->acceptDrag( dtde
.DropAction
);
332 // -----------------------------------------------------------------------
334 void SAL_CALL
MyDragAndDropListener::dragDropEnd( const DragSourceDropEvent
& dsde
) throw(RuntimeException
)
336 printf( "XDragSourceListener::dropDropEnd called ( Window: %p, %s ).\n", m_pWindow
, dsde
.DropSuccess
? "success" : "failed" );
339 // -----------------------------------------------------------------------
341 void SAL_CALL
MyDragAndDropListener::dragEnter( const DragSourceDragEvent
& ) throw(RuntimeException
)
343 printf( "XDragSourceListener::dragEnter called ( Window: %p ).\n", m_pWindow
);
346 // -----------------------------------------------------------------------
348 void SAL_CALL
MyDragAndDropListener::dragExit( const DragSourceEvent
& ) throw(RuntimeException
)
350 printf( "XDragSourceListener::dragExit called ( Window: %p ).\n", m_pWindow
);
353 // -----------------------------------------------------------------------
355 void SAL_CALL
MyDragAndDropListener::dragOver( const DragSourceDragEvent
& ) throw(RuntimeException
)
357 printf( "XDragSourceListener::dragOver called ( Window: %p ).\n", m_pWindow
);
360 // -----------------------------------------------------------------------
362 void SAL_CALL
MyDragAndDropListener::dropActionChanged( const DragSourceDragEvent
& ) throw(RuntimeException
)
364 printf( "XDragSourceListener::dropActionChanged called ( Window: %p ).\n", m_pWindow
);
367 // -----------------------------------------------------------------------
369 void SAL_CALL
MyDragAndDropListener::disposing( const EventObject
& ) throw(RuntimeException
)
371 printf( "XEventListener::disposing called ( Window: %p ).\n", m_pWindow
);
374 // -----------------------------------------------------------------------
376 MyInfoBox::MyInfoBox( Window
* pParent
) : InfoBox( pParent
,
377 OUString("dragging over this box should result in another window id in the drag log.") )
379 Reference
< XDropTargetListener
> xListener
= new MyDragAndDropListener( this );
381 Reference
< XDropTarget
> xDropTarget
= GetDropTarget();
382 if( xDropTarget
.is() )
384 xDropTarget
->addDropTargetListener( xListener
);
385 xDropTarget
->setActive( sal_True
);
388 Reference
< XDragGestureRecognizer
> xRecognizer
= GetDragGestureRecognizer();
389 if( xRecognizer
.is() )
390 xRecognizer
->addDragGestureListener( Reference
< XDragGestureListener
> ( xListener
, UNO_QUERY
) );
393 // -----------------------------------------------------------------------
395 MyListBox::MyListBox( Window
* pParent
) : ListBox( pParent
)
397 Reference
< XDropTargetListener
> xListener
= new MyDragAndDropListener( this );
399 Reference
< XDropTarget
> xDropTarget
= GetDropTarget();
400 if( xDropTarget
.is() )
402 xDropTarget
->addDropTargetListener( xListener
);
403 xDropTarget
->setActive( sal_True
);
406 Reference
< XDragGestureRecognizer
> xRecognizer
= GetDragGestureRecognizer();
407 if( xRecognizer
.is() )
408 xRecognizer
->addDragGestureListener( Reference
< XDragGestureListener
> ( xListener
, UNO_QUERY
) );
411 // -----------------------------------------------------------------------
413 Any SAL_CALL
StringTransferable::getTransferData( const DataFlavor
& )
414 throw(UnsupportedFlavorException
, IOException
, RuntimeException
)
416 return makeAny( m_aData
);
419 // -----------------------------------------------------------------------
421 Sequence
< DataFlavor
> SAL_CALL
StringTransferable::getTransferDataFlavors( )
422 throw(RuntimeException
)
424 return m_aFlavorList
;
427 // -----------------------------------------------------------------------
429 sal_Bool SAL_CALL
StringTransferable::isDataFlavorSupported( const DataFlavor
& )
430 throw(RuntimeException
)
436 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */