| 1 |
namespace phoenix { |
| 2 |
|
| 3 |
void pComboButton::append(string text) { |
| 4 |
SendMessage(hwnd, CB_ADDSTRING, 0, (LPARAM)(wchar_t*)utf16_t(text)); |
| 5 |
if(SendMessage(hwnd, CB_GETCOUNT, 0, 0) == 1) setSelection(0); |
| 6 |
} |
| 7 |
|
| 8 |
Size pComboButton::minimumSize() { |
| 9 |
unsigned maximumWidth = 0; |
| 10 |
for(auto& text : comboButton.state.text) maximumWidth = max(maximumWidth, pFont::size(hfont, text).width); |
| 11 |
return {maximumWidth + 24, pFont::size(hfont, " ").height + 10}; |
| 12 |
} |
| 13 |
|
| 14 |
void pComboButton::remove(unsigned selection) { |
| 15 |
locked = true; |
| 16 |
SendMessage(hwnd, CB_DELETESTRING, selection, 0); |
| 17 |
locked = false; |
| 18 |
|
| 19 |
if(selection == comboButton.state.selection) comboButton.setSelection(0); |
| 20 |
} |
| 21 |
|
| 22 |
void pComboButton::reset() { |
| 23 |
SendMessage(hwnd, CB_RESETCONTENT, 0, 0); |
| 24 |
} |
| 25 |
|
| 26 |
void pComboButton::setGeometry(Geometry geometry) { |
| 27 |
//height = minimum drop-down list height; use CB_SETITEMHEIGHT to control actual widget height |
| 28 |
pWidget::setGeometry({geometry.x, geometry.y, geometry.width, 1}); |
| 29 |
RECT rc; |
| 30 |
GetWindowRect(hwnd, &rc); |
| 31 |
unsigned adjustedHeight = geometry.height - ((rc.bottom - rc.top) - SendMessage(hwnd, CB_GETITEMHEIGHT, (WPARAM)-1, 0)); |
| 32 |
SendMessage(hwnd, CB_SETITEMHEIGHT, (WPARAM)-1, adjustedHeight); |
| 33 |
} |
| 34 |
|
| 35 |
void pComboButton::setSelection(unsigned selection) { |
| 36 |
SendMessage(hwnd, CB_SETCURSEL, selection, 0); |
| 37 |
} |
| 38 |
|
| 39 |
void pComboButton::setText(unsigned selection, string text) { |
| 40 |
locked = true; |
| 41 |
SendMessage(hwnd, CB_DELETESTRING, selection, 0); |
| 42 |
SendMessage(hwnd, CB_INSERTSTRING, selection, (LPARAM)(wchar_t*)utf16_t(text)); |
| 43 |
setSelection(comboButton.state.selection); |
| 44 |
locked = false; |
| 45 |
} |
| 46 |
|
| 47 |
void pComboButton::constructor() { |
| 48 |
hwnd = CreateWindow( |
| 49 |
L"COMBOBOX", L"", |
| 50 |
WS_CHILD | WS_TABSTOP | CBS_DROPDOWNLIST | CBS_HASSTRINGS, |
| 51 |
0, 0, 0, 0, |
| 52 |
parentHwnd, (HMENU)id, GetModuleHandle(0), 0 |
| 53 |
); |
| 54 |
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)&comboButton); |
| 55 |
setDefaultFont(); |
| 56 |
for(auto& text : comboButton.state.text) append(text); |
| 57 |
setSelection(comboButton.state.selection); |
| 58 |
synchronize(); |
| 59 |
} |
| 60 |
|
| 61 |
void pComboButton::destructor() { |
| 62 |
DestroyWindow(hwnd); |
| 63 |
} |
| 64 |
|
| 65 |
void pComboButton::orphan() { |
| 66 |
destructor(); |
| 67 |
constructor(); |
| 68 |
} |
| 69 |
|
| 70 |
void pComboButton::onChange() { |
| 71 |
unsigned selection = SendMessage(hwnd, CB_GETCURSEL, 0, 0); |
| 72 |
if(selection == comboButton.state.selection) return; |
| 73 |
comboButton.state.selection = selection; |
| 74 |
if(comboButton.onChange) comboButton.onChange(); |
| 75 |
} |
| 76 |
|
| 77 |
} |