]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/wxListCtrl.py
Added possibility of 2-step initialisation, and wxWIZARD_EX_HELPBUTTON style.
[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 *
15
16#---------------------------------------------------------------------------
17
dcd38683
RD
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"),
cd096152
RD
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"),
dcd38683
RD
58}
59
96bfd053 60import images
dcd38683 61
cf694132
RD
62class TestListCtrlPanel(wxPanel):
63 def __init__(self, parent, log):
c368d904 64 wxPanel.__init__(self, parent, -1, style=wxWANTS_CHARS)
cf694132
RD
65
66 self.log = log
f6bcfd97 67 tID = wxNewId()
cf694132
RD
68
69 self.il = wxImageList(16, 16)
96bfd053 70 bmp = images.getSmilesBitmap()
d56cebe7
RD
71 #idx1 = self.il.AddWithColourMask(bmp, wxWHITE)
72 idx1 = self.il.Add(bmp)
cf694132 73
c368d904 74 self.list = wxListCtrl(self, tID,
c7e7022c 75 style=wxLC_REPORT|wxSUNKEN_BORDER)#|wxLC_VRULES|wxLC_HRULES)
cf694132
RD
76 self.list.SetImageList(self.il, wxIMAGE_LIST_SMALL)
77
185d7c3e 78 # Why doesn't this show up on MSW???
cf694132 79 self.list.SetToolTip(wxToolTip("This is a ToolTip!"))
cf694132 80
dcd38683 81 self.list.InsertColumn(0, "Artist")
c368d904 82 self.list.InsertColumn(1, "Title", wxLIST_FORMAT_RIGHT)
dcd38683
RD
83 self.list.InsertColumn(2, "Genre")
84 items = musicdata.items()
85 for x in range(len(items)):
86 key, data = items[x]
87 self.list.InsertImageStringItem(x, data[0], idx1)
88 self.list.SetStringItem(x, 1, data[1])
89 self.list.SetStringItem(x, 2, data[2])
90 self.list.SetItemData(x, key)
cf694132
RD
91
92 self.list.SetColumnWidth(0, wxLIST_AUTOSIZE)
93 self.list.SetColumnWidth(1, wxLIST_AUTOSIZE)
dcd38683 94 ##self.list.SetColumnWidth(2, wxLIST_AUTOSIZE)
cf694132 95
9416aa89 96 self.list.SetItemState(5, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED)
c368d904
RD
97
98 #self.list.SetItemState(25, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED)
99 #self.list.EnsureVisible(25)
100
101 # show how to change the colour of an item
102 item = self.list.GetItem(1)
103 item.SetTextColour(wxBLUE)
104 self.list.SetItem(item)
bb0054cd 105
c7e7022c
RD
106 item = self.list.GetItem(4)
107 item.SetTextColour(wxRED)
108 self.list.SetItem(item)
109
bb0054cd 110 self.currentItem = 0
f6bcfd97 111 EVT_SIZE(self, self.OnSize)
bb0054cd 112 EVT_LIST_ITEM_SELECTED(self, tID, self.OnItemSelected)
c368d904 113 EVT_LIST_ITEM_ACTIVATED(self, tID, self.OnItemActivated)
8bf5d46e 114 EVT_LIST_DELETE_ITEM(self, tID, self.OnItemDelete)
dcd38683 115 EVT_LIST_COL_CLICK(self, tID, self.OnColClick)
bb0054cd 116 EVT_LEFT_DCLICK(self.list, self.OnDoubleClick)
a08cbc01
RD
117 EVT_RIGHT_DOWN(self.list, self.OnRightDown)
118
64be6958
RD
119 # for wxMSW
120 EVT_COMMAND_RIGHT_CLICK(self.list, tID, self.OnRightClick)
121
122 # for wxGTK
123 EVT_RIGHT_UP(self.list, self.OnRightClick)
124
a08cbc01
RD
125
126 def OnRightDown(self, event):
127 self.x = event.GetX()
64be6958
RD
128 self.y = event.GetY()
129 self.log.WriteText("x, y = %s\n" % str((self.x, self.y)))
a08cbc01 130 event.Skip()
bb0054cd 131
185d7c3e
RD
132
133 def getColumnText(self, index, col):
134 item = self.list.GetItem(index, col)
135 return item.GetText()
136
137
bb0054cd
RD
138 def OnItemSelected(self, event):
139 self.currentItem = event.m_itemIndex
9416aa89
RD
140 self.log.WriteText("OnItemSelected: %s, %s, %s, %s\n" %
141 (self.currentItem,
142 self.list.GetItemText(self.currentItem),
185d7c3e
RD
143 self.getColumnText(self.currentItem, 1),
144 self.getColumnText(self.currentItem, 2)))
9416aa89
RD
145 if self.currentItem == 10:
146 self.log.WriteText("OnItemSelected: Veto'd selection\n")
147 #event.Veto() # doesn't work
148 # this does
149 self.list.SetItemState(10, 0, wxLIST_STATE_SELECTED)
185d7c3e 150
c368d904
RD
151 def OnItemActivated(self, event):
152 self.currentItem = event.m_itemIndex
153 self.log.WriteText("OnItemActivated: %s\n" % self.list.GetItemText(self.currentItem))
154
8bf5d46e
RD
155 def OnItemDelete(self, event):
156 self.log.WriteText("OnItemDelete\n")
157
dcd38683
RD
158 def OnColClick(self, event):
159 self.log.WriteText("OnColClick: %d\n" % event.m_col)
160 self.col = event.m_col
161 self.list.SortItems(self.ColumnSorter)
162
163 def ColumnSorter(self, key1, key2):
164 item1 = musicdata[key1][self.col]
165 item2 = musicdata[key2][self.col]
c7e7022c
RD
166
167 # when the items are identical, compare someting else to make the sort key unique...
168 if item1 == item2: return key1 - key2
dcd38683
RD
169 elif item1 < item2: return -1
170 else: return 1
171
8bf5d46e 172
bb0054cd
RD
173 def OnDoubleClick(self, event):
174 self.log.WriteText("OnDoubleClick item %s\n" % self.list.GetItemText(self.currentItem))
185d7c3e 175 event.Skip()
a08cbc01 176
bb0054cd
RD
177 def OnRightClick(self, event):
178 self.log.WriteText("OnRightClick %s\n" % self.list.GetItemText(self.currentItem))
e166644c 179 menu = wxMenu()
a08cbc01
RD
180 tPopupID1 = 0
181 tPopupID2 = 1
182 tPopupID3 = 2
8bf5d46e 183 tPopupID4 = 3
e166644c 184 tPopupID5 = 5
d56cebe7
RD
185 #menu.Append(tPopupID1, "One")
186 item = wxMenuItem(menu, tPopupID1,"One")
187 item.SetBitmap(images.getSmilesBitmap())
188 menu.AppendItem(item)
e166644c
RD
189 menu.Append(tPopupID2, "Two")
190 menu.Append(tPopupID3, "Three")
191 menu.Append(tPopupID4, "DeleteAllItems")
192 menu.Append(tPopupID5, "GetItem")
a08cbc01
RD
193 EVT_MENU(self, tPopupID1, self.OnPopupOne)
194 EVT_MENU(self, tPopupID2, self.OnPopupTwo)
195 EVT_MENU(self, tPopupID3, self.OnPopupThree)
8bf5d46e 196 EVT_MENU(self, tPopupID4, self.OnPopupFour)
e166644c
RD
197 EVT_MENU(self, tPopupID5, self.OnPopupFive)
198 self.PopupMenu(menu, wxPoint(self.x, self.y))
199 menu.Destroy()
185d7c3e 200 event.Skip()
a08cbc01
RD
201
202 def OnPopupOne(self, event):
203 self.log.WriteText("Popup one\n")
204
205 def OnPopupTwo(self, event):
206 self.log.WriteText("Popup two\n")
207
208 def OnPopupThree(self, event):
209 self.log.WriteText("Popup three\n")
cf694132 210
8bf5d46e
RD
211 def OnPopupFour(self, event):
212 self.list.DeleteAllItems()
213
e166644c
RD
214 def OnPopupFive(self, event):
215 item = self.list.GetItem(self.currentItem)
216 print item.m_text, item.m_itemId, self.list.GetItemData(self.currentItem)
217
cf694132
RD
218 def OnSize(self, event):
219 w,h = self.GetClientSizeTuple()
220 self.list.SetDimensions(0, 0, w, h)
221
222
223
224
a08cbc01
RD
225
226
cf694132
RD
227#---------------------------------------------------------------------------
228
229def runTest(frame, nb, log):
230 win = TestListCtrlPanel(nb, log)
231 return win
232
233#---------------------------------------------------------------------------
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250overview = """\
251A list control presents lists in a number of formats: list view, report view, icon view and small icon view. Elements are numbered from zero.
252
cf694132 253"""