]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxListCtrl.py
compilation fixes for MSW+Univ+Mingw32
[wxWidgets.git] / wxPython / demo / wxListCtrl.py
1 #!/bin/env python
2 #----------------------------------------------------------------------------
3 # Name: ListCtrl.py
4 # Purpose: Testing lots of stuff, controls, window types, etc.
5 #
6 # Author: Robin Dunn & Gary Dumer
7 #
8 # Created:
9 # RCS-ID: $Id$
10 # Copyright: (c) 1998 by Total Control Software
11 # Licence: wxWindows license
12 #----------------------------------------------------------------------------
13
14 from wxPython.wx import *
15 from wxPython.lib.mixins.listctrl import wxColumnSorterMixin
16
17 #---------------------------------------------------------------------------
18
19 musicdata = {
20 1 : ("Bad English", "The Price Of Love", "Rock"),
21 2 : ("DNA featuring Suzanne Vega", "Tom's Diner", "Rock"),
22 3 : ("George Michael", "Praying For Time", "Rock"),
23 4 : ("Gloria Estefan", "Here We Are", "Rock"),
24 5 : ("Linda Ronstadt", "Don't Know Much", "Rock"),
25 6 : ("Michael Bolton", "How Am I Supposed To Live Without You", "Blues"),
26 7 : ("Paul Young", "Oh Girl", "Rock"),
27 8 : ("Paula Abdul", "Opposites Attract", "Rock"),
28 9 : ("Richard Marx", "Should've Known Better", "Rock"),
29 10: ("Rod Stewart", "Forever Young", "Rock"),
30 11: ("Roxette", "Dangerous", "Rock"),
31 12: ("Sheena Easton", "The Lover In Me", "Rock"),
32 13: ("Sinead O'Connor", "Nothing Compares 2 U", "Rock"),
33 14: ("Stevie B.", "Because I Love You", "Rock"),
34 15: ("Taylor Dayne", "Love Will Lead You Back", "Rock"),
35 16: ("The Bangles", "Eternal Flame", "Rock"),
36 17: ("Wilson Phillips", "Release Me", "Rock"),
37 18: ("Billy Joel", "Blonde Over Blue", "Rock"),
38 19: ("Billy Joel", "Famous Last Words", "Rock"),
39 20: ("Billy Joel", "Lullabye (Goodnight, My Angel)", "Rock"),
40 21: ("Billy Joel", "The River Of Dreams", "Rock"),
41 22: ("Billy Joel", "Two Thousand Years", "Rock"),
42 23: ("Janet Jackson", "Alright", "Rock"),
43 24: ("Janet Jackson", "Black Cat", "Rock"),
44 25: ("Janet Jackson", "Come Back To Me", "Rock"),
45 26: ("Janet Jackson", "Escapade", "Rock"),
46 27: ("Janet Jackson", "Love Will Never Do (Without You)", "Rock"),
47 28: ("Janet Jackson", "Miss You Much", "Rock"),
48 29: ("Janet Jackson", "Rhythm Nation", "Rock"),
49 30: ("Janet Jackson", "State Of The World", "Rock"),
50 31: ("Janet Jackson", "The Knowledge", "Rock"),
51 32: ("Spyro Gyra", "End of Romanticism", "Jazz"),
52 33: ("Spyro Gyra", "Heliopolis", "Jazz"),
53 34: ("Spyro Gyra", "Jubilee", "Jazz"),
54 35: ("Spyro Gyra", "Little Linda", "Jazz"),
55 36: ("Spyro Gyra", "Morning Dance", "Jazz"),
56 37: ("Spyro Gyra", "Song for Lorraine", "Jazz"),
57 38: ("Yes", "Owner Of A Lonely Heart", "Rock"),
58 39: ("Yes", "Rhythm Of Love", "Rock"),
59 40: ("Cusco", "Dream Catcher", "New Age"),
60 41: ("Cusco", "Geronimos Laughter", "New Age"),
61 42: ("Cusco", "Ghost Dance", "New Age"),
62 43: ("Blue Man Group", "Drumbone", "New Age"),
63 44: ("Blue Man Group", "Endless Column", "New Age"),
64 45: ("Blue Man Group", "Klein Mandelbrot", "New Age"),
65 46: ("Kenny G", "Silhouette", "Jazz"),
66 47: ("Sade", "Smooth Operator", "Jazz"),
67 48: ("David Arkenstone", "Papillon (On The Wings Of The Butterfly)", "New Age"),
68 49: ("David Arkenstone", "Stepping Stars", "New Age"),
69 50: ("David Arkenstone", "Carnation Lily Lily Rose", "New Age"),
70 51: ("David Lanz", "Behind The Waterfall", "New Age"),
71 52: ("David Lanz", "Cristofori's Dream", "New Age"),
72 53: ("David Lanz", "Heartsounds", "New Age"),
73 54: ("David Lanz", "Leaves on the Seine", "New Age"),
74 }
75
76 import images
77
78 class TestListCtrlPanel(wxPanel, wxColumnSorterMixin):
79 def __init__(self, parent, log):
80 wxPanel.__init__(self, parent, -1, style=wxWANTS_CHARS)
81
82 self.log = log
83 tID = wxNewId()
84
85 self.il = wxImageList(16, 16)
86 bmp = images.getSmilesBitmap()
87 #idx1 = self.il.AddWithColourMask(bmp, wxWHITE)
88 idx1 = self.il.Add(bmp)
89 bmp = images.getSmallUpArrowBitmap()
90 self.sm_up = self.il.Add(bmp)
91 bmp = images.getSmallDnArrowBitmap()
92 self.sm_dn = self.il.Add(bmp)
93
94
95 self.list = wxListCtrl(self, tID,
96 style=wxLC_REPORT|wxSUNKEN_BORDER)#|wxLC_VRULES|wxLC_HRULES)
97 self.list.SetImageList(self.il, wxIMAGE_LIST_SMALL)
98
99 # Why doesn't this show up on MSW???
100 self.list.SetToolTip(wxToolTip("This is a ToolTip!"))
101
102 if 0:
103 # for normal simple columns, you can add them like this:
104 self.list.InsertColumn(0, "Artist")
105 self.list.InsertColumn(1, "Title", wxLIST_FORMAT_RIGHT)
106 self.list.InsertColumn(2, "Genre")
107 else:
108 # but since we want images on the column header we have to do it the hard way:
109 info = wxListItem()
110 info.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_IMAGE | wxLIST_MASK_FORMAT
111 info.m_image = -1
112 info.m_format = 0
113 info.m_text = "Artist"
114 self.list.InsertColumnInfo(0, info)
115
116 info.m_format = wxLIST_FORMAT_RIGHT
117 info.m_text = "Title"
118 self.list.InsertColumnInfo(1, info)
119
120 info.m_format = 0
121 info.m_text = "Genre"
122 self.list.InsertColumnInfo(2, info)
123
124
125 items = musicdata.items()
126 for x in range(len(items)):
127 key, data = items[x]
128 self.list.InsertImageStringItem(x, data[0], idx1)
129 self.list.SetStringItem(x, 1, data[1])
130 self.list.SetStringItem(x, 2, data[2])
131 self.list.SetItemData(x, key)
132
133 # Now that the list exists we can init the other base class,
134 # see wxPython/lib/mixins/listctrl.py
135 self.itemDataMap = musicdata
136 wxColumnSorterMixin.__init__(self, 3)
137 #self.SortListItems(0, true)
138
139 self.list.SetColumnWidth(0, wxLIST_AUTOSIZE)
140 self.list.SetColumnWidth(1, wxLIST_AUTOSIZE)
141
142 # show how to select an item
143 self.list.SetItemState(5, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED)
144
145 # show how to change the colour of a couple items
146 item = self.list.GetItem(1)
147 item.SetTextColour(wxBLUE)
148 self.list.SetItem(item)
149 item = self.list.GetItem(4)
150 item.SetTextColour(wxRED)
151 self.list.SetItem(item)
152
153 self.currentItem = 0
154 EVT_SIZE(self, self.OnSize)
155 EVT_LIST_ITEM_SELECTED(self, tID, self.OnItemSelected)
156 EVT_LIST_ITEM_ACTIVATED(self, tID, self.OnItemActivated)
157 EVT_LIST_DELETE_ITEM(self, tID, self.OnItemDelete)
158 EVT_LIST_COL_CLICK(self, tID, self.OnColClick)
159 EVT_LIST_COL_RIGHT_CLICK(self, tID, self.OnColRightClick)
160 EVT_LIST_COL_BEGIN_DRAG(self, tID, self.OnColBeginDrag)
161 EVT_LIST_COL_DRAGGING(self, tID, self.OnColDragging)
162 EVT_LIST_COL_END_DRAG(self, tID, self.OnColEndDrag)
163
164 EVT_LEFT_DCLICK(self.list, self.OnDoubleClick)
165 EVT_RIGHT_DOWN(self.list, self.OnRightDown)
166
167 # for wxMSW
168 EVT_COMMAND_RIGHT_CLICK(self.list, tID, self.OnRightClick)
169
170 # for wxGTK
171 EVT_RIGHT_UP(self.list, self.OnRightClick)
172
173
174 # Used by the wxColumnSorterMixin, see wxPython/lib/mixins/listctrl.py
175 def GetListCtrl(self):
176 return self.list
177
178 # Used by the wxColumnSorterMixin, see wxPython/lib/mixins/listctrl.py
179 def GetSortImages(self):
180 return (self.sm_dn, self.sm_up)
181
182
183
184 def OnRightDown(self, event):
185 self.x = event.GetX()
186 self.y = event.GetY()
187 self.log.WriteText("x, y = %s\n" % str((self.x, self.y)))
188 print event.GetEventObject()
189 event.Skip()
190
191
192 def getColumnText(self, index, col):
193 item = self.list.GetItem(index, col)
194 return item.GetText()
195
196
197 def OnItemSelected(self, event):
198 self.currentItem = event.m_itemIndex
199 self.log.WriteText("OnItemSelected: %s, %s, %s, %s\n" %
200 (self.currentItem,
201 self.list.GetItemText(self.currentItem),
202 self.getColumnText(self.currentItem, 1),
203 self.getColumnText(self.currentItem, 2)))
204 if self.currentItem == 10:
205 self.log.WriteText("OnItemSelected: Veto'd selection\n")
206 #event.Veto() # doesn't work
207 # this does
208 self.list.SetItemState(10, 0, wxLIST_STATE_SELECTED)
209
210 def OnItemActivated(self, event):
211 self.currentItem = event.m_itemIndex
212 self.log.WriteText("OnItemActivated: %s\n" % self.list.GetItemText(self.currentItem))
213
214 def OnItemDelete(self, event):
215 self.log.WriteText("OnItemDelete\n")
216
217 def OnColClick(self, event):
218 self.log.WriteText("OnColClick: %d\n" % event.GetColumn())
219
220 def OnColRightClick(self, event):
221 self.log.WriteText("OnColRightClick: %d\n" % event.GetColumn())
222
223 def OnColBeginDrag(self, event):
224 self.log.WriteText("OnColBeginDrag\n")
225
226 def OnColDragging(self, event):
227 self.log.WriteText("OnColDragging\n")
228
229 def OnColEndDrag(self, event):
230 self.log.WriteText("OnColEndDrag\n")
231
232 def OnDoubleClick(self, event):
233 self.log.WriteText("OnDoubleClick item %s\n" % self.list.GetItemText(self.currentItem))
234 event.Skip()
235
236 def OnRightClick(self, event):
237 self.log.WriteText("OnRightClick %s\n" % self.list.GetItemText(self.currentItem))
238 menu = wxMenu()
239 tPopupID1 = 0
240 tPopupID2 = 1
241 tPopupID3 = 2
242 tPopupID4 = 3
243 tPopupID5 = 5
244 #menu.Append(tPopupID1, "One")
245 item = wxMenuItem(menu, tPopupID1,"One")
246 item.SetBitmap(images.getSmilesBitmap())
247 menu.AppendItem(item)
248 menu.Append(tPopupID2, "Two")
249 menu.Append(tPopupID3, "Three")
250 menu.Append(tPopupID4, "DeleteAllItems")
251 menu.Append(tPopupID5, "GetItem")
252 EVT_MENU(self, tPopupID1, self.OnPopupOne)
253 EVT_MENU(self, tPopupID2, self.OnPopupTwo)
254 EVT_MENU(self, tPopupID3, self.OnPopupThree)
255 EVT_MENU(self, tPopupID4, self.OnPopupFour)
256 EVT_MENU(self, tPopupID5, self.OnPopupFive)
257 self.PopupMenu(menu, wxPoint(self.x, self.y))
258 menu.Destroy()
259 event.Skip()
260
261 def OnPopupOne(self, event):
262 self.log.WriteText("Popup one\n")
263
264 def OnPopupTwo(self, event):
265 self.log.WriteText("Popup two\n")
266
267 def OnPopupThree(self, event):
268 self.log.WriteText("Popup three\n")
269
270 def OnPopupFour(self, event):
271 self.list.DeleteAllItems()
272
273 def OnPopupFive(self, event):
274 item = self.list.GetItem(self.currentItem)
275 print item.m_text, item.m_itemId, self.list.GetItemData(self.currentItem)
276
277 def OnSize(self, event):
278 w,h = self.GetClientSizeTuple()
279 self.list.SetDimensions(0, 0, w, h)
280
281
282
283
284
285
286 #---------------------------------------------------------------------------
287
288 def runTest(frame, nb, log):
289 win = TestListCtrlPanel(nb, log)
290 return win
291
292 #---------------------------------------------------------------------------
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309 overview = """\
310 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.
311
312 """