File: dlgWndBase.cpp

package info (click to toggle)
beid 3.5.2.dfsg-10
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 147,240 kB
  • ctags: 34,507
  • sloc: cpp: 149,944; ansic: 41,577; java: 8,927; cs: 6,528; sh: 2,426; perl: 1,866; xml: 805; python: 463; makefile: 263; lex: 92
file content (407 lines) | stat: -rw-r--r-- 12,654 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/* ****************************************************************************

 * eID Middleware Project.
 * Copyright (C) 2008-2009 FedICT.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version
 * 3.0 as published by the Free Software Foundation.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, see
 * http://www.gnu.org/licenses/.

**************************************************************************** */

#include "stdafx.h"
#include "resource.h"
#include "../langUtil.h"
#include "Log.h"

TD_WNDMAP WndMap;
Win32Dialog *Win32Dialog::Active_lpWnd = NULL;
HWND Win32Dialog::Active_hWnd = NULL;
extern HMODULE g_hDLLInstance;// = (HMODULE)NULL;

Win32Dialog::Win32Dialog(const wchar_t *appName)
{
	m_ModalHold = true;
	m_hDC = NULL;					// Private GDI Device Context
	m_hWnd = NULL;					// Holds Our Window Handle
	m_hInstance = g_hDLLInstance;	// Grab An Instance From our DLL module to become able to Create our windows for/from
	//m_appName = "DialogBase";		// Application Core-Name
	dlgResult = eIDMW::DLG_CANCEL;	// Dialog Result
	m_appName=_wcsdup(appName);
}

Win32Dialog::~Win32Dialog()
{
	if( m_hWnd )
		KillWindow( );

	if(m_appName)
	{
		free(m_appName);
		m_appName=NULL;
	}

	m_ModalHold = false;
}


bool Win32Dialog::CreateWnd( const wchar_t* title, int width, int height, int Icon, HWND Parent )
{
	if( m_hWnd )
		return false;

	MWLOG(LEV_DEBUG, MOD_DLG, L"  --> Win32Dialog::CreateWnd (Parent=%X)",Parent);

	DWORD		dwExStyle;				// Window Extended Style
	DWORD		dwStyle;				// Window Style
	RECT		WindowRect;				// Grabs Rectangle Upper Left / Lower Right Values
	RECT		DeskRect;

	WindowRect.left = (long)0;			// Set Left Value To 0
	WindowRect.right = (long)width;		// Set Right Value To Requested Width
	WindowRect.top = (long)0;			// Set Top Value To 0
	WindowRect.bottom = (long)height;	// Set Bottom Value To Requested Height
	GetClientRect( GetDesktopWindow(), &DeskRect );

	WNDCLASS	wc;						// Windows Class Structure
	HICON hIco;
	if( Icon == 0 )
		hIco = LoadIcon( NULL, IDI_WINLOGO );
	else
		hIco = LoadIcon( m_hInstance, MAKEINTRESOURCE(Icon) );

	wc.style			= CS_HREDRAW | CS_VREDRAW; // | CS_OWNDC;	// Redraw On Size, And Own DC For Window.
	wc.lpfnWndProc		= (WNDPROC)WndProc;							// WndProc Handles Messages
	wc.cbClsExtra		= 0;										// No Extra Window Data
	wc.cbWndExtra		= 0; //DLGWINDOWEXTRA;							// No Extra Window Data
	wc.hInstance		= m_hInstance;								// Set The Instance
	wc.hIcon			= hIco;			// Load The Default Icon
	wc.hCursor			= LoadCursor( NULL, IDC_ARROW );			// Load The Arrow Pointer
	wc.hbrBackground	= (HBRUSH)GetSysColorBrush( COLOR_3DFACE );	// What Color we want in our background
	wc.lpszMenuName		= NULL;										// We Don't Want A Menu
	wc.lpszClassName	= m_appName;								// Set The Class Name

	if( !RegisterClass( &wc ) )									// Attempt To Register The Window Class
	{
		unsigned long err = GetLastError();
		MWLOG(LEV_WARN, MOD_DLG, L"  --> Win32Dialog::CreateWnd - Failed To Register The Window Class - Error=%ld",err);
		return false;											// Return FALSE
	}

	dwStyle = WS_CAPTION | WS_VISIBLE |  WS_SYSMENU | WS_OVERLAPPED;
	dwExStyle = WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_WINDOWEDGE | WS_EX_TOPMOST;
	if( m_ModalHold )
	{
		dwStyle |= WS_POPUP;
		dwExStyle |= WS_EX_DLGMODALFRAME;
	}

	AdjustWindowRectEx( &WindowRect, dwStyle, FALSE, dwExStyle );	// Adjust Window To True Requested Size

	Active_lpWnd = this;
	// Create The Window
	if( !( m_hWnd = Active_hWnd = CreateWindowEx(	dwExStyle,			// Extended Style For The Window
								m_appName,							// Class Name
								title,								// Window Title
								dwStyle,							// Defined Window Style
								DeskRect.right/2 - (WindowRect.right-WindowRect.left)/2,
								DeskRect.bottom/2 - (WindowRect.bottom-WindowRect.top)/2,
								//CW_USEDEFAULT, CW_USEDEFAULT,		// Window Position
								WindowRect.right-WindowRect.left,	// Calculate Window Width
								WindowRect.bottom-WindowRect.top,	// Calculate Window Height
								Parent,								// No Parent Window
								NULL,								// No Menu
								m_hInstance,							// Instance
								(LPVOID)Active_lpWnd)))								// Dont Pass Anything To WM_CREATE
	{
		unsigned long err = GetLastError();
		KillWindow( );								// Reset The Display
		MWLOG(LEV_WARN, MOD_DLG, L"  --> Win32Dialog::CreateWnd - Window Creation Error - Error=%ld",err);
		return false;								// Return FALSE
	}
	MWLOG(LEV_DEBUG, MOD_DLG, L"  --> Win32Dialog::CreateWnd - CreateWindowEx (m_hWnd=%X)",m_hWnd);

	WndMap.insert( TD_WNDPAIR( m_hWnd, this ) );

	return true;
}

