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 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
|
// Windows Template Library - WTL version 8.0
// Copyright (C) Microsoft Corporation. All rights reserved.
//
// This file is a part of the Windows Template Library.
// The use and distribution terms for this software are covered by the
// Microsoft Permissive License (Ms-PL) which can be found in the file
// Ms-PL.txt at the root of this distribution.
#ifndef __ATLDDX_H__
#define __ATLDDX_H__
#pragma once
#ifndef __cplusplus
#error ATL requires C++ compilation (use a .cpp suffix)
#endif
#ifndef __ATLAPP_H__
#error atlddx.h requires atlapp.h to be included first
#endif
#if defined(_ATL_USE_DDX_FLOAT) && defined(_ATL_MIN_CRT)
#error Cannot use floating point DDX with _ATL_MIN_CRT defined
#endif // defined(_ATL_USE_DDX_FLOAT) && defined(_ATL_MIN_CRT)
#ifdef _ATL_USE_DDX_FLOAT
#include <float.h>
#endif // _ATL_USE_DDX_FLOAT
///////////////////////////////////////////////////////////////////////////////
// Classes in this file:
//
// CWinDataExchange<T>
namespace WTL
{
// Constants
#define DDX_LOAD FALSE
#define DDX_SAVE TRUE
// DDX map macros
#define BEGIN_DDX_MAP(thisClass) \
BOOL DoDataExchange(BOOL bSaveAndValidate = FALSE, UINT nCtlID = (UINT)-1) \
{ \
bSaveAndValidate; \
nCtlID;
#define DDX_TEXT(nID, var) \
if(nCtlID == (UINT)-1 || nCtlID == nID) \
{ \
if(!DDX_Text(nID, var, sizeof(var), bSaveAndValidate)) \
return FALSE; \
}
#define DDX_TEXT_LEN(nID, var, len) \
if(nCtlID == (UINT)-1 || nCtlID == nID) \
{ \
if(!DDX_Text(nID, var, sizeof(var), bSaveAndValidate, TRUE, len)) \
return FALSE; \
}
#define DDX_INT(nID, var) \
if(nCtlID == (UINT)-1 || nCtlID == nID) \
{ \
if(!DDX_Int(nID, var, TRUE, bSaveAndValidate)) \
return FALSE; \
}
#define DDX_INT_RANGE(nID, var, min, max) \
if(nCtlID == (UINT)-1 || nCtlID == nID) \
{ \
if(!DDX_Int(nID, var, TRUE, bSaveAndValidate, TRUE, min, max)) \
return FALSE; \
}
#define DDX_UINT(nID, var) \
if(nCtlID == (UINT)-1 || nCtlID == nID) \
{ \
if(!DDX_Int(nID, var, FALSE, bSaveAndValidate)) \
return FALSE; \
}
#define DDX_UINT_RANGE(nID, var, min, max) \
if(nCtlID == (UINT)-1 || nCtlID == nID) \
{ \
if(!DDX_Int(nID, var, FALSE, bSaveAndValidate, TRUE, min, max)) \
return FALSE; \
}
#ifdef _ATL_USE_DDX_FLOAT
#define DDX_FLOAT(nID, var) \
if(nCtlID == (UINT)-1 || nCtlID == nID) \
{ \
if(!DDX_Float(nID, var, bSaveAndValidate)) \
return FALSE; \
}
#define DDX_FLOAT_RANGE(nID, var, min, max) \
if(nCtlID == (UINT)-1 || nCtlID == nID) \
{ \
if(!DDX_Float(nID, var, bSaveAndValidate, TRUE, min, max)) \
return FALSE; \
}
#define DDX_FLOAT_P(nID, var, precision) \
if(nCtlID == (UINT)-1 || nCtlID == nID) \
{ \
if(!DDX_Float(nID, var, bSaveAndValidate, FALSE, 0, 0, precision)) \
return FALSE; \
}
#define DDX_FLOAT_P_RANGE(nID, var, min, max, precision) \
if(nCtlID == (UINT)-1 || nCtlID == nID) \
{ \
if(!DDX_Float(nID, var, bSaveAndValidate, TRUE, min, max, precision)) \
return FALSE; \
}
#endif // _ATL_USE_DDX_FLOAT
#define DDX_CONTROL(nID, obj) \
if(nCtlID == (UINT)-1 || nCtlID == nID) \
DDX_Control(nID, obj, bSaveAndValidate);
#define DDX_CONTROL_HANDLE(nID, obj) \
if(nCtlID == (UINT)-1 || nCtlID == nID) \
DDX_Control_Handle(nID, obj, bSaveAndValidate);
#define DDX_CHECK(nID, var) \
if(nCtlID == (UINT)-1 || nCtlID == nID) \
DDX_Check(nID, var, bSaveAndValidate);
#define DDX_RADIO(nID, var) \
if(nCtlID == (UINT)-1 || nCtlID == nID) \
DDX_Radio(nID, var, bSaveAndValidate);
#define END_DDX_MAP() \
return TRUE; \
}
///////////////////////////////////////////////////////////////////////////////
// CWinDataExchange - provides support for DDX
template <class T>
class CWinDataExchange
{
public:
// Data exchange method - override in your derived class
BOOL DoDataExchange(BOOL /*bSaveAndValidate*/ = FALSE, UINT /*nCtlID*/ = (UINT)-1)
{
// this one should never be called, override it in
// your derived class by implementing DDX map
ATLASSERT(FALSE);
return FALSE;
}
// Helpers for validation error reporting
enum _XDataType
{
ddxDataNull = 0,
ddxDataText = 1,
ddxDataInt = 2,
ddxDataFloat = 3,
ddxDataDouble = 4
};
struct _XTextData
{
int nLength;
int nMaxLength;
};
struct _XIntData
{
long nVal;
long nMin;
long nMax;
};
struct _XFloatData
{
double nVal;
double nMin;
double nMax;
};
struct _XData
{
_XDataType nDataType;
union
{
_XTextData textData;
_XIntData intData;
_XFloatData floatData;
};
};
// Text exchange
BOOL DDX_Text(UINT nID, LPTSTR lpstrText, int cbSize, BOOL bSave, BOOL bValidate = FALSE, int nLength = 0)
{
T* pT = static_cast<T*>(this);
BOOL bSuccess = TRUE;
if(bSave)
{
HWND hWndCtrl = pT->GetDlgItem(nID);
int nRetLen = ::GetWindowText(hWndCtrl, lpstrText, cbSize / sizeof(TCHAR));
if(nRetLen < ::GetWindowTextLength(hWndCtrl))
bSuccess = FALSE;
}
else
{
ATLASSERT(!bValidate || lstrlen(lpstrText) <= nLength);
bSuccess = pT->SetDlgItemText(nID, lpstrText);
}
if(!bSuccess)
{
pT->OnDataExchangeError(nID, bSave);
}
else if(bSave && bValidate) // validation
{
ATLASSERT(nLength > 0);
if(lstrlen(lpstrText) > nLength)
{
_XData data = { ddxDataText };
data.textData.nLength = lstrlen(lpstrText);
data.textData.nMaxLength = nLength;
pT->OnDataValidateError(nID, bSave, data);
bSuccess = FALSE;
}
}
return bSuccess;
}
BOOL DDX_Text(UINT nID, BSTR& bstrText, int /*cbSize*/, BOOL bSave, BOOL bValidate = FALSE, int nLength = 0)
{
T* pT = static_cast<T*>(this);
BOOL bSuccess = TRUE;
if(bSave)
{
bSuccess = pT->GetDlgItemText(nID, bstrText);
}
else
{
USES_CONVERSION;
LPTSTR lpstrText = OLE2T(bstrText);
ATLASSERT(!bValidate || lstrlen(lpstrText) <= nLength);
bSuccess = pT->SetDlgItemText(nID, lpstrText);
}
if(!bSuccess)
{
pT->OnDataExchangeError(nID, bSave);
}
else if(bSave && bValidate) // validation
{
ATLASSERT(nLength > 0);
if((int)::SysStringLen(bstrText) > nLength)
{
_XData data = { ddxDataText };
data.textData.nLength = (int)::SysStringLen(bstrText);
data.textData.nMaxLength = nLength;
pT->OnDataValidateError(nID, bSave, data);
bSuccess = FALSE;
}
}
return bSuccess;
}
BOOL DDX_Text(UINT nID, ATL::CComBSTR& bstrText, int /*cbSize*/, BOOL bSave, BOOL bValidate = FALSE, int nLength = 0)
{
T* pT = static_cast<T*>(this);
BOOL bSuccess = TRUE;
if(bSave)
{
bSuccess = pT->GetDlgItemText(nID, (BSTR&)bstrText);
}
else
{
USES_CONVERSION;
LPTSTR lpstrText = OLE2T(bstrText);
ATLASSERT(!bValidate || lstrlen(lpstrText) <= nLength);
bSuccess = pT->SetDlgItemText(nID, lpstrText);
}
if(!bSuccess)
{
pT->OnDataExchangeError(nID, bSave);
}
else if(bSave && bValidate) // validation
{
ATLASSERT(nLength > 0);
if((int)bstrText.Length() > nLength)
{
_XData data = { ddxDataText };
data.textData.nLength = (int)bstrText.Length();
data.textData.nMaxLength = nLength;
pT->OnDataValidateError(nID, bSave, data);
bSuccess = FALSE;
}
}
return bSuccess;
}
#if defined(_WTL_USE_CSTRING) || defined(__ATLSTR_H__)
BOOL DDX_Text(UINT nID, _CSTRING_NS::CString& strText, int /*cbSize*/, BOOL bSave, BOOL bValidate = FALSE, int nLength = 0)
{
T* pT = static_cast<T*>(this);
BOOL bSuccess = TRUE;
if(bSave)
{
HWND hWndCtrl = pT->GetDlgItem(nID);
int nLen = ::GetWindowTextLength(hWndCtrl);
int nRetLen = -1;
LPTSTR lpstr = strText.GetBufferSetLength(nLen);
if(lpstr != NULL)
{
nRetLen = ::GetWindowText(hWndCtrl, lpstr, nLen + 1);
strText.ReleaseBuffer();
}
if(nRetLen < nLen)
bSuccess = FALSE;
}
else
{
bSuccess = pT->SetDlgItemText(nID, strText);
}
if(!bSuccess)
{
pT->OnDataExchangeError(nID, bSave);
}
else if(bSave && bValidate) // validation
{
ATLASSERT(nLength > 0);
if(strText.GetLength() > nLength)
{
_XData data = { ddxDataText };
data.textData.nLength = strText.GetLength();
data.textData.nMaxLength = nLength;
pT->OnDataValidateError(nID, bSave, data);
bSuccess = FALSE;
}
}
return bSuccess;
}
#endif // defined(_WTL_USE_CSTRING) || defined(__ATLSTR_H__)
// Numeric exchange
template <class Type>
BOOL DDX_Int(UINT nID, Type& nVal, BOOL bSigned, BOOL bSave, BOOL bValidate = FALSE, Type nMin = 0, Type nMax = 0)
{
T* pT = static_cast<T*>(this);
BOOL bSuccess = TRUE;
if(bSave)
{
nVal = (Type)pT->GetDlgItemInt(nID, &bSuccess, bSigned);
}
else
{
ATLASSERT(!bValidate || nVal >= nMin && nVal <= nMax);
bSuccess = pT->SetDlgItemInt(nID, nVal, bSigned);
}
if(!bSuccess)
{
pT->OnDataExchangeError(nID, bSave);
}
else if(bSave && bValidate) // validation
{
ATLASSERT(nMin != nMax);
if(nVal < nMin || nVal > nMax)
{
_XData data = { ddxDataInt };
data.intData.nVal = (long)nVal;
data.intData.nMin = (long)nMin;
data.intData.nMax = (long)nMax;
pT->OnDataValidateError(nID, bSave, data);
bSuccess = FALSE;
}
}
return bSuccess;
}
// Float exchange
#ifdef _ATL_USE_DDX_FLOAT
static BOOL _AtlSimpleFloatParse(LPCTSTR lpszText, double& d)
{
ATLASSERT(lpszText != NULL);
while (*lpszText == _T(' ') || *lpszText == _T('\t'))
lpszText++;
TCHAR chFirst = lpszText[0];
d = _tcstod(lpszText, (LPTSTR*)&lpszText);
if (d == 0.0 && chFirst != _T('0'))
return FALSE; // could not convert
while (*lpszText == _T(' ') || *lpszText == _T('\t'))
lpszText++;
if (*lpszText != _T('\0'))
return FALSE; // not terminated properly
return TRUE;
}
BOOL DDX_Float(UINT nID, float& nVal, BOOL bSave, BOOL bValidate = FALSE, float nMin = 0.F, float nMax = 0.F, int nPrecision = FLT_DIG)
{
T* pT = static_cast<T*>(this);
BOOL bSuccess = TRUE;
const int cchBuff = 32;
TCHAR szBuff[cchBuff] = { 0 };
if(bSave)
{
pT->GetDlgItemText(nID, szBuff, cchBuff);
double d = 0;
if(_AtlSimpleFloatParse(szBuff, d))
nVal = (float)d;
else
bSuccess = FALSE;
}
else
{
ATLASSERT(!bValidate || nVal >= nMin && nVal <= nMax);
SecureHelper::sprintf_x(szBuff, cchBuff, _T("%.*g"), nPrecision, nVal);
bSuccess = pT->SetDlgItemText(nID, szBuff);
}
if(!bSuccess)
{
pT->OnDataExchangeError(nID, bSave);
}
else if(bSave && bValidate) // validation
{
ATLASSERT(nMin != nMax);
if(nVal < nMin || nVal > nMax)
{
_XData data = { ddxDataFloat };
data.floatData.nVal = (double)nVal;
data.floatData.nMin = (double)nMin;
data.floatData.nMax = (double)nMax;
pT->OnDataValidateError(nID, bSave, data);
bSuccess = FALSE;
}
}
return bSuccess;
}
BOOL DDX_Float(UINT nID, double& nVal, BOOL bSave, BOOL bValidate = FALSE, double nMin = 0., double nMax = 0., int nPrecision = DBL_DIG)
{
T* pT = static_cast<T*>(this);
BOOL bSuccess = TRUE;
const int cchBuff = 32;
TCHAR szBuff[cchBuff] = { 0 };
if(bSave)
{
pT->GetDlgItemText(nID, szBuff, cchBuff);
double d = 0;
if(_AtlSimpleFloatParse(szBuff, d))
nVal = d;
else
bSuccess = FALSE;
}
else
{
ATLASSERT(!bValidate || nVal >= nMin && nVal <= nMax);
SecureHelper::sprintf_x(szBuff, cchBuff, _T("%.*g"), nPrecision, nVal);
bSuccess = pT->SetDlgItemText(nID, szBuff);
}
if(!bSuccess)
{
pT->OnDataExchangeError(nID, bSave);
}
else if(bSave && bValidate) // validation
{
ATLASSERT(nMin != nMax);
if(nVal < nMin || nVal > nMax)
{
_XData data = { ddxDataFloat };
data.floatData.nVal = nVal;
data.floatData.nMin = nMin;
data.floatData.nMax = nMax;
pT->OnDataValidateError(nID, bSave, data);
bSuccess = FALSE;
}
}
return bSuccess;
}
#endif // _ATL_USE_DDX_FLOAT
// Full control subclassing (for CWindowImpl derived controls)
template <class TControl>
void DDX_Control(UINT nID, TControl& ctrl, BOOL bSave)
{
if(!bSave && ctrl.m_hWnd == NULL)
{
T* pT = static_cast<T*>(this);
ctrl.SubclassWindow(pT->GetDlgItem(nID));
}
}
// Simple control attaching (for HWND wrapper controls)
template <class TControl>
void DDX_Control_Handle(UINT nID, TControl& ctrl, BOOL bSave)
{
if(!bSave && ctrl.m_hWnd == NULL)
{
T* pT = static_cast<T*>(this);
ctrl = pT->GetDlgItem(nID);
}
}
// Control state
void DDX_Check(UINT nID, int& nValue, BOOL bSave)
{
T* pT = static_cast<T*>(this);
HWND hWndCtrl = pT->GetDlgItem(nID);
if(bSave)
{
nValue = (int)::SendMessage(hWndCtrl, BM_GETCHECK, 0, 0L);
ATLASSERT(nValue >= 0 && nValue <= 2);
}
else
{
if(nValue < 0 || nValue > 2)
{
ATLTRACE2(atlTraceUI, 0, _T("ATL: Warning - dialog data checkbox value (%d) out of range.\n"), nValue);
nValue = 0; // default to off
}
::SendMessage(hWndCtrl, BM_SETCHECK, nValue, 0L);
}
}
// variant that supports bool (checked/not-checked, no intermediate state)
void DDX_Check(UINT nID, bool& bCheck, BOOL bSave)
{
int nValue = bCheck ? 1 : 0;
DDX_Check(nID, nValue, bSave);
if(bSave)
{
if(nValue == 2)
ATLTRACE2(atlTraceUI, 0, _T("ATL: Warning - checkbox state (%d) out of supported range.\n"), nValue);
bCheck = (nValue == 1);
}
}
void DDX_Radio(UINT nID, int& nValue, BOOL bSave)
{
T* pT = static_cast<T*>(this);
HWND hWndCtrl = pT->GetDlgItem(nID);
ATLASSERT(hWndCtrl != NULL);
// must be first in a group of auto radio buttons
ATLASSERT(::GetWindowLong(hWndCtrl, GWL_STYLE) & WS_GROUP);
ATLASSERT(::SendMessage(hWndCtrl, WM_GETDLGCODE, 0, 0L) & DLGC_RADIOBUTTON);
if(bSave)
nValue = -1; // value if none found
// walk all children in group
int nButton = 0;
do
{
if(::SendMessage(hWndCtrl, WM_GETDLGCODE, 0, 0L) & DLGC_RADIOBUTTON)
{
// control in group is a radio button
if(bSave)
{
if(::SendMessage(hWndCtrl, BM_GETCHECK, 0, 0L) != 0)
{
ATLASSERT(nValue == -1); // only set once
nValue = nButton;
}
}
else
{
// select button
::SendMessage(hWndCtrl, BM_SETCHECK, (nButton == nValue), 0L);
}
nButton++;
}
else
{
ATLTRACE2(atlTraceUI, 0, _T("ATL: Warning - skipping non-radio button in group.\n"));
}
hWndCtrl = ::GetWindow(hWndCtrl, GW_HWNDNEXT);
}
while (hWndCtrl != NULL && !(GetWindowLong(hWndCtrl, GWL_STYLE) & WS_GROUP));
}
// Overrideables
void OnDataExchangeError(UINT nCtrlID, BOOL /*bSave*/)
{
// Override to display an error message
::MessageBeep((UINT)-1);
T* pT = static_cast<T*>(this);
::SetFocus(pT->GetDlgItem(nCtrlID));
}
void OnDataValidateError(UINT nCtrlID, BOOL /*bSave*/, _XData& /*data*/)
{
// Override to display an error message
::MessageBeep((UINT)-1);
T* pT = static_cast<T*>(this);
::SetFocus(pT->GetDlgItem(nCtrlID));
}
};
}; // namespace WTL
#endif // __ATLDDX_H__
|