fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / dtrans / test / win32 / dnd / atlwindow.cxx
blob35e2cb5b1aca99b28283c09dc534dd892253169c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <com/sun/star/uno/Reference.h>
21 #include <com/sun/star/lang/XComponent.hpp>
22 #include <com/sun/star/lang/XInitialization.hpp>
23 #include <com/sun/star/datatransfer/dnd/XDropTarget.hpp>
24 #include <com/sun/star/datatransfer/dnd/DNDConstants.hpp>
26 #include <cppuhelper/servicefactory.hxx>
27 #include <rtl/string.h>
29 #include "atlwindow.hxx"
30 #include "targetlistener.hxx"
31 #include "sourcelistener.hxx"
32 #include <map>
34 #include <winbase.h>
35 using namespace com::sun::star::lang;
36 using namespace com::sun::star::datatransfer::dnd;
37 using namespace com::sun::star::datatransfer::dnd::DNDConstants;
38 using namespace cppu;
39 using namespace std;
41 LRESULT APIENTRY EditSubclassProc( HWND hwnd, UINT uMsg,WPARAM wParam, LPARAM lParam) ;
43 extern Reference< XMultiServiceFactory > MultiServiceFactory;
44 DWORD WINAPI MTAFunc(LPVOID pParams);
46 char* szSTAWin= "XDragSource::executeDrag is called from the same "
47 "OLE STA thread that created the window.";
48 char* szMTAWin= "XDragSource::executeDrag is called from a MTA thread "
49 "that did not create the window.";
51 WNDPROC wpOrigEditProc;
53 map<HWND, HWND> mapEditToMainWnd;
55 LRESULT AWindow::OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
57 Reference<XComponent> xcompSource( m_xDragSource, UNO_QUERY);
59 PostQuitMessage(0);
61 m_xDropTarget=0;
62 m_xDragSource=0;
64 // Remove the subclass from the edit control.
65 ::SetWindowLong(m_hwndEdit, GWL_WNDPROC,
66 (LONG) wpOrigEditProc);
68 return 0;
71 LRESULT AWindow::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
73 // Prepare the EDIT control
74 m_hwndEdit = CreateWindowA(
75 "EDIT", // predefined class
76 NULL, // no window title
77 WS_CHILD | WS_VISIBLE | WS_VSCROLL |
78 ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL,
79 0, 0, 0, 0, // set size in WM_SIZE message
80 m_hWnd, // parent window
81 (HMENU) NULL, // edit control ID
82 (HINSTANCE) GetWindowLong( GWL_HINSTANCE),
83 NULL);
85 // the map is used in the window procedure for the edit window to associate the
86 // it to the right main window ( AWindow)
87 mapEditToMainWnd[m_hwndEdit]= m_hWnd;
88 // Superclass the edit window, because we want to process mouse messages
89 wpOrigEditProc = (WNDPROC) ::SetWindowLongA(m_hwndEdit,
90 GWL_WNDPROC, (LONG) EditSubclassProc);
92 // Add text to the window.
93 if( m_isMTA)
94 ::SendMessageA(m_hwndEdit, WM_SETTEXT, 0, (LPARAM) szMTAWin);
95 else
96 ::SendMessageA(m_hwndEdit, WM_SETTEXT, 0, (LPARAM) szSTAWin);
98 // create the DragSource
99 Reference< XInterface> xint= MultiServiceFactory->createInstance(OUString(L"com.sun.star.datatransfer.dnd.OleDragSource"));
100 m_xDragSource= Reference<XDragSource>( xint, UNO_QUERY);
101 Reference<XInitialization> xInit( xint, UNO_QUERY);
103 Any ar[2];
104 ar[1]<<= (sal_uInt32)m_hWnd;
105 xInit->initialize( Sequence<Any>( ar, 2) );
107 //create the DropTarget
108 Reference< XInterface> xintTarget= MultiServiceFactory->createInstance(OUString(L"com.sun.star.datatransfer.dnd.OleDropTarget"));
109 m_xDropTarget= Reference<XDropTarget>( xintTarget, UNO_QUERY);
110 Reference<XInitialization> xInitTarget( xintTarget, UNO_QUERY);
112 Any any;
113 any <<= (sal_uInt32)m_hWnd;
114 xInitTarget->initialize( Sequence<Any>( &any, 1) );
116 m_xDropTarget->addDropTargetListener( static_cast<XDropTargetListener*>
117 ( new DropTargetListener( m_hwndEdit)) );
118 // // make this window tho a drop target
119 m_xDropTarget->setActive(sal_True);
121 return 0;
124 // When the mouse is dragged for a second than a drag is initiated
125 LRESULT AWindow::OnMouseAction(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
127 if( uMsg== WM_LBUTTONDOWN)
129 SetTimer( 1, 1000);
132 else if( uMsg == WM_LBUTTONUP)
134 KillTimer( 1);
137 return 0;
140 LRESULT AWindow::OnTimer(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
142 HRESULT hr;
143 USES_CONVERSION;
144 KillTimer( 1);
145 if(m_xDragSource.is())
148 //Get the Text out of the Edit window
149 int length= (int)::SendMessageA( m_hwndEdit, WM_GETTEXTLENGTH, 0, 0);
150 char * pBuffer= new char[length + 1];
151 ZeroMemory( pBuffer, length + 1);
152 ::SendMessageA( m_hwndEdit, WM_GETTEXT, length, (LPARAM) pBuffer);
154 IDataObject* pData= NULL;
155 HRESULT hr= CreateDataCache( NULL, CLSID_NULL, __uuidof(IDataObject),(void**) &pData);
156 if( pData)
158 FORMATETC format={ CF_TEXT, NULL, DVASPECT_CONTENT, -1, };
160 HGLOBAL mem= GlobalAlloc(GHND, length + 1 );
161 void* pMem= GlobalLock( mem);
162 memcpy( pMem, pBuffer, length+1);
163 GlobalUnlock( mem);
165 STGMEDIUM medium;
166 medium.tymed= TYMED_HGLOBAL;
167 medium.hGlobal= mem;
168 medium.pUnkForRelease= NULL;
170 pData->SetData( &format, &medium, TRUE); // releases HGLOBAL eventually
172 Reference<XTransferable> xTrans= m_aDataConverter.createTransferableFromDataObj(
173 MultiServiceFactory, pData);
175 // call XDragSource::executeDrag from an MTA
176 if( m_isMTA )
178 DWORD mtaThreadId;
179 ThreadData data;
180 data.source= m_xDragSource;
181 data.transferable= xTrans;
183 data.evtThreadReady= CreateEvent( NULL, FALSE, FALSE, NULL);
185 HANDLE hThread= CreateThread( NULL, 0, MTAFunc, &data, 0, &mtaThreadId);
186 // We must wait until the thread copied the ThreadData structure
187 WaitForSingleObject( data.evtThreadReady, INFINITE);
188 CloseHandle( data.evtThreadReady);
191 else
193 m_xDragSource->startDrag( DragGestureEvent(),
194 ACTION_LINK|ACTION_MOVE|ACTION_COPY,
197 xTrans,
198 Reference<XDragSourceListener>( static_cast<XDragSourceListener*>(new DragSourceListener() ) ) );
202 delete[] pBuffer;
205 return 0;
208 LRESULT AWindow::OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
210 // Make the edit control the size of the window's
211 // client area.
212 ::MoveWindow(m_hwndEdit,
213 0, 0, // starting x- and y-coordinates
214 LOWORD(lParam), // width of client area
215 HIWORD(lParam), // height of client area
216 TRUE); // repaint window
218 return 0;
220 LRESULT AWindow::OnFocus(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
222 ::SetFocus(m_hwndEdit);
223 return 0;
226 // Subclass procedure for EDIT window
227 LRESULT APIENTRY EditSubclassProc( HWND hwnd, UINT uMsg,WPARAM wParam, LPARAM lParam)
230 if( uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
232 HWND hAWindow= mapEditToMainWnd[hwnd];
233 ::SendMessage( hAWindow, uMsg, wParam, lParam);
236 return CallWindowProc( wpOrigEditProc, hwnd, uMsg,
237 wParam, lParam);
240 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */