| 1 | |
| 2 | import wx |
| 3 | import wx.combo |
| 4 | |
| 5 | #---------------------------------------------------------------------- |
| 6 | # This ComboBox class graphically displays the various pen styles that |
| 7 | # are available, making it easy for the user to choose the style they |
| 8 | # want. |
| 9 | |
| 10 | class PenStyleComboBox(wx.combo.OwnerDrawnComboBox): |
| 11 | |
| 12 | # Overridden from OwnerDrawnComboBox, called to draw each |
| 13 | # item in the list |
| 14 | def OnDrawItem(self, dc, rect, item, flags): |
| 15 | if item == wx.NOT_FOUND: |
| 16 | # painting the control, but there is no valid item selected yet |
| 17 | return |
| 18 | |
| 19 | r = wx.Rect(*rect) # make a copy |
| 20 | r.Deflate(3, 5) |
| 21 | |
| 22 | penStyle = wx.SOLID |
| 23 | if item == 1: |
| 24 | penStyle = wx.TRANSPARENT |
| 25 | elif item == 2: |
| 26 | penStyle = wx.DOT |
| 27 | elif item == 3: |
| 28 | penStyle = wx.LONG_DASH |
| 29 | elif item == 4: |
| 30 | penStyle = wx.SHORT_DASH |
| 31 | elif item == 5: |
| 32 | penStyle = wx.DOT_DASH |
| 33 | elif item == 6: |
| 34 | penStyle = wx.BDIAGONAL_HATCH |
| 35 | elif item == 7: |
| 36 | penStyle = wx.CROSSDIAG_HATCH |
| 37 | elif item == 8: |
| 38 | penStyle = wx.FDIAGONAL_HATCH |
| 39 | elif item == 9: |
| 40 | penStyle = wx.CROSS_HATCH |
| 41 | elif item == 10: |
| 42 | penStyle = wx.HORIZONTAL_HATCH |
| 43 | elif item == 11: |
| 44 | penStyle = wx.VERTICAL_HATCH |
| 45 | |
| 46 | pen = wx.Pen(dc.GetTextForeground(), 3, penStyle) |
| 47 | dc.SetPen(pen) |
| 48 | |
| 49 | if flags & wx.combo.ODCB_PAINTING_CONTROL: |
| 50 | # for painting the control itself |
| 51 | dc.DrawLine( r.x+5, r.y+r.height/2, r.x+r.width - 5, r.y+r.height/2 ) |
| 52 | |
| 53 | else: |
| 54 | # for painting the items in the popup |
| 55 | dc.DrawText(self.GetString( item ), |
| 56 | r.x + 3, |
| 57 | (r.y + 0) + ( (r.height/2) - dc.GetCharHeight() )/2 |
| 58 | ) |
| 59 | dc.DrawLine( r.x+5, r.y+((r.height/4)*3)+1, r.x+r.width - 5, r.y+((r.height/4)*3)+1 ) |
| 60 | |
| 61 | |
| 62 | # Overridden from OwnerDrawnComboBox, called for drawing the |
| 63 | # background area of each item. |
| 64 | def OnDrawBackground(self, dc, rect, item, flags): |
| 65 | # If the item is selected, or its item # iseven, or we are painting the |
| 66 | # combo control itself, then use the default rendering. |
| 67 | if (item & 1 == 0 or flags & (wx.combo.ODCB_PAINTING_CONTROL | |
| 68 | wx.combo.ODCB_PAINTING_SELECTED)): |
| 69 | wx.combo.OwnerDrawnComboBox.OnDrawBackground(self, dc, rect, item, flags) |
| 70 | return |
| 71 | |
| 72 | # Otherwise, draw every other background with different colour. |
| 73 | bgCol = wx.Colour(240,240,250) |
| 74 | dc.SetBrush(wx.Brush(bgCol)) |
| 75 | dc.SetPen(wx.Pen(bgCol)) |
| 76 | dc.DrawRectangleRect(rect); |
| 77 | |
| 78 | |
| 79 | |
| 80 | # Overridden from OwnerDrawnComboBox, should return the height |
| 81 | # needed to display an item in the popup, or -1 for default |
| 82 | def OnMeasureItem(self, item): |
| 83 | # Simply demonstrate the ability to have variable-height items |
| 84 | if item & 1: |
| 85 | return 36 |
| 86 | else: |
| 87 | return 24 |
| 88 | |
| 89 | # Overridden from OwnerDrawnComboBox. Callback for item width, or |
| 90 | # -1 for default/undetermined |
| 91 | def OnMeasureItemWidth(self, item): |
| 92 | return -1; # default - will be measured from text width |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | #---------------------------------------------------------------------- |
| 98 | |
| 99 | class TestPanel(wx.Panel): |
| 100 | def __init__(self, parent, log): |
| 101 | self.log = log |
| 102 | wx.Panel.__init__(self, parent, -1) |
| 103 | |
| 104 | penStyles = [ |
| 105 | "Solid", |
| 106 | "Transparent", |
| 107 | "Dot", |
| 108 | "Long Dash", |
| 109 | "Short Dash", |
| 110 | "Dot Dash", |
| 111 | "Backward Diagonal Hatch", |
| 112 | "Cross-diagonal Hatch", |
| 113 | "Forward Diagonal Hatch", |
| 114 | "Cross Hatch", |
| 115 | "Horizontal Hatch", |
| 116 | "Vertical Hatch", |
| 117 | ] |
| 118 | |
| 119 | wx.StaticText(self, -1, "Pen Styles:", (20, 20)) |
| 120 | pscb = PenStyleComboBox(self, choices=penStyles, style=wx.CB_READONLY, |
| 121 | pos=(20,40), size=(250, -1)) |
| 122 | |
| 123 | self.pscb = pscb |
| 124 | |
| 125 | #---------------------------------------------------------------------- |
| 126 | |
| 127 | def runTest(frame, nb, log): |
| 128 | win = TestPanel(nb, log) |
| 129 | return win |
| 130 | |
| 131 | #---------------------------------------------------------------------- |
| 132 | |
| 133 | |
| 134 | |
| 135 | overview = """<html><body> |
| 136 | <h2><center>wx.combo.OwnerDrawnComboBox</center></h2> |
| 137 | |
| 138 | wx.combo.OwnerDrawnComboBox is a combobox with owner-drawn list |
| 139 | items. In essence, it is a wx.combo.ComboCtrl with wx.VListBox popup and |
| 140 | wx.ControlWithItems interface. |
| 141 | |
| 142 | </body></html> |
| 143 | """ |
| 144 | |
| 145 | |
| 146 | |
| 147 | if __name__ == '__main__': |
| 148 | import sys,os |
| 149 | import run |
| 150 | run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:]) |
| 151 | |