Added the sample code from wxPython In Action to the samples dir

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@42925 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robin Dunn
2006-11-01 22:36:23 +00:00
parent bb2775b9e8
commit be05b43451
184 changed files with 9122 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
# Just some simple data structures for the report mode listctrl examples
columns = ["Request ID", "Summary", "Date", "Submitted By"]
rows = [
("987441", "additions to RTTI?", "2004-07-08 10:22", "g00fy"),
("846368", "wxTextCtrl - disable auto-scrolling", "2003-11-20 21:25", "ryannpcs"),
("846367", "Less flicker when resizing a window", "2003-11-20 21:24", "ryannpcs"),
("846366", "Wishlist - wxDbGetConnection return value", "2003-11-20 21:23", "ryannpcs"),
("846364", "wxPostscriptDC with floating point coordinates", "2003-11-20 21:22", "ryannpcs"),
("846363", "Wishlist - Better wxString Formatting", "2003-11-20 21:22", "ryannpcs"),
("846362", "Wishlist - Crossplatform wxRichText Widget", "2003-11-20 21:20", "ryannpcs"),
("953341", "Support for paper trays when printing", "2004-05-13 08:01", "tonye"),
("952466", "mac menu - Window menu", "2004-05-12 03:19", "pimbuur"),
("928899", "FloatCanvas demo should work with numarray", "2004-04-03 08:30", "glchapman"),
("912714", "wxGrid: Support for Search / Replace", "2004-03-09 05:46", "rclund"),
("901061", "wxComboBox - add small icons as in MSW CComboBoxEx ", "2004-02-20 04:04", "tomash"),
("900768", "Please add more codepages support to your source built", "2004-02-19 15:49", "jsat66"),
("894921", "trigger on event-system creation", "2004-02-11 08:10", "g00fy"),
("869808", "HitTest in wxCheckListBox", "2004-01-03 01:22", "dickkniep"),
("863306", "wxGrid - Thaw/Freeze column/row", "2003-12-19 17:54", "zinit"),
("975435", "wxMenu anchor right position in wxMenuBar", "2004-06-18 08:44", "jmt2715"),
("969811", "wxColourEnumerator", "2004-06-09 11:41", "wyo"),
("959849", "wx.Grid gridlines past max row/col", "2004-05-24 19:39", "dodywijaya"),
("959158", "wxGrid: Arbitrary controls in the grid", "2004-05-23 16:56", "somecoder"),
("953824", "mac menu - Window menu", "2004-05-14 02:06", "pimbuur"),
("863301", "wxTextCtrl - edit mode", "2003-12-19 17:48", "zinit"),
("855902", "virtual window classes", "2003-12-07 12:39", "cursorstar"),
("852379", "wxGrid row/col size limits", "2003-12-01 15:47", "jbrouwers"),
("846375", "wxGraphicsPath", "2003-11-20 21:30", "ryannpcs"),
("846374", "wxToolBar - return tool at position", "2003-11-20 21:29", "ryannpcs"),
("846373", "Scrolling improvements", "2003-11-20 21:28", "ryannpcs"),
("846372", "Hooks for standard remote events", "2003-11-20 21:27", "ryannpcs"),
("846370", "wxDial - dial widget", "2003-11-20 21:27", "ryannpcs"),
("846369", "wxGird - auto-scrolling", "2003-11-20 21:26", "ryannpcs"),
("846361", "Wishlist - wxStaticText that takes fonts, colors, etc.", "2003-11-20 21:19", "ryannpcs"),
("819559", "wxListCtrl column widths in wxLC_ICON mode ", "2003-10-07 13:34", "nwmoriarty"),
("817429", "OS X wheel mouse", "2003-10-03 14:05", "mdcowles"),
]

Binary file not shown.

After

Width:  |  Height:  |  Size: 359 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 351 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 454 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 337 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 435 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 478 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 382 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 445 B

View File

@@ -0,0 +1,32 @@
import wx
import sys, glob
class DemoFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1,
"wx.ListCtrl in wx.LC_ICON mode",
size=(600,400))
# load some images into an image list
il = wx.ImageList(32,32, True)
for name in glob.glob("icon??.png"):
bmp = wx.Bitmap(name, wx.BITMAP_TYPE_PNG)
il_max = il.Add(bmp)
# create the list control
self.list = wx.ListCtrl(self, -1,
style=wx.LC_ICON | wx.LC_AUTOARRANGE)
# assign the image list to it
self.list.AssignImageList(il, wx.IMAGE_LIST_NORMAL)
# create some items for the list
for x in range(25):
img = x % (il_max+1)
self.list.InsertImageStringItem(x,
"This is item %02d" % x, img)
app = wx.PySimpleApp()
frame = DemoFrame()
frame.Show()
app.MainLoop()

