1 #----------------------------------------------------------------------------
3 # Purpose: Testing lots of stuff, controls, window types, etc.
5 # Author: Robin Dunn & Gary Dumer
9 # Copyright: (c) 1998 by Total Control Software
10 # Licence: wxWindows license
11 #----------------------------------------------------------------------------
13 from wxPython
.wx
import *
14 from wxPython
.lib
.mixins
.listctrl
import wxColumnSorterMixin
, wxListCtrlAutoWidthMixin
16 #---------------------------------------------------------------------------
19 1 : ("Bad English", "The Price Of Love", "Rock"),
20 2 : ("DNA featuring Suzanne Vega", "Tom's Diner", "Rock"),
21 3 : ("George Michael", "Praying For Time", "Rock"),
22 4 : ("Gloria Estefan", "Here We Are", "Rock"),
23 5 : ("Linda Ronstadt", "Don't Know Much", "Rock"),
24 6 : ("Michael Bolton", "How Am I Supposed To Live Without You", "Blues"),
25 7 : ("Paul Young", "Oh Girl", "Rock"),
26 8 : ("Paula Abdul", "Opposites Attract", "Rock"),
27 9 : ("Richard Marx", "Should've Known Better", "Rock"),
28 10: ("Rod Stewart", "Forever Young", "Rock"),
29 11: ("Roxette", "Dangerous", "Rock"),
30 12: ("Sheena Easton", "The Lover In Me", "Rock"),
31 13: ("Sinead O'Connor", "Nothing Compares 2 U", "Rock"),
32 14: ("Stevie B.", "Because I Love You", "Rock"),
33 15: ("Taylor Dayne", "Love Will Lead You Back", "Rock"),
34 16: ("The Bangles", "Eternal Flame", "Rock"),
35 17: ("Wilson Phillips", "Release Me", "Rock"),
36 18: ("Billy Joel", "Blonde Over Blue", "Rock"),
37 19: ("Billy Joel", "Famous Last Words", "Rock"),
38 20: ("Billy Joel", "Lullabye (Goodnight, My Angel)", "Rock"),
39 21: ("Billy Joel", "The River Of Dreams", "Rock"),
40 22: ("Billy Joel", "Two Thousand Years", "Rock"),
41 23: ("Janet Jackson", "Alright", "Rock"),
42 24: ("Janet Jackson", "Black Cat", "Rock"),
43 25: ("Janet Jackson", "Come Back To Me", "Rock"),
44 26: ("Janet Jackson", "Escapade", "Rock"),
45 27: ("Janet Jackson", "Love Will Never Do (Without You)", "Rock"),
46 28: ("Janet Jackson", "Miss You Much", "Rock"),
47 29: ("Janet Jackson", "Rhythm Nation", "Rock"),
48 30: ("Janet Jackson", "State Of The World", "Rock"),
49 31: ("Janet Jackson", "The Knowledge", "Rock"),
50 32: ("Spyro Gyra", "End of Romanticism", "Jazz"),
51 33: ("Spyro Gyra", "Heliopolis", "Jazz"),
52 34: ("Spyro Gyra", "Jubilee", "Jazz"),
53 35: ("Spyro Gyra", "Little Linda", "Jazz"),
54 36: ("Spyro Gyra", "Morning Dance", "Jazz"),
55 37: ("Spyro Gyra", "Song for Lorraine", "Jazz"),
56 38: ("Yes", "Owner Of A Lonely Heart", "Rock"),
57 39: ("Yes", "Rhythm Of Love", "Rock"),
58 40: ("Cusco", "Dream Catcher", "New Age"),
59 41: ("Cusco", "Geronimos Laughter", "New Age"),
60 42: ("Cusco", "Ghost Dance", "New Age"),
61 43: ("Blue Man Group", "Drumbone", "New Age"),
62 44: ("Blue Man Group", "Endless Column", "New Age"),
63 45: ("Blue Man Group", "Klein Mandelbrot", "New Age"),
64 46: ("Kenny G", "Silhouette", "Jazz"),
65 47: ("Sade", "Smooth Operator", "Jazz"),
66 48: ("David Arkenstone", "Papillon (On The Wings Of The Butterfly)", "New Age"),
67 49: ("David Arkenstone", "Stepping Stars", "New Age"),
68 50: ("David Arkenstone", "Carnation Lily Lily Rose", "New Age"),
69 51: ("David Lanz", "Behind The Waterfall", "New Age"),
70 52: ("David Lanz", "Cristofori's Dream", "New Age"),
71 53: ("David Lanz", "Heartsounds", "New Age"),
72 54: ("David Lanz", "Leaves on the Seine", "New Age"),
78 class TestListCtrl(wxListCtrl
, wxListCtrlAutoWidthMixin
):
79 def __init__(self
, parent
, ID
, pos
=wxDefaultPosition
,
80 size
=wxDefaultSize
, style
=0):
81 wxListCtrl
.__init
__(self
, parent
, ID
, pos
, size
, style
)
82 wxListCtrlAutoWidthMixin
.__init
__(self
)
86 class TestListCtrlPanel(wxPanel
, wxColumnSorterMixin
):
87 def __init__(self
, parent
, log
):
88 wxPanel
.__init
__(self
, parent
, -1, style
=wxWANTS_CHARS
)
93 self
.il
= wxImageList(16, 16)
95 self
.idx1
= self
.il
.Add(images
.getSmilesBitmap())
96 self
.sm_up
= self
.il
.Add(images
.getSmallUpArrowBitmap())
97 self
.sm_dn
= self
.il
.Add(images
.getSmallDnArrowBitmap())
99 self
.list = TestListCtrl(self
, tID
,
100 style
=wxLC_REPORT | wxSUNKEN_BORDER
103 #| wxLC_VRULES | wxLC_HRULES
105 self
.list.SetImageList(self
.il
, wxIMAGE_LIST_SMALL
)
109 # Now that the list exists we can init the other base class,
110 # see wxPython/lib/mixins/listctrl.py
111 self
.itemDataMap
= musicdata
112 wxColumnSorterMixin
.__init
__(self
, 3)
113 #self.SortListItems(0, True)
115 EVT_SIZE(self
, self
.OnSize
)
116 EVT_LIST_ITEM_SELECTED(self
, tID
, self
.OnItemSelected
)
117 EVT_LIST_ITEM_DESELECTED(self
, tID
, self
.OnItemDeselected
)
118 EVT_LIST_ITEM_ACTIVATED(self
, tID
, self
.OnItemActivated
)
119 EVT_LIST_DELETE_ITEM(self
, tID
, self
.OnItemDelete
)
120 EVT_LIST_COL_CLICK(self
, tID
, self
.OnColClick
)
121 EVT_LIST_COL_RIGHT_CLICK(self
, tID
, self
.OnColRightClick
)
122 EVT_LIST_COL_BEGIN_DRAG(self
, tID
, self
.OnColBeginDrag
)
123 EVT_LIST_COL_DRAGGING(self
, tID
, self
.OnColDragging
)
124 EVT_LIST_COL_END_DRAG(self
, tID
, self
.OnColEndDrag
)
125 EVT_LIST_BEGIN_LABEL_EDIT(self
, tID
, self
.OnBeginEdit
)
127 EVT_LEFT_DCLICK(self
.list, self
.OnDoubleClick
)
128 EVT_RIGHT_DOWN(self
.list, self
.OnRightDown
)
131 EVT_COMMAND_RIGHT_CLICK(self
.list, tID
, self
.OnRightClick
)
134 EVT_RIGHT_UP(self
.list, self
.OnRightClick
)
137 def PopulateList(self
):
139 # for normal, simple columns, you can add them like this:
140 self
.list.InsertColumn(0, "Artist")
141 self
.list.InsertColumn(1, "Title", wxLIST_FORMAT_RIGHT
)
142 self
.list.InsertColumn(2, "Genre")
144 # but since we want images on the column header we have to do it the hard way:
146 info
.m_mask
= wxLIST_MASK_TEXT | wxLIST_MASK_IMAGE | wxLIST_MASK_FORMAT
149 info
.m_text
= "Artist"
150 self
.list.InsertColumnInfo(0, info
)
152 info
.m_format
= wxLIST_FORMAT_RIGHT
153 info
.m_text
= "Title"
154 self
.list.InsertColumnInfo(1, info
)
157 info
.m_text
= "Genre"
158 self
.list.InsertColumnInfo(2, info
)
160 items
= musicdata
.items()
161 for x
in range(len(items
)):
163 self
.list.InsertImageStringItem(x
, data
[0], self
.idx1
)
164 self
.list.SetStringItem(x
, 1, data
[1])
165 self
.list.SetStringItem(x
, 2, data
[2])
166 self
.list.SetItemData(x
, key
)
168 self
.list.SetColumnWidth(0, wxLIST_AUTOSIZE
)
169 self
.list.SetColumnWidth(1, wxLIST_AUTOSIZE
)
170 self
.list.SetColumnWidth(2, 100)
172 # show how to select an item
173 self
.list.SetItemState(5, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
)
175 # show how to change the colour of a couple items
176 item
= self
.list.GetItem(1)
177 item
.SetTextColour(wxBLUE
)
178 self
.list.SetItem(item
)
179 item
= self
.list.GetItem(4)
180 item
.SetTextColour(wxRED
)
181 self
.list.SetItem(item
)
186 # Used by the wxColumnSorterMixin, see wxPython/lib/mixins/listctrl.py
187 def GetListCtrl(self
):
190 # Used by the wxColumnSorterMixin, see wxPython/lib/mixins/listctrl.py
191 def GetSortImages(self
):
192 return (self
.sm_dn
, self
.sm_up
)
195 def OnRightDown(self
, event
):
196 self
.x
= event
.GetX()
197 self
.y
= event
.GetY()
198 self
.log
.WriteText("x, y = %s\n" % str((self
.x
, self
.y
)))
199 item
, flags
= self
.list.HitTest((self
.x
, self
.y
))
200 if flags
& wxLIST_HITTEST_ONITEM
:
201 self
.list.Select(item
)
205 def getColumnText(self
, index
, col
):
206 item
= self
.list.GetItem(index
, col
)
207 return item
.GetText()
210 def OnItemSelected(self
, event
):
211 ##print event.GetItem().GetTextColour()
212 self
.currentItem
= event
.m_itemIndex
213 self
.log
.WriteText("OnItemSelected: %s, %s, %s, %s\n" %
215 self
.list.GetItemText(self
.currentItem
),
216 self
.getColumnText(self
.currentItem
, 1),
217 self
.getColumnText(self
.currentItem
, 2)))
218 if self
.currentItem
== 10:
219 self
.log
.WriteText("OnItemSelected: Veto'd selection\n")
220 #event.Veto() # doesn't work
222 self
.list.SetItemState(10, 0, wxLIST_STATE_SELECTED
)
226 def OnItemDeselected(self
, evt
):
228 self
.log
.WriteText("OnItemDeselected: %d" % evt
.m_itemIndex
)
230 # Show how to reselect something we don't want deselected
231 if evt
.m_itemIndex
== 11:
232 wxCallAfter(self
.list.SetItemState
, 11, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
)
235 def OnItemActivated(self
, event
):
236 self
.currentItem
= event
.m_itemIndex
237 self
.log
.WriteText("OnItemActivated: %s\nTopItem: %s" %
238 (self
.list.GetItemText(self
.currentItem
), self
.list.GetTopItem()))
240 def OnBeginEdit(self
, event
):
241 self
.log
.WriteText("OnBeginEdit")
244 def OnItemDelete(self
, event
):
245 self
.log
.WriteText("OnItemDelete\n")
247 def OnColClick(self
, event
):
248 self
.log
.WriteText("OnColClick: %d\n" % event
.GetColumn())
250 def OnColRightClick(self
, event
):
251 item
= self
.list.GetColumn(event
.GetColumn())
252 self
.log
.WriteText("OnColRightClick: %d %s\n" %
253 (event
.GetColumn(), (item
.GetText(), item
.GetAlign(),
254 item
.GetWidth(), item
.GetImage())))
256 def OnColBeginDrag(self
, event
):
257 self
.log
.WriteText("OnColBeginDrag\n")
258 ## Show how to not allow a column to be resized
259 #if event.GetColumn() == 0:
263 def OnColDragging(self
, event
):
264 self
.log
.WriteText("OnColDragging\n")
266 def OnColEndDrag(self
, event
):
267 self
.log
.WriteText("OnColEndDrag\n")
269 def OnDoubleClick(self
, event
):
270 self
.log
.WriteText("OnDoubleClick item %s\n" % self
.list.GetItemText(self
.currentItem
))
273 def OnRightClick(self
, event
):
274 self
.log
.WriteText("OnRightClick %s\n" % self
.list.GetItemText(self
.currentItem
))
276 # only do this part the first time so the events are only bound once
277 if not hasattr(self
, "popupID1"):
278 self
.popupID1
= wxNewId()
279 self
.popupID2
= wxNewId()
280 self
.popupID3
= wxNewId()
281 self
.popupID4
= wxNewId()
282 self
.popupID5
= wxNewId()
283 self
.popupID6
= wxNewId()
284 EVT_MENU(self
, self
.popupID1
, self
.OnPopupOne
)
285 EVT_MENU(self
, self
.popupID2
, self
.OnPopupTwo
)
286 EVT_MENU(self
, self
.popupID3
, self
.OnPopupThree
)
287 EVT_MENU(self
, self
.popupID4
, self
.OnPopupFour
)
288 EVT_MENU(self
, self
.popupID5
, self
.OnPopupFive
)
289 EVT_MENU(self
, self
.popupID6
, self
.OnPopupSix
)
294 menu
.Append(self
.popupID1
, "FindItem tests")
295 menu
.Append(self
.popupID2
, "Iterate Selected")
296 menu
.Append(self
.popupID3
, "ClearAll and repopulate")
297 menu
.Append(self
.popupID4
, "DeleteAllItems")
298 menu
.Append(self
.popupID5
, "GetItem")
299 menu
.Append(self
.popupID6
, "Edit")
301 # Popup the menu. If an item is selected then its handler
302 # will be called before PopupMenu returns.
303 self
.PopupMenu(menu
, wxPoint(self
.x
, self
.y
))
307 def OnPopupOne(self
, event
):
308 self
.log
.WriteText("Popup one\n")
309 print "FindItem:", self
.list.FindItem(-1, "Roxette")
310 print "FindItemData:", self
.list.FindItemData(-1, 11)
312 def OnPopupTwo(self
, event
):
313 self
.log
.WriteText("Selected items:\n")
314 index
= self
.list.GetFirstSelected()
316 self
.log
.WriteText(" %s: %s\n" % (self
.list.GetItemText(index
), self
.getColumnText(index
, 1)))
317 index
= self
.list.GetNextSelected(index
)
320 def OnPopupThree(self
, event
):
321 self
.log
.WriteText("Popup three\n")
323 wxCallAfter(self
.PopulateList
)
325 def OnPopupFour(self
, event
):
326 self
.list.DeleteAllItems()
328 def OnPopupFive(self
, event
):
329 item
= self
.list.GetItem(self
.currentItem
)
330 print item
.m_text
, item
.m_itemId
, self
.list.GetItemData(self
.currentItem
)
332 def OnPopupSix(self
, event
):
333 self
.list.EditLabel(self
.currentItem
)
336 def OnSize(self
, event
):
337 w
,h
= self
.GetClientSizeTuple()
338 self
.list.SetDimensions(0, 0, w
, h
)
345 #---------------------------------------------------------------------------
347 def runTest(frame
, nb
, log
):
348 win
= TestListCtrlPanel(nb
, log
)
351 #---------------------------------------------------------------------------
357 A list control presents lists in a number of formats: list view, report view, icon view and small icon view. Elements are numbered from zero.
365 if __name__
== '__main__':
368 run
.main(['', os
.path
.basename(sys
.argv
[0])])