bool Win32Dialog::exec()
{
	if( !m_hWnd )
		return false;
	
	ShowWindow( m_hWnd, SW_SHOW );					// Show The Window
	//SetFocus( m_hWnd );								// Sets Keyboard Focus To The Window


	MSG msg;
	while( GetMessage( &msg, NULL, 0, 0 ) && m_ModalHold )
	{
		if( NULL == m_hWnd || !IsDialogMessage( m_hWnd, &msg ) )
		{
			TranslateMessage( &msg );			// Translate The Message
			DispatchMessage( &msg );			// Dispatch The Message
		}
		if( msg.hwnd == m_hWnd )
		{
			if( msg.message == 274 || msg.message == 161 ) // || msg.message == 513 ) // 161 == WM_RESIZE | WM_MOVE; 274 == WM_CLOSE; 513 == WM_COMMAND
			{
				//check if it's minimized 
				if( IsIconic( m_hWnd ) ) 
				{
					break; // Exit Loop
				}
			}
		}
	}
	return 	dlgResult != eIDMW::DLG_CANCEL;
}
void Win32Dialog::show()
{
	ShowWindow( m_hWnd, SW_SHOW );					// Show The Window
	SetFocus( m_hWnd );								// Sets Keyboard Focus To The Window
}

void Win32Dialog::close()
{
	//CloseWindow( m_hWnd ) and ShowWindow( m_hWnd, SW_MINIMIZE ) should do the same
	//but they don't.
	//This is a workarond for signing mail with Oulook 2003 (with Word as editor and 1 processor)
	//If the ShowWindow is used in place of the CloseWindow, Word hangs ?????

	CloseWindow( m_hWnd );					
	//ShowWindow( m_hWnd, SW_MINIMIZE );					// Show The Window

	m_ModalHold = false;							// Sets Keyboard Focus To The Window
}