View File

@@ -0,0 +1,34 @@
import wx
import sys, glob
class DemoFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1,
"wx.ListCtrl in wx.LC_LIST mode",
size=(600,400))
# load some images into an image list
il = wx.ImageList(16,16, True)
for name in glob.glob("smicon??.png"):
bmp = wx.Bitmap(name, wx.BITMAP_TYPE_PNG)
il_max = il.Add(bmp)
# create the list control
self.list = wx.ListCtrl(self, -1, style=wx.LC_LIST)
# assign the image list to it
self.list.AssignImageList(il, wx.IMAGE_LIST_SMALL)
# create some items for the list
for x in range(25):
img = x % (il_max+1)
self.list.InsertImageStringItem(x,
"This is item %02d" % x,
img)
app = wx.PySimpleApp()
frame = DemoFrame()
frame.Show()
app.MainLoop()

View File

@@ -0,0 +1,42 @@
import wx
import sys, glob, random
import data
class DemoFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1,
"wx.ListCtrl in wx.LC_REPORT mode",
size=(600,400))
il = wx.ImageList(16,16, True)
for name in glob.glob("smicon??.png"):
bmp = wx.Bitmap(name, wx.BITMAP_TYPE_PNG)
il_max = il.Add(bmp)
self.list = wx.ListCtrl(self, -1, style=wx.LC_REPORT)
self.list.AssignImageList(il, wx.IMAGE_LIST_SMALL)
# Add some columns
for col, text in enumerate(data.columns):
self.list.InsertColumn(col, text)
# add the rows
for item in data.rows:
index = self.list.InsertStringItem(sys.maxint, item[0])
for col, text in enumerate(item[1:]):
self.list.SetStringItem(index, col+1, text)
# give each item a random image
img = random.randint(0, il_max)
self.list.SetItemImage(index, img, img)
# set the width of the columns in various ways
self.list.SetColumnWidth(0, 120)
self.list.SetColumnWidth(1, wx.LIST_AUTOSIZE)
self.list.SetColumnWidth(2, wx.LIST_AUTOSIZE)
self.list.SetColumnWidth(3, wx.LIST_AUTOSIZE_USEHEADER)
app = wx.PySimpleApp()
frame = DemoFrame()
frame.Show()
app.MainLoop()

View File

@@ -0,0 +1,70 @@
import wx
import wx.lib.mixins.listctrl
import sys, glob, random
import data
class DemoFrame(wx.Frame, wx.lib.mixins.listctrl.ColumnSorterMixin):
def __init__(self):
wx.Frame.__init__(self, None, -1,
"wx.ListCtrl with ColumnSorterMixin",
size=(600,400))
# load some images into an image list
il = wx.ImageList(16,16, True)
for name in glob.glob("smicon??.png"):
bmp = wx.Bitmap(name, wx.BITMAP_TYPE_PNG)
il_max = il.Add(bmp)
# add some arrows for the column sorter
self.up = il.AddWithColourMask(
wx.Bitmap("sm_up.bmp", wx.BITMAP_TYPE_BMP), "blue")
self.dn = il.AddWithColourMask(
wx.Bitmap("sm_down.bmp", wx.BITMAP_TYPE_BMP), "blue")
# create the list control
self.list = wx.ListCtrl(self, -1, style=wx.LC_REPORT)
# assign the image list to it
self.list.AssignImageList(il, wx.IMAGE_LIST_SMALL)
# Add some columns
for col, text in enumerate(data.columns):
self.list.InsertColumn(col, text)
# add the rows
self.itemDataMap = {}
for item in data.rows:
index = self.list.InsertStringItem(sys.maxint, item[0])
for col, text in enumerate(item[1:]):
self.list.SetStringItem(index, col+1, text)
# give each item a data value, and map it back to the
# item values, for the column sorter
self.list.SetItemData(index, index)
self.itemDataMap[index] = item
# give each item a random image
img = random.randint(0, il_max)
self.list.SetItemImage(index, img, img)
# set the width of the columns in various ways
self.list.SetColumnWidth(0, 120)
self.list.SetColumnWidth(1, wx.LIST_AUTOSIZE)
self.list.SetColumnWidth(2, wx.LIST_AUTOSIZE)
self.list.SetColumnWidth(3, wx.LIST_AUTOSIZE_USEHEADER)
# initialize the column sorter
wx.lib.mixins.listctrl.ColumnSorterMixin.__init__(self,
len(data.columns))
def GetListCtrl(self):
return self.list
def GetSortImages(self):
return (self.dn, self.up)
app = wx.PySimpleApp()
frame = DemoFrame()
frame.Show()
app.MainLoop()

