wx.combo.ComboCtrl

Inheritance diagram for wx.combo.ComboCtrl:



Description

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 wx.combo.ComboCtrl:

wx.combo.ComboCtrl needs to be told somehow which control to use and this is done by SetPopupControl(). However, we need something more than just a wx.Control in this method as, for example, we need to call SetStringValue (“initial text value”) and wx.Control doesn’t have such method. So we also need a wx.combo.ComboPopup which is an interface which must be implemented by a control to be usable as a popup.

We couldn’t derive wx.combo.ComboPopup from wx.Control 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 wx.ListCtrl popup:

class ListCtrlComboPopup(wx.ListCtrl, wx.combo.ComboPopup):

    def __init__(self):

        # Since we are using multiple inheritance, and don't know yet
        # which window is to be the parent, we'll do 2-phase create of
        # the ListCtrl instead, and call its Create method later in
        # our Create method.  (See Create below.)
        self.PostCreate(wx.PreListCtrl())

        # Also init the ComboPopup base class.
        wx.combo.ComboPopup.__init__(self)


    def AddItem(self, txt):

        self.InsertStringItem(self.GetItemCount(), txt)


    def OnMotion(self, evt):

        item, flags = self.HitTest(evt.GetPosition())
        if item >= 0:
            self.Select(item)
            self.curitem = item


    def OnLeftDown(self, evt):

        self.value = self.curitem
        self.Dismiss()


    # The following methods are those that are overridable from the
    # ComboPopup base class.

    def Init(self):
        """ This is called immediately after construction finishes.  You can
        use self.GetCombo if needed to get to the ComboCtrl instance. """

        self.value = -1
        self.curitem = -1


    def Create(self, parent):
        """ Create the popup child control. Return True for success. """

        wx.ListCtrl.Create(self, parent,
                           style=wx.LC_LIST|wx.LC_SINGLE_SEL|wx.SIMPLE_BORDER)
        self.Bind(wx.EVT_MOTION, self.OnMotion)
        self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)

        return True


    def GetControl(self):
        """ Return the widget that is to be used for the popup. """

        return self

    def SetStringValue(self, val):
        """ Called just prior to displaying the popup, you can use it to
        'select' the current item. """

        idx = self.FindItem(-1, val)

        if idx != wx.NOT_FOUND:
            self.Select(idx)


    def GetStringValue(self):
        """ Return a string representation of the current item. """

        if self.value >= 0:
            return self.GetItemText(self.value)

        return ""


    def OnPopup(self):
        """ Called immediately after the popup is shown. """

        wx.combo.ComboPopup.OnPopup(self)


    def OnDismiss(self):
        " Called when popup is dismissed. """

        wx.combo.ComboPopup.OnDismiss(self)

Here’s how you would create and populate it in a dialog/frame constructor:

cc = wx.combo.ComboCtrl(self, style=0, size=(250,-1))

# Create a Popup
popup = ListCtrlComboPopup()

# Associate them with each other.  This also triggers the
# creation of the ListCtrl.
cc.SetPopupControl(popup)

# Add some items to the listctrl.
for x in range(75):
    popup.AddItem("Item-%02d" % x)

Window Styles

Window Style Description
wx.CB_READONLY Same as wx.CB_DROPDOWN but only the strings specified as the combobox choices can be selected, it is impossible to select (even from a program) a string which is not in the choices list.
wx.CB_SORT Sorts the entries in the list alphabetically.
wx.TE_PROCESS_ENTER The control will generate the event wx.EVT_COMMAND_TEXT_ENTER (otherwise pressing Enter key is either processed internally by the control or used for navigation between dialog controls). Windows only.
wx.combo.CC_SPECIAL_DCLICK Double-clicking triggers a call to popup’s OnComboDoubleClick. Actual behaviour is defined by a derived class. For instance, wx.combo.OwnerDrawnComboBox will cycle an item. This style only applies if wx.CB_READONLY is used as well.
wx.combo.CC_STD_BUTTON Drop button will behave more like a standard push button.

Event Handling

Event Name Description
wx.EVT_TEXT(id, func) Process a wx.wxEVT_COMMAND_TEXT_UPDATED event, when the text changes.
wx.EVT_TEXT_ENTER(id, func) Process a wx.wxEVT_COMMAND_TEXT_ENTER event, when RETURN is pressed in the combo control.

Control Appearance


wxMSW

wxMSW

wxMAC

wxMAC

wxGTK

wxGTK


Class API

Methods