void Win32Dialog::CreateBitapMask( HBITMAP & BmpSource, HBITMAP & BmpMask )
{
	BITMAP bm;
	// Get the dimensions of the source bitmap
	GetObject( BmpSource, sizeof(BITMAP), &bm);

	HDC hdcSrc, hdcDst;
	hdcSrc = CreateCompatibleDC( NULL );
	hdcDst = CreateCompatibleDC( NULL );

	// Create the mask bitmap
	BmpMask = CreateBitmap( bm.bmWidth, bm.bmHeight, 1, 1, NULL );

	// Load the bitmaps into memory DC
	HBITMAP hbmSrcT = (HBITMAP) SelectObject( hdcSrc, BmpSource );
	HBITMAP hbmDstT = (HBITMAP) SelectObject( hdcDst, BmpMask );

	// Set the transparent color
	COLORREF clrTrans = RGB( 255, 255, 255 );

	// Change the background to trans color
	COLORREF clrSaveBk  = SetBkColor( hdcSrc, clrTrans );

	// This call sets up the mask bitmap.
	BitBlt( hdcDst, 0, 0, bm.bmWidth, bm.bmHeight, hdcSrc, 0, 0, NOTSRCCOPY ); // NOTSRCCOPY );


	// Now, we need to paint onto the original image, making
	// sure that the "transparent" area is set to black. What
	// we do is AND the monochrome image onto the color Image
	// first. When blitting from mono to color, the monochrome
	// pixel is first transformed as follows:
	// if  1 (black) it is mapped to the color set by SetTextColor().
	// if  0 (white) is is mapped to the color set by SetBkColor().
	// Only then is the raster operation performed.

	COLORREF clrSaveDstText = SetTextColor( hdcSrc, 0xFFFFFFFF );
	SetBkColor( hdcSrc, 0x00000000 );

	SetTextColor( hdcDst, clrSaveDstText );

	SetBkColor( hdcSrc, clrSaveBk );
	SelectObject( hdcSrc, hbmSrcT );
	SelectObject( hdcDst, hbmDstT );

	DeleteDC( hdcSrc );
	DeleteDC( hdcDst );

	// connect the bitmap to it's handle
	GetObject( BmpMask, sizeof(BITMAP), &bm);

}

void Win32Dialog::Destroy()
{
}

