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