Update ooo320-m1
[ooovba.git] / dtrans / test / win32 / dnd / dndTest.cxx
blob773751e9ccb0e76b90b9d8d424db99d80a170e4e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: dndTest.cxx,v $
10 * $Revision: 1.12 $
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"
34 #if defined _MSC_VER
35 #pragma warning(push,1)
36 #endif
37 #include <windows.h>
38 #include <comdef.h>
39 #include <tchar.h>
40 #include <atlbase.h>
41 CComModule _Module;
42 #include<atlcom.h>
43 #include<atlimpl.cpp>
44 #if defined _MSC_VER
45 #pragma warning(pop)
46 #endif
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)
62 END_OBJECT_MAP()
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;
69 using namespace rtl;
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
76 HRESULT doTest();
77 DWORD WINAPI MTAFunc( void* threadData);
79 Reference< XMultiServiceFactory > MultiServiceFactory;
80 //int APIENTRY WinMain(HINSTANCE hInstance,
81 // HINSTANCE hPrevInstance,
82 // LPSTR lpCmdLine,
83 // int nCmdShow)
84 //int _tmain( int argc, TCHAR *argv[ ], TCHAR *envp[ ] )
85 int main( int argc, char *argv[ ], char *envp[ ] )
87 HRESULT hr;
88 if( FAILED( hr=CoInitialize(NULL )))
90 _tprintf(_T("CoInitialize failed \n"));
91 return -1;
95 _Module.Init( ObjectMap, GetModuleHandle( NULL));
97 if( FAILED(hr=doTest()))
99 _com_error err( hr);
100 const TCHAR * errMsg= err.ErrorMessage();
101 // MessageBox( NULL, errMsg, "Test failed", MB_ICONERROR);
105 _Module.Term();
106 CoUninitialize();
107 return 0;
110 HRESULT doTest()
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);
118 DWORD threadIdMTA=0;
119 HANDLE hMTAThread= CreateThread( NULL, 0, MTAFunc, &evt, 0, &threadIdMTA);
120 WaitForSingleObject( evt, INFINITE);
121 CloseHandle(evt);
124 HRESULT hr= S_OK;
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);
139 MSG msg;
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);
151 return S_OK;
154 extern Reference<XMultiServiceFactory> MultiServiceFactory;
155 DWORD WINAPI MTAFunc( void* threadData)
157 HRESULT hr= S_OK;
158 hr= CoInitializeEx( NULL, COINIT_MULTITHREADED);
159 ATLASSERT( FAILED(hr) );
160 MSG msg;
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) )
171 switch( msg.message)
173 case WM_SOURCE_INIT:
175 InitializationData* pData= (InitializationData*)msg.wParam;
176 Any any;
177 any <<= (sal_uInt32) pData->hWnd;
178 pData->xInit->initialize( Sequence<Any>( &any, 1));
180 CoTaskMemFree( pData);
181 break;
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,
197 pData->transferable,
198 Reference<XDragSourceListener>( static_cast<XDragSourceListener*>
199 ( new DragSourceListener())));
200 CoTaskMemFree( pData);
201 break;
204 } // end switch
206 TranslateMessage( &msg);
207 DispatchMessage( &msg);
211 CoUninitialize();
212 return 0;