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 
36 #include "stdafx.h"
37 
38 #include <windows.h>
39 
40 #include <ole2.h>
41 #include <objidl.h>
42 #include <objbase.h>
43 #include <process.h>
44 #include <olectl.h>
45 #include <stdlib.h>
46 #include <malloc.h>
47 #include <crtdbg.h>
48 #include <..\..\inc\systools\win32\MtaOleClipb.h>
49 
50 #include "resource.h"
51 
52 #define MAX_LOADSTRING 100
53 
54 // Globale Variablen:
55 HINSTANCE			g_hInst;						// aktuelle Instanz
56 HWND			    g_hwndMain;
57 WCHAR				szTitle[MAX_LOADSTRING];			// Text der Titelzeile
58 WCHAR				szWindowClass[MAX_LOADSTRING];	// Text der Titelzeile
59 LPSTREAM			g_pStm    = NULL;
60 char*				pTextBuff = NULL;
61 DWORD				lData     = 0;
62 UINT				g_nCBChanges = 0;
63 
64 // forward declaration
65 ATOM  MyRegisterClass( HINSTANCE hInstance );
66 BOOL  InitInstance( HINSTANCE, int );
67 HMENU GetSubMenuHandle( HWND hwndParent, UINT uiTopLevelIndex );
68 BOOL  IsClipboardViewer( HWND hwndParent );
69 void  SwitchMenuState( HWND hwndParent );
70 void  RegisterClipboardViewer( BOOL bRegister );
71 void ShowAvailableClipboardFormats( HWND hWnd, HDC hdc, PAINTSTRUCT ps, RECT rcWnd );
72 void ClearClipboardContent( HWND hWnd );
73 
74 void    CALLBACK OnClipboardContentChange( void );
75 LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
76 LRESULT CALLBACK About( HWND, UINT, WPARAM, LPARAM );
77 
78 //----------------------------------------------------
79 // WinMain
80 //----------------------------------------------------
81 
82 int APIENTRY WinMain(HINSTANCE hInstance,
83                      HINSTANCE hPrevInstance,
84                      LPSTR     lpCmdLine,
85                      int       nCmdShow )
86 {
87  	// ZU ERLEDIGEN: F�gen Sie hier den Code ein.
88 	MSG     msg;
89 	HACCEL  hAccelTable;
90 	HRESULT hr = E_FAIL;
91 
92 	// it's important to initialize ole
93 	// in order to use the clipboard
94 	//hr = OleInitialize( NULL );
95 	hr = CoInitializeEx( NULL, COINIT_MULTITHREADED );
96 
97 	g_hInst = hInstance;
98 
99 	// Globale Zeichenfolgen initialisieren
100 	LoadStringW(g_hInst, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
101 	LoadStringW(g_hInst, IDC_TESTWIN32, szWindowClass, MAX_LOADSTRING);
102 	MyRegisterClass(g_hInst);
103 
104 	// Initialisierung der Anwendung durchf�hren:
105 	if( !InitInstance( g_hInst, nCmdShow ) )
106 	{
107 		return FALSE;
108 	}
109 
110 	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_TESTWIN32);
111 
112 	// Hauptnachrichtenschleife:
113 	while( GetMessage(&msg, NULL, 0, 0) )
114 	{
115 		if( !TranslateAccelerator (msg.hwnd, hAccelTable, &msg) )
116 		{
117 			TranslateMessage( &msg );
118 			DispatchMessage( &msg );
119 		}
120 	}
121 
122 	// uninitializing the ole libraries
123 	//OleUninitialize( );
124 	CoUninitialize( );
125 
126 	return msg.wParam;
127 }
128 
129 
130 
131 //
132 //  FUNKTION: MyRegisterClass()
133 //
134 //  AUFGABE: Registriert die Fensterklasse.
135 //
136 //  KOMMENTARE:
137 //
138 //    Diese Funktion und ihre Verwendung sind nur notwendig, wenn dieser Code
139 //    mit Win32-Systemen vor der 'RegisterClassEx'-Funktion kompatibel sein soll,
140 //    die zu Windows 95 hinzugef�gt wurde. Es ist wichtig diese Funktion aufzurufen,
141 //    damit der Anwendung kleine Symbole mit den richtigen Proportionen zugewiesen
142 //    werden.
143 //
144 ATOM MyRegisterClass( HINSTANCE hInstance )
145 {
146 	WNDCLASSEXW wcex;
147 
148 	wcex.cbSize = sizeof(WNDCLASSEX);
149 
150 	wcex.style			= CS_HREDRAW | CS_VREDRAW;
151 	wcex.lpfnWndProc	= (WNDPROC)WndProc;
152 	wcex.cbClsExtra		= 0;
153 	wcex.cbWndExtra		= 0;
154 	wcex.hInstance		= hInstance;
155 	wcex.hIcon			= LoadIcon(hInstance, (LPCTSTR)IDI_TESTWIN32);
156 	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
157 	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
158 	wcex.lpszMenuName	= (LPCWSTR)IDC_TESTWIN32;
159 	wcex.lpszClassName	= szWindowClass;
160 	wcex.hIconSm		= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
161 
162 	return RegisterClassExW(&wcex);
163 }
164 
165 //
166 //   FUNKTION: InitInstance(HANDLE, int)
167 //
168 //   AUFGABE: Speichert die Instanzzugriffsnummer und erstellt das Hauptfenster
169 //
170 //   KOMMENTARE:
171 //
172 //        In dieser Funktion wird die Instanzzugriffsnummer in einer globalen Variable
173 //        gespeichert und das Hauptprogrammfenster erstellt und angezeigt.
174 //
175 BOOL InitInstance( HINSTANCE hInstance, int nCmdShow )
176 {
177    g_hwndMain = CreateWindowExW(0, szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
178       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
179 
180    if( !IsWindow( g_hwndMain ) )
181    {
182       return FALSE;
183    }
184 
185    ShowWindow( g_hwndMain, nCmdShow );
186    UpdateWindow( g_hwndMain );
187 
188    return TRUE;
189 }
190 
191 //
192 //  FUNKTION: WndProc(HWND, unsigned, WORD, LONG)
193 //
194 //  AUFGABE:  Verarbeitet Nachrichten f�r das Hauptfenster.
195 //
196 //  WM_COMMAND	- Anwendungsmen� verarbeiten
197 //  WM_PAINT	- Hauptfenster darstellen
198 //  WM_DESTROY	- Beendigungsnachricht ausgeben und zur�ckkehren
199 //
200 //
201 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
202 {
203 	int         wmId;
204 	int         wmEvent;
205 	PAINTSTRUCT ps;
206 	HDC         hdc;
207 	WCHAR       szHello[MAX_LOADSTRING];
208 
209 
210 	LoadStringW(g_hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
211 
212 	switch( message )
213 	{
214 	case WM_CREATE:
215 		ClearClipboardContent( hWnd );
216 		break;
217 
218 		case WM_COMMAND:
219 			wmId    = LOWORD(wParam);
220 			wmEvent = HIWORD(wParam);
221 			// Men�auswahlen analysieren:
222 			switch( wmId )
223 			{
224 				case IDD_CBVIEWER:
225 					SwitchMenuState( hWnd );
226 					RegisterClipboardViewer( IsClipboardViewer( hWnd ) );
227 					break;
228 
229 				case IDM_EXIT:
230 				   DestroyWindow( hWnd );
231 				   break;
232 
233 				default:
234 				   return DefWindowProc( hWnd, message, wParam, lParam );
235 			}
236 			break;
237 
238 		case WM_PAINT:
239 			hdc = BeginPaint (hWnd, &ps);
240 			// ZU ERLEDIGEN: Hier beliebigen Code zum Zeichnen hinzuf�gen...
241 			RECT rt;
242 			GetClientRect( hWnd, &rt );
243 
244 			if ( IsClipboardViewer( g_hwndMain ) )
245 			{
246 				ShowAvailableClipboardFormats( hWnd, hdc, ps, rt );
247 			}
248 			else
249 			{
250 				WCHAR wcString[MAX_LOADSTRING];
251 				LoadStringW(g_hInst, IDS_MSG_CBVIEWER_IS_OFF, wcString, MAX_LOADSTRING);
252 				DrawTextW( hdc, wcString, wcslen( wcString ), &rt, DT_CENTER );
253 			}
254 
255 			EndPaint( hWnd, &ps );
256 			break;
257 
258 		case WM_DESTROY:
259 			PostQuitMessage( 0 );
260 			break;
261 
262 		default:
263 			return DefWindowProc( hWnd, message, wParam, lParam );
264    }
265    return 0;
266 }
267 
268 HMENU GetSubMenuHandle( HWND hwndParent, UINT uiTopLevelIndex )
269 {
270 	HMENU hMenuMain = GetMenu( hwndParent );
271 	_ASSERT( IsMenu( hMenu ) );
272 
273 	HMENU hSubMenu = GetSubMenu( hMenuMain, uiTopLevelIndex );
274 	_ASSERT( IsMenu( hSubMenu ) );
275 
276 	return hSubMenu;
277 }
278 
279 BOOL IsClipboardViewer( HWND hwndParent )
280 {
281 	HMENU hSubMenu = GetSubMenuHandle( hwndParent, 0 );
282 	UINT uiMState = GetMenuState( hSubMenu, 0, MF_BYPOSITION );
283 	return ( MF_CHECKED == uiMState );
284 }
285 
286 void SwitchMenuState( HWND hwndParent )
287 {
288 	HMENU hSubMenu = GetSubMenuHandle( hwndParent, 0 );
289 	WCHAR wcMenuString[MAX_LOADSTRING];
290 
291 	if ( IsClipboardViewer( hwndParent ) )
292 	{
293 		LoadStringW(g_hInst, IDS_CBVIEWER_OFF, wcMenuString, MAX_LOADSTRING);
294 		ModifyMenuW( hSubMenu, 0, MF_BYPOSITION | MF_STRING, IDD_CBVIEWER, wcMenuString );
295 		CheckMenuItem( hSubMenu, 0, MF_BYPOSITION | MF_UNCHECKED );
296 	}
297 	else
298 	{
299 		LoadStringW(g_hInst, IDS_CBVIEWER_ON, wcMenuString, MAX_LOADSTRING);
300 		ModifyMenuW( hSubMenu, 0, MF_BYPOSITION | MF_STRING, IDD_CBVIEWER, wcMenuString );
301 		CheckMenuItem( hSubMenu, 0, MF_BYPOSITION | MF_CHECKED );
302 	}
303 }
304 
305 void RegisterClipboardViewer( BOOL bRegister )
306 {
307 	if ( bRegister )
308 		MTARegisterClipboardViewer( OnClipboardContentChange );
309 	else // unregister
310 		MTARegisterClipboardViewer( NULL );
311 
312 	InvalidateRect( g_hwndMain, NULL, TRUE );
313 	UpdateWindow( g_hwndMain );
314 }
315 
316 void ShowAvailableClipboardFormats( HWND hWnd, HDC hdc, PAINTSTRUCT ps, RECT rcWnd )
317 {
318 	if ( !OpenClipboard( hWnd ) )
319 	{
320 		WCHAR szErrMsg[] = { L"Couldn't open the clipboard" };
321 		DrawTextW( hdc, szErrMsg, wcslen( szErrMsg ), &rcWnd, DT_CENTER );
322 		return;
323 	}
324 	else
325 	{
326 		WCHAR szCBChangedMsg[100];
327 
328 		wsprintfW( szCBChangedMsg, L"Clipboard content changed %d", g_nCBChanges );
329 		DrawTextW( hdc, szCBChangedMsg, wcslen( szCBChangedMsg ), &rcWnd, DT_CENTER );
330 
331 		CloseClipboard( );
332 	}
333 }
334 
335 void ClearClipboardContent( HWND hWnd )
336 {
337 	if ( OpenClipboard( hWnd ) )
338 	{
339 		EmptyClipboard( );
340 		CloseClipboard( );
341 	}
342 }
343 
344 // clipboard viewer callback function
345 void CALLBACK OnClipboardContentChange( void )
346 {
347 	++g_nCBChanges;
348 	InvalidateRect( g_hwndMain, NULL, TRUE );
349 	UpdateWindow( g_hwndMain );
350 }
351 
352