1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
#---------------------------------------------------------------------------
# Name: samples/combo/combo1.py
# Author: Robin Dunn
#
# Created: 1-June-2012
# Copyright: (c) 2012-2020 by Total Control Software
# License: wxWindows License
#---------------------------------------------------------------------------
"""
A simple test case for wx.ComboCtrl using a wx.ListCtrl for the popup
"""
import wx
class NullLog:
def write(self, *args):
#print(' '.join(args))
pass
#----------------------------------------------------------------------
# This class is used to provide an interface between a ComboCtrl and the
# ListCtrl that is used as the popup for the combo widget.
class ListCtrlComboPopup(wx.ComboPopup):
def __init__(self):
wx.ComboPopup.__init__(self)
self.log = NullLog()
self.lc = None
def AddItem(self, txt):
self.lc.InsertItem(self.lc.GetItemCount(), txt)
def OnMotion(self, evt):
item, flags = self.lc.HitTest(evt.GetPosition())
if item >= 0:
self.lc.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. Most of them are not required, but all
# are shown here for demonstration purposes.
# This is called immediately after construction finishes. You can
# use self.GetCombo if needed to get to the ComboCtrl instance.
def Init(self):
self.log.write("ListCtrlComboPopup.Init")
self.value = -1
self.curitem = -1
# Create the popup child control. Return true for success.
def Create(self, parent):
self.log.write("ListCtrlComboPopup.Create")
self.lc = wx.ListCtrl(parent, style=wx.LC_LIST|wx.LC_SINGLE_SEL|wx.SIMPLE_BORDER)
self.lc.Bind(wx.EVT_MOTION, self.OnMotion)
self.lc.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
return True
# Return the widget that is to be used for the popup
def GetControl(self):
#self.log.write("ListCtrlComboPopup.GetControl")
return self.lc
# Called just prior to displaying the popup, you can use it to
# 'select' the current item.
def SetStringValue(self, val):
self.log.write("ListCtrlComboPopup.SetStringValue")
idx = self.lc.FindItem(-1, val)
if idx != wx.NOT_FOUND:
self.lc.Select(idx)
# Return a string representation of the current item.
def GetStringValue(self):
self.log.write("ListCtrlComboPopup.GetStringValue: %d" % self.value)
if self.value >= 0:
return self.lc.GetItemText(self.value)
return ""
# Called immediately after the popup is shown
def OnPopup(self):
self.log.write("ListCtrlComboPopup.OnPopup")
wx.ComboPopup.OnPopup(self)
# Called when popup is dismissed
def OnDismiss(self):
self.log.write("ListCtrlComboPopup.OnDismiss")
wx.ComboPopup.OnDismiss(self)
# This is called to custom paint in the combo control itself
# (ie. not the popup). Default implementation draws value as
# string.
def PaintComboControl(self, dc, rect):
self.log.write("ListCtrlComboPopup.PaintComboControl")
wx.ComboPopup.PaintComboControl(self, dc, rect)
# Receives key events from the parent ComboCtrl. Events not
# handled should be skipped, as usual.
def OnComboKeyEvent(self, event):
self.log.write("ListCtrlComboPopup.OnComboKeyEvent")
wx.ComboPopup.OnComboKeyEvent(self, event)
# Implement if you need to support special action when user
# double-clicks on the parent wxComboCtrl.
def OnComboDoubleClick(self):
self.log.write("ListCtrlComboPopup.OnComboDoubleClick")
wx.ComboPopup.OnComboDoubleClick(self)
# Return final size of popup. Called on every popup, just prior to OnPopup.
# minWidth = preferred minimum width for window
# prefHeight = preferred height. Only applies if > 0,
# maxHeight = max height for window, as limited by screen size
# and should only be rounded down, if necessary.
def GetAdjustedSize(self, minWidth, prefHeight, maxHeight):
self.log.write("ListCtrlComboPopup.GetAdjustedSize: %d, %d, %d" % (minWidth, prefHeight, maxHeight))
return wx.ComboPopup.GetAdjustedSize(self, minWidth, prefHeight, maxHeight)
# Return true if you want delay the call to Create until the popup
# is shown for the first time. It is more efficient, but note that
# it is often more convenient to have the control created
# immediately.
# Default returns false.
def LazyCreate(self):
self.log.write("ListCtrlComboPopup.LazyCreate")
return wx.ComboPopup.LazyCreate(self)
#----------------------------------------------------------------------
class TestFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, title='combo1.py')
pnl = wx.Panel(self)
# Create a ComboCtrl
cc = self.cc = wx.ComboCtrl(pnl, pos=(10,10), size=(275,-1))
cc.SetHint('Click the button -->')
# 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)
#----------------------------------------------------------------------
if __name__ == '__main__':
app = wx.App(False)
frm = TestFrame(None)
frm.Show()
app.MainLoop()
|