View File

@@ -0,0 +1,210 @@
import wx
import sys, glob, random
import data
class DemoFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1,
"Other wx.ListCtrl Stuff",
size=(700,500))
self.list = None
self.editable = False
self.MakeMenu()
self.MakeListCtrl()
def MakeListCtrl(self, otherflags=0):
# if we already have a listctrl then get rid of it
if self.list:
self.list.Destroy()
if self.editable:
otherflags |= wx.LC_EDIT_LABELS
# load some images into an image list
il = wx.ImageList(16,16, True)
for name in glob.glob("smicon??.png"):
bmp = wx.Bitmap(name, wx.BITMAP_TYPE_PNG)
il_max = il.Add(bmp)
# create the list control
self.list = wx.ListCtrl(self, -1, style=wx.LC_REPORT|otherflags)
# assign the image list to it
self.list.AssignImageList(il, wx.IMAGE_LIST_SMALL)
# Add some columns
for col, text in enumerate(data.columns):
self.list.InsertColumn(col, text)
# add the rows
for row, item in enumerate(data.rows):
index = self.list.InsertStringItem(sys.maxint, item[0])
for col, text in enumerate(item[1:]):
self.list.SetStringItem(index, col+1, text)
# give each item a random image
img = random.randint(0, il_max)
self.list.SetItemImage(index, img, img)
# set the data value for each item to be its position in
# the data list
self.list.SetItemData(index, row)
# set the width of the columns in various ways
self.list.SetColumnWidth(0, 120)
self.list.SetColumnWidth(1, wx.LIST_AUTOSIZE)
self.list.SetColumnWidth(2, wx.LIST_AUTOSIZE)
self.list.SetColumnWidth(3, wx.LIST_AUTOSIZE_USEHEADER)
# bind some interesting events
self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected, self.list)
self.Bind(wx.EVT_LIST_ITEM_DESELECTED, self.OnItemDeselected, self.list)
self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnItemActivated, self.list)
# in case we are recreating the list tickle the frame a bit so
# it will redo the layout
self.SendSizeEvent()
def MakeMenu(self):
mbar = wx.MenuBar()
menu = wx.Menu()
item = menu.Append(-1, "E&xit\tAlt-X")
self.Bind(wx.EVT_MENU, self.OnExit, item)
mbar.Append(menu, "&File")
menu = wx.Menu()
item = menu.Append(-1, "Sort ascending")
self.Bind(wx.EVT_MENU, self.OnSortAscending, item)
item = menu.Append(-1, "Sort descending")
self.Bind(wx.EVT_MENU, self.OnSortDescending, item)
item = menu.Append(-1, "Sort by submitter")
self.Bind(wx.EVT_MENU, self.OnSortBySubmitter, item)
menu.AppendSeparator()
item = menu.Append(-1, "Show selected")
self.Bind(wx.EVT_MENU, self.OnShowSelected, item)
item = menu.Append(-1, "Select all")
self.Bind(wx.EVT_MENU, self.OnSelectAll, item)
item = menu.Append(-1, "Select none")
self.Bind(wx.EVT_MENU, self.OnSelectNone, item)
menu.AppendSeparator()
item = menu.Append(-1, "Set item text colour")
self.Bind(wx.EVT_MENU, self.OnSetTextColour, item)
item = menu.Append(-1, "Set item background colour")
self.Bind(wx.EVT_MENU, self.OnSetBGColour, item)
menu.AppendSeparator()
item = menu.Append(-1, "Enable item editing", kind=wx.ITEM_CHECK)
self.Bind(wx.EVT_MENU, self.OnEnableEditing, item)
item = menu.Append(-1, "Edit current item")
self.Bind(wx.EVT_MENU, self.OnEditItem, item)
mbar.Append(menu, "&Demo")
self.SetMenuBar(mbar)
def OnExit(self, evt):
self.Close()
def OnItemSelected(self, evt):
item = evt.GetItem()
print "Item selected:", item.GetText()
def OnItemDeselected(self, evt):
item = evt.GetItem()
print "Item deselected:", item.GetText()
def OnItemActivated(self, evt):
item = evt.GetItem()
print "Item activated:", item.GetText()
def OnSortAscending(self, evt):
# recreate the listctrl with a sort style
self.MakeListCtrl(wx.LC_SORT_ASCENDING)
def OnSortDescending(self, evt):
# recreate the listctrl with a sort style
self.MakeListCtrl(wx.LC_SORT_DESCENDING)
def OnSortBySubmitter(self, evt):
def compare_func(row1, row2):
# compare the values in the 4th col of the data
val1 = data.rows[row1][3]
val2 = data.rows[row2][3]
if val1 < val2: return -1
if val1 > val2: return 1
return 0
self.list.SortItems(compare_func)
def OnShowSelected(self, evt):
print "These items are selected:"
index = self.list.GetFirstSelected()
if index == -1:
print "\tNone"
return
while index != -1:
item = self.list.GetItem(index)
print "\t%s" % item.GetText()
index = self.list.GetNextSelected(index)
def OnSelectAll(self, evt):
for index in range(self.list.GetItemCount()):
self.list.Select(index, True)
def OnSelectNone(self, evt):
index = self.list.GetFirstSelected()
while index != -1:
self.list.Select(index, False)
index = self.list.GetNextSelected(index)
def OnSetTextColour(self, evt):
dlg = wx.ColourDialog(self)
if dlg.ShowModal() == wx.ID_OK:
colour = dlg.GetColourData().GetColour()
index = self.list.GetFirstSelected()
while index != -1:
self.list.SetItemTextColour(index, colour)
index = self.list.GetNextSelected(index)
dlg.Destroy()
def OnSetBGColour(self, evt):
dlg = wx.ColourDialog(self)
if dlg.ShowModal() == wx.ID_OK:
colour = dlg.GetColourData().GetColour()
index = self.list.GetFirstSelected()
while index != -1:
self.list.SetItemBackgroundColour(index, colour)
index = self.list.GetNextSelected(index)
dlg.Destroy()
def OnEnableEditing(self, evt):
self.editable = evt.IsChecked()
self.MakeListCtrl()
def OnEditItem(self, evt):
index = self.list.GetFirstSelected()
if index != -1:
self.list.EditLabel(index)
class DemoApp(wx.App):
def OnInit(self):
frame = DemoFrame()
self.SetTopWindow(frame)
print "Program output appears here..."
frame.Show()
return True
app = DemoApp(redirect=True)
app.MainLoop()

