-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDDX_DDV.cpp
526 lines (465 loc) · 13.8 KB
/
DDX_DDV.cpp
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
// DDX_DDV.cpp : implementation file
//
// $Id$
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "phrqtype.h" // LDBLE
#include "phreeqci2.h"
#include "DDX_DDV.h"
#include "mshflexgrid.h"
#include "EditGrid.h"
UINT BasicCheckProc(LPVOID pParam);
struct BasicCheckProcStruct
{
class rate* rate_ptr;
HANDLE hInfiniteLoop;
};
enum
{
BCP_OK,
BCP_ERRORS,
};
// prepares editgrid control for data transfer
// this is required since the editgrid doesn't recieve a WM_KILLFOCUS notification
// before DoDataExchange is called
CWnd* PASCAL PrepareEditGridCtrl(CDataExchange* pDX, int nIDC)
{
CWnd* pWndCtrl = pDX->m_pDlgWnd->GetDlgItem(nIDC);
ASSERT(pWndCtrl);
if (pWndCtrl)
{
pDX->m_idLastControl = nIDC;
pDX->m_bEditLastControl = FALSE;
VERIFY(pWndCtrl->SendMessage(EGM_ENDEDITCELLNOW, (WPARAM)(BOOL)FALSE/*fCancel*/, 0));
}
else
{
TRACE(traceAppMsg, 0, "Error: no data exchange control with ID 0x%04X.\n", nIDC);
ASSERT(FALSE);
::AfxThrowNotSupportedException();
}
return pWndCtrl;
}
// Taken from DLGDATA.CPP
// only supports '%d', '%u', '%sd', '%su', '%ld' and '%lu'
AFX_STATIC BOOL AFXAPI _AfxSimpleScanf(LPCTSTR lpszText,
LPCTSTR lpszFormat, va_list pData)
{
ASSERT(lpszText != NULL);
ASSERT(lpszFormat != NULL);
ASSERT(*lpszFormat == '%');
lpszFormat++; // skip '%'
BOOL bLong = FALSE;
BOOL bShort = FALSE;
if (*lpszFormat == 'l')
{
bLong = TRUE;
lpszFormat++;
}
else if (*lpszFormat == 's')
{
bShort = TRUE;
lpszFormat++;
}
ASSERT(*lpszFormat == 'd' || *lpszFormat == 'u');
ASSERT(lpszFormat[1] == '\0');
while (*lpszText == ' ' || *lpszText == '\t')
lpszText++;
TCHAR chFirst = lpszText[0];
long l, l2;
if (*lpszFormat == 'd')
{
// signed
l = _tcstol(lpszText, (LPTSTR*)&lpszText, 10);
l2 = (int)l;
}
else
{
// unsigned
if (*lpszText == '-')
return FALSE;
l = (long)_tcstoul(lpszText, (LPTSTR*)&lpszText, 10);
l2 = (unsigned int)l;
}
if (l == 0 && chFirst != '0')
return FALSE; // could not convert
while (*lpszText == ' ' || *lpszText == '\t')
lpszText++;
if (*lpszText != '\0')
return FALSE; // not terminated properly
if (bShort)
{
if ((short)l != l)
return FALSE; // too big for short
*va_arg(pData, short*) = (short)l;
}
else
{
ASSERT(sizeof(long) == sizeof(int));
ASSERT(l == l2);
*va_arg(pData, long*) = l;
}
// all ok
return TRUE;
}
//
// Modified from DLGDATA.CPP
//
AFX_STATIC void AFX_CDECL _Afx_DDX_TextWithFormat(CDataExchange* pDX, int nIDC,
long nRow, long nCol,
LPCTSTR lpszFormat, UINT nIDPrompt, ...)
// only supports windows output formats - no floating point
{
va_list pData;
va_start(pData, nIDPrompt);
CWnd* pWnd = PrepareEditGridCtrl(pDX, nIDC);
// validate grid cell
ASSERT_KINDOF(CMSHFlexGrid, pWnd); // only valid for grid control
ASSERT(nRow < ((CMSHFlexGrid*)pWnd)->GetRows());
ASSERT(nRow >= ((CMSHFlexGrid*)pWnd)->GetFixedRows());
ASSERT(nCol < ((CMSHFlexGrid*)pWnd)->GetCols(0));
ASSERT(nCol >= ((CMSHFlexGrid*)pWnd)->GetFixedCols());
static BYTE parms[] = VTS_I4;
// void CMSHFlexGrid::SetRow(long nNewValue)
pWnd->InvokeHelper(0xa, DISPATCH_PROPERTYPUT, VT_EMPTY, NULL, parms, nRow);
// void CMSHFlexGrid::SetCol(long nNewValue)
pWnd->InvokeHelper(0xb, DISPATCH_PROPERTYPUT, VT_EMPTY, NULL, parms, nCol);
TCHAR szT[32];
if (pDX->m_bSaveAndValidate)
{
// the following works for %d, %u, %ld, %lu
CString result;
static BYTE parms[] = VTS_I4 VTS_I4;
// CString CMSHFlexGrid::GetTextMatrix(long Row, long Col)
pWnd->InvokeHelper(0x41, DISPATCH_PROPERTYGET, VT_BSTR, (void*)&result, parms,
nRow, nCol);
if (!_AfxSimpleScanf(result, lpszFormat, pData))
{
DDX_GridFail(pDX, nIDPrompt);
}
}
else
{
wvsprintf(szT, lpszFormat, pData);
// does not support floating point numbers - see dlgfloat.cpp
static BYTE parms[] = VTS_I4 VTS_I4 VTS_BSTR;
// void CMSHFlexGrid::SetTextMatrix(long Row, long Col, LPCTSTR lpszNewValue)
pWnd->InvokeHelper(0x41, DISPATCH_PROPERTYPUT, VT_EMPTY, NULL, parms,
nRow, nCol, szT);
}
va_end(pData);
}
//
// Modified from:
// void AFXAPI DDX_Text(CDataExchange* pDX, int nIDC, int& value)
// (DLGDATA.CPP)
//
void AFXAPI DDX_GridText(CDataExchange* pDX, int nIDC,
long nRow, long nCol,
int& value)
{
if (pDX->m_bSaveAndValidate)
_Afx_DDX_TextWithFormat(pDX, nIDC, nRow, nCol, _T("%d"), AFX_IDP_PARSE_INT, &value);
else
_Afx_DDX_TextWithFormat(pDX, nIDC, nRow, nCol, _T("%d"), AFX_IDP_PARSE_INT, value);
}
void PASCAL DDX_GridText(CDataExchange* pDX, int nIDC,
long nRow, long nCol,
long& value)
{
if (pDX->m_bSaveAndValidate)
_Afx_DDX_TextWithFormat(pDX, nIDC, nRow, nCol, _T("%ld"), AFX_IDP_PARSE_INT, &value);
else
_Afx_DDX_TextWithFormat(pDX, nIDC, nRow, nCol, _T("%ld"), AFX_IDP_PARSE_INT, value);
}
//
// Modified from:
// void AFXAPI DDX_Text(CDataExchange* pDX, int nIDC, CString& value)
// (DLGDATA.CPP)
//
void AFXAPI DDX_GridText(CDataExchange* pDX, int nIDC,
long nRow, long nCol,
CString& value)
{
CWnd* pWnd = PrepareEditGridCtrl(pDX, nIDC);
// validate grid cell
ASSERT_KINDOF(CMSHFlexGrid, pWnd); // only valid for grid control
ASSERT(nRow < ((CMSHFlexGrid*)pWnd)->GetRows());
ASSERT(nRow >= ((CMSHFlexGrid*)pWnd)->GetFixedRows());
ASSERT(nCol < ((CMSHFlexGrid*)pWnd)->GetCols(0));
ASSERT(nCol >= ((CMSHFlexGrid*)pWnd)->GetFixedCols());
if (pDX->m_bSaveAndValidate)
{
static BYTE parms[] = VTS_I4 VTS_I4;
// CString CMSHFlexGrid::GetTextMatrix(long Row, long Col)
pWnd->InvokeHelper(0x41, DISPATCH_PROPERTYGET, VT_BSTR, (void*)&value, parms,
nRow, nCol);
static BYTE parm[] = VTS_I4;
// void CMSHFlexGrid::SetRow(long nNewValue)
pWnd->InvokeHelper(0xa, DISPATCH_PROPERTYPUT, VT_EMPTY, NULL, parm, nRow);
// void CMSHFlexGrid::SetCol(long nNewValue)
pWnd->InvokeHelper(0xb, DISPATCH_PROPERTYPUT, VT_EMPTY, NULL, parm, nCol);
}
else
{
static BYTE parms[] = VTS_I4 VTS_I4 VTS_BSTR;
// void CMSHFlexGrid::SetTextMatrix(long Row, long Col, LPCTSTR lpszNewValue)
pWnd->InvokeHelper(0x41, DISPATCH_PROPERTYPUT, VT_EMPTY, NULL, parms,
nRow, nCol, (LPCTSTR)value);
}
}
void PASCAL DDV_MinEqualInt(CDataExchange* pDX, int value, int minVal, UINT nIDPrompt /*= IDS_INT_MIN_EQUAL_137*/)
{
if (value < minVal)
{
if (!pDX->m_bSaveAndValidate)
{
TRACE0(_T("Warning: initial dialog data is out of range.\n"));
return; // don't stop now
}
TCHAR szMin[32];
wsprintf(szMin, _T("%ld"), minVal);
CString prompt;
AfxFormatString1(prompt, nIDPrompt, szMin);
DDX_GridFail(pDX, prompt);
}
}
/////////////////////////////////////////////////////////////////////////////
// Extra data validation procs for float/double support
/////////////////////////////////////////////////////////////////////////////
AFX_STATIC BOOL AFXAPI _AfxSimpleFloatParse(LPCTSTR lpszText, double& d)
{
ASSERT(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;
}
void AFXAPI AfxTextFloatFormat(CDataExchange* pDX, int nIDC,
long nRow, long nCol,
void* pData, double value, int nSizeGcvt,
UINT nIDPrompt)
{
ASSERT(pData != NULL);
CWnd* pWnd = PrepareEditGridCtrl(pDX, nIDC);
TCHAR szBuffer[32];
static BYTE parms[] = VTS_I4;
// validate grid cell
ASSERT_KINDOF(CMSHFlexGrid, pWnd); // only valid for grid control
ASSERT(nRow < ((CMSHFlexGrid*)pWnd)->GetRows());
ASSERT(nRow >= ((CMSHFlexGrid*)pWnd)->GetFixedRows());
ASSERT(nCol < ((CMSHFlexGrid*)pWnd)->GetCols(0));
ASSERT(nCol >= ((CMSHFlexGrid*)pWnd)->GetFixedCols());
// void CMSHFlexGrid::SetRow(long nNewValue)
pWnd->InvokeHelper(0xa, DISPATCH_PROPERTYPUT, VT_EMPTY, NULL, parms, nRow);
// void CMSHFlexGrid::SetCol(long nNewValue)
pWnd->InvokeHelper(0xb, DISPATCH_PROPERTYPUT, VT_EMPTY, NULL, parms, nCol);
if (pDX->m_bSaveAndValidate)
{
// ::GetWindowText(hWndCtrl, szBuffer, _countof(szBuffer));
CString result;
static BYTE parms[] = VTS_I4 VTS_I4;
// CString CMSHFlexGrid::GetTextMatrix(long Row, long Col)
pWnd->InvokeHelper(0x41, DISPATCH_PROPERTYGET, VT_BSTR, (void*)&result, parms,
nRow, nCol);
double d;
if (!_AfxSimpleFloatParse(result, d))
{
DDX_GridFail(pDX, nIDPrompt); // throws exception
}
if (nSizeGcvt == FLT_DIG)
*((float*)pData) = (float)d;
else
*((double*)pData) = d;
}
else
{
_stprintf(szBuffer, _T("%.*g"), nSizeGcvt, value);
static BYTE parms[] = VTS_I4 VTS_I4 VTS_BSTR;
// void CMSHFlexGrid::SetTextMatrix(long Row, long Col, LPCTSTR lpszNewValue)
pWnd->InvokeHelper(0x41, DISPATCH_PROPERTYPUT, VT_EMPTY, NULL, parms,
nRow, nCol, szBuffer);
}
}
void PASCAL DDX_GridText(CDataExchange* pDX, int nIDC,
long nRow, long nCol,
float& value, UINT nIDPrompt /* = AFX_IDP_PARSE_REAL */)
{
AfxTextFloatFormat(pDX, nIDC, nRow, nCol, &value, value, FLT_DIG, nIDPrompt);
}
void PASCAL DDX_GridText(CDataExchange* pDX, int nIDC,
long nRow, long nCol,
double& value, UINT nIDPrompt /* = AFX_IDP_PARSE_REAL */)
{
AfxTextFloatFormat(pDX, nIDC, nRow, nCol, &value, value, DBL_DIG, nIDPrompt);
}
void PASCAL DDX_GridText(CDataExchange* pDX, int nIDC,
long nRow, long nCol,
long double& value, UINT nIDPrompt /* = AFX_IDP_PARSE_REAL */)
{
ASSERT(DBL_DIG == LDBL_DIG);
ASSERT(sizeof(double) == sizeof(long double));
double dValue = (double) value;
AfxTextFloatFormat(pDX, nIDC, nRow, nCol, &dValue, dValue, LDBL_DIG, nIDPrompt);
value = (long double) dValue;
}
//
// Dialog Data Exchange Routine for MSHFlexGrid
//
// if (m_bSaveAndValidate == TRUE)
// If cell defined by nRow and nCol is empty the value is set to the
// default value of dDefault
//
// if (m_bSaveAndValidate == FALSE)
// The cell defined by nRow and nCol is set to the value
//
void PASCAL DDX_GridTextDefault(CDataExchange* pDX, int nIDC,
long nRow, long nCol,
double& value, const double dDefault)
{
if (pDX->m_bSaveAndValidate)
{
CString str;
DDX_GridText(pDX, nIDC, nRow, nCol, str);
if (str.IsEmpty())
{
value = dDefault;
}
else
{
DDX_GridText(pDX, nIDC, nRow, nCol, value);
}
}
else
{
DDX_GridText(pDX, nIDC, nRow, nCol, value);
}
}
//
// Dialog Data Exchange Routine for MSHFlexGrid
//
// if (m_bSaveAndValidate == TRUE)
// if cell defined by nRow and nCol is empty value is to
// std::numeric_limits<double>::signaling_NaN()
//
// if (m_bSaveAndValidate == FALSE)
// if value != value
// the cell defined by nRow and nCol is set to empty
//
void PASCAL DDX_GridTextNaN(CDataExchange* pDX, int nIDC,
long nRow, long nCol,
double& value)
{
ASSERT(std::numeric_limits<double>::has_signaling_NaN == true);
CString str(_T(""));
if (pDX->m_bSaveAndValidate)
{
DDX_GridText(pDX, nIDC, nRow, nCol, str);
if (str.IsEmpty())
{
value = std::numeric_limits<double>::signaling_NaN();
}
else
{
DDX_GridText(pDX, nIDC, nRow, nCol, value);
}
}
else
{
if (value != value)
{
DDX_GridText(pDX, nIDC, nRow, nCol, str);
}
else
{
DDX_GridText(pDX, nIDC, nRow, nCol, value);
}
}
}
//
// Display MessageBox containing string table nIDText and string table nIDCaption
// and highlight the bad cell
//
void PASCAL DDX_GridFail(CDataExchange* pDX, UINT nIDText, UINT nIDCaption /* = IDR_MAINFRAME*/)
{
if (!pDX->m_bSaveAndValidate)
{
TRACE0("Warning: CDataExchange::Fail called when not validating.\n");
// throw the exception anyway
}
else if (pDX->m_idLastControl != 0)
{
HWND hWndLastControl;
pDX->m_pDlgWnd->GetDlgItem(pDX->m_idLastControl, &hWndLastControl);
CMSHFlexGrid* pGrid = (CMSHFlexGrid*)CWnd::FromHandle(hWndLastControl);
ASSERT_KINDOF(CMSHFlexGrid, pGrid); // last control not CMSHFlexGrid
// This causes the current cell to be visible
pGrid->GetCellTop();
// let user see which cell contains error while msgbox is modal
ASSERT(pGrid->GetHighLight() == flexHighlightWithFocus);
pGrid->SetHighLight(flexHighlightAlways);
pGrid->SetRedraw(TRUE);
// display error msg
CString strText;
CString strCaption;
VERIFY(strText.LoadString(nIDText));
VERIFY(strCaption.LoadString(nIDCaption));
MessageBox(pGrid->m_hWnd, strText, strCaption, MB_ICONEXCLAMATION);
// reset grid
pGrid->SetHighLight(flexHighlightWithFocus);
pDX->Fail(); // throws exception
}
else
{
TRACE0("Error: fail validation with no control to restore focus to.\n");
// do nothing more
}
}
//
// Display MessageBox containing lpText and lpCaption
// and highlight the bad cell
//
void PASCAL DDX_GridFail(CDataExchange* pDX, LPCTSTR lpText, LPCTSTR lpCaption /* = NULL*/)
{
if (!pDX->m_bSaveAndValidate)
{
TRACE0("Warning: CDataExchange::Fail called when not validating.\n");
// throw the exception anyway
}
else if (pDX->m_idLastControl != 0)
{
HWND hWndLastControl;
pDX->m_pDlgWnd->GetDlgItem(pDX->m_idLastControl, &hWndLastControl);
CMSHFlexGrid* pGrid = (CMSHFlexGrid*)CWnd::FromHandle(hWndLastControl);
ASSERT_KINDOF(CMSHFlexGrid, pGrid); // last control not CMSHFlexGrid
// This causes the current cell to be visible
pGrid->GetCellTop();
// let user see which cell contains error while msgbox is modal
ASSERT(pGrid->GetHighLight() == flexHighlightWithFocus);
pGrid->SetHighLight(flexHighlightAlways);
pGrid->SetRedraw(TRUE);
// display error msg
CString strCaption;
if (lpCaption == NULL)
{
VERIFY(strCaption.LoadString(IDR_MAINFRAME));
lpCaption = strCaption;
}
MessageBox(pGrid->m_hWnd, lpText, lpCaption, MB_ICONEXCLAMATION);
/// reset grid
pGrid->SetHighLight(flexHighlightWithFocus);
pDX->Fail(); // throws exception
}
else
{
TRACE0("Error: fail validation with no control to restore focus to.\n");
// do nothing more
}
}