tagging release
[dasher.git] / Src / Win32 / Widgets / StatusControl.cpp
blobe6422e0361846e26a34158aa76da475886768459
1 #include "StatusControl.h"
2 #include "WinCommon.h"
4 #include <string>
6 // TODO: Make this a notify?
7 CONST UINT DASHER_SHOW_PREFS = RegisterWindowMessage(_DASHER_SHOW_PREFS);
9 CStatusControl::CStatusControl(CDasherInterfaceBase *pDasherInterface) {
10 m_pDasherInterface = pDasherInterface;
13 // TODO: ATL has more sophisticated handlers for conrol and notify messages - consider using them instead
14 LRESULT CStatusControl::OnCommand(UINT message, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
15 switch(HIWORD(wParam)) {
16 case CBN_SELCHANGE:
17 SelectAlphabet();
18 break;
19 default:
20 bHandled = false;
21 break;
24 return 0;
27 LRESULT CStatusControl::OnNotify(UINT message, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
28 NMHDR *pNotify((LPNMHDR)lParam);
30 switch(pNotify->code) {
31 case UDN_DELTAPOS:
32 UpdateSpeed(((LPNMUPDOWN) lParam)->iPos, ((LPNMUPDOWN) lParam)->iDelta);
33 break;
34 default:
35 bHandled = false;
36 break;
39 return 0;
42 LRESULT CStatusControl::OnSize(UINT message, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
43 LayoutChildrenUpdate();
44 return 0;
47 HWND CStatusControl::Create(HWND hParent) {
48 CWindowImpl<CStatusControl>::Create(hParent);
50 CreateChildren();
52 PopulateCombo();
53 PopulateSpeed();
55 LayoutChildrenInitial();
57 return m_hWnd;
60 void CStatusControl::CreateChildren() {
61 // First create the two static labels.
62 HGDIOBJ hGuiFont;
63 hGuiFont = GetStockObject(DEFAULT_GUI_FONT);
65 std::wstring strSpeedLabel(L"Speed:");
66 std::wstring strAlphabetLabel(L"Alphabet:");
68 // TODO: Wrap windows here in CWindow classes.
69 m_hSpeedLabel = CreateWindowEx(WS_EX_CONTROLPARENT, TEXT("STATIC"), strSpeedLabel.c_str(),
70 SS_CENTER | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0, 0, 0, 0, m_hWnd, NULL, WinHelper::hInstApp, NULL);
72 m_hAlphabetLabel = CreateWindowEx(WS_EX_CONTROLPARENT, TEXT("STATIC"), strAlphabetLabel.c_str(),
73 SS_CENTER | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0, 0, 0, 0, m_hWnd, NULL, WinHelper::hInstApp, NULL);
75 SendMessage(m_hSpeedLabel, WM_SETFONT, (WPARAM) hGuiFont, true);
76 SendMessage(m_hAlphabetLabel, WM_SETFONT, (WPARAM) hGuiFont, true);
78 SIZE sSize;
80 HDC hSpeedDC(::GetDC(m_hSpeedLabel));
81 SelectObject(hSpeedDC, hGuiFont);
82 GetTextExtentPoint32(hSpeedDC, strSpeedLabel.c_str(), strSpeedLabel.size(), &sSize);
83 ::MoveWindow(m_hSpeedLabel, 0, 0, sSize.cx + 4, sSize.cy, false);
84 ::ReleaseDC(m_hSpeedLabel, hSpeedDC);
86 HDC hAlphabetDC(::GetDC(m_hAlphabetLabel));
87 SelectObject(hAlphabetDC, hGuiFont);
88 GetTextExtentPoint32(hAlphabetDC, strAlphabetLabel.c_str(), strAlphabetLabel.size(), &sSize);
89 ::MoveWindow(m_hAlphabetLabel, 0, 0, sSize.cx + 4, sSize.cy, false);
90 ::ReleaseDC(m_hAlphabetLabel, hAlphabetDC);
92 // Next the edit box and up/down control
93 m_hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("EDIT"), TEXT(""), WS_CHILD | WS_TABSTOP | WS_VISIBLE | WS_GROUP, 0, 0, 0, CW_USEDEFAULT, m_hWnd, NULL, WinHelper::hInstApp, NULL);
94 SendMessage(m_hEdit, EM_LIMITTEXT, (WPARAM) 4, (LPARAM) 0);
96 HDC hdc = ::GetDC(m_hEdit);
97 TEXTMETRIC tmGui;
98 GetTextMetrics(hdc, &tmGui);
99 SendMessage(m_hEdit, WM_SETFONT, (WPARAM) hGuiFont, true);
100 ::ReleaseDC(m_hEdit, hdc);
102 int iEditHeight = tmGui.tmHeight + (GetSystemMetrics(SM_CYEDGE) * 2);
103 m_iEditWidth = tmGui.tmAveCharWidth * 7;
104 ::MoveWindow(m_hEdit, 0, 0, m_iEditWidth, iEditHeight, false);
106 m_hUpDown = CreateWindowEx(WS_EX_CLIENTEDGE, UPDOWN_CLASS, TEXT(""), UDS_ALIGNRIGHT | WS_CHILD | WS_TABSTOP |WS_VISIBLE | WS_GROUP, 0, 0, 16, 16, m_hWnd, NULL, WinHelper::hInstApp, NULL);
107 SendMessage(m_hUpDown, UDM_SETRANGE, 0, (LPARAM) MAKELONG(800, 1));
109 // And finally the combo box
110 m_hCombo = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("COMBOBOX"), TEXT(""), CBS_DROPDOWNLIST | WS_CHILD | WS_TABSTOP | WS_VISIBLE |WS_GROUP | ES_READONLY, 0, 0, 128, 128, m_hWnd, NULL, WinHelper::hInstApp, NULL);
111 SendMessage(m_hCombo, WM_SETFONT, (WPARAM) hGuiFont, true);
113 DeleteObject(hGuiFont);
115 // Set the height to what is finally required. The extra pixel is needed to get everything to line up nicely
116 MoveWindow(0, 0, 0, iEditHeight + 1, false);
119 void CStatusControl::LayoutChildrenInitial() {
120 RECT rc;
121 GetWindowRect(&rc);
123 // And how much is needed for the various bits and pieces
124 RECT sRect;
126 ::GetWindowRect(m_hSpeedLabel, &sRect);
127 int iSpeedLabelWidth(sRect.right - sRect.left);
128 int iSpeedLabelHeight(sRect.bottom - sRect.top);
130 ::GetWindowRect(m_hAlphabetLabel, &sRect);
131 int iAlphabetLabelWidth(sRect.right - sRect.left);
132 int iAlphabetLabelHeight(sRect.bottom - sRect.top);
134 ::GetWindowRect(m_hEdit, &sRect);
135 int iEditHeight(sRect.bottom - sRect.top);
137 ::GetWindowRect(m_hCombo, &sRect);
138 int iComboWidth(sRect.right - sRect.left);
139 int iComboHeight(sRect.bottom - sRect.top);
141 // Finally, do the layout
142 // TODO: This isn't necessary - the only thing which is going to change is the width of the combo
143 int iPosition(0);
145 ::MoveWindow(m_hSpeedLabel, iPosition, (iEditHeight - iSpeedLabelHeight) / 2, iSpeedLabelWidth, iSpeedLabelHeight, TRUE);
146 iPosition += iSpeedLabelWidth + 2;
148 ::MoveWindow(m_hEdit, iPosition, 0, m_iEditWidth, iEditHeight, TRUE);
149 ::SendMessage(m_hUpDown, UDM_SETBUDDY, (WPARAM)m_hEdit, 0);
150 iPosition += m_iEditWidth + 2;
152 ::MoveWindow(m_hAlphabetLabel, iPosition, (iEditHeight - iAlphabetLabelHeight) / 2, iAlphabetLabelWidth, iAlphabetLabelHeight, TRUE);
153 iPosition += iAlphabetLabelWidth + 2;
155 ::MoveWindow(m_hCombo, iPosition, 0, rc.right - rc.left - iPosition, iComboHeight, TRUE);
158 void CStatusControl::LayoutChildrenUpdate() {
159 // For now don't do anything special here
160 LayoutChildrenInitial();
163 void CStatusControl::PopulateCombo() {
164 int iCount(SendMessage(m_hCombo, CB_GETCOUNT, 0, 0));
166 for(int i(0); i < iCount; ++i)
167 SendMessage(m_hCombo, CB_DELETESTRING, 0, 0);
169 std::wstring strEntry;
171 WinUTF8::UTF8string_to_wstring(m_pDasherInterface->GetStringParameter(SP_ALPHABET_ID), strEntry);
172 SendMessage(m_hCombo, CB_ADDSTRING, 0, (LPARAM) (LPCTSTR)strEntry.c_str());
174 WinUTF8::UTF8string_to_wstring(m_pDasherInterface->GetStringParameter(SP_ALPHABET_1), strEntry);
175 if(strEntry.size() > 0)
176 SendMessage(m_hCombo, CB_ADDSTRING, 0, (LPARAM) (LPCTSTR)strEntry.c_str());
178 WinUTF8::UTF8string_to_wstring(m_pDasherInterface->GetStringParameter(SP_ALPHABET_2), strEntry);
179 if(strEntry.size() > 0)
180 SendMessage(m_hCombo, CB_ADDSTRING, 0, (LPARAM) (LPCTSTR)strEntry.c_str());
182 WinUTF8::UTF8string_to_wstring(m_pDasherInterface->GetStringParameter(SP_ALPHABET_3), strEntry);
183 if(strEntry.size() > 0)
184 SendMessage(m_hCombo, CB_ADDSTRING, 0, (LPARAM) (LPCTSTR)strEntry.c_str());
186 WinUTF8::UTF8string_to_wstring(m_pDasherInterface->GetStringParameter(SP_ALPHABET_4), strEntry);
187 if(strEntry.size() > 0)
188 SendMessage(m_hCombo, CB_ADDSTRING, 0, (LPARAM) (LPCTSTR)strEntry.c_str());
190 SendMessage(m_hCombo, CB_ADDSTRING, 0, (LPARAM) (LPCTSTR)L"More Alphabets...");
192 SendMessage(m_hCombo, CB_SETCURSEL, 0, 0);
195 void CStatusControl::SelectAlphabet() {
196 int iIndex(SendMessage(m_hCombo, CB_GETCURSEL, 0, 0));
197 int iLength(SendMessage(m_hCombo, CB_GETLBTEXTLEN, iIndex, 0));
199 TCHAR *szSelection = new TCHAR[iLength + 1];
200 SendMessage(m_hCombo, CB_GETLBTEXT, iIndex, (LPARAM)szSelection);
202 if(!_tcscmp(szSelection, L"More Alphabets...")) {
203 SendMessage(::GetParent(GetParent().m_hWnd), DASHER_SHOW_PREFS, 0, 0);
205 else {
206 std::string strNewValue;
207 WinUTF8::wstring_to_UTF8string(szSelection, strNewValue);
209 m_pDasherInterface->SetStringParameter(SP_ALPHABET_ID, strNewValue);
212 delete[] szSelection;
215 void CStatusControl::PopulateSpeed() {
216 int iValue(m_pDasherInterface->GetLongParameter(LP_MAX_BITRATE));
218 TCHAR *Buffer = new TCHAR[10];
219 _stprintf(Buffer, TEXT("%0.2f"), iValue / 100.0);
220 SendMessage(m_hEdit, WM_SETTEXT, 0, (LPARAM) (LPCSTR) Buffer);
221 delete[]Buffer;
223 SendMessage(m_hUpDown, UDM_SETPOS, 0, (LPARAM) MAKELONG ((short)iValue, 0));
226 void CStatusControl::UpdateSpeed(int iPos, int iDelta) {
227 int iValue(iPos + iDelta);
229 if(iValue > 800)
230 iValue = 800;
232 if(iValue < 1)
233 iValue = 1;
235 TCHAR *Buffer = new TCHAR[10];
236 _stprintf(Buffer, TEXT("%0.2f"), iValue / 100.0);
237 SendMessage(m_hEdit, WM_SETTEXT, 0, (LPARAM) (LPCSTR) Buffer);
238 delete[]Buffer;
240 m_pDasherInterface->SetLongParameter(LP_MAX_BITRATE, iValue);