View File

@@ -0,0 +1,66 @@
import wx
import sys, glob, random
import data
class DataSource:
"""
A simple data source class that just uses our sample data items.
A real data source class would manage fetching items from a
database or similar.
"""
def GetColumnHeaders(self):
return data.columns
def GetCount(self):
return len(data.rows)
def GetItem(self, index):
return data.rows[index]
def UpdateCache(self, start, end):
pass
class VirtualListCtrl(wx.ListCtrl):
"""
A generic virtual listctrl that fetches data from a DataSource.
"""
def __init__(self, parent, dataSource):
wx.ListCtrl.__init__(self, parent,
style=wx.LC_REPORT|wx.LC_SINGLE_SEL|wx.LC_VIRTUAL)
self.dataSource = dataSource
self.Bind(wx.EVT_LIST_CACHE_HINT, self.DoCacheItems)
self.SetItemCount(dataSource.GetCount())
columns = dataSource.GetColumnHeaders()
for col, text in enumerate(columns):
self.InsertColumn(col, text)
def DoCacheItems(self, evt):
self.dataSource.UpdateCache(
evt.GetCacheFrom(), evt.GetCacheTo())
def OnGetItemText(self, item, col):
data = self.dataSource.GetItem(item)
return data[col]
def OnGetItemAttr(self, item): return None
def OnGetItemImage(self, item): return -1
class DemoFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1,
"Virtual wx.ListCtrl",
size=(600,400))
self.list = VirtualListCtrl(self, DataSource())
app = wx.PySimpleApp()
frame = DemoFrame()
frame.Show()
app.MainLoop()

View File

@@ -0,0 +1,35 @@
import wx
import sys, glob
class DemoFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1,
"wx.ListCtrl in wx.LC_SMALL_ICON mode",
size=(600,400))
# load some images into an image list
il = wx.ImageList(16,16, True)
for name in glob.glob("smicon??.png"):
bmp = wx.Bitmap(name, wx.BITMAP_TYPE_PNG)
il_max = il.Add(bmp)
# create the list control
self.list = wx.ListCtrl(self, -1,
style=wx.LC_SMALL_ICON
| wx.LC_AUTOARRANGE
)
# assign the image list to it
self.list.AssignImageList(il, wx.IMAGE_LIST_SMALL)
# create some items for the list
for x in range(25):
img = x % (il_max+1)
self.list.InsertImageStringItem(x,
"This is item %02d" % x,
img)
app = wx.PySimpleApp()
frame = DemoFrame()
frame.Show()
app.MainLoop()

Binary file not shown.

After

Width:  |  Height:  |  Size: 246 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 246 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 266 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 212 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 205 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 249 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 257 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 226 B