xref: /AOO41X/main/sal/workben/clipboardwben/testpaste/cbptest.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 
29 // MARKER(update_precomp.py): autogen include statement, do not remove
30 #include "precompiled_sal.hxx"
31 // TestWin32.cpp : Definiert den Einsprungpunkt f�r die Anwendung.
32 //
33 
34 #define _WIN32_DCOM
35 
36 #include "stdafx.h"
37 
38 #include <windows.h>
39 
40 #include <ole2.h>
41 #include <objidl.h>
42 #include <objbase.h>
43 #include <process.h>
44 #include <olectl.h>
45 #include <stdlib.h>
46 #include <malloc.h>
47 #include <..\..\inc\systools\win32\MtaOleClipb.h>
48 
49 #include "resource.h"
50 
51 #define MAX_LOADSTRING 100
52 
53 // Globale Variablen:
54 HINSTANCE			hInst;						// aktuelle Instanz
55 WCHAR				szTitle[MAX_LOADSTRING];			// Text der Titelzeile
56 WCHAR				szWindowClass[MAX_LOADSTRING];	// Text der Titelzeile
57 ATOM				MyRegisterClass( HINSTANCE hInstance );
58 BOOL				InitInstance( HINSTANCE, int );
59 LRESULT CALLBACK	WndProc( HWND, UINT, WPARAM, LPARAM );
60 LRESULT CALLBACK	About( HWND, UINT, WPARAM, LPARAM );
61 void				PasteClipboardData(HWND hwndParent);
62 void				PasteClipboardData2(HWND hwndParent);
63 
64 LPSTREAM			g_pStm    = NULL;
65 char*				pTextBuff = NULL;
66 DWORD				lData     = 0;
67 
68 //----------------------------------------------------
69 // a thread function
70 //----------------------------------------------------
71 
72 unsigned int _stdcall ThreadProc(LPVOID pParam)
73 {
74 	IDataObject* pIDataObj = NULL;
75 	FORMATETC    formatETC;
76 	STGMEDIUM	 stgMedium;
77 	LPVOID		 pGlobMem;
78 	HWND		 hwnd;
79 	DWORD		 sizeGlobBuff;
80 	HRESULT		 hr;
81 
82 	hwnd = (HWND)pParam;
83 
84 	OleInitialize( NULL );
85 
86 	hr = OleGetClipboard( &pIDataObj );
87 
88 	hr = CoGetInterfaceAndReleaseStream(
89 		g_pStm,
90 		__uuidof(IDataObject),
91 		reinterpret_cast<LPVOID*>(&pIDataObj));
92 
93 	formatETC.cfFormat = CF_TEXT;
94 	formatETC.ptd      = NULL;
95 	formatETC.dwAspect = DVASPECT_CONTENT;
96 	formatETC.lindex   = -1;
97 	formatETC.tymed    = TYMED_HGLOBAL;
98 
99 	hr = pIDataObj->GetData( &formatETC, &stgMedium );
100 	pGlobMem = GlobalLock( stgMedium.hGlobal );
101 	if ( NULL != pGlobMem )
102 	{
103 		if ( NULL != pTextBuff )
104 		{
105 			free( pTextBuff );
106 		}
107 
108 		sizeGlobBuff = GlobalSize( stgMedium.hGlobal );
109 		pTextBuff = (char*)malloc( sizeGlobBuff + 1 );
110 		ZeroMemory( pTextBuff, sizeGlobBuff + 1 );
111 
112 		memcpy( pTextBuff, pGlobMem, sizeGlobBuff );
113 		lData = sizeGlobBuff;
114 
115 		InvalidateRect( hwnd, NULL, TRUE );
116 		UpdateWindow( hwnd );
117 	}
118 
119 	GlobalUnlock( stgMedium.hGlobal );
120 
121 	ReleaseStgMedium( &stgMedium );
122 
123 	pIDataObj->Release();
124 
125 	//CoUninitialize( );
126 
127 	OleUninitialize( );
128 
129 	return 0;
130 }
131 
132 //----------------------------------------------------
133 // WinMain
134 //----------------------------------------------------
135 
136 int APIENTRY WinMain(HINSTANCE hInstance,
137                      HINSTANCE hPrevInstance,
138                      LPSTR     lpCmdLine,
139                      int       nCmdShow )
140 {
141  	// ZU ERLEDIGEN: F�gen Sie hier den Code ein.
142 	MSG     msg;
143 	HACCEL  hAccelTable;
144 	HRESULT hr = E_FAIL;
145 
146 	// it's important to initialize ole
147 	// in order to use the clipboard
148 	//hr = OleInitialize( NULL );
149 	hr = CoInitializeEx( NULL, COINIT_MULTITHREADED );
150 	//hr = CoInitializeEx( NULL, COINIT_APARTMENTTHREADED );
151 
152 	// Globale Zeichenfolgen initialisieren
153 	LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
154 	LoadStringW(hInstance, IDC_TESTWIN32, szWindowClass, MAX_LOADSTRING);
155 	MyRegisterClass(hInstance);
156 
157 	// Initialisierung der Anwendung durchf�hren:
158 	if( !InitInstance( hInstance, nCmdShow ) )
159 	{
160 		return FALSE;
161 	}
162 
163 	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_TESTWIN32);
164 
165 	// Hauptnachrichtenschleife:
166 	while( GetMessage(&msg, NULL, 0, 0) )
167 	{
168 		if( !TranslateAccelerator (msg.hwnd, hAccelTable, &msg) )
169 		{
170 			TranslateMessage( &msg );
171 			DispatchMessage( &msg );
172 		}
173 	}
174 
175 	// uninitializing the ole libraries
176 	//OleUninitialize( );
177 	CoUninitialize( );
178 
179 	return msg.wParam;
180 }
181 
182 
183 
184 //
185 //  FUNKTION: MyRegisterClass()
186 //
187 //  AUFGABE: Registriert die Fensterklasse.
188 //
189 //  KOMMENTARE:
190 //
191 //    Diese Funktion und ihre Verwendung sind nur notwendig, wenn dieser Code
192 //    mit Win32-Systemen vor der 'RegisterClassEx'-Funktion kompatibel sein soll,
193 //    die zu Windows 95 hinzugef�gt wurde. Es ist wichtig diese Funktion aufzurufen,
194 //    damit der Anwendung kleine Symbole mit den richtigen Proportionen zugewiesen
195 //    werden.
196 //
197 ATOM MyRegisterClass( HINSTANCE hInstance )
198 {
199 	WNDCLASSEXW wcex;
200 
201 	wcex.cbSize = sizeof(WNDCLASSEX);
202 
203 	wcex.style			= CS_HREDRAW | CS_VREDRAW;
204 	wcex.lpfnWndProc	= (WNDPROC)WndProc;
205 	wcex.cbClsExtra		= 0;
206 	wcex.cbWndExtra		= 0;
207 	wcex.hInstance		= hInstance;
208 	wcex.hIcon			= LoadIcon(hInstance, (LPCTSTR)IDI_TESTWIN32);
209 	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
210 	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
211 	wcex.lpszMenuName	= (LPCWSTR)IDC_TESTWIN32;
212 	wcex.lpszClassName	= szWindowClass;
213 	wcex.hIconSm		= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
214 
215 	return RegisterClassExW(&wcex);
216 }
217 
218 //
219 //   FUNKTION: InitInstance(HANDLE, int)
220 //
221 //   AUFGABE: Speichert die Instanzzugriffsnummer und erstellt das Hauptfenster
222 //
223 //   KOMMENTARE:
224 //
225 //        In dieser Funktion wird die Instanzzugriffsnummer in einer globalen Variable
226 //        gespeichert und das Hauptprogrammfenster erstellt und angezeigt.
227 //
228 BOOL InitInstance( HINSTANCE hInstance, int nCmdShow )
229 {
230    HWND hWnd;
231 
232    hInst = hInstance; // Instanzzugriffsnummer in unserer globalen Variable speichern
233 
234    hWnd = CreateWindowExW(0, szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
235       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
236 
237    if( !hWnd )
238    {
239       return FALSE;
240    }
241 
242    ShowWindow( hWnd, nCmdShow );
243    UpdateWindow( hWnd );
244 
245    return TRUE;
246 }
247 
248 //
249 //  FUNKTION: WndProc(HWND, unsigned, WORD, LONG)
250 //
251 //  AUFGABE:  Verarbeitet Nachrichten f�r das Hauptfenster.
252 //
253 //  WM_COMMAND	- Anwendungsmen� verarbeiten
254 //  WM_PAINT	- Hauptfenster darstellen
255 //  WM_DESTROY	- Beendigungsnachricht ausgeben und zur�ckkehren
256 //
257 //
258 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
259 {
260 	int         wmId;
261 	int         wmEvent;
262 	PAINTSTRUCT ps;
263 	HDC         hdc;
264 	TCHAR       szHello[MAX_LOADSTRING];
265 
266 
267 	LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
268 
269 	switch( message )
270 	{
271 		case WM_COMMAND:
272 			wmId    = LOWORD(wParam);
273 			wmEvent = HIWORD(wParam);
274 			// Men�auswahlen analysieren:
275 			switch( wmId )
276 			{
277 				case IDD_PASTE:
278 					//PasteClipboardData(hWnd);
279 					PasteClipboardData2(hWnd);
280 					break;
281 
282 				case IDM_EXIT:
283 				   DestroyWindow( hWnd );
284 				   break;
285 
286 				default:
287 				   return DefWindowProc( hWnd, message, wParam, lParam );
288 			}
289 			break;
290 
291 		case WM_PAINT:
292 			hdc = BeginPaint (hWnd, &ps);
293 			// ZU ERLEDIGEN: Hier beliebigen Code zum Zeichnen hinzuf�gen...
294 			RECT rt;
295 			GetClientRect( hWnd, &rt );
296 
297 			if ( NULL != pTextBuff )
298 			{
299 				DrawText( hdc, pTextBuff, lData, &rt, DT_CENTER );
300 			}
301 			else
302 			{
303 				DrawText( hdc, szHello, strlen(szHello), &rt, DT_CENTER );
304 			}
305 
306 			EndPaint( hWnd, &ps );
307 			break;
308 
309 		case WM_DESTROY:
310 			PostQuitMessage( 0 );
311 			break;
312 
313 		default:
314 			return DefWindowProc( hWnd, message, wParam, lParam );
315    }
316    return 0;
317 }
318 
319 void PasteClipboardData2(HWND hwndParent)
320 {
321 	IDataObject* pIDataObject;
322 	HRESULT		 hr;
323 	FORMATETC    formatETC;
324 	STGMEDIUM	 stgMedium;
325 	LPVOID		 pGlobMem;
326 	HWND		 hwnd;
327 	DWORD		 sizeGlobBuff;
328 
329 	hr = MTAGetClipboard( &pIDataObject );
330 	if ( SUCCEEDED( hr ) )
331 	{
332 		formatETC.cfFormat = CF_TEXT;
333 		formatETC.ptd      = NULL;
334 		formatETC.dwAspect = DVASPECT_CONTENT;
335 		formatETC.lindex   = -1;
336 		formatETC.tymed    = TYMED_HGLOBAL;
337 
338 		hr = pIDataObject->GetData( &formatETC, &stgMedium );
339 		pGlobMem = GlobalLock( stgMedium.hGlobal );
340 		if ( NULL != pGlobMem )
341 		{
342 			if ( NULL != pTextBuff )
343 			{
344 				free( pTextBuff );
345 			}
346 
347 			sizeGlobBuff = GlobalSize( stgMedium.hGlobal );
348 			pTextBuff = (char*)malloc( sizeGlobBuff + 1 );
349 			ZeroMemory( pTextBuff, sizeGlobBuff + 1 );
350 
351 			memcpy( pTextBuff, pGlobMem, sizeGlobBuff );
352 			lData = sizeGlobBuff;
353 
354 			InvalidateRect( hwndParent, NULL, TRUE );
355 			UpdateWindow( hwndParent );
356 		}
357 
358 		GlobalUnlock( stgMedium.hGlobal );
359 
360 		ReleaseStgMedium( &stgMedium );
361 
362 		pIDataObject->Release();
363 	}
364 }
365 
366 //----------------------------------------------------
367 // clipboard handling
368 //----------------------------------------------------
369 
370 /*
371 void PasteClipboardData(HWND hwndParent)
372 {
373 	IDataObject* pIDataObj = NULL;
374 	HRESULT      hr        = E_FAIL;
375 	unsigned int dwId;
376 
377 	hr = OleGetClipboard( &pIDataObj );
378 	if ( SUCCEEDED( hr ) )
379 	{
380 		HRESULT hr = CoMarshalInterThreadInterfaceInStream(
381 			__uuidof(IDataObject), //The IID of inteface to be marshaled
382 			pIDataObj,       //The interface pointer
383 			&g_pStm          //IStream pointer
384 			);
385 
386 		HANDLE hThread = (HANDLE)_beginthreadex(
387 				NULL,		//Security
388 				0,			//Stack Size
389 				ThreadProc,	//Start Address
390 				NULL,	    //Parmeter
391 				(unsigned int)hwndParent,	//Creation Flag
392 				&dwId		//Thread Id
393 				);
394 
395 		//Wait for the thread to finish execution
396 		//A thread handle is signaled is thread execution
397 		//is complete
398 		for(;;)
399 		{
400 			DWORD dwRet = ::MsgWaitForMultipleObjects(
401 									1,			//Count of objects
402 									&hThread,	//pointer to the array of objects
403 									FALSE,		//Wait for all objects?
404 									INFINITE,	//Wait How Long?
405 									QS_ALLINPUT //Wait for all messges
406 									);
407 
408 			//This means that the object is signaled
409 			if ( dwRet != WAIT_OBJECT_0 + 1 )
410 				break;
411 
412 			//Remove the messages from the queue
413 			MSG msg;
414 
415 			while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) > 0)
416 			{
417 				//Not essential
418 				TranslateMessage(&msg);
419 				//Let the windowproc handle the message
420 				DispatchMessage(&msg);
421 			}
422 		}
423 
424 		CloseHandle( hThread );
425 		pIDataObj->Release();
426 	}
427 }
428 */
429