]> git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/wxPIA_book/Chapter-13/list_report_etc.py
don't use strlen() to verify the length of the string as it can contain embedded...
[wxWidgets.git] / wxPython / samples / wxPIA_book / Chapter-13 / list_report_etc.py
1 import wx
2 import sys, glob, random
3 import data
4
5 class DemoFrame(wx.Frame):
6 def __init__(self):
7 wx.Frame.__init__(self, None, -1,
8 "Other wx.ListCtrl Stuff",
9 size=(700,500))
10 self.list = None
11 self.editable = False
12 self.MakeMenu()
13 self.MakeListCtrl()
14
15
16 def MakeListCtrl(self, otherflags=0):
17 # if we already have a listctrl then get rid of it
18 if self.list:
19 self.list.Destroy()
20
21 if self.editable:
22 otherflags |= wx.LC_EDIT_LABELS
23
24 # load some images into an image list
25 il = wx.ImageList(16,16, True)
26 for name in glob.glob("smicon??.png"):
27 bmp = wx.Bitmap(name, wx.BITMAP_TYPE_PNG)
28 il_max = il.Add(bmp)
29
30 # create the list control
31 self.list = wx.ListCtrl(self, -1, style=wx.LC_REPORT|otherflags)
32
33 # assign the image list to it
34 self.list.AssignImageList(il, wx.IMAGE_LIST_SMALL)
35
36 # Add some columns
37 for col, text in enumerate(data.columns):
38 self.list.InsertColumn(col, text)
39
40 # add the rows
41 for row, item in enumerate(data.rows):
42 index = self.list.InsertStringItem(sys.maxint, item[0])
43 for col, text in enumerate(item[1:]):
44 self.list.SetStringItem(index, col+1, text)
45
46 # give each item a random image
47 img = random.randint(0, il_max)
48 self.list.SetItemImage(index, img, img)
49
50 # set the data value for each item to be its position in
51 # the data list
52 self.list.SetItemData(index, row)
53
54
55 # set the width of the columns in various ways
56 self.list.SetColumnWidth(0, 120)
57 self.list.SetColumnWidth(1, wx.LIST_AUTOSIZE)
58 self.list.SetColumnWidth(2, wx.LIST_AUTOSIZE)
59 self.list.SetColumnWidth(3, wx.LIST_AUTOSIZE_USEHEADER)
60
61 # bind some interesting events
62 self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected, self.list)
63 self.Bind(wx.EVT_LIST_ITEM_DESELECTED, self.OnItemDeselected, self.list)
64 self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnItemActivated, self.list)
65
66 # in case we are recreating the list tickle the frame a bit so
67 # it will redo the layout
68 self.SendSizeEvent()
69
70
71 def MakeMenu(self):
72 mbar = wx.MenuBar()
73 menu = wx.Menu()
74 item = menu.Append(-1, "E&xit\tAlt-X")
75 self.Bind(wx.EVT_MENU, self.OnExit, item)
76 mbar.Append(menu, "&File")
77
78 menu = wx.Menu()
79 item = menu.Append(-1, "Sort ascending")
80 self.Bind(wx.EVT_MENU, self.OnSortAscending, item)
81 item = menu.Append(-1, "Sort descending")
82 self.Bind(wx.EVT_MENU, self.OnSortDescending, item)
83 item = menu.Append(-1, "Sort by submitter")
84 self.Bind(wx.EVT_MENU, self.OnSortBySubmitter, item)
85
86 menu.AppendSeparator()
87 item = menu.Append(-1, "Show selected")
88 self.Bind(wx.EVT_MENU, self.OnShowSelected, item)
89 item = menu.Append(-1, "Select all")
90 self.Bind(wx.EVT_MENU, self.OnSelectAll, item)
91 item = menu.Append(-1, "Select none")
92 self.Bind(wx.EVT_MENU, self.OnSelectNone, item)
93
94 menu.AppendSeparator()
95 item = menu.Append(-1, "Set item text colour")
96 self.Bind(wx.EVT_MENU, self.OnSetTextColour, item)
97 item = menu.Append(-1, "Set item background colour")
98 self.Bind(wx.EVT_MENU, self.OnSetBGColour, item)
99
100 menu.AppendSeparator()
101 item = menu.Append(-1, "Enable item editing", kind=wx.ITEM_CHECK)
102 self.Bind(wx.EVT_MENU, self.OnEnableEditing, item)
103 item = menu.Append(-1, "Edit current item")
104 self.Bind(wx.EVT_MENU, self.OnEditItem, item)
105 mbar.Append(menu, "&Demo")
106
107 self.SetMenuBar(mbar)
108
109
110
111 def OnExit(self, evt):
112 self.Close()
113
114
115 def OnItemSelected(self, evt):
116 item = evt.GetItem()
117 print "Item selected:", item.GetText()
118
119 def OnItemDeselected(self, evt):
120 item = evt.GetItem()
121 print "Item deselected:", item.GetText()
122
123 def OnItemActivated(self, evt):
124 item = evt.GetItem()
125 print "Item activated:", item.GetText()
126
127 def OnSortAscending(self, evt):
128 # recreate the listctrl with a sort style
129 self.MakeListCtrl(wx.LC_SORT_ASCENDING)
130
131 def OnSortDescending(self, evt):
132 # recreate the listctrl with a sort style
133 self.MakeListCtrl(wx.LC_SORT_DESCENDING)
134
135 def OnSortBySubmitter(self, evt):
136 def compare_func(row1, row2):
137 # compare the values in the 4th col of the data
138 val1 = data.rows[row1][3]
139 val2 = data.rows[row2][3]
140 if val1 < val2: return -1
141 if val1 > val2: return 1
142 return 0
143
144 self.list.SortItems(compare_func)
145
146
147
148 def OnShowSelected(self, evt):
149 print "These items are selected:"
150 index = self.list.GetFirstSelected()
151 if index == -1:
152 print "\tNone"
153 return
154 while index != -1:
155 item = self.list.GetItem(index)
156 print "\t%s" % item.GetText()
157 index = self.list.GetNextSelected(index)
158
159 def OnSelectAll(self, evt):
160 for index in range(self.list.GetItemCount()):
161 self.list.Select(index, True)
162
163 def OnSelectNone(self, evt):
164 index = self.list.GetFirstSelected()
165 while index != -1:
166 self.list.Select(index, False)
167 index = self.list.GetNextSelected(index)
168
169
170 def OnSetTextColour(self, evt):
171 dlg = wx.ColourDialog(self)
172 if dlg.ShowModal() == wx.ID_OK:
173 colour = dlg.GetColourData().GetColour()
174 index = self.list.GetFirstSelected()
175 while index != -1:
176 self.list.SetItemTextColour(index, colour)
177 index = self.list.GetNextSelected(index)
178 dlg.Destroy()
179
180 def OnSetBGColour(self, evt):
181 dlg = wx.ColourDialog(self)
182 if dlg.ShowModal() == wx.ID_OK:
183 colour = dlg.GetColourData().GetColour()
184 index = self.list.GetFirstSelected()
185 while index != -1:
186 self.list.SetItemBackgroundColour(index, colour)
187 index = self.list.GetNextSelected(index)
188 dlg.Destroy()
189
190
191 def OnEnableEditing(self, evt):
192 self.editable = evt.IsChecked()
193 self.MakeListCtrl()
194
195 def OnEditItem(self, evt):
196 index = self.list.GetFirstSelected()
197 if index != -1:
198 self.list.EditLabel(index)
199
200
201 class DemoApp(wx.App):
202 def OnInit(self):
203 frame = DemoFrame()
204 self.SetTopWindow(frame)
205 print "Program output appears here..."
206 frame.Show()
207 return True
208
209 app = DemoApp(redirect=True)
210 app.MainLoop()