1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: dndTest.cxx,v $
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_dtrans.hxx"
35 #pragma warning(push,1)
47 #include <com/sun/star/uno/Reference.h>
48 #include <com/sun/star/lang/XInitialization.hpp>
49 #include <com/sun/star/datatransfer/dnd/XDropTarget.hpp>
50 #include <com/sun/star/datatransfer/dnd/DNDConstants.hpp>
52 #include <com/sun/star/lang/XInitialization.hpp>
53 #include <com/sun/star/lang/XComponent.hpp>
54 #include <rtl/process.h>
55 #include <cppuhelper/servicefactory.hxx>
56 //#include "transferable.hxx"
57 #include "sourcelistener.hxx"
60 #include "atlwindow.hxx"
61 BEGIN_OBJECT_MAP(ObjectMap
)
64 using namespace com::sun::star::lang
;
65 using namespace com::sun::star::datatransfer
;
66 using namespace com::sun::star::uno
;
67 using namespace com::sun::star::datatransfer::dnd
;
68 using namespace com::sun::star::datatransfer::dnd::DNDConstants
;
71 // defined in atlwindow.hxx
72 // #define WM_SOURCE_INIT WM_APP+100
73 // #define WM_SOURCE_STARTDRAG WM_APP+101
74 #define WM_CREATE_MTA_WND
77 DWORD WINAPI
MTAFunc( void* threadData
);
79 Reference
< XMultiServiceFactory
> MultiServiceFactory
;
80 //int APIENTRY WinMain(HINSTANCE hInstance,
81 // HINSTANCE hPrevInstance,
84 //int _tmain( int argc, TCHAR *argv[ ], TCHAR *envp[ ] )
85 int main( int argc
, char *argv
[ ], char *envp
[ ] )
88 if( FAILED( hr
=CoInitialize(NULL
)))
90 _tprintf(_T("CoInitialize failed \n"));
95 _Module
.Init( ObjectMap
, GetModuleHandle( NULL
));
97 if( FAILED(hr
=doTest()))
100 const TCHAR
* errMsg
= err
.ErrorMessage();
101 // MessageBox( NULL, errMsg, "Test failed", MB_ICONERROR);
113 MultiServiceFactory
= createRegistryServiceFactory( OUString(L
"types.rdb"), OUString( L
"services.rdb") , sal_True
);
115 // create the MTA thread that is used to realize MTA calls to the services
116 // We create the thread and wait until the thread has created its message queue
117 HANDLE evt
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
119 HANDLE hMTAThread
= CreateThread( NULL
, 0, MTAFunc
, &evt
, 0, &threadIdMTA
);
120 WaitForSingleObject( evt
, INFINITE
);
125 RECT pos1
={0,0,300,200};
126 AWindow
win(_T("DnD starting in Ole STA"), threadIdMTA
, pos1
);
128 RECT pos2
={ 0, 205, 300, 405};
129 AWindow
win2( _T("DnD starting in MTA"), threadIdMTA
, pos2
, true);
131 // win3 and win4 call initialize from an MTA but they are created in an STA
132 RECT pos3
={300,0,600,200};
133 AWindow
win3(_T("DnD starting in OLE STA"), threadIdMTA
, pos3
, false, true);
135 RECT pos4
={ 300, 205, 600, 405};
136 AWindow
win24( _T("DnD starting in Ole MTA"), threadIdMTA
, pos4
, true, true);
140 while( GetMessage(&msg
, (HWND
)NULL
, 0, 0) )
142 TranslateMessage( &msg
);
143 DispatchMessage( &msg
);
146 // Shut down the MTA thread
147 PostThreadMessage( threadIdMTA
, WM_QUIT
, 0, 0);
148 WaitForSingleObject(hMTAThread
, INFINITE
);
149 CloseHandle(hMTAThread
);
154 extern Reference
<XMultiServiceFactory
> MultiServiceFactory
;
155 DWORD WINAPI
MTAFunc( void* threadData
)
158 hr
= CoInitializeEx( NULL
, COINIT_MULTITHREADED
);
159 ATLASSERT( FAILED(hr
) );
161 // force the creation of a message queue
162 PeekMessage(&msg
, NULL
, WM_USER
, WM_USER
, PM_NOREMOVE
);
163 SetEvent( *(HANDLE
*)threadData
);
165 RECT pos
={0, 406, 300, 605};
166 AWindow
win(_T("DnD, full MTA"), GetCurrentThreadId(), pos
, false, true);
167 // ThreadData data= *( ThreadData*)pParams;
168 // SetEvent(data.evtThreadReady);
169 while( GetMessage(&msg
, (HWND
)NULL
, 0, 0) )
175 InitializationData
* pData
= (InitializationData
*)msg
.wParam
;
177 any
<<= (sal_uInt32
) pData
->hWnd
;
178 pData
->xInit
->initialize( Sequence
<Any
>( &any
, 1));
180 CoTaskMemFree( pData
);
183 case WM_SOURCE_STARTDRAG
:
185 // wParam contains necessary data
186 StartDragData
* pData
= (StartDragData
*)msg
.wParam
;
187 Sequence
<DataFlavor
> seq
= pData
->transferable
->getTransferDataFlavors();
188 // have a look what flavours are supported
189 for( int i
=0; i
<seq
.getLength(); i
++)
191 DataFlavor d
= seq
[i
];
193 pData
->source
->startDrag( DragGestureEvent(),
194 ACTION_LINK
|ACTION_MOVE
|ACTION_COPY
,
198 Reference
<XDragSourceListener
>( static_cast<XDragSourceListener
*>
199 ( new DragSourceListener())));
200 CoTaskMemFree( pData
);
206 TranslateMessage( &msg
);
207 DispatchMessage( &msg
);