void Win32Dialog::KillWindow( void )							// Properly Kill The Window
{
	if( !m_hWnd )
		return;

	if( m_hWnd && !DestroyWindow( m_hWnd ) )				// Are We Able To Destroy The Window?
	{
		MessageBox( NULL,L"Could Not Release hWnd.",L"SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION );
	}

	if( !UnregisterClass( m_appName, m_hInstance ) )		// Are We Able To Unregister Class
	{
		MessageBox( NULL,L"Could Not Unregister Class.",L"SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION );
	}

	WndMap.erase( m_hWnd );
	if( Active_hWnd == m_hWnd )
		Active_hWnd = NULL;
	
	m_hInstance = NULL;								// Set hInstance To NULL
	m_hWnd = NULL;									// Set hWnd To NULL
	Destroy();
}

bool Win32Dialog::isFriend( HWND f_hWnd )
{
	if( !WndMap.empty() )
	{
		try
		{
			TD_WNDMAP::iterator it_WndMap = WndMap.find( f_hWnd );
			if( it_WndMap != WndMap.end() )
				return true;
		}
		catch( ... ){}
	}

	return false;
}

LRESULT CALLBACK Win32Dialog::WndProc(	HWND	hWnd,			// Handle For This Window
						UINT	uMsg,			// Message For This Window
						WPARAM	wParam,			// Additional Message Information
						LPARAM	lParam)			// Additional Message Information
{
	if( !WndMap.empty() )
	{
		try // Call the WndProc Function from the 'HWND-Owner' Window Class 
		{
			TD_WNDMAP::iterator it_WndMap = WndMap.find( hWnd );
			if( it_WndMap != WndMap.end() )
				return (*it_WndMap).second->ProcecEvent( uMsg, wParam, lParam );
		}
		catch( ... ){}
	}
	if( uMsg == WM_CREATE || uMsg == WM_NCCREATE )  // Call the WndProc Function from the 'HWND-Owner' Window Class Retrieved from the lParam->(CREATESTRUCT *)lpCreateParams
	{
		try
		{
			if( ((CREATESTRUCT *)lParam)->lpCreateParams == (LPVOID)Active_lpWnd )
			{	
				((CREATESTRUCT *)lParam)->lpCreateParams = (LPVOID)hWnd; // Replace the Extra parameter with the handle to the window so the class resolving the message knows it's own handle too ( [Win32Dialog::CreateWnd(..)]CreateWindowEx did not yet return a value here )
				Active_lpWnd->ProcecEvent( uMsg, wParam, lParam );
			}
		}
		catch( ... ){}
	}

	return DefWindowProc( hWnd, uMsg, wParam, lParam );
}

LRESULT Win32Dialog::ProcecEvent
			(	UINT		uMsg,			// Message For This Window
				WPARAM		wParam,			// Additional Message Information
				LPARAM		lParam )		// Additional Message Information
{
	if( m_hWnd == NULL )
		return 0;

	PAINTSTRUCT ps;
	RECT rect;

	switch( uMsg )
	{
		case WM_PAINT:
		{
			m_hDC = BeginPaint( m_hWnd, &ps );
				SetBkColor( m_hDC, GetSysColor( COLOR_3DFACE ) );
				GetClientRect( m_hWnd, &rect );
				DrawText( m_hDC, L"Virtual Hello World!", -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER );
			EndPaint( m_hWnd, &ps );

			SetForegroundWindow( m_hWnd );

			return 0;
		}

		case WM_NCACTIVATE:
		{
			MWLOG(LEV_DEBUG, MOD_DLG, L"  --> Win32Dialog::ProcecEvent WM_NCACTIVATE (wParam=%X, lParam=%X)",wParam,lParam);
			
			if( !IsIconic( m_hWnd ) && m_ModalHold && Active_hWnd == m_hWnd )
			{
				ShowWindow( m_hWnd, SW_SHOW );
				SetFocus( m_hWnd );
				return 0;
			}
			break;
		}

		case WM_KILLFOCUS:
		{
			MWLOG(LEV_DEBUG, MOD_DLG, L"  --> Win32Dialog::ProcecEvent WM_KILLFOCUS (wParam=%X, lParam=%X)",wParam,lParam);

			if( !IsIconic( m_hWnd ) && m_ModalHold && Active_hWnd == m_hWnd )
			{
				if( GetParent((HWND)wParam ) != m_hWnd )
				{
					SetFocus( m_hWnd );
					return 0;
				}
			}
			break;
		}

		case WM_CREATE:
		{
			MWLOG(LEV_DEBUG, MOD_DLG, L"  --> Win32Dialog::ProcecEvent WM_CREATE (wParam=%X, lParam=%X)",wParam,lParam);

			HMENU hSysMenu;

			hSysMenu = GetSystemMenu( m_hWnd, FALSE);
			RemoveMenu( hSysMenu, 2, MF_BYPOSITION );
			return 0;
		}

		case WM_CLOSE:
		{
			MWLOG(LEV_DEBUG, MOD_DLG, L"  --> Win32Dialog::ProcecEvent WM_CLOSE (wParam=%X, lParam=%X)",wParam,lParam);

			if( IsIconic( m_hWnd ) )
				return DefWindowProc( m_hWnd, uMsg, wParam, lParam );
			if( m_ModalHold )
			{
				ShowWindow( m_hWnd, SW_MINIMIZE );
				return 0;
			}
		}

		case WM_DESTROY: 
		{
			MWLOG(LEV_DEBUG, MOD_DLG, L"  --> Win32Dialog::ProcecEvent WM_DESTROY (wParam=%X, lParam=%X)",wParam,lParam);
			break;
		}

		default:
		{
			return DefWindowProc( m_hWnd, uMsg, wParam, lParam );
		}
	}
	return DefWindowProc( m_hWnd, uMsg, wParam, lParam );
}