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