xref: /aoo42x/main/dtrans/test/win32/dnd/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_dtrans.hxx"
30 
31 #if defined _MSC_VER
32 #pragma warning(push,1)
33 #endif
34 #include <windows.h>
35 #include <comdef.h>
36 #include <tchar.h>
37 #include <atlbase.h>
38 CComModule _Module;
39 #include<atlcom.h>
40 #include<atlimpl.cpp>
41 #if defined _MSC_VER
42 #pragma warning(pop)
43 #endif
44 #include <com/sun/star/uno/Reference.h>
45 #include <com/sun/star/lang/XInitialization.hpp>
46 #include <com/sun/star/datatransfer/dnd/XDropTarget.hpp>
47 #include <com/sun/star/datatransfer/dnd/DNDConstants.hpp>
48 
49 #include <com/sun/star/lang/XInitialization.hpp>
50 #include <com/sun/star/lang/XComponent.hpp>
51 #include <rtl/process.h>
52 #include <cppuhelper/servicefactory.hxx>
53 //#include "transferable.hxx"
54 #include "sourcelistener.hxx"
55 
56 
57 #include "atlwindow.hxx"
58 BEGIN_OBJECT_MAP(ObjectMap)
59 END_OBJECT_MAP()
60 
61 using namespace com::sun::star::lang;
62 using namespace com::sun::star::datatransfer;
63 using namespace com::sun::star::uno;
64 using namespace com::sun::star::datatransfer::dnd;
65 using namespace com::sun::star::datatransfer::dnd::DNDConstants;
66 using namespace rtl;
67 
68 // defined in atlwindow.hxx
69 // #define WM_SOURCE_INIT WM_APP+100
70 // #define WM_SOURCE_STARTDRAG WM_APP+101
71 #define WM_CREATE_MTA_WND
72 
73 HRESULT doTest();
74 DWORD WINAPI MTAFunc( void* threadData);
75 
76 Reference< XMultiServiceFactory > MultiServiceFactory;
77 //int APIENTRY WinMain(HINSTANCE hInstance,
78 //                     HINSTANCE hPrevInstance,
79 //                     LPSTR     lpCmdLine,
80 //                     int       nCmdShow)
81 //int _tmain( int argc, TCHAR *argv[ ], TCHAR *envp[ ] )
82 int main( int argc, char *argv[ ], char *envp[ ] )
83 {
84 	HRESULT hr;
85 	if( FAILED( hr=CoInitialize(NULL )))
86 	{
87 		_tprintf(_T("CoInitialize failed \n"));
88 		return -1;
89 	}
90 
91 
92 	_Module.Init( ObjectMap, GetModuleHandle( NULL));
93 
94 	if( FAILED(hr=doTest()))
95 	{
96 		_com_error err( hr);
97 		const TCHAR * errMsg= err.ErrorMessage();
98 //		MessageBox( NULL, errMsg, "Test failed", MB_ICONERROR);
99 	}
100 
101 
102 	_Module.Term();
103 	CoUninitialize();
104 	return 0;
105 }
106 
107 HRESULT doTest()
108 {
109 
110 	MultiServiceFactory= createRegistryServiceFactory( OUString(L"types.rdb"), OUString( L"services.rdb") , sal_True);
111 
112 	// create the MTA thread that is used to realize MTA calls to the services
113 	// We create the thread and wait until the thread has created its message queue
114 	HANDLE evt= CreateEvent(NULL, FALSE, FALSE, NULL);
115 	DWORD threadIdMTA=0;
116 	HANDLE hMTAThread= CreateThread( NULL, 0, MTAFunc, &evt, 0, &threadIdMTA);
117 	WaitForSingleObject( evt, INFINITE);
118 	CloseHandle(evt);
119 
120 
121 	HRESULT hr= S_OK;
122 	RECT pos1={0,0,300,200};
123 	AWindow win(_T("DnD starting in Ole STA"), threadIdMTA, pos1);
124 
125 	RECT pos2={ 0, 205, 300, 405};
126 	AWindow win2( _T("DnD starting in MTA"), threadIdMTA, pos2, true);
127 
128 	// win3 and win4 call initialize from an MTA but they are created in an STA
129 	RECT pos3={300,0,600,200};
130 	AWindow win3(_T("DnD starting in OLE STA"), threadIdMTA, pos3, false, true);
131 
132 	RECT pos4={ 300, 205, 600, 405};
133 	AWindow win24( _T("DnD starting in Ole MTA"), threadIdMTA, pos4, true, true);
134 
135 
136 	MSG msg;
137 	while( GetMessage(&msg, (HWND)NULL, 0, 0) )
138 	{
139 		TranslateMessage(  &msg);
140 		DispatchMessage( &msg);
141 	}
142 
143 	// Shut down the MTA thread
144 	PostThreadMessage( threadIdMTA, WM_QUIT, 0, 0);
145 	WaitForSingleObject(hMTAThread, INFINITE);
146 	CloseHandle(hMTAThread);
147 
148 	return S_OK;
149 }
150 
151 extern Reference<XMultiServiceFactory> MultiServiceFactory;
152 DWORD WINAPI MTAFunc( void* threadData)
153 {
154 	HRESULT hr= S_OK;
155 	hr= CoInitializeEx( NULL, COINIT_MULTITHREADED);
156 	ATLASSERT( FAILED(hr) );
157 	MSG msg;
158 	// force the creation of a message queue
159 	PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
160 	SetEvent( *(HANDLE*)threadData );
161 
162 	RECT pos={0, 406, 300, 605};
163 	AWindow win(_T("DnD, full MTA"), GetCurrentThreadId(), pos, false, true);
164 //	ThreadData data= *( ThreadData*)pParams;
165 //	SetEvent(data.evtThreadReady);
166 	while( GetMessage(&msg, (HWND)NULL, 0, 0) )
167 	{
168 		switch( msg.message)
169 		{
170 		case  WM_SOURCE_INIT:
171 		{
172 			InitializationData* pData= (InitializationData*)msg.wParam;
173 			Any any;
174 			any <<= (sal_uInt32) pData->hWnd;
175 			pData->xInit->initialize( Sequence<Any>( &any, 1));
176 
177 			CoTaskMemFree( pData);
178 			break;
179 		}
180 		case WM_SOURCE_STARTDRAG:
181 		{
182 			// wParam contains necessary data
183 			StartDragData* pData= (StartDragData*)msg.wParam;
184 			Sequence<DataFlavor> seq= pData->transferable->getTransferDataFlavors();
185 			// have a look what flavours are supported
186 			for( int i=0; i<seq.getLength(); i++)
187 			{
188 				DataFlavor d= seq[i];
189 			}
190 			pData->source->startDrag( DragGestureEvent(),
191 									  ACTION_LINK|ACTION_MOVE|ACTION_COPY,
192 									  0,
193 									  0,
194 									  pData->transferable,
195 									  Reference<XDragSourceListener>( static_cast<XDragSourceListener*>
196 																	  ( new DragSourceListener())));
197 			CoTaskMemFree( pData);
198 			break;
199 		}
200 
201 		} // end switch
202 
203 		TranslateMessage(  &msg);
204 		DispatchMessage( &msg);
205 	}
206 
207 
208 	CoUninitialize();
209 	return 0;
210 }
211