update dev300-m57
[ooovba.git] / sal / workben / clipboardwben / testcopy / cbcpytest.cxx
blobe283aa7af11fb8fe1da02ae1cb26011aacd8c5c5
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: cbcpytest.cxx,v $
10 * $Revision: 1.4 $
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
38 #undef _UNICODE
40 #include "stdafx.h"
42 #include <windows.h>
44 #include <ole2.h>
45 #include <objidl.h>
46 #include <objbase.h>
47 #include <process.h>
48 #include <olectl.h>
49 #include <stdlib.h>
50 #include <malloc.h>
51 #include <..\..\inc\systools\win32\MtaOleClipb.h>
52 #include "XTDataObject.hxx"
54 #include "resource.h"
56 #define MAX_LOADSTRING 100
57 #undef USE_MTACB
59 #define MSG_FLUSHCLIPBOARD WM_USER + 1
61 // Globale Variablen:
62 HINSTANCE hInst; // aktuelle Instanz
63 TCHAR szTitle[MAX_LOADSTRING]; // Text der Titelzeile
64 TCHAR szWindowClass[MAX_LOADSTRING]; // Text der Titelzeile
65 ATOM MyRegisterClass( HINSTANCE hInstance );
66 BOOL InitInstance( HINSTANCE, int );
67 LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
68 LRESULT CALLBACK About( HWND, UINT, WPARAM, LPARAM );
69 void CopyClipboardData(HWND hwndParent);
70 void FlushClipboard( );
71 void PasteData( HWND hWnd );
72 void SetLocale();
75 LPSTREAM g_pStm = NULL;
76 char* pTextBuff = NULL;
77 DWORD lData = 0;
78 CXTDataObject* g_xtDo = NULL;
79 HWND g_hWnd;
80 HANDLE g_hEvent;
81 BOOL g_bEnd;
83 //----------------------------------------------------
84 // a thread function
85 //----------------------------------------------------
87 unsigned int _stdcall ThreadProc(LPVOID pParam)
89 while( !g_bEnd )
91 WaitForSingleObject( g_hEvent, INFINITE );
92 SendMessage( g_hWnd, MSG_FLUSHCLIPBOARD, WPARAM(0), LPARAM(0) );
95 return 0;
98 //----------------------------------------------------
99 // WinMain
100 //----------------------------------------------------
102 int APIENTRY WinMain(HINSTANCE hInstance,
103 HINSTANCE hPrevInstance,
104 LPSTR lpCmdLine,
105 int nCmdShow )
107 // ZU ERLEDIGEN: Fügen Sie hier den Code ein.
108 MSG msg;
109 HACCEL hAccelTable;
110 HRESULT hr = E_FAIL;
113 g_hEvent = CreateEvent( 0,
114 FALSE,
115 FALSE,
116 NULL
119 g_bEnd = FALSE;
121 _beginthreadex( ThreadProc,
123 NULL,
126 NULL );
129 // it's important to initialize ole
130 // in order to use the clipboard
131 #ifdef USE_MTACB
132 hr = CoInitializeEx( NULL, COINIT_MULTITHREADED );
133 #else
134 hr = OleInitialize( NULL );
135 #endif
138 // Globale Zeichenfolgen initialisieren
139 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
140 LoadString(hInstance, IDC_TESTWIN32, szWindowClass, MAX_LOADSTRING);
141 MyRegisterClass(hInstance);
143 // Initialisierung der Anwendung durchführen:
144 if( !InitInstance( hInstance, nCmdShow ) )
146 return FALSE;
149 hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_TESTWIN32);
151 // Hauptnachrichtenschleife:
152 while( GetMessage(&msg, NULL, 0, 0) )
154 if( !TranslateAccelerator (msg.hwnd, hAccelTable, &msg) )
156 TranslateMessage( &msg );
157 DispatchMessage( &msg );
161 // uninitializing the ole libraries
162 #ifdef USE_MTACB
163 CoUninitialize( );
164 #else
165 OleUninitialize( );
166 #endif
168 CloseHandle( g_hEvent );
170 return msg.wParam;
175 //----------------------------------------------------------------
176 // FUNKTION: MyRegisterClass()
178 // AUFGABE: Registriert die Fensterklasse.
180 // KOMMENTARE:
182 // Diese Funktion und ihre Verwendung sind nur notwendig, wenn dieser Code
183 // mit Win32-Systemen vor der 'RegisterClassEx'-Funktion kompatibel sein soll,
184 // die zu Windows 95 hinzugefügt wurde. Es ist wichtig diese Funktion aufzurufen,
185 // damit der Anwendung kleine Symbole mit den richtigen Proportionen zugewiesen
186 // werden.
187 //----------------------------------------------------------------
189 ATOM MyRegisterClass( HINSTANCE hInstance )
191 WNDCLASSEX wcex;
193 wcex.cbSize = sizeof(WNDCLASSEX);
195 wcex.style = CS_HREDRAW | CS_VREDRAW;
196 wcex.lpfnWndProc = (WNDPROC)WndProc;
197 wcex.cbClsExtra = 0;
198 wcex.cbWndExtra = 0;
199 wcex.hInstance = hInstance;
200 wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_TESTWIN32);
201 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
202 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
203 wcex.lpszMenuName = (LPCTSTR)IDC_TESTWIN32;
204 wcex.lpszClassName = _T(szWindowClass);
205 wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
207 return RegisterClassEx(&wcex);
210 //----------------------------------------------------------------
211 // FUNKTION: InitInstance(HANDLE, int)
213 // AUFGABE: Speichert die Instanzzugriffsnummer und erstellt das Hauptfenster
215 // KOMMENTARE:
217 // In dieser Funktion wird die Instanzzugriffsnummer in einer globalen Variable
218 // gespeichert und das Hauptprogrammfenster erstellt und angezeigt.
219 //----------------------------------------------------------------
221 BOOL InitInstance( HINSTANCE hInstance, int nCmdShow )
223 hInst = hInstance; // Instanzzugriffsnummer in unserer globalen Variable speichern
225 g_hWnd = CreateWindowEx(0, szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
226 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
228 if( !g_hWnd )
230 return FALSE;
233 ShowWindow( g_hWnd, nCmdShow );
234 UpdateWindow( g_hWnd );
236 return TRUE;
239 //----------------------------------------------------------------
240 // FUNKTION: WndProc(HWND, unsigned, WORD, LONG)
242 // AUFGABE: Verarbeitet Nachrichten für das Hauptfenster.
244 // WM_COMMAND - Anwendungsmenü verarbeiten
245 // WM_PAINT - Hauptfenster darstellen
246 // WM_DESTROY - Beendigungsnachricht ausgeben und zurückkehren
247 //----------------------------------------------------------------
249 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
251 int wmId;
252 int wmEvent;
253 PAINTSTRUCT ps;
254 HDC hdc;
255 TCHAR szHello[MAX_LOADSTRING];
258 LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
260 switch( message )
262 case WM_COMMAND:
263 wmId = LOWORD(wParam);
264 wmEvent = HIWORD(wParam);
265 // Menüauswahlen analysieren:
266 switch( wmId )
268 case IDD_COPY:
269 CopyClipboardData(hWnd);
270 break;
271 case IDD_PASTE2:
272 PasteData(hWnd);
273 break;
274 case IDD_LOCALE:
275 SetLocale();
276 break;
277 case IDM_EXIT:
278 DestroyWindow( hWnd );
279 break;
281 default:
282 return DefWindowProc( hWnd, message, wParam, lParam );
284 break;
286 case WM_PAINT:
287 hdc = BeginPaint (hWnd, &ps);
288 // ZU ERLEDIGEN: Hier beliebigen Code zum Zeichnen hinzufügen...
289 RECT rt;
290 GetClientRect( hWnd, &rt );
292 if ( NULL != pTextBuff )
294 DrawText( hdc, pTextBuff, lData, &rt, DT_CENTER );
296 else
298 DrawText( hdc, szHello, strlen(szHello), &rt, DT_CENTER );
301 EndPaint( hWnd, &ps );
302 break;
304 case WM_DESTROY:
305 g_bEnd = TRUE;
306 SetEvent( g_hEvent );
307 FlushClipboard( );
308 PostQuitMessage( 0 );
309 break;
311 default:
312 return DefWindowProc( hWnd, message, wParam, lParam );
314 return 0;
317 //----------------------------------------------
318 // copy data into the clipboard
319 //----------------------------------------------
321 void CopyClipboardData( HWND hWnd )
323 g_xtDo = new CXTDataObject( 1 );
324 #ifdef USE_MTACB
325 MTASetClipboard( static_cast< IDataObject* >( g_xtDo ) );
326 #else
327 OleSetClipboard( static_cast< IDataObject* >( g_xtDo ) );
328 #endif
331 //----------------------------------------------
332 // flush the content into the clipboard
333 //----------------------------------------------
335 void FlushClipboard( )
337 if ( NULL != g_xtDo )
339 #ifdef USE_MTACB
340 HRESULT hr = MTAIsCurrentClipboard( static_cast< IDataObject* >( g_xtDo ) );
341 if ( S_OK == hr )
342 MTAFlushClipboard( );
343 #else
344 HRESULT hr = OleIsCurrentClipboard( static_cast< IDataObject* >( g_xtDo ) );
345 if ( S_OK == hr )
346 OleFlushClipboard( );
347 #endif
349 static_cast< IDataObject* >( g_xtDo )->Release( );
354 void PasteData(HWND hWnd)
356 IDataObject* pDataObj;
358 //FlushClipboard( );
360 HRESULT hr = OleGetClipboard( &pDataObj );
361 if ( SUCCEEDED( hr ) )
363 FORMATETC fetc;
364 STGMEDIUM stgmedium;
366 fetc.cfFormat = CF_LOCALE;
367 fetc.ptd = NULL;
368 fetc.dwAspect = DVASPECT_CONTENT;
369 fetc.lindex = -1;
370 fetc.tymed = TYMED_HGLOBAL;
372 hr = pDataObj->GetData( &fetc, &stgmedium );
373 if ( SUCCEEDED( hr ) )
375 LPVOID lpData = GlobalLock( stgmedium.hGlobal );
377 if ( NULL != lpData )
379 LCID lcid = *( (WORD*)lpData );
381 WORD langID = LANGIDFROMLCID( lcid );
382 WORD sublangID = SUBLANGID( langID );
384 TCHAR buff[6];
385 int cbWritten = GetLocaleInfo( lcid, LOCALE_IDEFAULTANSICODEPAGE, buff, sizeof( buff ) );
386 cbWritten = GetLocaleInfo( lcid, LOCALE_IDEFAULTCODEPAGE, buff, sizeof( buff ) );
388 GlobalUnlock( stgmedium.hGlobal );
390 else
392 DWORD dwLastError = GetLastError( );
395 ReleaseStgMedium( &stgmedium );
401 void SetLocale()
403 LCID threadLcid = GetThreadLocale();