__init__(parent, id=wx.ID_ANY, value="", pos=wx.DefaultPosition, size=wx.DefaultSize, style=0, validator=wx.DefaultValidator, name=wx.PyComboBoxNameStr)

Constructor, creating and showing a combo control.

Parameters:


Returns:

wx.combo.ComboCtrl


AnimateShow(rect, flags)

Implement in derived class to create a drop-down animation. Returns True if finished immediately. Otherwise the popup is only shown when the derived class calls DoShowPopup.

flags are same as for DoShowPopup

Parameters:


Returns:

bool


Copy()
Copies the selected text to the clipboard.

Create(parent, id=-1, pos=wx.DefaultPosition, size=wx.DefaultSize, style=0, validator=wx.DefaultValidator, name=wx.PyComboBoxNameStr)

Creates the combo control for two-step construction. Derived classes should call or replace this function.

Parameters:


Returns:

bool


Cut()
Copies the selected text to the clipboard and removes the selection.

DoShowPopup(rect, 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).

flags may be one of the following bits:

Flags Description
wx.combo.ComboCtrl.ShowBelow Showing popup below the control
wx.combo.ComboCtrl.ShowAbove Showing popup above the control
wx.combo.ComboCtrl.CanDeferShow Can only return True from AnimateShow if this is set

Parameters:

  • rect (wx.Rect): Position to show the popup window at, in screen coordinates.
  • flags (int): Combination of any of the aforementioned flags.

EnablePopupAnimation(enable=True)

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

Parameters:

  • enable (bool)

GetBitmapDisabled()

Returns disabled button bitmap that has been set with SetButtonBitmaps.


Returns:

wx.Bitmap


GetBitmapHover()

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


Returns:

wx.Bitmap


GetBitmapNormal()

Returns default button bitmap that has been set with SetButtonBitmaps.


Returns:

wx.Bitmap


GetBitmapPressed()

Returns depressed button bitmap that has been set with SetButtonBitmaps.


Returns:

wx.Bitmap


GetButton()

Get the dropdown button which is part of the combobox.


Returns:

wx.Window

Note

it’s not necessarily a wx.Button or wx.BitmapButton.


GetButtonSize()

Returns current size of the dropdown button.


Returns:

wx.Size


GetCustomPaintWidth()

Returns custom painted area in control.


Returns:

int


GetFeatures()

Returns features supported by wx.combo.ComboCtrl.

Value returned is a combination of following flags (in wx.combo.ComboCtrlFeatures structure):

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

Returns:

int


GetInsertionPoint()

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


Returns:

long

Note

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


GetInternalFlags()
No docstrings available for this method.

GetLastPosition()

Returns the last position in the combo control text field.


Returns:

long


GetMainWindowOfCompositeControl()
No docstrings available for this method.

GetPopupControl()

Returns current popup interface that has been set with SetPopupControl.


Returns:

wx.combo.ComboPopup


GetPopupWindow()

Returns popup window containing the popup control.


Returns:

wx.Window


GetPopupWindowState()
No docstrings available for this method.

GetTextCtrl()

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


Returns:

wx.TextCtrl


GetTextIndent()

Returns actual indentation in pixels.


Returns:

int


GetTextRect()

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


Returns:

wx.Rect


GetValue()

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


Returns:

string


HidePopup()
Dismisses the popup window.

IsCreated()

Return True if Create has finished.


Returns:

bool


IsKeyPopupToggle(event)

Returns True if given key combination should toggle the popup.

Parameters:


Returns:

bool


IsPopupShown()

Returns True if the popup is currently shown


Returns:

bool


IsPopupWindowState(state)

Returns True if the popup window is in the given state. Possible values are:

Popup State Description
wx.combo.ComboCtrl.Hidden Popup window is hidden.
wx.combo.ComboCtrl.Animating Popup window is being shown, but the popup animation has not yet finished.
wx.combo.ComboCtrl.Visible Popup window is fully visible.

Parameters:

  • state (int)

Returns:

bool


OnButtonClick()
Implement in a derived class to define what happens on dropdown button click. Default action is to show the popup.

OnPopupDismiss()
Common code to be called on popup hide/dismiss.

Paste()
Pastes text from the clipboard to the text field.

PrepareBackground(dc, rect, flags)

Prepare background of combo control or an item in a dropdown list in a way typical on platform. This includes painting the focus/disabled background and setting the clipping region.

Unless you plan to paint your own focus indicator, you should always call this in your PaintComboControl implementation.

In addition, it sets pen and text colour to what looks good and proper against the background.

