]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxListCtrl.py
added missing refresh when changing focus in wxListCtrl, added test for it in the...
[wxWidgets.git] / wxPython / demo / wxListCtrl.py
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
14 from wxPython.wx import *
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 }
59
60 import images
61
62 class TestListCtrlPanel(wxPanel):
63 def __init__(self, parent, log):
64 wxPanel.__init__(self, parent, -1, style=wxWANTS_CHARS)
65
66 self.log = log
67 tID = wxNewId()
68
69 self.il = wxImageList(16, 16)
70 bmp = images.getSmilesBitmap()
71 #idx1 = self.il.AddWithColourMask(bmp, wxWHITE)
72 idx1 = self.il.Add(bmp)
73
74 self.list = wxListCtrl(self, tID,
75 style=wxLC_REPORT|wxSUNKEN_BORDER)
76 self.list.SetImageList(self.il, wxIMAGE_LIST_SMALL)
77
78 # Why doesn't this show up on MSW???
79 self.list.SetToolTip(wxToolTip("This is a ToolTip!"))
80
81 self.list.InsertColumn(0, "Artist")
82 self.list.InsertColumn(1, "Title", wxLIST_FORMAT_RIGHT)
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)
91
92 self.list.SetColumnWidth(0, wxLIST_AUTOSIZE)
93 self.list.SetColumnWidth(1, wxLIST_AUTOSIZE)
94 ##self.list.SetColumnWidth(2, wxLIST_AUTOSIZE)
95
96 self.list.SetItemState(5, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED)
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)
105
106 self.currentItem = 0
107 EVT_SIZE(self, self.OnSize)
108 EVT_LIST_ITEM_SELECTED(self, tID, self.OnItemSelected)
109 EVT_LIST_ITEM_ACTIVATED(self, tID, self.OnItemActivated)
110 EVT_LIST_DELETE_ITEM(self, tID, self.OnItemDelete)
111 EVT_LIST_COL_CLICK(self, tID, self.OnColClick)
112 EVT_LEFT_DCLICK(self.list, self.OnDoubleClick)
113 EVT_RIGHT_DOWN(self.list, self.OnRightDown)
114
115 # for wxMSW
116 EVT_COMMAND_RIGHT_CLICK(self.list, tID, self.OnRightClick)
117
118 # for wxGTK
119 EVT_RIGHT_UP(self.list, self.OnRightClick)
120
121
122 def OnRightDown(self, event):
123 self.x = event.GetX()
124 self.y = event.GetY()
125 self.log.WriteText("x, y = %s\n" % str((self.x, self.y)))
126 event.Skip()
127
128
129 def getColumnText(self, index, col):
130 item = self.list.GetItem(index, col)
131 return item.GetText()
132
133
134 def OnItemSelected(self, event):
135 self.currentItem = event.m_itemIndex
136 self.log.WriteText("OnItemSelected: %s, %s, %s, %s\n" %
137 (self.currentItem,
138 self.list.GetItemText(self.currentItem),
139 self.getColumnText(self.currentItem, 1),
140 self.getColumnText(self.currentItem, 2)))
141 if self.currentItem == 10:
142 self.log.WriteText("OnItemSelected: Veto'd selection\n")
143 #event.Veto() # doesn't work
144 # this does
145 self.list.SetItemState(10, 0, wxLIST_STATE_SELECTED)
146
147 def OnItemActivated(self, event):
148 self.currentItem = event.m_itemIndex
149 self.log.WriteText("OnItemActivated: %s\n" % self.list.GetItemText(self.currentItem))
150
151 def OnItemDelete(self, event):
152 self.log.WriteText("OnItemDelete\n")
153
154 def OnColClick(self, event):
155 self.log.WriteText("OnColClick: %d\n" % event.m_col)
156 self.col = event.m_col
157 self.list.SortItems(self.ColumnSorter)
158
159 def ColumnSorter(self, key1, key2):
160 item1 = musicdata[key1][self.col]
161 item2 = musicdata[key2][self.col]
162 if item1 == item2: return 0
163 elif item1 < item2: return -1
164 else: return 1
165
166
167 def OnDoubleClick(self, event):
168 self.log.WriteText("OnDoubleClick item %s\n" % self.list.GetItemText(self.currentItem))
169 event.Skip()
170
171 def OnRightClick(self, event):
172 self.log.WriteText("OnRightClick %s\n" % self.list.GetItemText(self.currentItem))
173 menu = wxMenu()
174 tPopupID1 = 0
175 tPopupID2 = 1
176 tPopupID3 = 2
177 tPopupID4 = 3
178 tPopupID5 = 5
179 #menu.Append(tPopupID1, "One")
180 item = wxMenuItem(menu, tPopupID1,"One")
181 item.SetBitmap(images.getSmilesBitmap())
182 menu.AppendItem(item)
183 menu.Append(tPopupID2, "Two")
184 menu.Append(tPopupID3, "Three")
185 menu.Append(tPopupID4, "DeleteAllItems")
186 menu.Append(tPopupID5, "GetItem")
187 EVT_MENU(self, tPopupID1, self.OnPopupOne)
188 EVT_MENU(self, tPopupID2, self.OnPopupTwo)
189 EVT_MENU(self, tPopupID3, self.OnPopupThree)
190 EVT_MENU(self, tPopupID4, self.OnPopupFour)
191 EVT_MENU(self, tPopupID5, self.OnPopupFive)
192 self.PopupMenu(menu, wxPoint(self.x, self.y))
193 menu.Destroy()
194 event.Skip()
195
196 def OnPopupOne(self, event):
197 self.log.WriteText("Popup one\n")
198
199 def OnPopupTwo(self, event):
200 self.log.WriteText("Popup two\n")
201
202 def OnPopupThree(self, event):
203 self.log.WriteText("Popup three\n")
204
205 def OnPopupFour(self, event):
206 self.list.DeleteAllItems()
207
208 def OnPopupFive(self, event):
209 item = self.list.GetItem(self.currentItem)
210 print item.m_text, item.m_itemId, self.list.GetItemData(self.currentItem)
211
212 def OnSize(self, event):
213 w,h = self.GetClientSizeTuple()
214 self.list.SetDimensions(0, 0, w, h)
215
216
217
218
219
220
221 #---------------------------------------------------------------------------
222
223 def runTest(frame, nb, log):
224 win = TestListCtrlPanel(nb, log)
225 return win
226
227 #---------------------------------------------------------------------------
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244 overview = """\
245 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.
246
247 """