]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/wxListCtrl.py
wxMGL update
[wxWidgets.git] / wxPython / demo / wxListCtrl.py
CommitLineData
cf694132
RD
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 *
6d19860f 15from wxPython.lib.mixins.listctrl import wxColumnSorterMixin
cf694132
RD
16
17#---------------------------------------------------------------------------
18
dcd38683
RD
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"),
cd096152
RD
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"),
b1cfebd9
RD
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"),
dcd38683
RD
74}
75
96bfd053 76import images
dcd38683 77
6d19860f 78class TestListCtrlPanel(wxPanel, wxColumnSorterMixin):
cf694132 79 def __init__(self, parent, log):
c368d904 80 wxPanel.__init__(self, parent, -1, style=wxWANTS_CHARS)
cf694132
RD
81
82 self.log = log
f6bcfd97 83 tID = wxNewId()
cf694132
RD
84
85 self.il = wxImageList(16, 16)
96bfd053 86 bmp = images.getSmilesBitmap()
d56cebe7
RD
87 #idx1 = self.il.AddWithColourMask(bmp, wxWHITE)
88 idx1 = self.il.Add(bmp)
6d19860f
RD
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
cf694132 94
c368d904 95 self.list = wxListCtrl(self, tID,
c7e7022c 96 style=wxLC_REPORT|wxSUNKEN_BORDER)#|wxLC_VRULES|wxLC_HRULES)
cf694132
RD
97 self.list.SetImageList(self.il, wxIMAGE_LIST_SMALL)
98
185d7c3e 99 # Why doesn't this show up on MSW???
cf694132 100 self.list.SetToolTip(wxToolTip("This is a ToolTip!"))
cf694132 101
6d19860f
RD
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
dcd38683
RD
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)
cf694132 132
6d19860f
RD
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
cf694132
RD
139 self.list.SetColumnWidth(0, wxLIST_AUTOSIZE)
140 self.list.SetColumnWidth(1, wxLIST_AUTOSIZE)
cf694132 141
6d19860f 142 # show how to select an item
9416aa89 143 self.list.SetItemState(5, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED)
c368d904 144
6d19860f 145 # show how to change the colour of a couple items
c368d904
RD
146 item = self.list.GetItem(1)
147 item.SetTextColour(wxBLUE)
148 self.list.SetItem(item)
c7e7022c
RD
149 item = self.list.GetItem(4)
150 item.SetTextColour(wxRED)
151 self.list.SetItem(item)
152
bb0054cd 153 self.currentItem = 0
f6bcfd97 154 EVT_SIZE(self, self.OnSize)
bb0054cd 155 EVT_LIST_ITEM_SELECTED(self, tID, self.OnItemSelected)
c368d904 156 EVT_LIST_ITEM_ACTIVATED(self, tID, self.OnItemActivated)
8bf5d46e 157 EVT_LIST_DELETE_ITEM(self, tID, self.OnItemDelete)
dcd38683 158 EVT_LIST_COL_CLICK(self, tID, self.OnColClick)
6d19860f
RD
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
bb0054cd 164 EVT_LEFT_DCLICK(self.list, self.OnDoubleClick)
a08cbc01
RD
165 EVT_RIGHT_DOWN(self.list, self.OnRightDown)
166
64be6958
RD
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
a08cbc01 173
6d19860f
RD
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
a08cbc01
RD
184 def OnRightDown(self, event):
185 self.x = event.GetX()
64be6958
RD
186 self.y = event.GetY()
187 self.log.WriteText("x, y = %s\n" % str((self.x, self.y)))
b1cfebd9 188 print event.GetEventObject()
a08cbc01 189 event.Skip()
bb0054cd 190
185d7c3e
RD
191
192 def getColumnText(self, index, col):
193 item = self.list.GetItem(index, col)
194 return item.GetText()
195
196
bb0054cd
RD
197 def OnItemSelected(self, event):
198 self.currentItem = event.m_itemIndex
9416aa89
RD
199 self.log.WriteText("OnItemSelected: %s, %s, %s, %s\n" %
200 (self.currentItem,
201 self.list.GetItemText(self.currentItem),
185d7c3e
RD
202 self.getColumnText(self.currentItem, 1),
203 self.getColumnText(self.currentItem, 2)))
9416aa89
RD
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)
185d7c3e 209
c368d904
RD
210 def OnItemActivated(self, event):
211 self.currentItem = event.m_itemIndex
212 self.log.WriteText("OnItemActivated: %s\n" % self.list.GetItemText(self.currentItem))
213
8bf5d46e
RD
214 def OnItemDelete(self, event):
215 self.log.WriteText("OnItemDelete\n")
216
dcd38683 217 def OnColClick(self, event):
6d19860f
RD
218 self.log.WriteText("OnColClick: %d\n" % event.GetColumn())
219
220 def OnColRightClick(self, event):
221 self.log.WriteText("OnColRightClick: %d\n" % event.GetColumn())
dcd38683 222
6d19860f
RD
223 def OnColBeginDrag(self, event):
224 self.log.WriteText("OnColBeginDrag\n")
c7e7022c 225
6d19860f
RD
226 def OnColDragging(self, event):
227 self.log.WriteText("OnColDragging\n")
dcd38683 228
6d19860f
RD
229 def OnColEndDrag(self, event):
230 self.log.WriteText("OnColEndDrag\n")
8bf5d46e 231
bb0054cd
RD
232 def OnDoubleClick(self, event):
233 self.log.WriteText("OnDoubleClick item %s\n" % self.list.GetItemText(self.currentItem))
185d7c3e 234 event.Skip()
a08cbc01 235
bb0054cd
RD
236 def OnRightClick(self, event):
237 self.log.WriteText("OnRightClick %s\n" % self.list.GetItemText(self.currentItem))
e166644c 238 menu = wxMenu()
a08cbc01
RD
239 tPopupID1 = 0
240 tPopupID2 = 1
241 tPopupID3 = 2
8bf5d46e 242 tPopupID4 = 3
e166644c 243 tPopupID5 = 5
d56cebe7
RD
244 #menu.Append(tPopupID1, "One")
245 item = wxMenuItem(menu, tPopupID1,"One")
246 item.SetBitmap(images.getSmilesBitmap())
247 menu.AppendItem(item)
e166644c
RD
248 menu.Append(tPopupID2, "Two")
249 menu.Append(tPopupID3, "Three")
250 menu.Append(tPopupID4, "DeleteAllItems")
251 menu.Append(tPopupID5, "GetItem")
a08cbc01
RD
252 EVT_MENU(self, tPopupID1, self.OnPopupOne)
253 EVT_MENU(self, tPopupID2, self.OnPopupTwo)
254 EVT_MENU(self, tPopupID3, self.OnPopupThree)
8bf5d46e 255 EVT_MENU(self, tPopupID4, self.OnPopupFour)
e166644c
RD
256 EVT_MENU(self, tPopupID5, self.OnPopupFive)
257 self.PopupMenu(menu, wxPoint(self.x, self.y))
258 menu.Destroy()
185d7c3e 259 event.Skip()
a08cbc01
RD
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")
cf694132 269
8bf5d46e
RD
270 def OnPopupFour(self, event):
271 self.list.DeleteAllItems()
272
e166644c
RD
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
cf694132
RD
277 def OnSize(self, event):
278 w,h = self.GetClientSizeTuple()
279 self.list.SetDimensions(0, 0, w, h)
280
281
282
283
a08cbc01
RD
284
285
cf694132
RD
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
cf694132 312"""