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