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