]> git.saurik.com Git - wxWidgets.git/blame_incremental - wxPython/demo/wxListCtrl.py
fixed crash in OnSize() handler
[wxWidgets.git] / wxPython / demo / wxListCtrl.py
... / ...
CommitLineData
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
14from wxPython.wx import *
15from wxPython.lib.mixins.listctrl import wxColumnSorterMixin
16
17#---------------------------------------------------------------------------
18
19musicdata = {
201 : ("Bad English", "The Price Of Love", "Rock"),
212 : ("DNA featuring Suzanne Vega", "Tom's Diner", "Rock"),
223 : ("George Michael", "Praying For Time", "Rock"),
234 : ("Gloria Estefan", "Here We Are", "Rock"),
245 : ("Linda Ronstadt", "Don't Know Much", "Rock"),
256 : ("Michael Bolton", "How Am I Supposed To Live Without You", "Blues"),
267 : ("Paul Young", "Oh Girl", "Rock"),
278 : ("Paula Abdul", "Opposites Attract", "Rock"),
289 : ("Richard Marx", "Should've Known Better", "Rock"),
2910: ("Rod Stewart", "Forever Young", "Rock"),
3011: ("Roxette", "Dangerous", "Rock"),
3112: ("Sheena Easton", "The Lover In Me", "Rock"),
3213: ("Sinead O'Connor", "Nothing Compares 2 U", "Rock"),
3314: ("Stevie B.", "Because I Love You", "Rock"),
3415: ("Taylor Dayne", "Love Will Lead You Back", "Rock"),
3516: ("The Bangles", "Eternal Flame", "Rock"),
3617: ("Wilson Phillips", "Release Me", "Rock"),
3718: ("Billy Joel", "Blonde Over Blue", "Rock"),
3819: ("Billy Joel", "Famous Last Words", "Rock"),
3920: ("Billy Joel", "Lullabye (Goodnight, My Angel)", "Rock"),
4021: ("Billy Joel", "The River Of Dreams", "Rock"),
4122: ("Billy Joel", "Two Thousand Years", "Rock"),
4223: ("Janet Jackson", "Alright", "Rock"),
4324: ("Janet Jackson", "Black Cat", "Rock"),
4425: ("Janet Jackson", "Come Back To Me", "Rock"),
4526: ("Janet Jackson", "Escapade", "Rock"),
4627: ("Janet Jackson", "Love Will Never Do (Without You)", "Rock"),
4728: ("Janet Jackson", "Miss You Much", "Rock"),
4829: ("Janet Jackson", "Rhythm Nation", "Rock"),
4930: ("Janet Jackson", "State Of The World", "Rock"),
5031: ("Janet Jackson", "The Knowledge", "Rock"),
5132: ("Spyro Gyra", "End of Romanticism", "Jazz"),
5233: ("Spyro Gyra", "Heliopolis", "Jazz"),
5334: ("Spyro Gyra", "Jubilee", "Jazz"),
5435: ("Spyro Gyra", "Little Linda", "Jazz"),
5536: ("Spyro Gyra", "Morning Dance", "Jazz"),
5637: ("Spyro Gyra", "Song for Lorraine", "Jazz"),
5738: ("Yes", "Owner Of A Lonely Heart", "Rock"),
5839: ("Yes", "Rhythm Of Love", "Rock"),
5940: ("Cusco", "Dream Catcher", "New Age"),
6041: ("Cusco", "Geronimos Laughter", "New Age"),
6142: ("Cusco", "Ghost Dance", "New Age"),
6243: ("Blue Man Group", "Drumbone", "New Age"),
6344: ("Blue Man Group", "Endless Column", "New Age"),
6445: ("Blue Man Group", "Klein Mandelbrot", "New Age"),
6546: ("Kenny G", "Silhouette", "Jazz"),
6647: ("Sade", "Smooth Operator", "Jazz"),
6748: ("David Arkenstone", "Papillon (On The Wings Of The Butterfly)", "New Age"),
6849: ("David Arkenstone", "Stepping Stars", "New Age"),
6950: ("David Arkenstone", "Carnation Lily Lily Rose", "New Age"),
7051: ("David Lanz", "Behind The Waterfall", "New Age"),
7152: ("David Lanz", "Cristofori's Dream", "New Age"),
7253: ("David Lanz", "Heartsounds", "New Age"),
7354: ("David Lanz", "Leaves on the Seine", "New Age"),
74}
75
76import images
77
78class 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
288def 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
309overview = """\
310A 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"""