update dev300-m57
[ooovba.git] / sal / workben / clipboardwben / testpaste / cbptest.cxx
blob048d30833afe8d38a3cf307861067dce8d1e27ba
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: cbptest.cxx,v $
10 * $Revision: 1.3 $
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 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_sal.hxx"
34 // TestWin32.cpp : Definiert den Einsprungpunkt für die Anwendung.
37 #define _WIN32_DCOM
39 #include "stdafx.h"
41 #include <windows.h>
43 #include <ole2.h>
44 #include <objidl.h>
45 #include <objbase.h>
46 #include <process.h>
47 #include <olectl.h>
48 #include <stdlib.h>
49 #include <malloc.h>
50 #include <..\..\inc\systools\win32\MtaOleClipb.h>
52 #include "resource.h"
54 #define MAX_LOADSTRING 100
56 // Globale Variablen:
57 HINSTANCE hInst; // aktuelle Instanz
58 WCHAR szTitle[MAX_LOADSTRING]; // Text der Titelzeile
59 WCHAR szWindowClass[MAX_LOADSTRING]; // Text der Titelzeile
60 ATOM MyRegisterClass( HINSTANCE hInstance );
61 BOOL InitInstance( HINSTANCE, int );
62 LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
63 LRESULT CALLBACK About( HWND, UINT, WPARAM, LPARAM );
64 void PasteClipboardData(HWND hwndParent);
65 void PasteClipboardData2(HWND hwndParent);
67 LPSTREAM g_pStm = NULL;
68 char* pTextBuff = NULL;
69 DWORD lData = 0;
71 //----------------------------------------------------
72 // a thread function
73 //----------------------------------------------------
75 unsigned int _stdcall ThreadProc(LPVOID pParam)
77 IDataObject* pIDataObj = NULL;
78 FORMATETC formatETC;
79 STGMEDIUM stgMedium;
80 LPVOID pGlobMem;
81 HWND hwnd;
82 DWORD sizeGlobBuff;
83 HRESULT hr;
85 hwnd = (HWND)pParam;
87 OleInitialize( NULL );
89 hr = OleGetClipboard( &pIDataObj );
91 hr = CoGetInterfaceAndReleaseStream(
92 g_pStm,
93 __uuidof(IDataObject),
94 reinterpret_cast<LPVOID*>(&pIDataObj));
96 formatETC.cfFormat = CF_TEXT;
97 formatETC.ptd = NULL;
98 formatETC.dwAspect = DVASPECT_CONTENT;
99 formatETC.lindex = -1;
100 formatETC.tymed = TYMED_HGLOBAL;
102 hr = pIDataObj->GetData( &formatETC, &stgMedium );
103 pGlobMem = GlobalLock( stgMedium.hGlobal );
104 if ( NULL != pGlobMem )
106 if ( NULL != pTextBuff )
108 free( pTextBuff );
111 sizeGlobBuff = GlobalSize( stgMedium.hGlobal );
112 pTextBuff = (char*)malloc( sizeGlobBuff + 1 );
113 ZeroMemory( pTextBuff, sizeGlobBuff + 1 );
115 memcpy( pTextBuff, pGlobMem, sizeGlobBuff );
116 lData = sizeGlobBuff;
118 InvalidateRect( hwnd, NULL, TRUE );
119 UpdateWindow( hwnd );
122 GlobalUnlock( stgMedium.hGlobal );
124 ReleaseStgMedium( &stgMedium );
126 pIDataObj->Release();
128 //CoUninitialize( );
130 OleUninitialize( );
132 return 0;
135 //----------------------------------------------------
136 // WinMain
137 //----------------------------------------------------
139 int APIENTRY WinMain(HINSTANCE hInstance,
140 HINSTANCE hPrevInstance,
141 LPSTR lpCmdLine,
142 int nCmdShow )
144 // ZU ERLEDIGEN: Fügen Sie hier den Code ein.
145 MSG msg;
146 HACCEL hAccelTable;
147 HRESULT hr = E_FAIL;
149 // it's important to initialize ole
150 // in order to use the clipboard
151 //hr = OleInitialize( NULL );
152 hr = CoInitializeEx( NULL, COINIT_MULTITHREADED );
153 //hr = CoInitializeEx( NULL, COINIT_APARTMENTTHREADED );
155 // Globale Zeichenfolgen initialisieren
156 LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
157 LoadStringW(hInstance, IDC_TESTWIN32, szWindowClass, MAX_LOADSTRING);
158 MyRegisterClass(hInstance);
160 // Initialisierung der Anwendung durchführen:
161 if( !InitInstance( hInstance, nCmdShow ) )
163 return FALSE;
166 hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_TESTWIN32);
168 // Hauptnachrichtenschleife:
169 while( GetMessage(&msg, NULL, 0, 0) )
171 if( !TranslateAccelerator (msg.hwnd, hAccelTable, &msg) )
173 TranslateMessage( &msg );
174 DispatchMessage( &msg );
178 // uninitializing the ole libraries
179 //OleUninitialize( );
180 CoUninitialize( );
182 return msg.wParam;
188 // FUNKTION: MyRegisterClass()
190 // AUFGABE: Registriert die Fensterklasse.
192 // KOMMENTARE:
194 // Diese Funktion und ihre Verwendung sind nur notwendig, wenn dieser Code
195 // mit Win32-Systemen vor der 'RegisterClassEx'-Funktion kompatibel sein soll,
196 // die zu Windows 95 hinzugefügt wurde. Es ist wichtig diese Funktion aufzurufen,
197 // damit der Anwendung kleine Symbole mit den richtigen Proportionen zugewiesen
198 // werden.
200 ATOM MyRegisterClass( HINSTANCE hInstance )
202 WNDCLASSEXW wcex;
204 wcex.cbSize = sizeof(WNDCLASSEX);
206 wcex.style = CS_HREDRAW | CS_VREDRAW;
207 wcex.lpfnWndProc = (WNDPROC)WndProc;
208 wcex.cbClsExtra = 0;
209 wcex.cbWndExtra = 0;
210 wcex.hInstance = hInstance;
211 wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_TESTWIN32);
212 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
213 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
214 wcex.lpszMenuName = (LPCWSTR)IDC_TESTWIN32;
215 wcex.lpszClassName = szWindowClass;
216 wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
218 return RegisterClassExW(&wcex);
222 // FUNKTION: InitInstance(HANDLE, int)
224 // AUFGABE: Speichert die Instanzzugriffsnummer und erstellt das Hauptfenster
226 // KOMMENTARE:
228 // In dieser Funktion wird die Instanzzugriffsnummer in einer globalen Variable
229 // gespeichert und das Hauptprogrammfenster erstellt und angezeigt.
231 BOOL InitInstance( HINSTANCE hInstance, int nCmdShow )
233 HWND hWnd;
235 hInst = hInstance; // Instanzzugriffsnummer in unserer globalen Variable speichern
237 hWnd = CreateWindowExW(0, szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
238 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
240 if( !hWnd )
242 return FALSE;
245 ShowWindow( hWnd, nCmdShow );
246 UpdateWindow( hWnd );
248 return TRUE;
252 // FUNKTION: WndProc(HWND, unsigned, WORD, LONG)
254 // AUFGABE: Verarbeitet Nachrichten für das Hauptfenster.
256 // WM_COMMAND - Anwendungsmenü verarbeiten
257 // WM_PAINT - Hauptfenster darstellen
258 // WM_DESTROY - Beendigungsnachricht ausgeben und zurückkehren
261 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
263 int wmId;
264 int wmEvent;
265 PAINTSTRUCT ps;
266 HDC hdc;
267 TCHAR szHello[MAX_LOADSTRING];
270 LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
272 switch( message )
274 case WM_COMMAND:
275 wmId = LOWORD(wParam);
276 wmEvent = HIWORD(wParam);
277 // Menüauswahlen analysieren:
278 switch( wmId )
280 case IDD_PASTE:
281 //PasteClipboardData(hWnd);
282 PasteClipboardData2(hWnd);
283 break;
285 case IDM_EXIT:
286 DestroyWindow( hWnd );
287 break;
289 default:
290 return DefWindowProc( hWnd, message, wParam, lParam );
292 break;
294 case WM_PAINT:
295 hdc = BeginPaint (hWnd, &ps);
296 // ZU ERLEDIGEN: Hier beliebigen Code zum Zeichnen hinzufügen...
297 RECT rt;
298 GetClientRect( hWnd, &rt );
300 if ( NULL != pTextBuff )
302 DrawText( hdc, pTextBuff, lData, &rt, DT_CENTER );
304 else
306 DrawText( hdc, szHello, strlen(szHello), &rt, DT_CENTER );
309 EndPaint( hWnd, &ps );
310 break;
312 case WM_DESTROY:
313 PostQuitMessage( 0 );
314 break;
316 default:
317 return DefWindowProc( hWnd, message, wParam, lParam );
319 return 0;
322 void PasteClipboardData2(HWND hwndParent)
324 IDataObject* pIDataObject;
325 HRESULT hr;
326 FORMATETC formatETC;
327 STGMEDIUM stgMedium;
328 LPVOID pGlobMem;
329 HWND hwnd;
330 DWORD sizeGlobBuff;
332 hr = MTAGetClipboard( &pIDataObject );
333 if ( SUCCEEDED( hr ) )
335 formatETC.cfFormat = CF_TEXT;
336 formatETC.ptd = NULL;
337 formatETC.dwAspect = DVASPECT_CONTENT;
338 formatETC.lindex = -1;
339 formatETC.tymed = TYMED_HGLOBAL;
341 hr = pIDataObject->GetData( &formatETC, &stgMedium );
342 pGlobMem = GlobalLock( stgMedium.hGlobal );
343 if ( NULL != pGlobMem )
345 if ( NULL != pTextBuff )
347 free( pTextBuff );
350 sizeGlobBuff = GlobalSize( stgMedium.hGlobal );
351 pTextBuff = (char*)malloc( sizeGlobBuff + 1 );
352 ZeroMemory( pTextBuff, sizeGlobBuff + 1 );
354 memcpy( pTextBuff, pGlobMem, sizeGlobBuff );
355 lData = sizeGlobBuff;
357 InvalidateRect( hwndParent, NULL, TRUE );
358 UpdateWindow( hwndParent );
361 GlobalUnlock( stgMedium.hGlobal );
363 ReleaseStgMedium( &stgMedium );
365 pIDataObject->Release();
369 //----------------------------------------------------
370 // clipboard handling
371 //----------------------------------------------------
374 void PasteClipboardData(HWND hwndParent)
376 IDataObject* pIDataObj = NULL;
377 HRESULT hr = E_FAIL;
378 unsigned int dwId;
380 hr = OleGetClipboard( &pIDataObj );
381 if ( SUCCEEDED( hr ) )
383 HRESULT hr = CoMarshalInterThreadInterfaceInStream(
384 __uuidof(IDataObject), //The IID of inteface to be marshaled
385 pIDataObj, //The interface pointer
386 &g_pStm //IStream pointer
389 HANDLE hThread = (HANDLE)_beginthreadex(
390 NULL, //Security
391 0, //Stack Size
392 ThreadProc, //Start Address
393 NULL, //Parmeter
394 (unsigned int)hwndParent, //Creation Flag
395 &dwId //Thread Id
398 //Wait for the thread to finish execution
399 //A thread handle is signaled is thread execution
400 //is complete
401 for(;;)
403 DWORD dwRet = ::MsgWaitForMultipleObjects(
404 1, //Count of objects
405 &hThread, //pointer to the array of objects
406 FALSE, //Wait for all objects?
407 INFINITE, //Wait How Long?
408 QS_ALLINPUT //Wait for all messges
411 //This means that the object is signaled
412 if ( dwRet != WAIT_OBJECT_0 + 1 )
413 break;
415 //Remove the messages from the queue
416 MSG msg;
418 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) > 0)
420 //Not essential
421 TranslateMessage(&msg);
422 //Let the windowproc handle the message
423 DispatchMessage(&msg);
427 CloseHandle( hThread );
428 pIDataObj->Release();