flags are the same as wx.RendererNative flags:

Flags Description
wx.CONTROL_ISSUBMENU drawing a list item instead of combo control
wx.CONTROL_SELECTED list item is selected
wx.CONTROL_DISABLED control/item is disabled

Parameters:


Remove(from, to)

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

Parameters:

  • from (long): The first position.
  • to (long): The last position.

Replace(from, to)

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

Parameters:

  • from (long): The first position.
  • to (long): The second position.

SetButtonBitmaps(bmpNormal, pushButtonBg=False, bmpPressed=wx.NullBitmap, bmpHover=wx.NullBitmap, bmpDisabled=wx.NullBitmap)

Sets custom dropdown button graphics.

Parameters:

  • bmpNormal (wx.Bitmap): Default button image.
  • pushButtonBg (bool): If True, blank push button background is painted below the image.
  • bmpPressed (wx.Bitmap): Depressed button image.
  • bmpHover (wx.Bitmap): Button image when mouse hovers above it. This should be ignored on platforms and themes that do not generally draw different kind of button on mouse hover.
  • bmpDisabled (wx.Bitmap): Disabled button image.

SetButtonPosition(width=-1, height=-1, side=wx.RIGHT, spacingX=0)

Sets size and position of dropdown button.

Parameters:

  • width (int): Button width. Value <= 0 specifies default.
  • height (int): Button height. Value <= 0 specifies default.
  • side (int): Indicates which side the button will be placed. Value can be wx.LEFT or wx.RIGHT.
  • spacingX (int): Horizontal spacing around the button. Default is 0.

SetCtrlMainWnd(wnd)

Parameters:


SetCustomPaintWidth(width)

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

Parameters:

  • width (int)

SetInsertionPoint(pos)

Sets the insertion point in the text field.

Parameters:

  • pos (int): The new insertion point.

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

SetMark(from, to)

Parameters:

  • from (long)
  • to (long)

SetPopupAnchor(anchorSide)

Set side of the control to which the popup will align itself. Valid values are wx.LEFT, wx.RIGHT and 0.

The default value 0 means that the most appropriate side is used (which, currently, is always wx.LEFT).

Parameters:

  • anchorSide (int)

SetPopupControl(popup)

Set popup interface class derived from wx.combo.ComboPopup.

This method should be called as soon as possible after the control has been created, unless OnButtonClick has been overridden.

Parameters:


SetPopupExtents(extLeft, extRight)

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

Parameters:

  • extLeft (int): How many pixel to extend beyond the left edge of the control. Default is 0.
  • extRight (int): How many pixel to extend beyond the right edge of the control. Default is 0.

Note

Popup minimum width may override arguments.


SetPopupMaxHeight(height)

Sets preferred maximum height of the popup.

Parameters:

  • height (int)

Note

Value -1 indicates the default.


SetPopupMinWidth(width)

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

Parameters:

  • width (int)

Note

Value -1 indicates the default.


SetText(value)

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

Parameters:

  • value (string)

SetTextIndent(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.

Parameters:

  • indent (int)

SetValue(value)

Sets the text for the combo control text field.

Parameters:

  • value (string)

Note

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


SetValueWithEvent(value, withEvent=True)

Same as SetValue, but also sends wx.CommandEvent of type wx.wxEVT_COMMAND_TEXT_UPDATED if withEvent is True.

Parameters:

  • value (string)
  • withEvent (bool)

ShouldDrawFocus()

Returns True if focus indicator should be drawn in the control.


Returns:

bool


ShowPopup()
Show the popup.

Undo()
Undoes the last edit in the text field. Windows only.

UseAltPopupWindow(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 wx.Dialog, and as such, when it is shown, its parent top-level window will appear as if the focus has been lost from it.

Parameters:

  • enable (bool)

Properties

BitmapDisabled
See GetBitmapDisabled
BitmapHover
See GetBitmapHover
BitmapNormal
See GetBitmapNormal
BitmapPressed
See GetBitmapPressed
Button
See GetButton
ButtonSize
See GetButtonSize
CustomPaintWidth
InsertionPoint
PopupControl
Returns the current popup interface that has been set with SetPopupControl.
Returns the popup window containing the popup control.
PopupWindowState
TextCtrl
Get the text control which is part of the combo control.
TextIndent
Returns actual indentation in pixels.
TextRect
Returns area covered by the text field (includes everything except borders and the dropdown button).
Value
Returns text representation of the current value. For writable combo control it always returns the value in the text field.