3 /////////////////////////////////////////////////////////////////////////////
5 // Copyright (c) 2002 Iain Murray, Inference Group, Cavendish, Cambridge.
7 /////////////////////////////////////////////////////////////////////////////
11 #include "SocketPage.h"
12 #include "../resource.h"
14 #include <utility> // for std::pair
16 using namespace Dasher
;
19 // Track memory leaks on Windows to the line that new'd the memory
22 #define DEBUG_NEW new( _NORMAL_BLOCK, THIS_FILE, __LINE__ )
25 static char THIS_FILE
[] = __FILE__
;
29 CSocketPage::CSocketPage(HWND Parent
, CDasherInterfaceBase
*DI
, CAppSettings
*pAppSettings
)
30 :CPrefsPageBase(Parent
, DI
, pAppSettings
) {
33 void CSocketPage::PopulateList() {
35 if(m_pAppSettings
->GetBoolParameter(BP_SOCKET_INPUT_ENABLE
)) {
36 SendMessage(GetDlgItem(m_hwnd
, IDC_SOCKET_ENABLE
), BM_SETCHECK
, BST_CHECKED
, 0);
39 SendMessage(GetDlgItem(m_hwnd
, IDC_SOCKET_ENABLE
), BM_SETCHECK
, BST_UNCHECKED
, 0);
42 const int buflen
=256; // code below assumes this is > 100
43 TCHAR m_tcBuffer
[buflen
];
44 _sntprintf(m_tcBuffer
, 100, _T("%ld"), m_pAppSettings
->GetLongParameter(LP_SOCKET_PORT
));
45 SendMessage(GetDlgItem(m_hwnd
, IDC_SOCKET_PORT
), WM_SETTEXT
, 0, (LPARAM
) m_tcBuffer
);
48 mbstowcs(m_tcBuffer
, m_pAppSettings
->GetStringParameter(SP_SOCKET_INPUT_X_LABEL
).c_str(), buflen
-1);
49 // Usually mbstowcs will terminate the target sting, except if we reached the max
50 // no. of chars, in which case we terminate ourselves:
51 m_tcBuffer
[buflen
-1] = 0; // terminate
52 SendMessage(GetDlgItem(m_hwnd
, IDC_SOCKET_X_LABEL
), WM_SETTEXT
, 0, (LPARAM
) m_tcBuffer
);
54 mbstowcs(m_tcBuffer
, m_pAppSettings
->GetStringParameter(SP_SOCKET_INPUT_Y_LABEL
).c_str(), buflen
-1);
55 m_tcBuffer
[buflen
-1] = 0; // terminate
56 SendMessage(GetDlgItem(m_hwnd
, IDC_SOCKET_Y_LABEL
), WM_SETTEXT
, 0, (LPARAM
) m_tcBuffer
);
58 _sntprintf(m_tcBuffer
, 100, _T("%.3f"), m_pAppSettings
->GetLongParameter(LP_SOCKET_INPUT_X_MIN
)/1000.0);
59 SendMessage(GetDlgItem(m_hwnd
, IDC_SOCKET_X_MIN
), WM_SETTEXT
, 0, (LPARAM
) m_tcBuffer
);
61 _sntprintf(m_tcBuffer
, 100, _T("%.3f"), m_pAppSettings
->GetLongParameter(LP_SOCKET_INPUT_X_MAX
)/1000.0);
62 SendMessage(GetDlgItem(m_hwnd
, IDC_SOCKET_X_MAX
), WM_SETTEXT
, 0, (LPARAM
) m_tcBuffer
);
64 _sntprintf(m_tcBuffer
, 100, _T("%.3f"), m_pAppSettings
->GetLongParameter(LP_SOCKET_INPUT_Y_MIN
)/1000.0);
65 SendMessage(GetDlgItem(m_hwnd
, IDC_SOCKET_Y_MIN
), WM_SETTEXT
, 0, (LPARAM
) m_tcBuffer
);
67 _sntprintf(m_tcBuffer
, 100, _T("%.3f"), m_pAppSettings
->GetLongParameter(LP_SOCKET_INPUT_Y_MAX
)/1000.0);
68 SendMessage(GetDlgItem(m_hwnd
, IDC_SOCKET_Y_MAX
), WM_SETTEXT
, 0, (LPARAM
) m_tcBuffer
);
70 SendMessage(GetDlgItem(m_hwnd
, IDC_SOCKET_DEBUG
), BM_SETCHECK
, m_pAppSettings
->GetBoolParameter(BP_SOCKET_DEBUG
) ? BST_CHECKED
: BST_UNCHECKED
, 0);
74 bool CSocketPage::Validate() {
75 OutputDebugString(TEXT("Validate() called\n"));
76 // Return false if something is wrong to prevent user from clicking to a different page. Please also pop up a dialogue informing the user at this point.
77 return validateTextBoxes(FALSE
, FALSE
); // don't apply; don't inhibit error messages
80 bool CSocketPage::Apply() {
81 OutputDebugString(TEXT("Apply() called\n"));
82 m_pAppSettings
->SetBoolParameter(BP_SOCKET_INPUT_ENABLE
, SendMessage(GetDlgItem(m_hwnd
, IDC_SOCKET_ENABLE
), BM_GETCHECK
, 0, 0));
83 m_pAppSettings
->SetBoolParameter(BP_SOCKET_DEBUG
, SendMessage(GetDlgItem(m_hwnd
, IDC_SOCKET_DEBUG
), BM_GETCHECK
, 0, 0));
84 return validateTextBoxes(TRUE
, FALSE
);
89 bool CSocketPage::validateTextBoxes(bool apply
, bool noerror
) {
91 // && short-circuits here, so the processing will abort after the first validation error
93 return checkPort(apply
, noerror
)
94 && checkLabel(apply
, noerror
, SP_SOCKET_INPUT_X_LABEL
, IDC_SOCKET_X_LABEL
)
95 && checkLabel(apply
, noerror
, SP_SOCKET_INPUT_Y_LABEL
, IDC_SOCKET_Y_LABEL
)
96 && checkMinOrMax(apply
, noerror
, LP_SOCKET_INPUT_X_MIN
, IDC_SOCKET_X_MIN
)
97 && checkMinOrMax(apply
, noerror
, LP_SOCKET_INPUT_X_MAX
, IDC_SOCKET_X_MAX
)
98 && checkMinOrMax(apply
, noerror
, LP_SOCKET_INPUT_Y_MIN
, IDC_SOCKET_Y_MIN
)
99 && checkMinOrMax(apply
, noerror
, LP_SOCKET_INPUT_Y_MAX
, IDC_SOCKET_Y_MAX
);
102 bool CSocketPage::checkPort(bool apply
, bool noerror
) {
104 TCHAR m_tcBuffer
[256];
105 SendMessage(GetDlgItem(m_hwnd
, IDC_SOCKET_PORT
), WM_GETTEXT
, 100, (LPARAM
) m_tcBuffer
);
106 if(_stscanf(m_tcBuffer
, TEXT("%ld"), &longval
) != 1 || longval
< 1 || longval
> 65535) {
108 Tstring ErrMsg
, ErrTitle
;
109 WinLocalisation::GetResourceString(IDS_SOCKET_ERROR_TITLE
, &ErrTitle
);
110 WinLocalisation::GetResourceString(IDS_ERR_SOCKET_PORT
, &ErrMsg
);
111 MessageBox(m_hwnd
, ErrMsg
.c_str(), ErrTitle
.c_str(), MB_OK
| MB_ICONEXCLAMATION
);
116 m_pAppSettings
->SetLongParameter(LP_SOCKET_PORT
, longval
);
121 bool CSocketPage::checkMinOrMax(bool apply
, bool noerror
, int paramID
, int idc
) {
124 TCHAR m_tcBuffer
[256];
125 SendMessage(GetDlgItem(m_hwnd
, idc
), WM_GETTEXT
, 100, (LPARAM
) m_tcBuffer
);
126 if(_stscanf(m_tcBuffer
, TEXT("%lf"), &doubleval
) != 1) {
128 Tstring ErrMsg
, ErrTitle
;
129 WinLocalisation::GetResourceString(IDS_SOCKET_ERROR_TITLE
, &ErrTitle
);
130 WinLocalisation::GetResourceString(IDS_ERR_SOCKET_MINMAX
, &ErrMsg
);
131 MessageBox(m_hwnd
, ErrMsg
.c_str(), ErrTitle
.c_str(), MB_OK
| MB_ICONEXCLAMATION
);
136 m_pAppSettings
->SetLongParameter(paramID
, (long) (doubleval
* 1000.0));
141 bool CSocketPage::checkLabel(bool apply
, bool noerror
, int paramID
, int idc
) {
142 TCHAR m_tcBuffer
[256];
143 SendMessage(GetDlgItem(m_hwnd
, idc
), WM_GETTEXT
, 100, (LPARAM
) m_tcBuffer
);
146 wcstombs(cbuf
, m_tcBuffer
, sizeof(cbuf
)-1);
147 cbuf
[sizeof(cbuf
)-1]=0; //failsafe termination
149 m_pAppSettings
->SetStringParameter(paramID
, s
);