fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / sal / workben / clipboardwben / testpaste / cbptest.cxx
blob9db93203d00696dadf14ee4ef2ee35125dbb7707
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 .
21 // TestWin32.cpp : Defines the entry point for the application
24 #define _WIN32_DCOM
26 #include "stdafx.h"
28 #include <windows.h>
30 #include <ole2.h>
31 #include <objidl.h>
32 #include <objbase.h>
33 #include <process.h>
34 #include <olectl.h>
35 #include <stdlib.h>
36 #include <malloc.h>
37 #include <..\..\inc\systools\win32\MtaOleClipb.h>
39 #include "resource.h"
41 #define MAX_LOADSTRING 100
43 // Global variables:
44 HINSTANCE hInst; // current instance
45 WCHAR szTitle[MAX_LOADSTRING]; // Text for title
46 WCHAR szWindowClass[MAX_LOADSTRING]; // Text for title
47 ATOM MyRegisterClass( HINSTANCE hInstance );
48 BOOL InitInstance( HINSTANCE, int );
49 LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
50 LRESULT CALLBACK About( HWND, UINT, WPARAM, LPARAM );
51 void PasteClipboardData(HWND hwndParent);
52 void PasteClipboardData2(HWND hwndParent);
54 LPSTREAM g_pStm = NULL;
55 char* pTextBuff = NULL;
56 DWORD lData = 0;
58 //----------------------------------------------------
59 // a thread function
60 //----------------------------------------------------
62 unsigned int _stdcall ThreadProc(LPVOID pParam)
64 IDataObject* pIDataObj = NULL;
65 FORMATETC formatETC;
66 STGMEDIUM stgMedium;
67 LPVOID pGlobMem;
68 HWND hwnd;
69 DWORD sizeGlobBuff;
70 HRESULT hr;
72 hwnd = (HWND)pParam;
74 OleInitialize( NULL );
76 hr = OleGetClipboard( &pIDataObj );
78 hr = CoGetInterfaceAndReleaseStream(
79 g_pStm,
80 __uuidof(IDataObject),
81 reinterpret_cast<LPVOID*>(&pIDataObj));
83 formatETC.cfFormat = CF_TEXT;
84 formatETC.ptd = NULL;
85 formatETC.dwAspect = DVASPECT_CONTENT;
86 formatETC.lindex = -1;
87 formatETC.tymed = TYMED_HGLOBAL;
89 hr = pIDataObj->GetData( &formatETC, &stgMedium );
90 pGlobMem = GlobalLock( stgMedium.hGlobal );
91 if ( NULL != pGlobMem )
93 if ( NULL != pTextBuff )
95 free( pTextBuff );
98 sizeGlobBuff = GlobalSize( stgMedium.hGlobal );
99 pTextBuff = (char*)malloc( sizeGlobBuff + 1 );
100 ZeroMemory( pTextBuff, sizeGlobBuff + 1 );
102 memcpy( pTextBuff, pGlobMem, sizeGlobBuff );
103 lData = sizeGlobBuff;
105 InvalidateRect( hwnd, NULL, TRUE );
106 UpdateWindow( hwnd );
109 GlobalUnlock( stgMedium.hGlobal );
111 ReleaseStgMedium( &stgMedium );
113 pIDataObj->Release();
115 //CoUninitialize( );
117 OleUninitialize( );
119 return 0;
122 //----------------------------------------------------
123 // WinMain
124 //----------------------------------------------------
126 int APIENTRY WinMain(HINSTANCE hInstance,
127 HINSTANCE hPrevInstance,
128 LPSTR lpCmdLine,
129 int nCmdShow )
131 // TODO: Add code here.
132 MSG msg;
133 HACCEL hAccelTable;
134 HRESULT hr = E_FAIL;
136 // it's important to initialize ole
137 // in order to use the clipboard
138 //hr = OleInitialize( NULL );
139 hr = CoInitializeEx( NULL, COINIT_MULTITHREADED );
140 //hr = CoInitializeEx( NULL, COINIT_APARTMENTTHREADED );
142 // Initialize global strings
143 LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
144 LoadStringW(hInstance, IDC_TESTWIN32, szWindowClass, MAX_LOADSTRING);
145 MyRegisterClass(hInstance);
147 // Initialization of the application to perform:
148 if( !InitInstance( hInstance, nCmdShow ) )
150 return FALSE;
153 hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_TESTWIN32);
155 // Main message loop:
156 while( GetMessage(&msg, NULL, 0, 0) )
158 if( !TranslateAccelerator (msg.hwnd, hAccelTable, &msg) )
160 TranslateMessage( &msg );
161 DispatchMessage( &msg );
165 // uninitializing the ole libraries
166 //OleUninitialize( );
167 CoUninitialize( );
169 return msg.wParam;
175 // FUNCTION: MyRegisterClass()
177 // PURPOSE: Registers the window class
179 // COMMENTS:
180 // This function and its usage are only necessary if this code
181 // needs to be compatible with Win32 systems prior to 'RegisterClassEx'
182 // function, which was added to Windows 95. If it important to call
183 // this function to allow the use of small icons in the correct proportions.
185 ATOM MyRegisterClass( HINSTANCE hInstance )
187 WNDCLASSEXW wcex;
189 wcex.cbSize = sizeof(WNDCLASSEX);
191 wcex.style = CS_HREDRAW | CS_VREDRAW;
192 wcex.lpfnWndProc = (WNDPROC)WndProc;
193 wcex.cbClsExtra = 0;
194 wcex.cbWndExtra = 0;
195 wcex.hInstance = hInstance;
196 wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_TESTWIN32);
197 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
198 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
199 wcex.lpszMenuName = (LPCWSTR)IDC_TESTWIN32;
200 wcex.lpszClassName = szWindowClass;
201 wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
203 return RegisterClassExW(&wcex);
207 // FUNCTION: InitInstance(HANDLE, int)
209 // PURPOSE: Saves instance handle and creates main window
211 // COMMENTS:
212 // In this function, the instance access number is stored in a global variable
213 // and the main program window is displayed.
215 BOOL InitInstance( HINSTANCE hInstance, int nCmdShow )
217 HWND hWnd;
219 hInst = hInstance; // Store instance access number in our global variable
221 hWnd = CreateWindowExW(0, szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
222 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
224 if( !hWnd )
226 return FALSE;
229 ShowWindow( hWnd, nCmdShow );
230 UpdateWindow( hWnd );
232 return TRUE;
236 // FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
238 // PURPOSE: Processes messages for the main window.
240 // WM_COMMAND - Handle application menu
241 // WM_PAINT - Display main windows
242 // WM_DESTROY - Output completion message and return
245 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
247 int wmId;
248 PAINTSTRUCT ps;
249 HDC hdc;
250 TCHAR szHello[MAX_LOADSTRING];
253 LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
255 switch( message )
257 case WM_COMMAND:
258 wmId = LOWORD(wParam);
259 // Analyze menu selections
260 switch( wmId )
262 case IDD_PASTE:
263 //PasteClipboardData(hWnd);
264 PasteClipboardData2(hWnd);
265 break;
267 case IDM_EXIT:
268 DestroyWindow( hWnd );
269 break;
271 default:
272 return DefWindowProc( hWnd, message, wParam, lParam );
274 break;
276 case WM_PAINT:
277 hdc = BeginPaint (hWnd, &ps);
278 // TODO: Add any code for drawing
279 RECT rt;
280 GetClientRect( hWnd, &rt );
282 if ( NULL != pTextBuff )
284 DrawText( hdc, pTextBuff, lData, &rt, DT_CENTER );
286 else
288 DrawText( hdc, szHello, strlen(szHello), &rt, DT_CENTER );
291 EndPaint( hWnd, &ps );
292 break;
294 case WM_DESTROY:
295 PostQuitMessage( 0 );
296 break;
298 default:
299 return DefWindowProc( hWnd, message, wParam, lParam );
301 return 0;
304 void PasteClipboardData2(HWND hwndParent)
306 IDataObject* pIDataObject;
307 HRESULT hr;
308 FORMATETC formatETC;
309 STGMEDIUM stgMedium;
310 LPVOID pGlobMem;
311 HWND hwnd;
312 DWORD sizeGlobBuff;
314 hr = MTAGetClipboard( &pIDataObject );
315 if ( SUCCEEDED( hr ) )
317 formatETC.cfFormat = CF_TEXT;
318 formatETC.ptd = NULL;
319 formatETC.dwAspect = DVASPECT_CONTENT;
320 formatETC.lindex = -1;
321 formatETC.tymed = TYMED_HGLOBAL;
323 hr = pIDataObject->GetData( &formatETC, &stgMedium );
324 pGlobMem = GlobalLock( stgMedium.hGlobal );
325 if ( NULL != pGlobMem )
327 if ( NULL != pTextBuff )
329 free( pTextBuff );
332 sizeGlobBuff = GlobalSize( stgMedium.hGlobal );
333 pTextBuff = (char*)malloc( sizeGlobBuff + 1 );
334 ZeroMemory( pTextBuff, sizeGlobBuff + 1 );
336 memcpy( pTextBuff, pGlobMem, sizeGlobBuff );
337 lData = sizeGlobBuff;
339 InvalidateRect( hwndParent, NULL, TRUE );
340 UpdateWindow( hwndParent );
343 GlobalUnlock( stgMedium.hGlobal );
345 ReleaseStgMedium( &stgMedium );
347 pIDataObject->Release();
351 //----------------------------------------------------
352 // clipboard handling
353 //----------------------------------------------------
356 void PasteClipboardData(HWND hwndParent)
358 IDataObject* pIDataObj = NULL;
359 HRESULT hr = E_FAIL;
360 unsigned int dwId;
362 hr = OleGetClipboard( &pIDataObj );
363 if ( SUCCEEDED( hr ) )
365 HRESULT hr = CoMarshalInterThreadInterfaceInStream(
366 __uuidof(IDataObject), //The IID of inteface to be marshaled
367 pIDataObj, //The interface pointer
368 &g_pStm //IStream pointer
371 HANDLE hThread = (HANDLE)_beginthreadex(
372 NULL, //Security
373 0, //Stack Size
374 ThreadProc, //Start Address
375 NULL, //Parmeter
376 (unsigned int)hwndParent, //Creation Flag
377 &dwId //Thread Id
380 //Wait for the thread to finish execution
381 //A thread handle is signaled is thread execution
382 //is complete
383 for(;;)
385 DWORD dwRet = ::MsgWaitForMultipleObjects(
386 1, //Count of objects
387 &hThread, //pointer to the array of objects
388 FALSE, //Wait for all objects?
389 INFINITE, //Wait How Long?
390 QS_ALLINPUT //Wait for all messges
393 //This means that the object is signaled
394 if ( dwRet != WAIT_OBJECT_0 + 1 )
395 break;
397 //Remove the messages from the queue
398 MSG msg;
400 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) > 0)
402 //Not essential
403 TranslateMessage(&msg);
404 //Let the windowproc handle the message
405 DispatchMessage(&msg);
409 CloseHandle( hThread );
410 pIDataObj->Release();
415 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */