]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxListCtrl.py
f5fad6d8ed8c4b766f41b3bc819ed72bf05e92d3
[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 from wxPython.lib.mixins.listctrl import wxColumnSorterMixin
16
17 #---------------------------------------------------------------------------
18
19 musicdata = {
20 1 : ("Bad English", "The Price Of Love", "Rock"),
21 2 : ("DNA featuring Suzanne Vega", "Tom's Diner", "Rock"),
22 3 : ("George Michael", "Praying For Time", "Rock"),
23 4 : ("Gloria Estefan", "Here We Are", "Rock"),
24 5 : ("Linda Ronstadt", "Don't Know Much", "Rock"),
25 6 : ("Michael Bolton", "How Am I Supposed To Live Without You", "Blues"),
26 7 : ("Paul Young", "Oh Girl", "Rock"),
27 8 : ("Paula Abdul", "Opposites Attract", "Rock"),
28 9 : ("Richard Marx", "Should've Known Better", "Rock"),
29 10: ("Rod Stewart", "Forever Young", "Rock"),
30 11: ("Roxette", "Dangerous", "Rock"),
31 12: ("Sheena Easton", "The Lover In Me", "Rock"),
32 13: ("Sinead O'Connor", "Nothing Compares 2 U", "Rock"),
33 14: ("Stevie B.", "Because I Love You", "Rock"),
34 15: ("Taylor Dayne", "Love Will Lead You Back", "Rock"),
35 16: ("The Bangles", "Eternal Flame", "Rock"),
36 17: ("Wilson Phillips", "Release Me", "Rock"),
37 18: ("Billy Joel", "Blonde Over Blue", "Rock"),
38 19: ("Billy Joel", "Famous Last Words", "Rock"),
39 20: ("Billy Joel", "Lullabye (Goodnight, My Angel)", "Rock"),
40 21: ("Billy Joel", "The River Of Dreams", "Rock"),
41 22: ("Billy Joel", "Two Thousand Years", "Rock"),
42 23: ("Janet Jackson", "Alright", "Rock"),
43 24: ("Janet Jackson", "Black Cat", "Rock"),
44 25: ("Janet Jackson", "Come Back To Me", "Rock"),
45 26: ("Janet Jackson", "Escapade", "Rock"),
46 27: ("Janet Jackson", "Love Will Never Do (Without You)", "Rock"),
47 28: ("Janet Jackson", "Miss You Much", "Rock"),
48 29: ("Janet Jackson", "Rhythm Nation", "Rock"),
49 30: ("Janet Jackson", "State Of The World", "Rock"),
50 31: ("Janet Jackson", "The Knowledge", "Rock"),
51 32: ("Spyro Gyra", "End of Romanticism", "Jazz"),
52 33: ("Spyro Gyra", "Heliopolis", "Jazz"),
53 34: ("Spyro Gyra", "Jubilee", "Jazz"),
54 35: ("Spyro Gyra", "Little Linda", "Jazz"),
55 36: ("Spyro Gyra", "Morning Dance", "Jazz"),
56 37: ("Spyro Gyra", "Song for Lorraine", "Jazz"),
57 38: ("Yes", "Owner Of A Lonely Heart", "Rock"),
58 39: ("Yes", "Rhythm Of Love", "Rock"),
59 }
60
61 import images
62
63 class TestListCtrlPanel(wxPanel, wxColumnSorterMixin):
64 def __init__(self, parent, log):
65 wxPanel.__init__(self, parent, -1, style=wxWANTS_CHARS)
66
67 self.log = log
68 tID = wxNewId()
69
70 self.il = wxImageList(16, 16)
71 bmp = images.getSmilesBitmap()
72 #idx1 = self.il.AddWithColourMask(bmp, wxWHITE)
73 idx1 = self.il.Add(bmp)
74 bmp = images.getSmallUpArrowBitmap()
75 self.sm_up = self.il.Add(bmp)
76 bmp = images.getSmallDnArrowBitmap()
77 self.sm_dn = self.il.Add(bmp)
78
79
80 self.list = wxListCtrl(self, tID,
81 style=wxLC_REPORT|wxSUNKEN_BORDER)#|wxLC_VRULES|wxLC_HRULES)
82 self.list.SetImageList(self.il, wxIMAGE_LIST_SMALL)
83
84 # Why doesn't this show up on MSW???
85 self.list.SetToolTip(wxToolTip("This is a ToolTip!"))
86
87 if 0:
88 # for normal simple columns, you can add them like this:
89 self.list.InsertColumn(0, "Artist")
90 self.list.InsertColumn(1, "Title", wxLIST_FORMAT_RIGHT)
91 self.list.InsertColumn(2, "Genre")
92 else:
93 # but since we want images on the column header we have to do it the hard way:
94 info = wxListItem()
95 info.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_IMAGE | wxLIST_MASK_FORMAT
96 info.m_image = -1
97 info.m_format = 0
98 info.m_text = "Artist"
99 self.list.InsertColumnInfo(0, info)
100
101 info.m_format = wxLIST_FORMAT_RIGHT
102 info.m_text = "Title"
103 self.list.InsertColumnInfo(1, info)
104
105 info.m_format = 0
106 info.m_text = "Genre"
107 self.list.InsertColumnInfo(2, info)
108
109
110 items = musicdata.items()
111 for x in range(len(items)):
112 key, data = items[x]
113 self.list.InsertImageStringItem(x, data[0], idx1)
114 self.list.SetStringItem(x, 1, data[1])
115 self.list.SetStringItem(x, 2, data[2])
116 self.list.SetItemData(x, key)
117
118 # Now that the list exists we can init the other base class,
119 # see wxPython/lib/mixins/listctrl.py
120 self.itemDataMap = musicdata
121 wxColumnSorterMixin.__init__(self, 3)
122 #self.SortListItems(0, true)
123
124 self.list.SetColumnWidth(0, wxLIST_AUTOSIZE)
125 self.list.SetColumnWidth(1, wxLIST_AUTOSIZE)
126
127 # show how to select an item
128 self.list.SetItemState(5, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED)
129
130 # show how to change the colour of a couple items
131 item = self.list.GetItem(1)
132 item.SetTextColour(wxBLUE)
133 self.list.SetItem(item)
134 item = self.list.GetItem(4)
135 item.SetTextColour(wxRED)
136 self.list.SetItem(item)
137
138 self.currentItem = 0
139 EVT_SIZE(self, self.OnSize)
140 EVT_LIST_ITEM_SELECTED(self, tID, self.OnItemSelected)
141 EVT_LIST_ITEM_ACTIVATED(self, tID, self.OnItemActivated)
142 EVT_LIST_DELETE_ITEM(self, tID, self.OnItemDelete)
143 EVT_LIST_COL_CLICK(self, tID, self.OnColClick)
144 EVT_LIST_COL_RIGHT_CLICK(self, tID, self.OnColRightClick)
145 EVT_LIST_COL_BEGIN_DRAG(self, tID, self.OnColBeginDrag)
146 EVT_LIST_COL_DRAGGING(self, tID, self.OnColDragging)
147 EVT_LIST_COL_END_DRAG(self, tID, self.OnColEndDrag)
148
149 EVT_LEFT_DCLICK(self.list, self.OnDoubleClick)
150 EVT_RIGHT_DOWN(self.list, self.OnRightDown)
151
152 # for wxMSW
153 EVT_COMMAND_RIGHT_CLICK(self.list, tID, self.OnRightClick)
154
155 # for wxGTK
156 EVT_RIGHT_UP(self.list, self.OnRightClick)
157
158
159 # Used by the wxColumnSorterMixin, see wxPython/lib/mixins/listctrl.py
160 def GetListCtrl(self):
161 return self.list
162
163 # Used by the wxColumnSorterMixin, see wxPython/lib/mixins/listctrl.py
164 def GetSortImages(self):
165 return (self.sm_dn, self.sm_up)
166
167
168
169 def OnRightDown(self, event):
170 self.x = event.GetX()
171 self.y = event.GetY()
172 self.log.WriteText("x, y = %s\n" % str((self.x, self.y)))
173 event.Skip()
174
175
176 def getColumnText(self, index, col):
177 item = self.list.GetItem(index, col)
178 return item.GetText()
179
180
181 def OnItemSelected(self, event):
182 self.currentItem = event.m_itemIndex
183 self.log.WriteText("OnItemSelected: %s, %s, %s, %s\n" %
184 (self.currentItem,
185 self.list.GetItemText(self.currentItem),
186 self.getColumnText(self.currentItem, 1),
187 self.getColumnText(self.currentItem, 2)))
188 if self.currentItem == 10:
189 self.log.WriteText("OnItemSelected: Veto'd selection\n")
190 #event.Veto() # doesn't work
191 # this does
192 self.list.SetItemState(10, 0, wxLIST_STATE_SELECTED)
193
194 def OnItemActivated(self, event):
195 self.currentItem = event.m_itemIndex
196 self.log.WriteText("OnItemActivated: %s\n" % self.list.GetItemText(self.currentItem))
197
198 def OnItemDelete(self, event):
199 self.log.WriteText("OnItemDelete\n")
200
201 def OnColClick(self, event):
202 self.log.WriteText("OnColClick: %d\n" % event.GetColumn())
203
204 def OnColRightClick(self, event):
205 self.log.WriteText("OnColRightClick: %d\n" % event.GetColumn())
206
207 def OnColBeginDrag(self, event):
208 self.log.WriteText("OnColBeginDrag\n")
209
210 def OnColDragging(self, event):
211 self.log.WriteText("OnColDragging\n")
212
213 def OnColEndDrag(self, event):
214 self.log.WriteText("OnColEndDrag\n")
215
216 def OnDoubleClick(self, event):
217 self.log.WriteText("OnDoubleClick item %s\n" % self.list.GetItemText(self.currentItem))
218 event.Skip()
219
220 def OnRightClick(self, event):
221 self.log.WriteText("OnRightClick %s\n" % self.list.GetItemText(self.currentItem))
222 menu = wxMenu()
223 tPopupID1 = 0
224 tPopupID2 = 1
225 tPopupID3 = 2
226 tPopupID4 = 3
227 tPopupID5 = 5
228 #menu.Append(tPopupID1, "One")
229 item = wxMenuItem(menu, tPopupID1,"One")
230 item.SetBitmap(images.getSmilesBitmap())
231 menu.AppendItem(item)
232 menu.Append(tPopupID2, "Two")
233 menu.Append(tPopupID3, "Three")
234 menu.Append(tPopupID4, "DeleteAllItems")
235 menu.Append(tPopupID5, "GetItem")
236 EVT_MENU(self, tPopupID1, self.OnPopupOne)
237 EVT_MENU(self, tPopupID2, self.OnPopupTwo)
238 EVT_MENU(self, tPopupID3, self.OnPopupThree)
239 EVT_MENU(self, tPopupID4, self.OnPopupFour)
240 EVT_MENU(self, tPopupID5, self.OnPopupFive)
241 self.PopupMenu(menu, wxPoint(self.x, self.y))
242 menu.Destroy()
243 event.Skip()
244
245 def OnPopupOne(self, event):
246 self.log.WriteText("Popup one\n")
247
248 def OnPopupTwo(self, event):
249 self.log.WriteText("Popup two\n")
250
251 def OnPopupThree(self, event):
252 self.log.WriteText("Popup three\n")
253
254 def OnPopupFour(self, event):
255 self.list.DeleteAllItems()
256
257 def OnPopupFive(self, event):
258 item = self.list.GetItem(self.currentItem)
259 print item.m_text, item.m_itemId, self.list.GetItemData(self.currentItem)
260
261 def OnSize(self, event):
262 w,h = self.GetClientSizeTuple()
263 self.list.SetDimensions(0, 0, w, h)
264
265
266
267
268
269
270 #---------------------------------------------------------------------------
271
272 def runTest(frame, nb, log):
273 win = TestListCtrlPanel(nb, log)
274 return win
275
276 #---------------------------------------------------------------------------
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293 overview = """\
294 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.
295
296 """