]>
Commit | Line | Data |
---|---|---|
cf694132 RD |
1 | #---------------------------------------------------------------------------- |
2 | # Name: ListCtrl.py | |
3 | # Purpose: Testing lots of stuff, controls, window types, etc. | |
4 | # | |
5 | # Author: Robin Dunn & Gary Dumer | |
6 | # | |
7 | # Created: | |
8 | # RCS-ID: $Id$ | |
9 | # Copyright: (c) 1998 by Total Control Software | |
10 | # Licence: wxWindows license | |
11 | #---------------------------------------------------------------------------- | |
12 | ||
13 | from wxPython.wx import * | |
726fc00a | 14 | from wxPython.lib.mixins.listctrl import wxColumnSorterMixin, wxListCtrlAutoWidthMixin |
cf694132 RD |
15 | |
16 | #--------------------------------------------------------------------------- | |
17 | ||
dcd38683 RD |
18 | musicdata = { |
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"), | |
cd096152 RD |
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"), | |
b1cfebd9 RD |
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"), | |
dcd38683 RD |
73 | } |
74 | ||
96bfd053 | 75 | import images |
dcd38683 | 76 | |
726fc00a RD |
77 | |
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) | |
83 | ||
84 | ||
85 | ||
6d19860f | 86 | class TestListCtrlPanel(wxPanel, wxColumnSorterMixin): |
cf694132 | 87 | def __init__(self, parent, log): |
c368d904 | 88 | wxPanel.__init__(self, parent, -1, style=wxWANTS_CHARS) |
cf694132 RD |
89 | |
90 | self.log = log | |
f6bcfd97 | 91 | tID = wxNewId() |
cf694132 RD |
92 | |
93 | self.il = wxImageList(16, 16) | |
6d19860f | 94 | |
3979290c | 95 | self.idx1 = self.il.Add(images.getSmilesBitmap()) |
00887b39 RD |
96 | self.sm_up = self.il.Add(images.getSmallUpArrowBitmap()) |
97 | self.sm_dn = self.il.Add(images.getSmallDnArrowBitmap()) | |
98 | ||
726fc00a | 99 | self.list = TestListCtrl(self, tID, |
c7e7022c | 100 | style=wxLC_REPORT|wxSUNKEN_BORDER)#|wxLC_VRULES|wxLC_HRULES) |
cf694132 RD |
101 | self.list.SetImageList(self.il, wxIMAGE_LIST_SMALL) |
102 | ||
3979290c RD |
103 | self.PopulateList() |
104 | ||
105 | # Now that the list exists we can init the other base class, | |
106 | # see wxPython/lib/mixins/listctrl.py | |
107 | self.itemDataMap = musicdata | |
108 | wxColumnSorterMixin.__init__(self, 3) | |
109 | #self.SortListItems(0, true) | |
110 | ||
111 | EVT_SIZE(self, self.OnSize) | |
112 | EVT_LIST_ITEM_SELECTED(self, tID, self.OnItemSelected) | |
113 | EVT_LIST_ITEM_DESELECTED(self, tID, self.OnItemDeselected) | |
114 | EVT_LIST_ITEM_ACTIVATED(self, tID, self.OnItemActivated) | |
115 | EVT_LIST_DELETE_ITEM(self, tID, self.OnItemDelete) | |
116 | EVT_LIST_COL_CLICK(self, tID, self.OnColClick) | |
117 | EVT_LIST_COL_RIGHT_CLICK(self, tID, self.OnColRightClick) | |
118 | EVT_LIST_COL_BEGIN_DRAG(self, tID, self.OnColBeginDrag) | |
119 | EVT_LIST_COL_DRAGGING(self, tID, self.OnColDragging) | |
120 | EVT_LIST_COL_END_DRAG(self, tID, self.OnColEndDrag) | |
121 | ||
122 | EVT_LEFT_DCLICK(self.list, self.OnDoubleClick) | |
123 | EVT_RIGHT_DOWN(self.list, self.OnRightDown) | |
124 | ||
125 | # for wxMSW | |
126 | EVT_COMMAND_RIGHT_CLICK(self.list, tID, self.OnRightClick) | |
127 | ||
128 | # for wxGTK | |
129 | EVT_RIGHT_UP(self.list, self.OnRightClick) | |
130 | ||
cf694132 | 131 | |
3979290c | 132 | def PopulateList(self): |
6d19860f | 133 | if 0: |
726fc00a | 134 | # for normal, simple columns, you can add them like this: |
6d19860f RD |
135 | self.list.InsertColumn(0, "Artist") |
136 | self.list.InsertColumn(1, "Title", wxLIST_FORMAT_RIGHT) | |
137 | self.list.InsertColumn(2, "Genre") | |
138 | else: | |
139 | # but since we want images on the column header we have to do it the hard way: | |
140 | info = wxListItem() | |
141 | info.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_IMAGE | wxLIST_MASK_FORMAT | |
142 | info.m_image = -1 | |
143 | info.m_format = 0 | |
144 | info.m_text = "Artist" | |
145 | self.list.InsertColumnInfo(0, info) | |
146 | ||
147 | info.m_format = wxLIST_FORMAT_RIGHT | |
148 | info.m_text = "Title" | |
149 | self.list.InsertColumnInfo(1, info) | |
150 | ||
151 | info.m_format = 0 | |
152 | info.m_text = "Genre" | |
153 | self.list.InsertColumnInfo(2, info) | |
154 | ||
dcd38683 RD |
155 | items = musicdata.items() |
156 | for x in range(len(items)): | |
157 | key, data = items[x] | |
3979290c | 158 | self.list.InsertImageStringItem(x, data[0], self.idx1) |
dcd38683 RD |
159 | self.list.SetStringItem(x, 1, data[1]) |
160 | self.list.SetStringItem(x, 2, data[2]) | |
161 | self.list.SetItemData(x, key) | |
cf694132 RD |
162 | |
163 | self.list.SetColumnWidth(0, wxLIST_AUTOSIZE) | |
164 | self.list.SetColumnWidth(1, wxLIST_AUTOSIZE) | |
0e947004 | 165 | self.list.SetColumnWidth(2, 100) |
cf694132 | 166 | |
6d19860f | 167 | # show how to select an item |
9416aa89 | 168 | self.list.SetItemState(5, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED) |
c368d904 | 169 | |
6d19860f | 170 | # show how to change the colour of a couple items |
c368d904 RD |
171 | item = self.list.GetItem(1) |
172 | item.SetTextColour(wxBLUE) | |
173 | self.list.SetItem(item) | |
c7e7022c RD |
174 | item = self.list.GetItem(4) |
175 | item.SetTextColour(wxRED) | |
176 | self.list.SetItem(item) | |
177 | ||
bb0054cd | 178 | self.currentItem = 0 |
64be6958 | 179 | |
a08cbc01 | 180 | |
6d19860f RD |
181 | # Used by the wxColumnSorterMixin, see wxPython/lib/mixins/listctrl.py |
182 | def GetListCtrl(self): | |
183 | return self.list | |
184 | ||
185 | # Used by the wxColumnSorterMixin, see wxPython/lib/mixins/listctrl.py | |
186 | def GetSortImages(self): | |
187 | return (self.sm_dn, self.sm_up) | |
188 | ||
189 | ||
a08cbc01 RD |
190 | def OnRightDown(self, event): |
191 | self.x = event.GetX() | |
64be6958 RD |
192 | self.y = event.GetY() |
193 | self.log.WriteText("x, y = %s\n" % str((self.x, self.y))) | |
b1cfebd9 | 194 | print event.GetEventObject() |
a08cbc01 | 195 | event.Skip() |
bb0054cd | 196 | |
185d7c3e RD |
197 | |
198 | def getColumnText(self, index, col): | |
199 | item = self.list.GetItem(index, col) | |
200 | return item.GetText() | |
201 | ||
202 | ||
bb0054cd RD |
203 | def OnItemSelected(self, event): |
204 | self.currentItem = event.m_itemIndex | |
9416aa89 RD |
205 | self.log.WriteText("OnItemSelected: %s, %s, %s, %s\n" % |
206 | (self.currentItem, | |
207 | self.list.GetItemText(self.currentItem), | |
185d7c3e RD |
208 | self.getColumnText(self.currentItem, 1), |
209 | self.getColumnText(self.currentItem, 2))) | |
9416aa89 RD |
210 | if self.currentItem == 10: |
211 | self.log.WriteText("OnItemSelected: Veto'd selection\n") | |
212 | #event.Veto() # doesn't work | |
213 | # this does | |
214 | self.list.SetItemState(10, 0, wxLIST_STATE_SELECTED) | |
185d7c3e | 215 | |
9c2abc2c RD |
216 | # Show how to reselect something we don't want deselected |
217 | def OnItemDeselected(self, evt): | |
218 | item = evt.GetItem() | |
198f875b | 219 | print evt.m_itemIndex |
9c2abc2c RD |
220 | if evt.m_itemIndex == 11: |
221 | wxCallAfter(self.list.SetItemState, 11, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED) | |
222 | ||
223 | ||
c368d904 RD |
224 | def OnItemActivated(self, event): |
225 | self.currentItem = event.m_itemIndex | |
226 | self.log.WriteText("OnItemActivated: %s\n" % self.list.GetItemText(self.currentItem)) | |
227 | ||
8bf5d46e RD |
228 | def OnItemDelete(self, event): |
229 | self.log.WriteText("OnItemDelete\n") | |
230 | ||
dcd38683 | 231 | def OnColClick(self, event): |
6d19860f RD |
232 | self.log.WriteText("OnColClick: %d\n" % event.GetColumn()) |
233 | ||
234 | def OnColRightClick(self, event): | |
3115ef3e RD |
235 | item = self.list.GetColumn(event.GetColumn()) |
236 | self.log.WriteText("OnColRightClick: %d %s\n" % | |
237 | (event.GetColumn(), (item.GetText(), item.GetAlign(), | |
238 | item.GetWidth(), item.GetImage()))) | |
dcd38683 | 239 | |
6d19860f RD |
240 | def OnColBeginDrag(self, event): |
241 | self.log.WriteText("OnColBeginDrag\n") | |
1192dbd4 RD |
242 | ## Show how to not allow a column to be resized |
243 | #if event.GetColumn() == 0: | |
244 | # event.Veto() | |
245 | ||
c7e7022c | 246 | |
6d19860f RD |
247 | def OnColDragging(self, event): |
248 | self.log.WriteText("OnColDragging\n") | |
dcd38683 | 249 | |
6d19860f RD |
250 | def OnColEndDrag(self, event): |
251 | self.log.WriteText("OnColEndDrag\n") | |
8bf5d46e | 252 | |
bb0054cd RD |
253 | def OnDoubleClick(self, event): |
254 | self.log.WriteText("OnDoubleClick item %s\n" % self.list.GetItemText(self.currentItem)) | |
185d7c3e | 255 | event.Skip() |
a08cbc01 | 256 | |
bb0054cd RD |
257 | def OnRightClick(self, event): |
258 | self.log.WriteText("OnRightClick %s\n" % self.list.GetItemText(self.currentItem)) | |
e166644c | 259 | menu = wxMenu() |
a08cbc01 RD |
260 | tPopupID1 = 0 |
261 | tPopupID2 = 1 | |
262 | tPopupID3 = 2 | |
8bf5d46e | 263 | tPopupID4 = 3 |
e166644c | 264 | tPopupID5 = 5 |
cce80896 RD |
265 | |
266 | # Show how to put an icon in the menu | |
d56cebe7 | 267 | item = wxMenuItem(menu, tPopupID1,"One") |
04c883f8 | 268 | item.SetBitmap(images.getSmilesBitmap()) |
cce80896 | 269 | |
d56cebe7 | 270 | menu.AppendItem(item) |
e166644c | 271 | menu.Append(tPopupID2, "Two") |
3979290c | 272 | menu.Append(tPopupID3, "ClearAll and repopulate") |
e166644c RD |
273 | menu.Append(tPopupID4, "DeleteAllItems") |
274 | menu.Append(tPopupID5, "GetItem") | |
a08cbc01 RD |
275 | EVT_MENU(self, tPopupID1, self.OnPopupOne) |
276 | EVT_MENU(self, tPopupID2, self.OnPopupTwo) | |
277 | EVT_MENU(self, tPopupID3, self.OnPopupThree) | |
8bf5d46e | 278 | EVT_MENU(self, tPopupID4, self.OnPopupFour) |
e166644c RD |
279 | EVT_MENU(self, tPopupID5, self.OnPopupFive) |
280 | self.PopupMenu(menu, wxPoint(self.x, self.y)) | |
281 | menu.Destroy() | |
185d7c3e | 282 | event.Skip() |
a08cbc01 RD |
283 | |
284 | def OnPopupOne(self, event): | |
285 | self.log.WriteText("Popup one\n") | |
3b5ccda1 RD |
286 | print "FindItem:", self.list.FindItem(-1, "Roxette") |
287 | print "FindItemData:", self.list.FindItemData(-1, 11) | |
a08cbc01 RD |
288 | |
289 | def OnPopupTwo(self, event): | |
290 | self.log.WriteText("Popup two\n") | |
291 | ||
292 | def OnPopupThree(self, event): | |
293 | self.log.WriteText("Popup three\n") | |
3979290c RD |
294 | self.list.ClearAll() |
295 | wxCallAfter(self.PopulateList) | |
296 | #wxYield() | |
297 | #self.PopulateList() | |
cf694132 | 298 | |
8bf5d46e RD |
299 | def OnPopupFour(self, event): |
300 | self.list.DeleteAllItems() | |
301 | ||
e166644c RD |
302 | def OnPopupFive(self, event): |
303 | item = self.list.GetItem(self.currentItem) | |
304 | print item.m_text, item.m_itemId, self.list.GetItemData(self.currentItem) | |
305 | ||
cf694132 RD |
306 | def OnSize(self, event): |
307 | w,h = self.GetClientSizeTuple() | |
308 | self.list.SetDimensions(0, 0, w, h) | |
309 | ||
310 | ||
311 | ||
312 | ||
a08cbc01 RD |
313 | |
314 | ||
cf694132 RD |
315 | #--------------------------------------------------------------------------- |
316 | ||
317 | def runTest(frame, nb, log): | |
318 | win = TestListCtrlPanel(nb, log) | |
319 | return win | |
320 | ||
321 | #--------------------------------------------------------------------------- | |
322 | ||
323 | ||
324 | ||
325 | ||
3115ef3e RD |
326 | overview = """\ |
327 | 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. | |
cf694132 | 328 | |
3115ef3e | 329 | """ |
cf694132 RD |
330 | |
331 | ||
332 | ||
333 | ||
334 | ||
3115ef3e RD |
335 | if __name__ == '__main__': |
336 | import sys,os | |
337 | import run | |
338 | run.main(['', os.path.basename(sys.argv[0])]) | |
cf694132 | 339 |