-
Notifications
You must be signed in to change notification settings - Fork 35
/
combobox.h
104 lines (86 loc) · 2.79 KB
/
combobox.h
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
/**
* Part of WinLamb - Win32 API Lambda Library
* https://github.com/rodrigocfd/winlamb
* Copyright 2017-present Rodrigo Cesar de Freitas Dias
* This library is released under the MIT License
*/
#pragma once
#include "internals/base_native_ctrl_pubm.h"
#include "internals/styler.h"
#include "str.h"
#include "wnd.h"
namespace wl {
// Wrapper to native combobox control.
class combobox final :
public wnd,
public _wli::base_native_ctrl_pubm<combobox>
{
public:
enum class sort { SORTED, UNSORTED };
private:
HWND _hWnd = nullptr;
_wli::base_native_ctrl _baseNativeCtrl{_hWnd};
public:
// Wraps window style changes done by Get/SetWindowLongPtr.
_wli::styler<combobox> style{this};
combobox() noexcept :
wnd(_hWnd), base_native_ctrl_pubm(_baseNativeCtrl) { }
combobox(combobox&&) = default;
combobox& operator=(combobox&&) = default; // movable only
combobox& create(HWND hParent, int ctrlId, POINT pos,
LONG width, sort sortType)
{
this->_baseNativeCtrl.create(hParent, ctrlId, nullptr,
pos, {width, 0}, L"combobox",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | CBS_DROPDOWNLIST |
(sortType == sort::SORTED ? CBS_SORT : 0), 0);
return *this;
}
combobox& create(const wnd* parent, int ctrlId, POINT pos,
LONG width, sort sortType)
{
return this->create(parent->hwnd(), ctrlId, pos, width, sortType);
}
size_t count() const noexcept {
return SendMessageW(this->_hWnd, CB_GETCOUNT, 0, 0);
}
size_t get_selected_index() const noexcept {
return static_cast<size_t>(SendMessageW(this->_hWnd, CB_GETCURSEL, 0, 0));
}
combobox& remove_all() noexcept {
SendMessageW(this->_hWnd, CB_RESETCONTENT, 0, 0);
return *this;
}
combobox& add(const wchar_t* entries, wchar_t delimiter = L'|') {
wchar_t delim[2]{delimiter, L'\0'};
std::vector<std::wstring> vals = str::split(entries, delim);
for (const std::wstring& s : vals) {
SendMessageW(this->_hWnd, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>(s.c_str()));
}
return *this;
}
combobox& add(std::initializer_list<const wchar_t*> entries) noexcept {
for (const wchar_t* s : entries) {
SendMessageW(this->_hWnd, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>(s));
}
return *this;
}
std::wstring get_text(size_t index) const {
std::wstring buf;
size_t len = SendMessageW(this->_hWnd, CB_GETLBTEXTLEN, index, 0);
if (len) {
buf.resize(len, L'\0');
SendMessageW(this->_hWnd, CB_GETLBTEXT, index, reinterpret_cast<LPARAM>(&buf[0]));
buf.resize(len);
}
return buf;
}
std::wstring get_selected_text() const {
return this->get_text(this->get_selected_index());
}
combobox& select(size_t index) noexcept {
SendMessageW(this->_hWnd, CB_SETCURSEL, index, 0);
return *this;
}
};
}//namespace wl