update dev300-m58
[ooovba.git] / dtrans / test / win32 / dnd / atlwindow.cxx
blobeba7d5a251aa3808174b90c504f136a18102b261
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: atlwindow.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 #include <com/sun/star/uno/Reference.h>
35 #include <com/sun/star/lang/XComponent.hpp>
36 #include <com/sun/star/lang/XInitialization.hpp>
37 #include <com/sun/star/datatransfer/dnd/XDropTarget.hpp>
38 #include <com/sun/star/datatransfer/dnd/DNDConstants.hpp>
40 #include <cppuhelper/servicefactory.hxx>
41 #include <rtl/string.h>
43 #include "atlwindow.hxx"
44 #include "targetlistener.hxx"
45 #include "sourcelistener.hxx"
46 //#include "transferable.hxx"
47 #include <map>
49 #include <winbase.h>
50 using namespace com::sun::star::lang;
51 using namespace com::sun::star::datatransfer::dnd;
52 using namespace com::sun::star::datatransfer::dnd::DNDConstants;
53 using namespace cppu;
54 using namespace rtl;
55 using namespace std;
57 LRESULT APIENTRY EditSubclassProc( HWND hwnd, UINT uMsg,WPARAM wParam, LPARAM lParam) ;
60 extern Reference< XMultiServiceFactory > MultiServiceFactory;
61 DWORD WINAPI MTAFunc(LPVOID pParams);
63 char* szSTAWin= "XDragSource::executeDrag is called from the same "
64 "OLE STA thread that created the window.";
65 char* szMTAWin= "XDragSource::executeDrag is called from a MTA thread "
66 "that did not create the window.";
68 WNDPROC wpOrigEditProc;
70 map<HWND, HWND> mapEditToMainWnd;
72 LRESULT AWindow::OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
74 Reference<XComponent> xcompSource( m_xDragSource, UNO_QUERY);
76 PostQuitMessage(0);
79 m_xDropTarget=0;
80 m_xDragSource=0;
83 // Remove the subclass from the edit control.
84 ::SetWindowLong(m_hwndEdit, GWL_WNDPROC,
85 (LONG) wpOrigEditProc);
87 return 0;
91 LRESULT AWindow::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
93 // Prepare the EDIT control
94 m_hwndEdit = CreateWindowA(
95 "EDIT", // predefined class
96 NULL, // no window title
97 WS_CHILD | WS_VISIBLE | WS_VSCROLL |
98 ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL,
99 0, 0, 0, 0, // set size in WM_SIZE message
100 m_hWnd, // parent window
101 (HMENU) NULL, // edit control ID
102 (HINSTANCE) GetWindowLong( GWL_HINSTANCE),
103 NULL);
105 // the map is used in the window procedure for the edit window to associate the
106 // it to the right main window ( AWindow)
107 mapEditToMainWnd[m_hwndEdit]= m_hWnd;
108 // Superclass the edit window, because we want to process mouse messages
109 wpOrigEditProc = (WNDPROC) ::SetWindowLongA(m_hwndEdit,
110 GWL_WNDPROC, (LONG) EditSubclassProc);
113 // Add text to the window.
114 if( m_isMTA)
115 ::SendMessageA(m_hwndEdit, WM_SETTEXT, 0, (LPARAM) szMTAWin);
116 else
117 ::SendMessageA(m_hwndEdit, WM_SETTEXT, 0, (LPARAM) szSTAWin);
120 // create the DragSource
121 Reference< XInterface> xint= MultiServiceFactory->createInstance(OUString(L"com.sun.star.datatransfer.dnd.OleDragSource"));
122 m_xDragSource= Reference<XDragSource>( xint, UNO_QUERY);
123 Reference<XInitialization> xInit( xint, UNO_QUERY);
125 Any ar[2];
126 ar[1]<<= (sal_uInt32)m_hWnd;
127 xInit->initialize( Sequence<Any>( ar, 2) );
129 //create the DropTarget
130 Reference< XInterface> xintTarget= MultiServiceFactory->createInstance(OUString(L"com.sun.star.datatransfer.dnd.OleDropTarget"));
131 m_xDropTarget= Reference<XDropTarget>( xintTarget, UNO_QUERY);
132 Reference<XInitialization> xInitTarget( xintTarget, UNO_QUERY);
134 Any any;
135 any <<= (sal_uInt32)m_hWnd;
136 xInitTarget->initialize( Sequence<Any>( &any, 1) );
139 m_xDropTarget->addDropTargetListener( static_cast<XDropTargetListener*>
140 ( new DropTargetListener( m_hwndEdit)) );
141 // // make this window tho a drop target
142 m_xDropTarget->setActive(sal_True);
144 return 0;
147 // When the mouse is dragged for a second than a drag is initiated
148 LRESULT AWindow::OnMouseAction(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
150 if( uMsg== WM_LBUTTONDOWN)
152 SetTimer( 1, 1000);
155 else if( uMsg == WM_LBUTTONUP)
157 KillTimer( 1);
160 return 0;
163 LRESULT AWindow::OnTimer(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
165 HRESULT hr;
166 USES_CONVERSION;
167 KillTimer( 1);
168 if(m_xDragSource.is())
171 //Get the Text out of the Edit window
172 int length= (int)::SendMessageA( m_hwndEdit, WM_GETTEXTLENGTH, 0, 0);
173 char * pBuffer= new char[length + 1];
174 ZeroMemory( pBuffer, length + 1);
175 ::SendMessageA( m_hwndEdit, WM_GETTEXT, length, (LPARAM) pBuffer);
177 IDataObject* pData= NULL;
178 HRESULT hr= CreateDataCache( NULL, CLSID_NULL, __uuidof(IDataObject),(void**) &pData);
179 if( pData)
181 FORMATETC format={ CF_TEXT, NULL, DVASPECT_CONTENT, -1, };
183 HGLOBAL mem= GlobalAlloc(GHND, length + 1 );
184 void* pMem= GlobalLock( mem);
185 memcpy( pMem, pBuffer, length+1);
186 GlobalUnlock( mem);
188 STGMEDIUM medium;
189 medium.tymed= TYMED_HGLOBAL;
190 medium.hGlobal= mem;
191 medium.pUnkForRelease= NULL;
193 pData->SetData( &format, &medium, TRUE); // releases HGLOBAL eventually
195 Reference<XTransferable> xTrans= m_aDataConverter.createTransferableFromDataObj(
196 MultiServiceFactory, pData);
198 // call XDragSource::executeDrag from an MTA
199 if( m_isMTA )
201 DWORD mtaThreadId;
202 ThreadData data;
203 data.source= m_xDragSource;
204 data.transferable= xTrans;
206 data.evtThreadReady= CreateEvent( NULL, FALSE, FALSE, NULL);
208 HANDLE hThread= CreateThread( NULL, 0, MTAFunc, &data, 0, &mtaThreadId);
209 // We must wait until the thread copied the ThreadData structure
210 WaitForSingleObject( data.evtThreadReady, INFINITE);
211 CloseHandle( data.evtThreadReady);
215 else
217 m_xDragSource->startDrag( DragGestureEvent(),
218 ACTION_LINK|ACTION_MOVE|ACTION_COPY,
221 xTrans,
222 Reference<XDragSourceListener>( static_cast<XDragSourceListener*>(new DragSourceListener() ) ) );
226 delete[] pBuffer;
229 return 0;
232 LRESULT AWindow::OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
234 // Make the edit control the size of the window's
235 // client area.
236 ::MoveWindow(m_hwndEdit,
237 0, 0, // starting x- and y-coordinates
238 LOWORD(lParam), // width of client area
239 HIWORD(lParam), // height of client area
240 TRUE); // repaint window
242 return 0;
244 LRESULT AWindow::OnFocus(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
246 ::SetFocus(m_hwndEdit);
247 return 0;
252 // Subclass procedure for EDIT window
253 LRESULT APIENTRY EditSubclassProc( HWND hwnd, UINT uMsg,WPARAM wParam, LPARAM lParam)
256 if( uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
258 HWND hAWindow= mapEditToMainWnd[hwnd];
259 ::SendMessage( hAWindow, uMsg, wParam, lParam);
262 return CallWindowProc( wpOrigEditProc, hwnd, uMsg,
263 wParam, lParam);