xref: /aoo41x/main/dtrans/test/win32/dnd/dndTest.cxx (revision 48123e16)
1*48123e16SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*48123e16SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*48123e16SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*48123e16SAndrew Rist  * distributed with this work for additional information
6*48123e16SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*48123e16SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*48123e16SAndrew Rist  * "License"); you may not use this file except in compliance
9*48123e16SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*48123e16SAndrew Rist  *
11*48123e16SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*48123e16SAndrew Rist  *
13*48123e16SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*48123e16SAndrew Rist  * software distributed under the License is distributed on an
15*48123e16SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*48123e16SAndrew Rist  * KIND, either express or implied.  See the License for the
17*48123e16SAndrew Rist  * specific language governing permissions and limitations
18*48123e16SAndrew Rist  * under the License.
19*48123e16SAndrew Rist  *
20*48123e16SAndrew Rist  *************************************************************/
21*48123e16SAndrew Rist 
22*48123e16SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_dtrans.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #if defined _MSC_VER
28cdf0e10cSrcweir #pragma warning(push,1)
29cdf0e10cSrcweir #endif
30cdf0e10cSrcweir #include <windows.h>
31cdf0e10cSrcweir #include <comdef.h>
32cdf0e10cSrcweir #include <tchar.h>
33cdf0e10cSrcweir #include <atlbase.h>
34cdf0e10cSrcweir CComModule _Module;
35cdf0e10cSrcweir #include<atlcom.h>
36cdf0e10cSrcweir #include<atlimpl.cpp>
37cdf0e10cSrcweir #if defined _MSC_VER
38cdf0e10cSrcweir #pragma warning(pop)
39cdf0e10cSrcweir #endif
40cdf0e10cSrcweir #include <com/sun/star/uno/Reference.h>
41cdf0e10cSrcweir #include <com/sun/star/lang/XInitialization.hpp>
42cdf0e10cSrcweir #include <com/sun/star/datatransfer/dnd/XDropTarget.hpp>
43cdf0e10cSrcweir #include <com/sun/star/datatransfer/dnd/DNDConstants.hpp>
44cdf0e10cSrcweir 
45cdf0e10cSrcweir #include <com/sun/star/lang/XInitialization.hpp>
46cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
47cdf0e10cSrcweir #include <rtl/process.h>
48cdf0e10cSrcweir #include <cppuhelper/servicefactory.hxx>
49cdf0e10cSrcweir //#include "transferable.hxx"
50cdf0e10cSrcweir #include "sourcelistener.hxx"
51cdf0e10cSrcweir 
52cdf0e10cSrcweir 
53cdf0e10cSrcweir #include "atlwindow.hxx"
54cdf0e10cSrcweir BEGIN_OBJECT_MAP(ObjectMap)
55cdf0e10cSrcweir END_OBJECT_MAP()
56cdf0e10cSrcweir 
57cdf0e10cSrcweir using namespace com::sun::star::lang;
58cdf0e10cSrcweir using namespace com::sun::star::datatransfer;
59cdf0e10cSrcweir using namespace com::sun::star::uno;
60cdf0e10cSrcweir using namespace com::sun::star::datatransfer::dnd;
61cdf0e10cSrcweir using namespace com::sun::star::datatransfer::dnd::DNDConstants;
62cdf0e10cSrcweir using namespace rtl;
63cdf0e10cSrcweir 
64cdf0e10cSrcweir // defined in atlwindow.hxx
65cdf0e10cSrcweir // #define WM_SOURCE_INIT WM_APP+100
66cdf0e10cSrcweir // #define WM_SOURCE_STARTDRAG WM_APP+101
67cdf0e10cSrcweir #define WM_CREATE_MTA_WND
68cdf0e10cSrcweir 
69cdf0e10cSrcweir HRESULT doTest();
70cdf0e10cSrcweir DWORD WINAPI MTAFunc( void* threadData);
71cdf0e10cSrcweir 
72cdf0e10cSrcweir Reference< XMultiServiceFactory > MultiServiceFactory;
73cdf0e10cSrcweir //int APIENTRY WinMain(HINSTANCE hInstance,
74cdf0e10cSrcweir //                     HINSTANCE hPrevInstance,
75cdf0e10cSrcweir //                     LPSTR     lpCmdLine,
76cdf0e10cSrcweir //                     int       nCmdShow)
77cdf0e10cSrcweir //int _tmain( int argc, TCHAR *argv[ ], TCHAR *envp[ ] )
main(int argc,char * argv[],char * envp[])78cdf0e10cSrcweir int main( int argc, char *argv[ ], char *envp[ ] )
79cdf0e10cSrcweir {
80cdf0e10cSrcweir 	HRESULT hr;
81cdf0e10cSrcweir 	if( FAILED( hr=CoInitialize(NULL )))
82cdf0e10cSrcweir 	{
83cdf0e10cSrcweir 		_tprintf(_T("CoInitialize failed \n"));
84cdf0e10cSrcweir 		return -1;
85cdf0e10cSrcweir 	}
86cdf0e10cSrcweir 
87cdf0e10cSrcweir 
88cdf0e10cSrcweir 	_Module.Init( ObjectMap, GetModuleHandle( NULL));
89cdf0e10cSrcweir 
90cdf0e10cSrcweir 	if( FAILED(hr=doTest()))
91cdf0e10cSrcweir 	{
92cdf0e10cSrcweir 		_com_error err( hr);
93cdf0e10cSrcweir 		const TCHAR * errMsg= err.ErrorMessage();
94cdf0e10cSrcweir //		MessageBox( NULL, errMsg, "Test failed", MB_ICONERROR);
95cdf0e10cSrcweir 	}
96cdf0e10cSrcweir 
97cdf0e10cSrcweir 
98cdf0e10cSrcweir 	_Module.Term();
99cdf0e10cSrcweir 	CoUninitialize();
100cdf0e10cSrcweir 	return 0;
101cdf0e10cSrcweir }
102cdf0e10cSrcweir 
doTest()103cdf0e10cSrcweir HRESULT doTest()
104cdf0e10cSrcweir {
105cdf0e10cSrcweir 
106cdf0e10cSrcweir 	MultiServiceFactory= createRegistryServiceFactory( OUString(L"types.rdb"), OUString( L"services.rdb") , sal_True);
107cdf0e10cSrcweir 
108cdf0e10cSrcweir 	// create the MTA thread that is used to realize MTA calls to the services
109cdf0e10cSrcweir 	// We create the thread and wait until the thread has created its message queue
110cdf0e10cSrcweir 	HANDLE evt= CreateEvent(NULL, FALSE, FALSE, NULL);
111cdf0e10cSrcweir 	DWORD threadIdMTA=0;
112cdf0e10cSrcweir 	HANDLE hMTAThread= CreateThread( NULL, 0, MTAFunc, &evt, 0, &threadIdMTA);
113cdf0e10cSrcweir 	WaitForSingleObject( evt, INFINITE);
114cdf0e10cSrcweir 	CloseHandle(evt);
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 
117cdf0e10cSrcweir 	HRESULT hr= S_OK;
118cdf0e10cSrcweir 	RECT pos1={0,0,300,200};
119cdf0e10cSrcweir 	AWindow win(_T("DnD starting in Ole STA"), threadIdMTA, pos1);
120cdf0e10cSrcweir 
121cdf0e10cSrcweir 	RECT pos2={ 0, 205, 300, 405};
122cdf0e10cSrcweir 	AWindow win2( _T("DnD starting in MTA"), threadIdMTA, pos2, true);
123cdf0e10cSrcweir 
124cdf0e10cSrcweir 	// win3 and win4 call initialize from an MTA but they are created in an STA
125cdf0e10cSrcweir 	RECT pos3={300,0,600,200};
126cdf0e10cSrcweir 	AWindow win3(_T("DnD starting in OLE STA"), threadIdMTA, pos3, false, true);
127cdf0e10cSrcweir 
128cdf0e10cSrcweir 	RECT pos4={ 300, 205, 600, 405};
129cdf0e10cSrcweir 	AWindow win24( _T("DnD starting in Ole MTA"), threadIdMTA, pos4, true, true);
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 
132cdf0e10cSrcweir 	MSG msg;
133cdf0e10cSrcweir 	while( GetMessage(&msg, (HWND)NULL, 0, 0) )
134cdf0e10cSrcweir 	{
135cdf0e10cSrcweir 		TranslateMessage(  &msg);
136cdf0e10cSrcweir 		DispatchMessage( &msg);
137cdf0e10cSrcweir 	}
138cdf0e10cSrcweir 
139cdf0e10cSrcweir 	// Shut down the MTA thread
140cdf0e10cSrcweir 	PostThreadMessage( threadIdMTA, WM_QUIT, 0, 0);
141cdf0e10cSrcweir 	WaitForSingleObject(hMTAThread, INFINITE);
142cdf0e10cSrcweir 	CloseHandle(hMTAThread);
143cdf0e10cSrcweir 
144cdf0e10cSrcweir 	return S_OK;
145cdf0e10cSrcweir }
146cdf0e10cSrcweir 
147cdf0e10cSrcweir extern Reference<XMultiServiceFactory> MultiServiceFactory;
MTAFunc(void * threadData)148cdf0e10cSrcweir DWORD WINAPI MTAFunc( void* threadData)
149cdf0e10cSrcweir {
150cdf0e10cSrcweir 	HRESULT hr= S_OK;
151cdf0e10cSrcweir 	hr= CoInitializeEx( NULL, COINIT_MULTITHREADED);
152cdf0e10cSrcweir 	ATLASSERT( FAILED(hr) );
153cdf0e10cSrcweir 	MSG msg;
154cdf0e10cSrcweir 	// force the creation of a message queue
155cdf0e10cSrcweir 	PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
156cdf0e10cSrcweir 	SetEvent( *(HANDLE*)threadData );
157cdf0e10cSrcweir 
158cdf0e10cSrcweir 	RECT pos={0, 406, 300, 605};
159cdf0e10cSrcweir 	AWindow win(_T("DnD, full MTA"), GetCurrentThreadId(), pos, false, true);
160cdf0e10cSrcweir //	ThreadData data= *( ThreadData*)pParams;
161cdf0e10cSrcweir //	SetEvent(data.evtThreadReady);
162cdf0e10cSrcweir 	while( GetMessage(&msg, (HWND)NULL, 0, 0) )
163cdf0e10cSrcweir 	{
164cdf0e10cSrcweir 		switch( msg.message)
165cdf0e10cSrcweir 		{
166cdf0e10cSrcweir 		case  WM_SOURCE_INIT:
167cdf0e10cSrcweir 		{
168cdf0e10cSrcweir 			InitializationData* pData= (InitializationData*)msg.wParam;
169cdf0e10cSrcweir 			Any any;
170cdf0e10cSrcweir 			any <<= (sal_uInt32) pData->hWnd;
171cdf0e10cSrcweir 			pData->xInit->initialize( Sequence<Any>( &any, 1));
172cdf0e10cSrcweir 
173cdf0e10cSrcweir 			CoTaskMemFree( pData);
174cdf0e10cSrcweir 			break;
175cdf0e10cSrcweir 		}
176cdf0e10cSrcweir 		case WM_SOURCE_STARTDRAG:
177cdf0e10cSrcweir 		{
178cdf0e10cSrcweir 			// wParam contains necessary data
179cdf0e10cSrcweir 			StartDragData* pData= (StartDragData*)msg.wParam;
180cdf0e10cSrcweir 			Sequence<DataFlavor> seq= pData->transferable->getTransferDataFlavors();
181cdf0e10cSrcweir 			// have a look what flavours are supported
182cdf0e10cSrcweir 			for( int i=0; i<seq.getLength(); i++)
183cdf0e10cSrcweir 			{
184cdf0e10cSrcweir 				DataFlavor d= seq[i];
185cdf0e10cSrcweir 			}
186cdf0e10cSrcweir 			pData->source->startDrag( DragGestureEvent(),
187cdf0e10cSrcweir 									  ACTION_LINK|ACTION_MOVE|ACTION_COPY,
188cdf0e10cSrcweir 									  0,
189cdf0e10cSrcweir 									  0,
190cdf0e10cSrcweir 									  pData->transferable,
191cdf0e10cSrcweir 									  Reference<XDragSourceListener>( static_cast<XDragSourceListener*>
192cdf0e10cSrcweir 																	  ( new DragSourceListener())));
193cdf0e10cSrcweir 			CoTaskMemFree( pData);
194cdf0e10cSrcweir 			break;
195cdf0e10cSrcweir 		}
196cdf0e10cSrcweir 
197cdf0e10cSrcweir 		} // end switch
198cdf0e10cSrcweir 
199cdf0e10cSrcweir 		TranslateMessage(  &msg);
200cdf0e10cSrcweir 		DispatchMessage( &msg);
201cdf0e10cSrcweir 	}
202cdf0e10cSrcweir 
203cdf0e10cSrcweir 
204cdf0e10cSrcweir 	CoUninitialize();
205cdf0e10cSrcweir 	return 0;
206cdf0e10cSrcweir }
207