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