Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[6a7162]: GeneralMsgBox.cpp Maximize Restore History

Download this file

GeneralMsgBox.cpp    552 lines (429 with data), 14.9 kB

  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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/*
* Copyright (c) 2003-2008 Rony Shapiro <ronys@users.sourceforge.net>.
* All rights reserved. Use of the code is allowed under the
* Artistic License 2.0 terms, as specified in the LICENSE file
* distributed with this code, or available from
* http://www.opensource.org/licenses/artistic-license-2.0.php
*/
// GeneralMsgBox.cpp
/*
* GeneralMsgBox.h
*
* Implements the extended Message Box class.
*
* This is a cut down version of TcxMsgBox by Thales P. Carvalho but then
* significantly enhanced to support text with HTML formatting and links
* instead of a RTF string by using a CRichEditCtrlExtn control.
* See www.codeproject.com for the orgininal code
*/
#include "stdafx.h"
#include "GeneralMsgBox.h"
#include "RichEditCtrlExtn.h"
#include <RichEdit.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// Local constants
/*
The following _dlgData corresponds to:
DLGTEMPLATEEX dtex;
dtex.wDlgVer = 1;
dtex.wSignature = 0xFFFF;
dtex.dwHelpID = 0;
dtex.dwStyle = WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | DS_SETFONT | DS_MODALFRAME;
dtex.dwExStyle = WS_EX_APPWINDOW;
dtex.cDlgItems = 0;
dtex.x = 0;
dtex.y = 0;
dtex.cx = 185;
dtex.cy = 92;
no menu ( (WORD)0 )
no windowClass ( (WORD)0 )
caption L"."
point size (only if DS_SETFONT specified)
weight "
italic "
charset "
typeface "
*/
static const BYTE _dlgData[] =
{
0x01, 0x00, // wDlgVer (must be on DWORD boundary)
0xff, 0xff, // wSignature
0x00, 0x00, 0x00, 0x00, // dwhelpID
0x00, 0x00, 0x04, 0x00, // dsStyle
0xc0, 0x00, 0xc8, 0x90, // dwExStyle
0x00, 0x00, // cDlgItems
0x00, 0x00, // x
0x00, 0x00, // y
0xb9, 0x00, // cx
0x5c, 0x00, // cy
0x00, 0x00, // no menu (must be on WORD boundary)
0x00, 0x00, // no windowClass
0x2e, 0x00, 0x00, 0x00, // Caption = '.\0'
0x08, 0x00, // point size
0x00, 0x00, // weight
0x00, // italic
0x01, // charset
// typeface (must be on WORD boundary)
0x4d, 0x00, 0x53, 0x00, 0x20, 0x00, 0x53, 0x00, // 'MS S'
0x61, 0x00, 0x6e, 0x00, 0x73, 0x00, 0x20, 0x00, // 'ans '
0x53, 0x00, 0x65, 0x00, 0x72, 0x00, 0x69, 0x00, // 'Seri'
0x66, 0x00, 0x00, 0x00 // 'f\0'
};
/////////////////////////////////////////////////////////////////////////////
// CGeneralMsgBox
// Constructor
CGeneralMsgBox::CGeneralMsgBox(CWnd* pParentWnd)
: m_uDefCmdId((UINT)IDC_STATIC), m_uEscCmdId((UINT)IDC_STATIC),
m_hIcon(NULL), m_strTitle(AfxGetApp()->m_pszAppName)
{
m_pParentWnd = pParentWnd;
// default metric values
const int _aDefMetrics[NUM_OF_METRICS] =
{
10, // CX_LEFT_BORDER,
10, // CX_RIGHT_BORDER,
10, // CY_TOP_BORDER,
7, // CY_BOTTOM_BORDER,
10, // CX_ICON_MSG_SPACE,
7, // CY_BTNS_MSG_SPACE,
7, // CX_BTN_BORDER,
4, // CY_BTN_BORDER,
7, // CX_BTNS_SPACE,
40, // CX_MIN_BTN,
};
for (int i = 0; i < NUM_OF_METRICS; ++i) {
m_aMetrics[i] = _aDefMetrics[i];
}
}
// Destructor
CGeneralMsgBox::~CGeneralMsgBox()
{
}
// Replaces CDialog::DoModal
int CGeneralMsgBox::DoModal()
{
InitModalIndirect((LPCDLGTEMPLATE)_dlgData, m_pParentWnd);
return CDialog::DoModal();
}
/////////////////////////////////////////////////////////////////////////////
// CGeneralMsgBox - Button operations
// Add a button
void CGeneralMsgBox::AddButton(UINT uIDC, // button command ID
LPCTSTR pszText, // button text
BOOL bIsDefault, // set the button as default
BOOL bIsEscape) // return this command if user press escape
{
ASSERT(uIDC != (UINT)IDC_STATIC);
BTNDATA btndata;
btndata.uIDC = uIDC;
btndata.strBtn = pszText;
m_aBtns.Add(btndata);
if (bIsEscape)
m_uEscCmdId = uIDC;
if (bIsDefault)
m_uDefCmdId = uIDC;
}
// Add a button
void CGeneralMsgBox::AddButton(UINT uIDC, // button command ID
UINT uIdText, // string ID of button's text
BOOL bIsDefault, // set the button as default
BOOL bIsEscape) // return this command if user press escape
{
CString str;
if (uIdText == (UINT)-1)
uIdText = uIDC;
VERIFY(str.LoadString(uIdText));
AddButton(uIDC, str, bIsDefault, bIsEscape);
}
/////////////////////////////////////////////////////////////////////////////
// CGeneralMsgBox - Plain text operations
BOOL CGeneralMsgBox::SetMsg(UINT uMsgId)
{
return m_strMsg.LoadString(uMsgId);
}
BOOL CGeneralMsgBox::SetMsg(LPCTSTR pszMsg)
{
m_strMsg = pszMsg;
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CGeneralMsgBox - Icon operations
void CGeneralMsgBox::SetIcon(HICON hIcon)
{
m_hIcon = hIcon;
if (m_hIcon != NULL) {
// loading the icon and extracting it's dimensions
ICONINFO ii;
GetIconInfo(m_hIcon, &ii);
BITMAP bm;
GetObject(ii.hbmColor, sizeof(bm), &bm);
m_dimIcon.cx = bm.bmWidth;
m_dimIcon.cy = bm.bmHeight;
DeleteObject(ii.hbmColor);
DeleteObject(ii.hbmMask);
} else {
m_dimIcon.cx = 0;
m_dimIcon.cy = 0;
}
}
void CGeneralMsgBox::SetIcon(UINT uIcon)
{
SetIcon(AfxGetApp()->LoadIcon(uIcon));
}
void CGeneralMsgBox::SetStandardIcon(LPCTSTR pszIconName)
{
SetIcon(AfxGetApp()->LoadStandardIcon(pszIconName));
}
void CGeneralMsgBox::SetStandardIcon(UINT uIcon)
{
LPCTSTR pszIconName;
switch (uIcon) {
case MB_ICONEXCLAMATION: // Also: MB_ICONWARNING
pszIconName = IDI_WARNING;
break;
case MB_ICONASTERISK: // Also: MB_ICONINFORMATION,
pszIconName = IDI_INFORMATION;
break;
case MB_ICONQUESTION:
pszIconName = IDI_QUESTION;
break;
case MB_ICONHAND: // Also: ICONSTOP, MB_ICONERROR
pszIconName = IDI_ERROR;
break;
default:
pszIconName = IDI_ERROR; // to satisfy the compiler!
ASSERT(0);
}
SetIcon(AfxGetApp()->LoadStandardIcon(pszIconName));
}
/////////////////////////////////////////////////////////////////////////////
// CGeneralMsgBox - Overrides
BOOL CGeneralMsgBox::OnInitDialog()
{
if (!CDialog::OnInitDialog())
return FALSE;
SetWindowText(m_strTitle);
// Getting the base dialog unit used in pixel <-> d.u. conversion
CRect rc(0, 0, CX_DLGUNIT_BASE, CY_DLGUNIT_BASE);
MapDialogRect(rc);
m_dimDlgUnit = rc.Size();
// Creating the nested controls
CreateRtfCtrl();
CreateIcon();
CreateBtns();
// Updating the layout - preparing to show
UpdateLayout();
// Disabling the ESC key
if (m_uEscCmdId == (UINT)IDC_STATIC)
ModifyStyle(WS_SYSMENU, NULL);
// Focusing and setting the defaul button
if (m_uDefCmdId != (UINT)IDC_STATIC) {
GetDlgItem(m_uDefCmdId)->SetFocus();
SetDefID(m_uDefCmdId);
return FALSE;
}
return TRUE;
}
BOOL CGeneralMsgBox::OnWndMsg(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
if (message == WM_NOTIFY) {
REQRESIZE* prr = (REQRESIZE*)lParam;
if (prr->nmhdr.code == EN_REQUESTRESIZE) {
// (1)
// The rich edit control requested a resize.
// Store the requested resize.
m_dimMsg.cx = prr->rc.right - prr->rc.left;
m_dimMsg.cy = prr->rc.bottom - prr->rc.top;
*pResult = NULL;
return TRUE;
}
} else if (message == WM_CLOSE) {
if (m_uEscCmdId != (UINT)IDC_STATIC)
EndDialog(m_uEscCmdId);
return TRUE;
}
return CDialog::OnWndMsg(message, wParam, lParam, pResult);
}
BOOL CGeneralMsgBox::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
{
if (nCode == CN_COMMAND) {
if (pHandlerInfo == NULL && nID != (WORD)IDC_STATIC) {
EndDialog(nID);
return TRUE;
}
}
return CDialog::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}
BOOL CGeneralMsgBox::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN) {
if (pMsg->wParam == VK_RETURN) {
CWnd* pWnd = GetFocus();
if (pWnd != NULL) {
UINT uIDC = (UINT)pWnd->GetDlgCtrlID();
for (int i = 0; i < m_aBtns.GetSize(); ++i)
if (m_aBtns[i].uIDC == uIDC) {
m_uDefCmdId = uIDC;
break;
}
}
EndDialog(m_uDefCmdId);
return TRUE;
}
else if (pMsg->wParam == VK_ESCAPE || pMsg->wParam == VK_CANCEL) {
if (m_uEscCmdId != (UINT)IDC_STATIC)
EndDialog(m_uEscCmdId);
return TRUE;
}
}
return CDialog::PreTranslateMessage(pMsg);
}
/////////////////////////////////////////////////////////////////////////////
// CGeneralMsgBox - Overrides
void CGeneralMsgBox::CreateRtfCtrl()
{
// Creating the Rich Edit control
CRect rcDummy; // dimension doesn't matter here
m_edCtrl.Create(WS_CHILD | WS_VISIBLE | ES_LEFT | ES_MULTILINE | ES_READONLY,
rcDummy, this, (UINT)IDC_STATIC);
m_edCtrl.SetBackgroundColor(FALSE, ::GetSysColor(COLOR_3DFACE));
m_edCtrl.SetFont(GetFont());
m_strMsg.Trim();
m_edCtrl.SetWindowText(m_strMsg);
/////////////////////////////////////////////////////////
// Calculating the best Rich Edit control dimension
//
// Note:
// There's no Rich Edit operation to get the minimum
// width it needs to show its contents without line
// breaks. However, given a width, we can check
// how much height it needs to show its text (see
// bottomless rich edit controls).
// We'll use this mechanism to perform a binary search
// and find that best width value.
m_edCtrl.SetEventMask(ENM_REQUESTRESIZE);
m_dimMsg.cx = 0;
m_dimMsg.cy = 0;
// Performing a binary search for the best dimension
int cxFirst = 0;
int cxLast = ::GetSystemMetrics(SM_CXFULLSCREEN);
int cyMax = 0;
do {
// taking a guess
int cx = (cxFirst + cxLast) / 2;
// testing it
CRect rc(0, 0, cx, 1);
m_edCtrl.MoveWindow(rc);
m_edCtrl.SetRect(rc);
m_edCtrl.RequestResize();
// if it's the first guess, take it anyway
if (cyMax == 0)
cyMax = m_dimMsg.cy;
// refining the guess
if (m_dimMsg.cy > cyMax)
cxFirst = cx + 1;
else
cxLast = cx - 1;
}
while(cxFirst < cxLast);
}
void CGeneralMsgBox::CreateBtns()
{
// The minimum button's dimension
m_dimBtn = CSize(FromDlgX(m_aMetrics[CX_MIN_BTN]), 0);
// DC and Font for use in some dimension calculations
CClientDC dc(this);
CFont* pWndFont = GetFont();
dc.SelectObject(pWndFont);
CRect rcDummy; // dimesion doesn't matter here
int cBtns = m_aBtns.GetSize();
for (int i = 0; i < cBtns; ++i) {
BTNDATA& btndata = m_aBtns[i];
// Finding the minimum dimension needed to
// properly show any button
CSize dimBtn = dc.GetTextExtent(btndata.strBtn);
m_dimBtn.cx = max(m_dimBtn.cx, dimBtn.cx);
m_dimBtn.cy = max(m_dimBtn.cy, dimBtn.cy);
// Creating the button with MFC's CButton help.
CButton btnCtrl;
btnCtrl.Create(btndata.strBtn,
WS_CHILD|WS_VISIBLE|WS_TABSTOP,
rcDummy, this, btndata.uIDC);
btnCtrl.SetFont(pWndFont);
btnCtrl.UnsubclassWindow();
}
// Add the button margins
m_dimBtn.cx += 2 * FromDlgX(m_aMetrics[CX_BTN_BORDER]);
m_dimBtn.cy += 2 * FromDlgY(m_aMetrics[CY_BTN_BORDER]);
}
void CGeneralMsgBox::CreateIcon()
{
if (m_hIcon != NULL) {
CRect rcDummy; // dimesion doesn't matter here
// Creating the icon control
m_stIconCtrl.Create(NULL, WS_CHILD|WS_VISIBLE|WS_DISABLED|SS_ICON, rcDummy, this);
m_stIconCtrl.SetIcon(m_hIcon);
}
}
// Converts d.u. in pixel (x axe)
int CGeneralMsgBox::FromDlgX(int x)
{ return x * m_dimDlgUnit.cx / CX_DLGUNIT_BASE; }
// Converts d.u. in pixel (y axe)
int CGeneralMsgBox::FromDlgY(int y)
{ return y * m_dimDlgUnit.cy / CY_DLGUNIT_BASE; }
// Updates the layout
void CGeneralMsgBox::UpdateLayout()
{
// Caching the borders
int cxLeft = FromDlgX(m_aMetrics[CX_LEFT_BORDER]);
int cxRight = FromDlgX(m_aMetrics[CX_RIGHT_BORDER]);
int cyTop = FromDlgY(m_aMetrics[CY_TOP_BORDER]);
int cyBottom = FromDlgY(m_aMetrics[CY_BOTTOM_BORDER]);
// Caching the space between buttons
int cxBtnsSpace = FromDlgX(m_aMetrics[CX_BTNS_SPACE]);
// The minimum dimensios of the client area
// (starting with the message)
CSize dimClient = m_dimMsg;
int xMsg = 0;
if (m_hIcon != NULL) {
// Adding the icon
xMsg = m_dimIcon.cx + FromDlgX(m_aMetrics[CX_ICON_MSG_SPACE]);
dimClient.cx += xMsg;
if (dimClient.cy < m_dimIcon.cy)
dimClient.cy = m_dimIcon.cy;
}
xMsg += cxLeft;
// Caching the minimum width needed for all buttons
int cBtns = m_aBtns.GetSize();
int cxBtns = (cBtns - 1) * FromDlgX(m_aMetrics[CX_BTNS_SPACE]) +
cBtns * m_dimBtn.cx;
if (dimClient.cx < cxBtns)
dimClient.cx = cxBtns;
dimClient.cx += cxLeft + cxRight;
dimClient.cy += cyTop + cyBottom + m_dimBtn.cy + FromDlgY(m_aMetrics[CY_BTNS_MSG_SPACE]);
// Set client dimensions
CRect rc(0, 0, dimClient.cx, dimClient.cy);
CalcWindowRect(rc);
MoveWindow(rc);
CenterWindow();
// Icon layout
if (m_hIcon != NULL)
m_stIconCtrl.MoveWindow(cxLeft, cyTop, m_dimIcon.cx, m_dimIcon.cy);
// Message layout
m_dimMsg.cx += 2;
xMsg = (xMsg + dimClient.cx - cxRight - m_dimMsg.cx) / 2;
m_edCtrl.MoveWindow(xMsg, cyTop, m_dimMsg.cx, m_dimMsg.cy);
// Buttons layout
int x = (dimClient.cx - cxBtns) / 2;
int y = dimClient.cy - cyBottom - m_dimBtn.cy;
for (int i = 0; i < cBtns; ++i) {
CWnd* pWndCtrl = GetDlgItem(m_aBtns[i].uIDC);
pWndCtrl->MoveWindow(x, y, m_dimBtn.cx, m_dimBtn.cy);
x += m_dimBtn.cx + cxBtnsSpace;
}
}