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();

    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.