Skip to content

Commit

Permalink
clComboBox: use native button on all platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
eranif committed Mar 18, 2022
1 parent 4d88fcf commit 9ef2c4a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
43 changes: 28 additions & 15 deletions Plugin/clComboBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,12 @@ 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);
m_button = new clButton(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT);
m_button->SetHasDropDownMenu(true);
#if wxUSE_NATIVE_BUTTON
m_button->SetText(wxT("")); // this will force size calculation
#else
m_button->SetText(""); // this will force size calculation
#endif
m_button = new wxButton(this, wxID_ANY, wxT(""), wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT);
// m_button->SetHasDropDownMenu(true);
// #if wxUSE_NATIVE_BUTTON
// #else
// m_button->SetText(""); // this will force size calculation
// #endif
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);
Expand All @@ -84,9 +83,29 @@ void clComboBox::DoCreate(const wxString& value)
if(m_cbStyle & wxCB_READONLY) {
m_textCtrl->SetEditable(false);
}
GetSizer()->Add(m_button, 0, wxALIGN_CENTER_VERTICAL | wxALL, 1);
GetSizer()->Add(m_button, 0, wxALIGN_CENTER_VERTICAL);
GetSizer()->Fit(this);
wxRect textRect = m_textCtrl->GetSize();
textRect.Inflate(1);
m_button->SetSizeHints(-1, 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)
{
Expand Down Expand Up @@ -114,13 +133,7 @@ void clComboBox::OnButtonClicked(wxCommandEvent& event)
item->GetId());
}

wxPoint menuPos = GetClientRect().GetBottomLeft();
#ifdef __WXOSX__
menuPos.y += 5;
#endif
menuPos = m_button->ScreenToClient(ClientToScreen(menuPos));

m_button->ShowMenu(menu, &menuPos);
ButtonShowMenu(m_button, menu, nullptr);
m_textCtrl->CallAfter(&wxTextCtrl::SetFocus);
}

Expand Down
5 changes: 3 additions & 2 deletions Plugin/clComboBox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "clButton.h"
#include "codelite_exports.h"

#include <wx/button.h>
#include <wx/combobox.h>
#include <wx/control.h>

Expand All @@ -14,7 +15,7 @@ class WXDLLIMPEXP_SDK clComboBox : public wxControl
{
wxArrayString m_choices;
clThemedTextCtrl* m_textCtrl = nullptr;
clButton* m_button = nullptr;
wxButton* m_button = nullptr;
size_t m_selection = INVALID_SIZE_T;
size_t m_cbStyle = 0;

Expand Down Expand Up @@ -141,7 +142,7 @@ class WXDLLIMPEXP_SDK clComboBox : public wxControl
* @brief return the combo box strings
*/
wxArrayString GetStrings() const;
clButton* GetButton() const { return m_button; }
wxButton* GetButton() const { return m_button; }
clThemedTextCtrl* GetTextCtrl() const { return m_textCtrl; }
};

Expand Down

0 comments on commit 9ef2c4a

Please sign in to comment.