tagging release
[dasher.git] / trunk / Src / Win32 / Widgets / ControlPage.cpp
blob4f95b3fd60a79211fc37b732b94d34407ab32e81
1 // ControlPage.cpp
2 //
3 /////////////////////////////////////////////////////////////////////////////
4 //
5 // Copyright (c) 2002 Iain Murray, Inference Group, Cavendish, Cambridge.
6 //
7 /////////////////////////////////////////////////////////////////////////////
9 #include "WinCommon.h"
11 #include "ControlPage.h"
12 #include "../resource.h"
13 #include "../Common/StringUtils.h"
14 #include "../AppSettings.h"
15 #include "../ButtonPrefs.h"
18 #include <utility> // for std::pair
20 using namespace Dasher;
21 using namespace std;
24 CControlPage::CControlPage(HWND Parent, CDasherInterfaceBase *DI, CAppSettings *pAppSettings)
25 :CPrefsPageBase(Parent, DI, pAppSettings) {
28 struct menuentry {
29 int paramNum; // enum value in Parameters.h for setting store
30 int idcNum; // #define value in resource.h for dasher.rc
33 // List of menu items that will be displayed in the General Preferences
34 static menuentry menutable[] = {
35 {BP_START_MOUSE, IDC_LEFT},
36 {BP_START_SPACE, IDC_SPACE},
37 {BP_START_STYLUS, IDC_STYLUS},
38 {BP_STOP_IDLE, IDC_STOPIDLE},
39 {BP_PAUSE_OUTSIDE, IDC_WINDOWPAUSE},
40 {BP_AUTO_SPEEDCONTROL, IDC_AUTOSPEED},
41 {BP_AUTOCALIBRATE, IDC_AUTOCALIBRATE},
42 {BP_CONTROL_MODE, IDC_CONTROLMODE}
45 static menuentry listtable[] = {
46 {SP_INPUT_FILTER, IDC_CONTROL_LIST},
47 {SP_INPUT_DEVICE, IDC_INPUT_LIST}
50 void CControlPage::PopulateList() {
51 // Populate the controls in the dialogue box based on the relevent parameters
52 // in m_pDasher
54 SB_slider = GetDlgItem(m_hwnd, IDC_SPEEDSLIDER);
56 SendMessage(SB_slider, TBM_SETPAGESIZE, 0L, 20); // PgUp and PgDown change bitrate by reasonable amount
57 SendMessage(SB_slider, TBM_SETTICFREQ, 100, 0L);
58 SendMessage(SB_slider, TBM_SETRANGE, FALSE, (LPARAM) MAKELONG(10, 800));
60 speedbox = GetDlgItem(m_hwnd, IDC_SPEEDVAL);
62 SendMessage(SB_slider, TBM_SETPOS, TRUE, (LPARAM) m_pAppSettings->GetLongParameter(LP_MAX_BITRATE));
63 _sntprintf(m_tcBuffer, 100, TEXT("%0.2f"), m_pAppSettings->GetLongParameter(LP_MAX_BITRATE) / 100.0);
64 SendMessage(speedbox, WM_SETTEXT, 0, (LPARAM) m_tcBuffer);
66 m_hMousePosStyle = GetDlgItem(m_hwnd, IDC_MOUSEPOS_STYLE);
67 SendMessage(m_hMousePosStyle, CB_ADDSTRING, 0, (LPARAM)L"Centre circle");
68 SendMessage(m_hMousePosStyle, CB_ADDSTRING, 0, (LPARAM)L"Two box");
70 if(m_pAppSettings->GetBoolParameter(BP_MOUSEPOS_MODE)) {
71 SendMessage(m_hMousePosStyle, CB_SETCURSEL, 1, 0);
73 else {
74 SendMessage(m_hMousePosStyle, CB_SETCURSEL, 0, 0);
77 if(m_pAppSettings->GetBoolParameter(BP_MOUSEPOS_MODE) || m_pAppSettings->GetBoolParameter(BP_CIRCLE_START)) {
78 SendMessage(GetDlgItem(m_hwnd, IDC_MOUSEPOS), BM_SETCHECK, BST_CHECKED, 0);
80 else {
81 SendMessage(GetDlgItem(m_hwnd, IDC_MOUSEPOS), BM_SETCHECK, BST_UNCHECKED, 0);
84 // all the button checkboxes
85 for(int ii = 0; ii<sizeof(menutable)/sizeof(menuentry); ii++)
87 if(m_pAppSettings->GetBoolParameter(menutable[ii].paramNum))
89 SendMessage(GetDlgItem(m_hwnd, menutable[ii].idcNum), BM_SETCHECK, BST_CHECKED, 0);
91 else
93 SendMessage(GetDlgItem(m_hwnd, menutable[ii].idcNum), BM_SETCHECK, BST_UNCHECKED, 0);
97 // enable idletime control if button checked
98 BOOL bIdle = m_pAppSettings->GetBoolParameter(BP_STOP_IDLE) ? TRUE : FALSE;
99 EnableWindow( GetDlgItem(m_hwnd, IDC_IDLETIME), bIdle);
100 EnableWindow( GetDlgItem(m_hwnd, IDC_STATICIDLETIME), bIdle);
102 // Set the idle time data
103 SetDlgItemInt ( m_hwnd, IDC_IDLETIME, m_pAppSettings->GetLongParameter( LP_STOP_IDLETIME) , TRUE);
105 // List entries:
107 for(int i(0); i<sizeof(listtable)/sizeof(menuentry); ++i) {
108 // {
109 // int i(0);
110 std::vector<std::string> vValues;
111 m_pDasherInterface->GetPermittedValues(listtable[i].paramNum, vValues);
113 for(std::vector<std::string>::iterator it(vValues.begin()); it != vValues.end(); ++it) {
114 Tstring Item;
115 WinUTF8::UTF8string_to_wstring(*it, Item);
116 int iIdx(SendMessage(GetDlgItem(m_hwnd, listtable[i].idcNum), LB_ADDSTRING, 0, (LPARAM) Item.c_str()));
118 if(*it == m_pAppSettings->GetStringParameter(listtable[i].paramNum))
119 SendMessage(GetDlgItem(m_hwnd, listtable[i].idcNum), LB_SETCURSEL, iIdx, 0);
124 bool CControlPage::Validate() {
125 // 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.
126 return TRUE;
129 bool CControlPage::Apply()
132 int iIdle;
133 bool bSuccess = wincommon::GetWindowInt( GetDlgItem(m_hwnd, IDC_IDLETIME) , iIdle);
134 if (bSuccess)
135 m_pAppSettings->SetLongParameter( LP_STOP_IDLETIME, iIdle);
136 else
138 return FALSE; // TODO notify user
141 double NewSpeed;
142 NewSpeed = SendMessage(SB_slider, TBM_GETPOS, 0, 0);
143 m_pAppSettings->SetLongParameter( LP_MAX_BITRATE, NewSpeed);
145 if(SendMessage(GetDlgItem(m_hwnd, IDC_MOUSEPOS), BM_GETCHECK, 0, 0) == BST_CHECKED ) {
146 int iComboIdx;
147 iComboIdx = SendMessage(m_hMousePosStyle, CB_GETCURSEL, 0, 0);
149 if(iComboIdx == 0) {
150 m_pAppSettings->SetBoolParameter(BP_MOUSEPOS_MODE, false);
151 m_pAppSettings->SetBoolParameter(BP_CIRCLE_START, true);
153 else {
154 m_pAppSettings->SetBoolParameter(BP_MOUSEPOS_MODE, true);
155 m_pAppSettings->SetBoolParameter(BP_CIRCLE_START, false);
158 else {
159 m_pAppSettings->SetBoolParameter(BP_MOUSEPOS_MODE, false);
160 m_pAppSettings->SetBoolParameter(BP_CIRCLE_START, false);
163 for(int ii = 0; ii<sizeof(menutable)/sizeof(menuentry); ii++)
165 m_pAppSettings->SetBoolParameter(menutable[ii].paramNum,
166 SendMessage(GetDlgItem(m_hwnd, menutable[ii].idcNum), BM_GETCHECK, 0, 0) == BST_CHECKED );
169 for(int i(0); i < sizeof(listtable)/sizeof(menuentry); ++i) {
170 int iSelection(SendMessage(GetDlgItem(m_hwnd, listtable[i].idcNum), LB_GETCURSEL, 0, 0));
172 int iLength(SendMessage(GetDlgItem(m_hwnd, listtable[i].idcNum), LB_GETTEXTLEN, iSelection, 0));
173 TCHAR *szData(new TCHAR[iLength + 1]);
174 SendMessage(GetDlgItem(m_hwnd, listtable[i].idcNum), LB_GETTEXT, iSelection, (LPARAM)szData);
176 std::string strNewValue;
177 WinUTF8::wstring_to_UTF8string(szData, strNewValue);
178 delete[] szData;
180 m_pAppSettings->SetStringParameter(listtable[i].paramNum, strNewValue);
183 // Return false (and notify the user) if something is wrong.
184 return TRUE;
187 LRESULT CControlPage::WndProc(HWND Window, UINT message, WPARAM wParam, LPARAM lParam) {
189 if(message == WM_MS_CLOSE) {
190 if(m_pModuleSettingsDialogue) {
191 m_pModuleSettingsDialogue->DestroyWindow();
193 m_pModuleSettingsDialogue = NULL;
194 EnableWindow(m_hwnd, true);
195 return 0;
198 double NewSpeed;
199 switch (message)
201 case WM_COMMAND:
202 switch (LOWORD(wParam))
204 case (IDC_DISPLAY):
205 break;
206 case IDC_BUTTON_PREFS:
208 //CButtonPrefs ButtonPrefs(m_hwnd, 0, m_pAppSettings);
209 int iSelection(SendMessage(GetDlgItem(m_hwnd, listtable[0].idcNum), LB_GETCURSEL, 0, 0));
211 int iLength(SendMessage(GetDlgItem(m_hwnd, listtable[0].idcNum), LB_GETTEXTLEN, iSelection, 0));
212 TCHAR *szData(new TCHAR[iLength + 1]);
213 SendMessage(GetDlgItem(m_hwnd, listtable[0].idcNum), LB_GETTEXT, iSelection, (LPARAM)szData);
215 std::string strNewValue;
216 WinUTF8::wstring_to_UTF8string(szData, strNewValue);
217 delete[] szData;
219 SModuleSettings *pSettings;
220 int iSettingsCount;
222 if(!m_pDasherInterface->GetModuleSettings(strNewValue, &pSettings, &iSettingsCount))
223 break;
225 RECT sRect;
227 m_pModuleSettingsDialogue = new CModuleSettings(strNewValue, pSettings, iSettingsCount, m_pDasherInterface);
228 m_pModuleSettingsDialogue->Create(m_hwnd, &sRect);
229 m_pModuleSettingsDialogue->ShowWindow(SW_RESTORE);
231 EnableWindow(m_hwnd, false);
233 break;
234 case IDC_BUTTON_PREFS2:
236 //CButtonPrefs ButtonPrefs(m_hwnd, 0, m_pAppSettings);
237 int iSelection(SendMessage(GetDlgItem(m_hwnd, listtable[1].idcNum), LB_GETCURSEL, 0, 0));
239 int iLength(SendMessage(GetDlgItem(m_hwnd, listtable[1].idcNum), LB_GETTEXTLEN, iSelection, 0));
240 TCHAR *szData(new TCHAR[iLength + 1]);
241 SendMessage(GetDlgItem(m_hwnd, listtable[1].idcNum), LB_GETTEXT, iSelection, (LPARAM)szData);
243 std::string strNewValue;
244 WinUTF8::wstring_to_UTF8string(szData, strNewValue);
245 delete[] szData;
247 SModuleSettings *pSettings;
248 int iSettingsCount;
250 if(!m_pDasherInterface->GetModuleSettings(strNewValue, &pSettings, &iSettingsCount))
251 break;
253 RECT sRect;
255 m_pModuleSettingsDialogue = new CModuleSettings(strNewValue, pSettings, iSettingsCount, m_pDasherInterface);
256 m_pModuleSettingsDialogue->Create(m_hwnd, &sRect);
257 m_pModuleSettingsDialogue->ShowWindow(SW_RESTORE);
259 EnableWindow(m_hwnd, false);
261 break;
262 case IDC_STOPIDLE:
263 // set activity of the idle time edit control
264 BOOL bChecked = SendMessage(GetDlgItem(m_hwnd, IDC_STOPIDLE), BM_GETCHECK, 0, 0) == BST_CHECKED;
265 EnableWindow( GetDlgItem(m_hwnd, IDC_IDLETIME), bChecked);
266 EnableWindow( GetDlgItem(m_hwnd, IDC_STATICIDLETIME), bChecked);
267 // don't return: also want to pass event on to superclass to trigger Apply activation
268 break;
270 break;
271 case WM_HSCROLL:
272 if((LOWORD(wParam) == SB_THUMBPOSITION) | (LOWORD(wParam) == SB_THUMBTRACK)) {
273 // Some messages give the new postion
274 NewSpeed = HIWORD(wParam);
276 else {
277 // Otherwise we have to ask for it
278 long Pos = SendMessage(SB_slider, TBM_GETPOS, 0, 0);
279 NewSpeed = Pos;
282 _sntprintf(m_tcBuffer, 100, TEXT("%0.2f"), NewSpeed / 100);
283 SendMessage(speedbox, WM_SETTEXT, 0, (LPARAM) m_tcBuffer);
285 return TRUE;
286 break;
289 // pass on to superclass:
290 return CPrefsPageBase::WndProc(Window, message, wParam, lParam);