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