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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
|
#include "clComboBox.hpp"
#include "clThemedTextCtrl.hpp"
#include <wx/button.h>
#include <wx/settings.h>
#include <wx/sizer.h>
namespace
{
class clComboBoxROLocker
{
clComboBox* m_cb = nullptr;
bool m_isReadOnly = true;
public:
clComboBoxROLocker(clComboBox* cb)
: m_cb(cb)
, m_isReadOnly(m_cb && !m_cb->GetTextCtrl()->IsEditable())
{
if(m_isReadOnly) {
m_cb->GetTextCtrl()->SetEditable(true);
}
}
~clComboBoxROLocker()
{
if(!m_isReadOnly) {
return;
}
m_cb->GetTextCtrl()->SetEditable(false);
}
};
} // namespace
clComboBox::clComboBox(wxWindow* parent, wxWindowID id, const wxString& value, const wxPoint& pos, const wxSize& size,
size_t n, const wxString choices[], long style, const wxValidator& validator,
const wxString& name)
: wxControl(parent, id, pos, size, wxNO_BORDER)
{
wxUnusedVar(validator);
wxUnusedVar(name);
m_cbStyle = style & ~wxWINDOW_STYLE_MASK;
m_choices.reserve(n);
for(size_t i = 0; i < n; ++i) {
m_choices.Add(choices[i]);
}
DoCreate(value);
}
clComboBox::clComboBox(wxWindow* parent, wxWindowID id, const wxString& value, const wxPoint& pos, const wxSize& size,
const wxArrayString& choices, long style, const wxValidator& validator, const wxString& name)
: wxControl(parent, id, pos, size, wxNO_BORDER)
{
wxUnusedVar(validator);
wxUnusedVar(name);
m_cbStyle = style & ~wxWINDOW_STYLE_MASK;
m_choices.reserve(choices.size());
m_choices = choices;
DoCreate(value);
}
clComboBox::clComboBox() {}
clComboBox::~clComboBox() {}
bool clComboBox::Create(wxWindow* parent, wxWindowID id, const wxString& value, const wxPoint& pos, const wxSize& size,
size_t n, const wxString choices[], long style, const wxValidator& validator,
const wxString& name)
{
wxUnusedVar(validator);
wxUnusedVar(name);
bool res = wxControl::Create(parent, id, pos, size, wxNO_BORDER);
m_choices.reserve(n);
for(size_t i = 0; i < n; ++i) {
m_choices.push_back(choices[i]);
}
m_cbStyle = style & ~wxWINDOW_STYLE_MASK;
DoCreate(value);
return res;
}
bool clComboBox::Create(wxWindow* parent, wxWindowID id, const wxString& value, const wxPoint& pos, const wxSize& size,
const wxArrayString& choices, long style, const wxValidator& validator, const wxString& name)
{
wxUnusedVar(validator);
wxUnusedVar(name);
bool res = wxControl::Create(parent, id, pos, size, wxNO_BORDER);
m_cbStyle = style & ~wxWINDOW_STYLE_MASK;
m_choices = choices;
DoCreate(value);
return res;
}
void clComboBox::DoCreate(const wxString& value)
{
SetSizer(new wxBoxSizer(wxHORIZONTAL));
m_textCtrl = new clThemedTextCtrl(this, wxID_ANY, value);
GetSizer()->Add(m_textCtrl, 1, wxEXPAND | wxALL, 1);
const wxString arrowSymbol = wxT(" \u25BC ");
m_button = new wxButton(this, wxID_ANY, arrowSymbol, wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT);
wxColour text_colour = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNTEXT);
m_button->SetForegroundColour(DrawingUtils::IsDark(text_colour) ? text_colour.ChangeLightness(120)
: text_colour.ChangeLightness(80));
m_button->Bind(wxEVT_BUTTON, &clComboBox::OnButtonClicked, this);
m_textCtrl->Bind(wxEVT_TEXT, &clComboBox::OnText, this);
m_textCtrl->Bind(wxEVT_CHAR_HOOK, &clComboBox::OnCharHook, this);
Bind(wxEVT_SET_FOCUS, &clComboBox::OnFocus, this);
if(m_cbStyle & wxCB_READONLY) {
m_textCtrl->SetEditable(false);
}
GetSizer()->Add(m_button, 0, wxALIGN_CENTER_VERTICAL);
GetSizer()->Fit(this);
wxRect textRect = m_textCtrl->GetSize();
textRect.Inflate(1);
m_button->SetSizeHints(textRect.GetHeight(), textRect.GetHeight());
}
namespace
{
void ButtonShowMenu(wxButton* button, wxMenu& menu, wxPoint* point)
{
wxPoint menuPos;
if(point) {
menuPos = *point;
} else {
menuPos = button->GetClientRect().GetBottomLeft();
#ifdef __WXOSX__
menuPos.y += 5;
#endif
}
button->PopupMenu(&menu, menuPos);
}
} // namespace
void clComboBox::OnButtonClicked(wxCommandEvent& event)
{
wxUnusedVar(event);
wxMenu menu;
for(size_t i = 0; i < m_choices.size(); ++i) {
const wxString& choice = m_choices.Item(i);
auto item = menu.Append(wxID_ANY, choice, "", wxITEM_CHECK);
item->Check(i == (size_t)m_selection);
menu.Bind(
wxEVT_MENU,
[this, choice, i](wxCommandEvent& e) {
wxUnusedVar(e);
// update the selected text
clComboBoxROLocker locker(this);
m_textCtrl->ChangeValue(choice);
m_selection = i;
// Notify about selection change
wxCommandEvent selectionChanged(wxEVT_COMBOBOX);
selectionChanged.SetEventObject(this);
selectionChanged.SetInt(i); // the new selection
GetEventHandler()->AddPendingEvent(selectionChanged);
},
item->GetId());
}
if(!m_custom_commands.IsEmpty()) {
if(!m_choices.empty()) {
menu.AppendSeparator();
}
for(auto item : m_custom_commands) {
menu.Append(item.first, item.second);
menu.Bind(
wxEVT_MENU, [this](wxCommandEvent& e) { GetEventHandler()->ProcessEvent(e); }, item.first);
}
}
ButtonShowMenu(m_button, menu, nullptr);
m_textCtrl->CallAfter(&wxTextCtrl::SetFocus);
}
void clComboBox::SetHint(const wxString& hint) { wxUnusedVar(hint); }
void clComboBox::SetSelection(size_t sel)
{
if(sel == INVALID_SIZE_T) {
m_textCtrl->Clear();
m_selection = INVALID_SIZE_T;
} else {
if(sel >= m_choices.GetCount()) {
return;
}
m_selection = sel;
// else, selection is a valid index
SetValue(m_choices.Item(m_selection));
}
}
void clComboBox::OnText(wxCommandEvent& event)
{
wxUnusedVar(event);
wxCommandEvent textEvent(wxEVT_TEXT);
textEvent.SetEventObject(this);
GetEventHandler()->AddPendingEvent(textEvent);
m_selection = m_choices.Index(m_textCtrl->GetValue());
}
void clComboBox::DoTextEnter()
{
wxCommandEvent textEvent(wxEVT_TEXT_ENTER);
textEvent.SetEventObject(this);
GetEventHandler()->AddPendingEvent(textEvent);
}
void clComboBox::SetString(size_t n, const wxString& text)
{
if(n >= m_choices.size()) {
return;
}
m_choices[n] = text;
if(GetSelection() == (size_t)n) {
SetValue(m_choices[n]);
}
}
void clComboBox::SetValue(const wxString& text)
{
clComboBoxROLocker locker(this);
m_textCtrl->SetValue(text);
SetStringSelection(text);
}
wxString clComboBox::GetStringSelection() const { return m_textCtrl->GetValue(); }
void clComboBox::SetStringSelection(const wxString& text)
{
clComboBoxROLocker locker(this);
for(size_t i = 0; i < m_choices.size(); ++i) {
if(m_choices.Item(i).CmpNoCase(text) == 0) {
m_textCtrl->ChangeValue(m_choices.Item(i));
m_selection = i;
break;
}
}
}
void clComboBox::SetFocus() { m_textCtrl->SetFocus(); }
void clComboBox::Append(const std::vector<wxString>& strings)
{
if(strings.empty()) {
return;
}
m_choices.reserve(strings.size() + m_choices.size());
for(const auto& str : strings) {
m_choices.Add(str);
}
}
void clComboBox::Append(const wxArrayString& strings)
{
if(strings.empty()) {
return;
}
m_choices.reserve(strings.size() + m_choices.size());
m_choices.insert(m_choices.end(), strings.begin(), strings.end());
}
size_t clComboBox::Append(const wxString& text)
{
m_choices.Add(text);
return m_choices.size() - 1;
}
void clComboBox::Clear()
{
m_choices.Clear();
m_selection = INVALID_SIZE_T;
clComboBoxROLocker locker(this);
m_textCtrl->ChangeValue(wxEmptyString);
}
void clComboBox::Delete(size_t index)
{
if(index >= m_choices.size()) {
return;
}
if(index <= m_selection) {
// the removed item is _before_ the selected item
// invalidate the selection
m_selection = INVALID_SIZE_T;
clComboBoxROLocker locker(this);
m_textCtrl->ChangeValue(wxEmptyString);
}
m_choices.RemoveAt(index);
}
size_t clComboBox::FindString(const wxString& s, bool bCase) const { return m_choices.Index(s, bCase); }
wxString clComboBox::GetValue() const { return m_textCtrl->GetValue(); }
wxArrayString clComboBox::GetStrings() const
{
wxArrayString strings;
strings.reserve(m_choices.size() + 1);
if(GetSelection() == INVALID_SIZE_T && !GetValue().empty()) {
strings.Add(GetValue());
}
strings.insert(strings.end(), m_choices.begin(), m_choices.end());
return strings;
}
void clComboBox::OnCharHook(wxKeyEvent& event)
{
if(event.GetKeyCode() == WXK_TAB) {
Navigate(event.ShiftDown() ? wxNavigationKeyEvent::IsBackward : wxNavigationKeyEvent::IsForward);
} else if(event.GetKeyCode() == WXK_NUMPAD_ENTER || event.GetKeyCode() == WXK_RETURN) {
DoTextEnter();
} else {
event.Skip();
}
}
void clComboBox::OnFocus(wxFocusEvent& event)
{
event.Skip();
m_textCtrl->CallAfter(&wxTextCtrl::SetFocus);
}
bool clComboBox::IsTextEmpty() const { return m_textCtrl->IsEmpty(); }
void clComboBox::AddCommand(int command_id, const wxString& label)
{
if(m_custom_commands.Contains(command_id)) {
m_custom_commands.Remove(command_id);
}
m_custom_commands.PushBack(command_id, label);
}
|