Updated core
[LibreOffice.git] / sal / workben / clipboardwben / testviewer / cbvtest.cxx
blob663a083cc99d35fe4dd68a3dcb3a69c787544cc4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 // TestWin32.cpp : Defines the entry point for the application.
24 #define _WIN32_DCOM
26 #include "stdafx.h"
28 #include <windows.h>
30 #include <ole2.h>
31 #include <objidl.h>
32 #include <objbase.h>
33 #include <process.h>
34 #include <olectl.h>
35 #include <stdlib.h>
36 #include <malloc.h>
37 #include <crtdbg.h>
38 #include <..\..\inc\systools\win32\MtaOleClipb.h>
40 #include "resource.h"
42 #define MAX_LOADSTRING 100
44 // Global variables:
45 HINSTANCE g_hInst; // current instance
46 HWND g_hwndMain;
47 WCHAR szTitle[MAX_LOADSTRING]; // Text for title
48 WCHAR szWindowClass[MAX_LOADSTRING]; // Text for title
49 LPSTREAM g_pStm = NULL;
50 char* pTextBuff = NULL;
51 DWORD lData = 0;
52 UINT g_nCBChanges = 0;
54 // forward declaration
55 ATOM MyRegisterClass( HINSTANCE hInstance );
56 BOOL InitInstance( HINSTANCE, int );
57 HMENU GetSubMenuHandle( HWND hwndParent, UINT uiTopLevelIndex );
58 BOOL IsClipboardViewer( HWND hwndParent );
59 void SwitchMenuState( HWND hwndParent );
60 void RegisterClipboardViewer( BOOL bRegister );
61 void ShowAvailableClipboardFormats( HWND hWnd, HDC hdc, PAINTSTRUCT ps, RECT rcWnd );
62 void ClearClipboardContent( HWND hWnd );
64 void CALLBACK OnClipboardContentChange( void );
65 LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
66 LRESULT CALLBACK About( HWND, UINT, WPARAM, LPARAM );
68 //----------------------------------------------------
69 // WinMain
70 //----------------------------------------------------
72 int APIENTRY WinMain(HINSTANCE hInstance,
73 HINSTANCE hPrevInstance,
74 LPSTR lpCmdLine,
75 int nCmdShow )
77 // TODO: Add code here.
78 MSG msg;
79 HACCEL hAccelTable;
80 HRESULT hr = E_FAIL;
82 // it's important to initialize ole
83 // in order to use the clipboard
84 //hr = OleInitialize( NULL );
85 hr = CoInitializeEx( NULL, COINIT_MULTITHREADED );
87 g_hInst = hInstance;
89 // Initialize global strings
90 LoadStringW(g_hInst, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
91 LoadStringW(g_hInst, IDC_TESTWIN32, szWindowClass, MAX_LOADSTRING);
92 MyRegisterClass(g_hInst);
94 // Initialization of the applications to carry out
95 if( !InitInstance( g_hInst, nCmdShow ) )
97 return FALSE;
100 hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_TESTWIN32);
102 // Main message loop:
103 while( GetMessage(&msg, NULL, 0, 0) )
105 if( !TranslateAccelerator (msg.hwnd, hAccelTable, &msg) )
107 TranslateMessage( &msg );
108 DispatchMessage( &msg );
112 // uninitializing the ole libraries
113 //OleUninitialize( );
114 CoUninitialize( );
116 return msg.wParam;
122 // FUNCTION: MyRegisterClass()
124 // PURPOSE: Registers the window class.
126 // COMMENTS:
127 // This function and its usage are only necessary if this code
128 // needs to be compatible with Win32 systems prior to 'RegisterClassEx'
129 // function, which was added to Windows 95. If it important to call
130 // this function to allow the use of small icons in the correct proportions.
132 ATOM MyRegisterClass( HINSTANCE hInstance )
134 WNDCLASSEXW wcex;
136 wcex.cbSize = sizeof(WNDCLASSEX);
138 wcex.style = CS_HREDRAW | CS_VREDRAW;
139 wcex.lpfnWndProc = (WNDPROC)WndProc;
140 wcex.cbClsExtra = 0;
141 wcex.cbWndExtra = 0;
142 wcex.hInstance = hInstance;
143 wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_TESTWIN32);
144 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
145 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
146 wcex.lpszMenuName = (LPCWSTR)IDC_TESTWIN32;
147 wcex.lpszClassName = szWindowClass;
148 wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
150 return RegisterClassExW(&wcex);
154 // FUNKTION: InitInstance(HANDLE, int)
156 // PURPOSE: Saves instance access number and creates main window
158 // Comments:
159 // In this function, the instance access number is stored in a global variable
160 // and the main program window is displayed.
163 BOOL InitInstance( HINSTANCE hInstance, int nCmdShow )
165 g_hwndMain = CreateWindowExW(0, szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
166 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
168 if( !IsWindow( g_hwndMain ) )
170 return FALSE;
173 ShowWindow( g_hwndMain, nCmdShow );
174 UpdateWindow( g_hwndMain );
176 return TRUE;
180 // FUNKTION: WndProc(HWND, unsigned, WORD, LONG)
182 // FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
184 // PURPOSE: Processes messages for the main window.
186 // WM_COMMAND - Handle application menu
187 // WM_PAINT - Display main windows
188 // WM_DESTROY - Output completion message and return
190 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
192 int wmId;
193 PAINTSTRUCT ps;
194 HDC hdc;
195 WCHAR szHello[MAX_LOADSTRING];
198 LoadStringW(g_hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
200 switch( message )
202 case WM_CREATE:
203 ClearClipboardContent( hWnd );
204 break;
206 case WM_COMMAND:
207 wmId = LOWORD(wParam);
208 // Analyze menu selections:
209 switch( wmId )
211 case IDD_CBVIEWER:
212 SwitchMenuState( hWnd );
213 RegisterClipboardViewer( IsClipboardViewer( hWnd ) );
214 break;
216 case IDM_EXIT:
217 DestroyWindow( hWnd );
218 break;
220 default:
221 return DefWindowProc( hWnd, message, wParam, lParam );
223 break;
225 case WM_PAINT:
226 hdc = BeginPaint (hWnd, &ps);
227 // TODO: Add any code for drawing
228 RECT rt;
229 GetClientRect( hWnd, &rt );
231 if ( IsClipboardViewer( g_hwndMain ) )
233 ShowAvailableClipboardFormats( hWnd, hdc, ps, rt );
235 else
237 WCHAR wcString[MAX_LOADSTRING];
238 LoadStringW(g_hInst, IDS_MSG_CBVIEWER_IS_OFF, wcString, MAX_LOADSTRING);
239 DrawTextW( hdc, wcString, wcslen( wcString ), &rt, DT_CENTER );
242 EndPaint( hWnd, &ps );
243 break;
245 case WM_DESTROY:
246 PostQuitMessage( 0 );
247 break;
249 default:
250 return DefWindowProc( hWnd, message, wParam, lParam );
252 return 0;
255 HMENU GetSubMenuHandle( HWND hwndParent, UINT uiTopLevelIndex )
257 HMENU hMenuMain = GetMenu( hwndParent );
258 _ASSERT( IsMenu( hMenu ) );
260 HMENU hSubMenu = GetSubMenu( hMenuMain, uiTopLevelIndex );
261 _ASSERT( IsMenu( hSubMenu ) );
263 return hSubMenu;
266 BOOL IsClipboardViewer( HWND hwndParent )
268 HMENU hSubMenu = GetSubMenuHandle( hwndParent, 0 );
269 UINT uiMState = GetMenuState( hSubMenu, 0, MF_BYPOSITION );
270 return ( MF_CHECKED == uiMState );
273 void SwitchMenuState( HWND hwndParent )
275 HMENU hSubMenu = GetSubMenuHandle( hwndParent, 0 );
276 WCHAR wcMenuString[MAX_LOADSTRING];
278 if ( IsClipboardViewer( hwndParent ) )
280 LoadStringW(g_hInst, IDS_CBVIEWER_OFF, wcMenuString, MAX_LOADSTRING);
281 ModifyMenuW( hSubMenu, 0, MF_BYPOSITION | MF_STRING, IDD_CBVIEWER, wcMenuString );
282 CheckMenuItem( hSubMenu, 0, MF_BYPOSITION | MF_UNCHECKED );
284 else
286 LoadStringW(g_hInst, IDS_CBVIEWER_ON, wcMenuString, MAX_LOADSTRING);
287 ModifyMenuW( hSubMenu, 0, MF_BYPOSITION | MF_STRING, IDD_CBVIEWER, wcMenuString );
288 CheckMenuItem( hSubMenu, 0, MF_BYPOSITION | MF_CHECKED );
292 void RegisterClipboardViewer( BOOL bRegister )
294 if ( bRegister )
295 MTARegisterClipboardViewer( OnClipboardContentChange );
296 else // unregister
297 MTARegisterClipboardViewer( NULL );
299 InvalidateRect( g_hwndMain, NULL, TRUE );
300 UpdateWindow( g_hwndMain );
303 void ShowAvailableClipboardFormats( HWND hWnd, HDC hdc, PAINTSTRUCT ps, RECT rcWnd )
305 if ( !OpenClipboard( hWnd ) )
307 WCHAR szErrMsg[] = { L"Couldn't open the clipboard" };
308 DrawTextW( hdc, szErrMsg, wcslen( szErrMsg ), &rcWnd, DT_CENTER );
309 return;
311 else
313 WCHAR szCBChangedMsg[100];
315 wsprintfW( szCBChangedMsg, L"Clipboard content changed %d", g_nCBChanges );
316 DrawTextW( hdc, szCBChangedMsg, wcslen( szCBChangedMsg ), &rcWnd, DT_CENTER );
318 CloseClipboard( );
322 void ClearClipboardContent( HWND hWnd )
324 if ( OpenClipboard( hWnd ) )
326 EmptyClipboard( );
327 CloseClipboard( );
331 // clipboard viewer callback function
332 void CALLBACK OnClipboardContentChange( void )
334 ++g_nCBChanges;
335 InvalidateRect( g_hwndMain, NULL, TRUE );
336 UpdateWindow( g_hwndMain );
339 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */