]> git.saurik.com Git - wxWidgets.git/blame_incremental - wxPython/demo/wxListCtrl.py
In GetPath(), don't append separator if there's already one at the end
[wxWidgets.git] / wxPython / demo / wxListCtrl.py
... / ...
CommitLineData
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
13from wxPython.wx import *
14from wxPython.lib.mixins.listctrl import wxColumnSorterMixin, wxListCtrlAutoWidthMixin
15
16#---------------------------------------------------------------------------
17
18musicdata = {
191 : ("Bad English", "The Price Of Love", "Rock"),
202 : ("DNA featuring Suzanne Vega", "Tom's Diner", "Rock"),
213 : ("George Michael", "Praying For Time", "Rock"),
224 : ("Gloria Estefan", "Here We Are", "Rock"),
235 : ("Linda Ronstadt", "Don't Know Much", "Rock"),
246 : ("Michael Bolton", "How Am I Supposed To Live Without You", "Blues"),
257 : ("Paul Young", "Oh Girl", "Rock"),
268 : ("Paula Abdul", "Opposites Attract", "Rock"),
279 : ("Richard Marx", "Should've Known Better", "Rock"),
2810: ("Rod Stewart", "Forever Young", "Rock"),
2911: ("Roxette", "Dangerous", "Rock"),
3012: ("Sheena Easton", "The Lover In Me", "Rock"),
3113: ("Sinead O'Connor", "Nothing Compares 2 U", "Rock"),
3214: ("Stevie B.", "Because I Love You", "Rock"),
3315: ("Taylor Dayne", "Love Will Lead You Back", "Rock"),
3416: ("The Bangles", "Eternal Flame", "Rock"),
3517: ("Wilson Phillips", "Release Me", "Rock"),
3618: ("Billy Joel", "Blonde Over Blue", "Rock"),
3719: ("Billy Joel", "Famous Last Words", "Rock"),
3820: ("Billy Joel", "Lullabye (Goodnight, My Angel)", "Rock"),
3921: ("Billy Joel", "The River Of Dreams", "Rock"),
4022: ("Billy Joel", "Two Thousand Years", "Rock"),
4123: ("Janet Jackson", "Alright", "Rock"),
4224: ("Janet Jackson", "Black Cat", "Rock"),
4325: ("Janet Jackson", "Come Back To Me", "Rock"),
4426: ("Janet Jackson", "Escapade", "Rock"),
4527: ("Janet Jackson", "Love Will Never Do (Without You)", "Rock"),
4628: ("Janet Jackson", "Miss You Much", "Rock"),
4729: ("Janet Jackson", "Rhythm Nation", "Rock"),
4830: ("Janet Jackson", "State Of The World", "Rock"),
4931: ("Janet Jackson", "The Knowledge", "Rock"),
5032: ("Spyro Gyra", "End of Romanticism", "Jazz"),
5133: ("Spyro Gyra", "Heliopolis", "Jazz"),
5234: ("Spyro Gyra", "Jubilee", "Jazz"),
5335: ("Spyro Gyra", "Little Linda", "Jazz"),
5436: ("Spyro Gyra", "Morning Dance", "Jazz"),
5537: ("Spyro Gyra", "Song for Lorraine", "Jazz"),
5638: ("Yes", "Owner Of A Lonely Heart", "Rock"),
5739: ("Yes", "Rhythm Of Love", "Rock"),
5840: ("Cusco", "Dream Catcher", "New Age"),
5941: ("Cusco", "Geronimos Laughter", "New Age"),
6042: ("Cusco", "Ghost Dance", "New Age"),
6143: ("Blue Man Group", "Drumbone", "New Age"),
6244: ("Blue Man Group", "Endless Column", "New Age"),
6345: ("Blue Man Group", "Klein Mandelbrot", "New Age"),
6446: ("Kenny G", "Silhouette", "Jazz"),
6547: ("Sade", "Smooth Operator", "Jazz"),
6648: ("David Arkenstone", "Papillon (On The Wings Of The Butterfly)", "New Age"),
6749: ("David Arkenstone", "Stepping Stars", "New Age"),
6850: ("David Arkenstone", "Carnation Lily Lily Rose", "New Age"),
6951: ("David Lanz", "Behind The Waterfall", "New Age"),
7052: ("David Lanz", "Cristofori's Dream", "New Age"),
7153: ("David Lanz", "Heartsounds", "New Age"),
7254: ("David Lanz", "Leaves on the Seine", "New Age"),
73}
74
75import images
76
77
78class 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
86class 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_ACTIVATED(self, tID, self.OnItemActivated)
167 EVT_LIST_DELETE_ITEM(self, tID, self.OnItemDelete)
168 EVT_LIST_COL_CLICK(self, tID, self.OnColClick)
169 EVT_LIST_COL_RIGHT_CLICK(self, tID, self.OnColRightClick)
170 EVT_LIST_COL_BEGIN_DRAG(self, tID, self.OnColBeginDrag)
171 EVT_LIST_COL_DRAGGING(self, tID, self.OnColDragging)
172 EVT_LIST_COL_END_DRAG(self, tID, self.OnColEndDrag)
173
174 EVT_LEFT_DCLICK(self.list, self.OnDoubleClick)
175 EVT_RIGHT_DOWN(self.list, self.OnRightDown)
176
177 # for wxMSW
178 EVT_COMMAND_RIGHT_CLICK(self.list, tID, self.OnRightClick)
179
180 # for wxGTK
181 EVT_RIGHT_UP(self.list, self.OnRightClick)
182
183
184 # Used by the wxColumnSorterMixin, see wxPython/lib/mixins/listctrl.py
185 def GetListCtrl(self):
186 return self.list
187
188 # Used by the wxColumnSorterMixin, see wxPython/lib/mixins/listctrl.py
189 def GetSortImages(self):
190 return (self.sm_dn, self.sm_up)
191
192
193
194 def OnRightDown(self, event):
195 self.x = event.GetX()
196 self.y = event.GetY()
197 self.log.WriteText("x, y = %s\n" % str((self.x, self.y)))
198 print event.GetEventObject()
199 event.Skip()
200
201
202 def getColumnText(self, index, col):
203 item = self.list.GetItem(index, col)
204 return item.GetText()
205
206
207 def OnItemSelected(self, event):
208 self.currentItem = event.m_itemIndex
209 self.log.WriteText("OnItemSelected: %s, %s, %s, %s\n" %
210 (self.currentItem,
211 self.list.GetItemText(self.currentItem),
212 self.getColumnText(self.currentItem, 1),
213 self.getColumnText(self.currentItem, 2)))
214 if self.currentItem == 10:
215 self.log.WriteText("OnItemSelected: Veto'd selection\n")
216 #event.Veto() # doesn't work
217 # this does
218 self.list.SetItemState(10, 0, wxLIST_STATE_SELECTED)
219
220 def OnItemActivated(self, event):
221 self.currentItem = event.m_itemIndex
222 self.log.WriteText("OnItemActivated: %s\n" % self.list.GetItemText(self.currentItem))
223
224 def OnItemDelete(self, event):
225 self.log.WriteText("OnItemDelete\n")
226
227 def OnColClick(self, event):
228 self.log.WriteText("OnColClick: %d\n" % event.GetColumn())
229
230 def OnColRightClick(self, event):
231 self.log.WriteText("OnColRightClick: %d\n" % event.GetColumn())
232
233 def OnColBeginDrag(self, event):
234 self.log.WriteText("OnColBeginDrag\n")
235 ## Show how to not allow a column to be resized
236 #if event.GetColumn() == 0:
237 # event.Veto()
238
239
240 def OnColDragging(self, event):
241 self.log.WriteText("OnColDragging\n")
242
243 def OnColEndDrag(self, event):
244 self.log.WriteText("OnColEndDrag\n")
245
246 def OnDoubleClick(self, event):
247 self.log.WriteText("OnDoubleClick item %s\n" % self.list.GetItemText(self.currentItem))
248 event.Skip()
249
250 def OnRightClick(self, event):
251 self.log.WriteText("OnRightClick %s\n" % self.list.GetItemText(self.currentItem))
252 menu = wxMenu()
253 tPopupID1 = 0
254 tPopupID2 = 1
255 tPopupID3 = 2
256 tPopupID4 = 3
257 tPopupID5 = 5
258
259 # Show how to put an icon in the menu
260 item = wxMenuItem(menu, tPopupID1,"One")
261 if wxPlatform == '__WXMSW__':
262 item.SetBitmap(images.getSmilesBitmap())
263
264 menu.AppendItem(item)
265 menu.Append(tPopupID2, "Two")
266 menu.Append(tPopupID3, "Three")
267 menu.Append(tPopupID4, "DeleteAllItems")
268 menu.Append(tPopupID5, "GetItem")
269 EVT_MENU(self, tPopupID1, self.OnPopupOne)
270 EVT_MENU(self, tPopupID2, self.OnPopupTwo)
271 EVT_MENU(self, tPopupID3, self.OnPopupThree)
272 EVT_MENU(self, tPopupID4, self.OnPopupFour)
273 EVT_MENU(self, tPopupID5, self.OnPopupFive)
274 self.PopupMenu(menu, wxPoint(self.x, self.y))
275 menu.Destroy()
276 event.Skip()
277
278 def OnPopupOne(self, event):
279 self.log.WriteText("Popup one\n")
280
281 def OnPopupTwo(self, event):
282 self.log.WriteText("Popup two\n")
283
284 def OnPopupThree(self, event):
285 self.log.WriteText("Popup three\n")
286
287 def OnPopupFour(self, event):
288 self.list.DeleteAllItems()
289
290 def OnPopupFive(self, event):
291 item = self.list.GetItem(self.currentItem)
292 print item.m_text, item.m_itemId, self.list.GetItemData(self.currentItem)
293
294 def OnSize(self, event):
295 w,h = self.GetClientSizeTuple()
296 self.list.SetDimensions(0, 0, w, h)
297
298
299
300
301
302
303#---------------------------------------------------------------------------
304
305def runTest(frame, nb, log):
306 win = TestListCtrlPanel(nb, log)
307 return win
308
309#---------------------------------------------------------------------------
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326overview = """\
327A list control presents lists in a number of formats: list view, report view, icon view and small icon view. Elements are numbered from zero.
328
329"""