wxcomboctrla 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 include files <combo.h> window styles
see also window styles overview. event handling
see also wxcombobox, wxchoice, wxownerdrawncombobox, wxcombopopup, wxcommandevent members
wxcomboctrl::wxcomboctrl
wxcomboctrl::wxcomboctrlwxcomboctrl() 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::animateshowvirtual 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::createbool 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::copyvoid copy() copies the selected text to the clipboard.
wxcomboctrl::cutvoid cut() copies the selected text to the clipboard and removes the selection.
wxcomboctrl::dosetpopupcontrolvoid 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::doshowpopupvirtual 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::enablepopupanimationvoid enablepopupanimation(bool enable = true) enables or disables popup animation, if any, depending on the value of the argument.
wxcomboctrl::getbitmapdisabledconst wxbitmap& getbitmapdisabled() const returns disabled button bitmap that has been set with setbuttonbitmaps. return value a reference to the disabled state bitmap.
wxcomboctrl::getbitmaphoverconst 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::getbitmapnormalconst wxbitmap& getbitmapnormal() const returns default button bitmap that has been set with setbuttonbitmaps. return value a reference to the normal state bitmap.
wxcomboctrl::getbitmappressedconst wxbitmap& getbitmappressed() const returns depressed button bitmap that has been set with setbuttonbitmaps. return value a reference to the depressed state bitmap.
wxcomboctrl::getbuttonsizewxsize getbuttonsize() returns current size of the dropdown button.
wxcomboctrl::getcustompaintwidthint getcustompaintwidth() const returns custom painted area in control. see also wxcomboctrl::setcustompaintwidth.
wxcomboctrl::getfeaturesstatic 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:
wxcomboctrl::getinsertionpointlong 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::ispopupwindowstatebool ispopupwindowstate(int state) const returns true if the popup window is in the given state. possible values are:
wxcomboctrl::getlastpositionlong getlastposition() const returns the last position in the combo control text field.
wxcomboctrl::getpopupcontrolwxcombopopup* getpopupcontrol() returns current popup interface that has been set with setpopupcontrol.
wxcomboctrl::getpopupwindowwxwindow* getpopupwindow() const returns popup window containing the popup control.
wxcomboctrl::gettextctrlwxtextctrl* gettextctrl() const get the text control which is part of the combo control.
wxcomboctrl::gettextindentwxcoord gettextindent() const returns actual indentation in pixels.
wxcomboctrl::gettextrectconst wxrect& gettextrect() const returns area covered by the text field (includes everything except borders and the dropdown button).
wxcomboctrl::getvaluewxstring getvalue() const returns text representation of the current value. for writable combo control it always returns the value in the text field.
wxcomboctrl::hidepopupvoid hidepopup() dismisses the popup window.
wxcomboctrl::ispopupshownbool ispopupshown() const returns true if the popup is currently shown
wxcomboctrl::onbuttonclickvoid 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::pastevoid paste() pastes text from the clipboard to the text field.
wxcomboctrl::removevoid remove(long from, long to) removes the text between the two positions in the combo control text field. parameters from
to
wxcomboctrl::replacevoid 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::setbuttonbitmapsvoid 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
wxcomboctrl::setbuttonpositionvoid setbuttonposition(int width = -1, int height = -1, int side = wxright, int spacingx = 0) sets size and position of dropdown button. parameters width
wxcomboctrl::setcustompaintwidthvoid 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::setinsertionpointvoid setinsertionpoint(long pos) sets the insertion point in the text field. parameters pos
wxcomboctrl::setinsertionpointendvoid setinsertionpointend() sets the insertion point at the end of the combo control text field.
wxcomboctrl::setpopupanchorvoid 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::setpopupcontrolvoid 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::setpopupextentsvoid setpopupextents(int extleft, int extright) extends popup size horizontally, relative to the edges of the combo control. parameters extleft
remarks popup minimum width may override arguments. it is up to the popup to fully take this into account.
wxcomboctrl::setpopupmaxheightvoid 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::setpopupminwidthvoid 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::setselectionvoid setselection(long from, long to) selects the text between the two positions, in the combo control text field. parameters from
to
wxcomboctrl::settextvoid 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::settextindentvoid 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::setvaluevoid 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::setvaluewitheventvoid 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::showpopupvoid showpopup() show the popup.
wxcomboctrl::undovoid undo() undoes the last edit in the text field. windows only.
wxcomboctrl::usealtpopupwindowvoid 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.
|