1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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.
38 #include <..\..\inc\systools\win32\MtaOleClipb.h>
42 #define MAX_LOADSTRING 100
45 HINSTANCE g_hInst
; // current instance
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
;
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 //----------------------------------------------------
70 //----------------------------------------------------
72 int APIENTRY
WinMain(HINSTANCE hInstance
,
73 HINSTANCE hPrevInstance
,
77 // TODO: Add code here.
82 // it's important to initialize ole
83 // in order to use the clipboard
84 //hr = OleInitialize( NULL );
85 hr
= CoInitializeEx( NULL
, COINIT_MULTITHREADED
);
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
) )
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( );
122 // FUNCTION: MyRegisterClass()
124 // PURPOSE: Registers the window class.
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
)
136 wcex
.cbSize
= sizeof(WNDCLASSEX
);
138 wcex
.style
= CS_HREDRAW
| CS_VREDRAW
;
139 wcex
.lpfnWndProc
= (WNDPROC
)WndProc
;
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
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
) )
173 ShowWindow( g_hwndMain
, nCmdShow
);
174 UpdateWindow( g_hwndMain
);
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
)
195 WCHAR szHello
[MAX_LOADSTRING
];
198 LoadStringW(g_hInst
, IDS_HELLO
, szHello
, MAX_LOADSTRING
);
203 ClearClipboardContent( hWnd
);
207 wmId
= LOWORD(wParam
);
208 // Analyze menu selections:
212 SwitchMenuState( hWnd
);
213 RegisterClipboardViewer( IsClipboardViewer( hWnd
) );
217 DestroyWindow( hWnd
);
221 return DefWindowProc( hWnd
, message
, wParam
, lParam
);
226 hdc
= BeginPaint (hWnd
, &ps
);
227 // TODO: Add any code for drawing
229 GetClientRect( hWnd
, &rt
);
231 if ( IsClipboardViewer( g_hwndMain
) )
233 ShowAvailableClipboardFormats( hWnd
, hdc
, ps
, rt
);
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
);
246 PostQuitMessage( 0 );
250 return DefWindowProc( hWnd
, message
, wParam
, lParam
);
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
) );
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
);
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
)
295 MTARegisterClipboardViewer( OnClipboardContentChange
);
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
);
313 WCHAR szCBChangedMsg
[100];
315 wsprintfW( szCBChangedMsg
, L
"Clipboard content changed %d", g_nCBChanges
);
316 DrawTextW( hdc
, szCBChangedMsg
, wcslen( szCBChangedMsg
), &rcWnd
, DT_CENTER
);
322 void ClearClipboardContent( HWND hWnd
)
324 if ( OpenClipboard( hWnd
) )
331 // clipboard viewer callback function
332 void CALLBACK
OnClipboardContentChange( void )
335 InvalidateRect( g_hwndMain
, NULL
, TRUE
);
336 UpdateWindow( g_hwndMain
);
339 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */