Contents Up Previous Next

wxComboCtrl

A combo control is a generic combobox that allows totally custom popup. In addition it has other customization features. For instance, position and size of the dropdown button can be changed.

Setting Custom Popup for wxComboCtrl

wxComboCtrl needs to be told somehow which control to use and this is done by SetPopupControl(). However, we need something more than just a wxControl in this method as, for example, we need to call SetStringValue("initial text value") and wxControl doesn't have such method. So we also need a wxComboPopup which is an interface which must be implemented by a control to be usable as a popup.

We couldn't derive wxComboPopup from wxControl as this would make it impossible to have a class deriving from a wxWidgets control and from it, so instead it is just a mix-in.

Here's a minimal sample of wxListView popup:

#include <wx/combo.h>
#include <wx/listctrl.h>

class wxListViewComboPopup : public wxListView,
                             public wxComboPopup
{
public:

    // Initialize member variables
    virtual void Init()
    {
        m_value = -1;
    }

    // Create popup control
    virtual bool Create(wxWindow* parent)
    {
        return wxListView::Create(parent,1,wxPoint(0,0),wxDefaultSize);
    }

    // Return pointer to the created control
    virtual wxWindow *GetControl() { return this; }

    // Translate string into a list selection
    virtual void SetStringValue(const wxString& s)
    {
        int n = wxListView::FindItem(-1,s);
        if ( n >= 0 && n < wxListView::GetItemCount() )
            wxListView::Select(n);
    }

    // Get list selection as a string
    virtual wxString GetStringValue() const
    {
        if ( m_value >= 0 )
            return wxListView::GetItemText(m_value);
        return wxEmptyString;
    }

    // Do mouse hot-tracking (which is typical in list popups)
    void OnMouseMove(wxMouseEvent& event)
    {
        // TODO: Move selection to cursor
    }

    // On mouse left up, set the value and close the popup
    void OnMouseClick(wxMouseEvent& WXUNUSED(event))
    {
        m_value = wxListView::GetFirstSelected();

        // TODO: Send event as well

        Dismiss();
    }

protected:

    int             m_value; // current item index

private:
    DECLARE_EVENT_TABLE()
};

BEGIN_EVENT_TABLE(wxListViewComboPopup, wxListView)
    EVT_MOTION(wxListViewComboPopup::OnMouseMove)
    EVT_LEFT_UP(wxListViewComboPopup::OnMouseClick)
END_EVENT_TABLE()

Here's how you would create and populate it in a dialog constructor:

    wxComboCtrl* comboCtrl = new wxComboCtrl(this,wxID_ANY,wxEmptyString);

    wxListViewComboPopup* popupCtrl = new wxListViewComboPopup();

    // It is important to call SetPopupControl() as soon as possible
    comboCtrl->SetPopupControl(popupCtrl);

    // Populate using wxListView methods
    popupCtrl->InsertItem(popupCtrl->GetItemCount(),wxT("First Item"));
    popupCtrl->InsertItem(popupCtrl->GetItemCount(),wxT("Second Item"));
    popupCtrl->InsertItem(popupCtrl->GetItemCount(),wxT("Third Item"));

Derived from

wxControl
wxWindow
wxEvtHandler
wxObject

Include files

<combo.h>

Window styles

wxCB_READONLY Text will not be editable.
wxCB_SORT Sorts the entries in the list alphabetically.
wxTE_PROCESS_ENTER The control will generate the event wxEVT_COMMAND_TEXT_ENTER (otherwise pressing Enter key is either processed internally by the control or used for navigation between dialog controls). Windows only.
wxCC_SPECIAL_DCLICK Double-clicking triggers a call to popup's OnComboDoubleClick. Actual behaviour is defined by a derived class. For instance, wxOwnerDrawnComboBox will cycle an item. This style only applies if wxCB_READONLY is used as well.
wxCC_STD_BUTTON Drop button will behave more like a standard push button.

See also window styles overview.

Event handling

EVT_TEXT(id, func) Process a wxEVT_COMMAND_TEXT_UPDATED event, when the text changes.
EVT_TEXT_ENTER(id, func) Process a wxEVT_COMMAND_TEXT_ENTER event, when <RETURN> is pressed in the combo control.

See also

wxComboBox, wxChoice, wxOwnerDrawnComboBox, wxComboPopup, wxCommandEvent

Members

wxComboCtrl::wxComboCtrl
wxComboCtrl::~wxComboCtrl
wxComboCtrl::AnimateShow
wxComboCtrl::Create
wxComboCtrl::Copy
wxComboCtrl::Cut
wxComboCtrl::DoSetPopupControl
wxComboCtrl::DoShowPopup
wxComboCtrl::EnablePopupAnimation
wxComboCtrl::GetBitmapDisabled
wxComboCtrl::GetBitmapHover
wxComboCtrl::GetBitmapNormal
wxComboCtrl::GetBitmapPressed
wxComboCtrl::GetButtonSize
wxComboCtrl::GetCustomPaintWidth
wxComboCtrl::GetFeatures
wxComboCtrl::GetInsertionPoint
wxComboCtrl::IsPopupWindowState
wxComboCtrl::GetLastPosition
wxComboCtrl::GetPopupControl
wxComboCtrl::GetPopupWindow
wxComboCtrl::GetTextCtrl
wxComboCtrl::GetTextIndent
wxComboCtrl::GetTextRect
wxComboCtrl::GetValue
wxComboCtrl::HidePopup
wxComboCtrl::IsPopupShown
wxComboCtrl::OnButtonClick
wxComboCtrl::Paste
wxComboCtrl::Remove
wxComboCtrl::Replace
wxComboCtrl::SetButtonBitmaps
wxComboCtrl::SetButtonPosition
wxComboCtrl::SetCustomPaintWidth
wxComboCtrl::SetInsertionPoint
wxComboCtrl::SetInsertionPointEnd
wxComboCtrl::SetPopupAnchor
wxComboCtrl::SetPopupControl
wxComboCtrl::SetPopupExtents
wxComboCtrl::SetPopupMaxHeight
wxComboCtrl::SetPopupMinWidth
wxComboCtrl::SetSelection
wxComboCtrl::SetText
wxComboCtrl::SetTextIndent
wxComboCtrl::SetValue
wxComboCtrl::SetValueWithEvent
wxComboCtrl::ShowPopup
wxComboCtrl::Undo
wxComboCtrl::UseAltPopupWindow


wxComboCtrl::wxComboCtrl

wxComboCtrl()

Default constructor.

wxComboCtrl(wxWindow* parent, wxWindowID id, const wxString& value = "", const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = 0, const wxValidator& validator = wxDefaultValidator, const wxString& name = "comboCtrl")

Constructor, creating and showing a combo control.

Parameters

parent

id

value

pos

size

style

validator

name

See also

wxComboCtrl::Create, wxValidator


wxComboCtrl::~wxComboCtrl

~wxComboCtrl()

Destructor, destroying the combo control.


wxComboCtrl::AnimateShow

virtual bool AnimateShow(const wxRect& rect, int flags)

This member function is not normally called in application code. Instead, it can be implemented in a derived class to create a custom popup animation.

Parameters

Same as in DoShowPopup.

Return value

true if animation finishes before the function returns. false otherwise. In the latter case you need to manually call DoShowPopup after the animation ends.


wxComboCtrl::Create

bool Create(wxWindow* parent, wxWindowID id, const wxString& value = "", const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = 0, const wxValidator& validator = wxDefaultValidator, const wxString& name = "comboCtrl")

Creates the combo control for two-step construction. Derived classes should call or replace this function. See wxComboCtrl::wxComboCtrl for further details.


wxComboCtrl::Copy

void Copy()

Copies the selected text to the clipboard.


wxComboCtrl::Cut

void Cut()

Copies the selected text to the clipboard and removes the selection.


wxComboCtrl::DoSetPopupControl

void DoSetPopupControl(wxComboPopup* popup)

This member function is not normally called in application code. Instead, it can be implemented in a derived class to return default wxComboPopup, incase popup is NULL.

Note: If you have implemented OnButtonClick to do something else than show the popup, then DoSetPopupControl must always return NULL.


wxComboCtrl::DoShowPopup

virtual void DoShowPopup(const wxRect& rect, int flags)

This member function is not normally called in application code. Instead, it must be called in a derived class to make sure popup is properly shown after a popup animation has finished (but only if AnimateShow did not finish the animation within it's function scope).

Parameters

rect

flags

wxComboCtrl::ShowAbove Popup is shown above the control instead of below.
wxComboCtrl::CanDeferShow Showing the popup can be deferred to happen sometime after ShowPopup has finished. In this case, AnimateShow must return false.


wxComboCtrl::EnablePopupAnimation

void EnablePopupAnimation(bool enable = true)

Enables or disables popup animation, if any, depending on the value of the argument.


wxComboCtrl::GetBitmapDisabled

const wxBitmap& GetBitmapDisabled() const

Returns disabled button bitmap that has been set with SetButtonBitmaps.

Return value

A reference to the disabled state bitmap.


wxComboCtrl::GetBitmapHover

const wxBitmap& GetBitmapHover() const

Returns button mouse hover bitmap that has been set with SetButtonBitmaps.

Return value

A reference to the mouse hover state bitmap.


wxComboCtrl::GetBitmapNormal

const wxBitmap& GetBitmapNormal() const

Returns default button bitmap that has been set with SetButtonBitmaps.

Return value

A reference to the normal state bitmap.


wxComboCtrl::GetBitmapPressed

const wxBitmap& GetBitmapPressed() const

Returns depressed button bitmap that has been set with SetButtonBitmaps.

Return value

A reference to the depressed state bitmap.


wxComboCtrl::GetButtonSize

wxSize GetButtonSize()

Returns current size of the dropdown button.


wxComboCtrl::GetCustomPaintWidth

int GetCustomPaintWidth() const

Returns custom painted area in control.

See also

wxComboCtrl::SetCustomPaintWidth.


wxComboCtrl::GetFeatures

static int GetFeatures()

Returns features supported by wxComboCtrl. If needed feature is missing, you need to instead use wxGenericComboCtrl, which however may lack native look and feel (but otherwise sports identical API).

Return value

Value returned is a combination of following flags:

wxComboCtrlFeatures::MovableButton Button can be on either side of the control.
wxComboCtrlFeatures::BitmapButton Button may be replaced with bitmap.
wxComboCtrlFeatures::ButtonSpacing Button can have spacing.
wxComboCtrlFeatures::TextIndent SetTextIndent works.
wxComboCtrlFeatures::PaintControl Combo control itself can be custom painted.
wxComboCtrlFeatures::PaintWritable A variable- width area in front of writable combo control's textctrl can be custom painted.
wxComboCtrlFeatures::Borderless wxNO_BORDER window style works.
wxComboCtrlFeatures::All All of the above.


wxComboCtrl::GetInsertionPoint

long GetInsertionPoint() const

Returns the insertion point for the combo control's text field.

Note: Under wxMSW, this function always returns 0 if the combo control doesn't have the focus.


wxComboCtrl::IsPopupWindowState

bool IsPopupWindowState(int state) const

Returns true if the popup window is in the given state. Possible values are:
wxComboCtrl::Hidden Popup window is hidden.
wxComboCtrl::Animating Popup window is being shown, but the popup animation has not yet finished.
wxComboCtrl::Visible Popup window is fully visible.


wxComboCtrl::GetLastPosition

long GetLastPosition() const

Returns the last position in the combo control text field.


wxComboCtrl::GetPopupControl

wxComboPopup* GetPopupControl()

Returns current popup interface that has been set with SetPopupControl.


wxComboCtrl::GetPopupWindow

wxWindow* GetPopupWindow() const

Returns popup window containing the popup control.


wxComboCtrl::GetTextCtrl

wxTextCtrl* GetTextCtrl() const

Get the text control which is part of the combo control.


wxComboCtrl::GetTextIndent

wxCoord GetTextIndent() const

Returns actual indentation in pixels.


wxComboCtrl::GetTextRect

const wxRect& GetTextRect() const

Returns area covered by the text field (includes everything except borders and the dropdown button).


wxComboCtrl::GetValue

wxString GetValue() const

Returns text representation of the current value. For writable combo control it always returns the value in the text field.


wxComboCtrl::HidePopup

void HidePopup()

Dismisses the popup window.


wxComboCtrl::IsPopupShown

bool IsPopupShown() const

Returns true if the popup is currently shown


wxComboCtrl::OnButtonClick

void OnButtonClick()

Implement in a derived class to define what happens on dropdown button click.

Default action is to show the popup.

Note: If you implement this to do something else than show the popup, you must then also implement DoSetPopupControl to always return NULL.


wxComboCtrl::Paste

void Paste()

Pastes text from the clipboard to the text field.


wxComboCtrl::Remove

void Remove(long from, long to)

Removes the text between the two positions in the combo control text field.

Parameters

from

to


wxComboCtrl::Replace

void Replace(long from, long to, const wxString& value)

Replaces the text between two positions with the given text, in the combo control text field.

Parameters

from

to

text


wxComboCtrl::SetButtonBitmaps

void SetButtonBitmaps(const wxBitmap& bmpNormal, bool pushButtonBg = false, const wxBitmap& bmpPressed = wxNullBitmap, const wxBitmap& bmpHover = wxNullBitmap, const wxBitmap& bmpDisabled = wxNullBitmap)

Sets custom dropdown button graphics.

Parameters

bmpNormal

pushButtonBg bmpPressed bmpHover bmpDisabled


wxComboCtrl::SetButtonPosition

void SetButtonPosition(int width = -1, int height = -1, int side = wxRIGHT, int spacingX = 0)

Sets size and position of dropdown button.

Parameters

width

height side spacingX


wxComboCtrl::SetCustomPaintWidth

void SetCustomPaintWidth(int width)

Set width, in pixels, of custom painted area in control without wxCB_READONLY style. In read-only wxOwnerDrawnComboBox, this is used to indicate area that is not covered by the focus rectangle.


wxComboCtrl::SetInsertionPoint

void SetInsertionPoint(long pos)

Sets the insertion point in the text field.

Parameters

pos


wxComboCtrl::SetInsertionPointEnd

void SetInsertionPointEnd()

Sets the insertion point at the end of the combo control text field.


wxComboCtrl::SetPopupAnchor

void SetPopupAnchor(int anchorSide)

Set side of the control to which the popup will align itself. Valid values are wxLEFT, wxRIGHT and 0. The default value 0 means that the most appropriate side is used (which, currently, is always wxLEFT).


wxComboCtrl::SetPopupControl

void SetPopupControl(wxComboPopup* popup)

Set popup interface class derived from wxComboPopup. This method should be called as soon as possible after the control has been created, unless OnButtonClick has been overridden.


wxComboCtrl::SetPopupExtents

void SetPopupExtents(int extLeft, int extRight)

Extends popup size horizontally, relative to the edges of the combo control.

Parameters

extLeft

extRight

Remarks

Popup minimum width may override arguments.

It is up to the popup to fully take this into account.


wxComboCtrl::SetPopupMaxHeight

void SetPopupMaxHeight(int height)

Sets preferred maximum height of the popup.

Remarks

Value -1 indicates the default.

Also, popup implementation may choose to ignore this.


wxComboCtrl::SetPopupMinWidth

void SetPopupMinWidth(int width)

Sets minimum width of the popup. If wider than combo control, it will extend to the left.

Remarks

Value -1 indicates the default.

Also, popup implementation may choose to ignore this.


wxComboCtrl::SetSelection

void SetSelection(long from, long to)

Selects the text between the two positions, in the combo control text field.

Parameters

from

to


wxComboCtrl::SetText

void SetText(const wxString& value)

Sets the text for the text field without affecting the popup. Thus, unlike SetValue, it works equally well with combo control using wxCB_READONLY style.


wxComboCtrl::SetTextIndent

void SetTextIndent(int indent)

This will set the space in pixels between left edge of the control and the text, regardless whether control is read-only or not. Value -1 can be given to indicate platform default.


wxComboCtrl::SetValue

void SetValue(const wxString& value)

Sets the text for the combo control text field.

NB: For a combo control with wxCB_READONLY style the string must be accepted by the popup (for instance, exist in the dropdown list), otherwise the call to SetValue() is ignored


wxComboCtrl::SetValueWithEvent

void SetValueWithEvent(const wxString& value, bool withEvent = true)

Same as SetValue, but also sends wxCommandEvent of type wxEVT_COMMAND_TEXT_UPDATED if withEvent is true.


wxComboCtrl::ShowPopup

void ShowPopup()

Show the popup.


wxComboCtrl::Undo

void Undo()

Undoes the last edit in the text field. Windows only.


wxComboCtrl::UseAltPopupWindow

void UseAltPopupWindow(bool enable = true)

Enable or disable usage of an alternative popup window, which guarantees ability to focus the popup control, and allows common native controls to function normally. This alternative popup window is usually a wxDialog, and as such, when it is shown, its parent top-level window will appear as if the focus has been lost from it.