]>
Commit | Line | Data |
---|---|---|
15513a80 RD |
1 | #---------------------------------------------------------------------------- |
2 | # Name: ListCtrl_edit.py | |
3 | # Purpose: Testing editing a ListCtrl | |
4 | # | |
5 | # Author: Pim van Heuven | |
6 | # | |
7 | # Created: 2004/10/15 | |
8 | # Copyright: (c) Pim Van Heuven | |
9 | # Licence: wxWindows license | |
10 | #---------------------------------------------------------------------------- | |
11 | ||
12 | import sys | |
13 | import wx | |
14 | import wx.lib.mixins.listctrl as listmix | |
15 | ||
16 | #--------------------------------------------------------------------------- | |
17 | ||
18 | listctrldata = { | |
19 | 1 : ("Hey!", "You can edit", "me!"), | |
20 | 2 : ("Try changing the contents", "by", "clicking"), | |
21 | 3 : ("in", "a", "cell"), | |
22 | 4 : ("See how the length columns", "change", "?"), | |
23 | 5 : ("You can use", "TAB,", "cursor down,"), | |
24 | 6 : ("and cursor up", "to", "navigate"), | |
25 | } | |
26 | ||
27 | #--------------------------------------------------------------------------- | |
28 | ||
29 | class TestListCtrl(wx.ListCtrl, | |
30 | listmix.ListCtrlAutoWidthMixin, | |
31 | listmix.TextEditMixin): | |
32 | ||
33 | def __init__(self, parent, ID, pos=wx.DefaultPosition, | |
34 | size=wx.DefaultSize, style=0): | |
35 | wx.ListCtrl.__init__(self, parent, ID, pos, size, style) | |
36 | ||
0bee2fce | 37 | listmix.ListCtrlAutoWidthMixin.__init__(self) |
15513a80 RD |
38 | self.Populate() |
39 | listmix.TextEditMixin.__init__(self) | |
40 | ||
41 | def Populate(self): | |
42 | # for normal, simple columns, you can add them like this: | |
43 | self.InsertColumn(0, "Column 1") | |
44 | self.InsertColumn(1, "Column 2") | |
45 | self.InsertColumn(2, "Column 3") | |
46 | self.InsertColumn(3, "Len 1", wx.LIST_FORMAT_RIGHT) | |
47 | self.InsertColumn(4, "Len 2", wx.LIST_FORMAT_RIGHT) | |
48 | self.InsertColumn(5, "Len 3", wx.LIST_FORMAT_RIGHT) | |
49 | ||
50 | items = listctrldata.items() | |
51 | for key, data in items: | |
52 | index = self.InsertStringItem(sys.maxint, data[0]) | |
53 | self.SetStringItem(index, 0, data[0]) | |
54 | self.SetStringItem(index, 1, data[1]) | |
55 | self.SetStringItem(index, 2, data[2]) | |
56 | self.SetItemData(index, key) | |
57 | ||
58 | self.SetColumnWidth(0, wx.LIST_AUTOSIZE) | |
59 | self.SetColumnWidth(1, wx.LIST_AUTOSIZE) | |
60 | self.SetColumnWidth(2, 100) | |
61 | ||
62 | self.currentItem = 0 | |
63 | ||
64 | ||
65 | def SetStringItem(self, index, col, data): | |
66 | if col in range(3): | |
67 | wx.ListCtrl.SetStringItem(self, index, col, data) | |
68 | wx.ListCtrl.SetStringItem(self, index, 3+col, str(len(data))) | |
69 | else: | |
70 | try: | |
71 | datalen = int(data) | |
72 | except: | |
73 | return | |
74 | ||
75 | wx.ListCtrl.SetStringItem(self, index, col, data) | |
76 | ||
77 | data = self.GetItem(index, col-3).GetText() | |
78 | wx.ListCtrl.SetStringItem(self, index, col-3, data[0:datalen]) | |
79 | ||
80 | ||
81 | ||
82 | ||
83 | class TestListCtrlPanel(wx.Panel): | |
84 | def __init__(self, parent, log): | |
85 | wx.Panel.__init__(self, parent, -1, style=wx.WANTS_CHARS) | |
86 | ||
87 | self.log = log | |
88 | tID = wx.NewId() | |
89 | ||
5924f6b3 KO |
90 | sizer = wx.BoxSizer(wx.VERTICAL) |
91 | ||
92 | if wx.Platform == "__WXMAC__": | |
93 | self.useNative = wx.CheckBox(self, -1, "Use native listctrl") | |
94 | self.useNative.SetValue( | |
95 | not wx.SystemOptions.GetOptionInt("mac.listctrl.always_use_generic") ) | |
96 | self.Bind(wx.EVT_CHECKBOX, self.OnUseNative, self.useNative) | |
97 | sizer.Add(self.useNative, 0, wx.ALL | wx.ALIGN_RIGHT, 4) | |
98 | ||
15513a80 RD |
99 | self.list = TestListCtrl(self, tID, |
100 | style=wx.LC_REPORT | |
101 | | wx.BORDER_NONE | |
102 | | wx.LC_SORT_ASCENDING | |
103 | ) | |
104 | ||
5924f6b3 KO |
105 | sizer.Add(self.list, 1, wx.EXPAND | wx.ALL, 4) |
106 | self.SetSizer(sizer) | |
107 | self.SetAutoLayout(True) | |
15513a80 RD |
108 | |
109 | ||
5924f6b3 KO |
110 | def OnUseNative(self, event): |
111 | wx.SystemOptions.SetOptionInt("mac.listctrl.always_use_generic", not event.IsChecked()) | |
112 | wx.GetApp().GetTopWindow().LoadDemo("ListCtrl_edit") | |
15513a80 RD |
113 | |
114 | ||
115 | ||
116 | #--------------------------------------------------------------------------- | |
117 | ||
118 | def runTest(frame, nb, log): | |
119 | win = TestListCtrlPanel(nb, log) | |
120 | return win | |
121 | ||
122 | #--------------------------------------------------------------------------- | |
123 | ||
124 | ||
125 | overview = """\ | |
126 | <html> | |
127 | <body> | |
128 | ||
129 | This demo demonstrates direct editing of all cells of a ListCtrl. To | |
130 | enable it just include the <b>TextEditMixin</b>. The ListCtrl can be | |
131 | navigated with the TAB and up/down cursor keys. | |
132 | ||
133 | <p>Another facet of this demo is that the remaining space of the | |
134 | ListCtrls is divided over the first three columns. This is achieved | |
135 | with the extended syntax of <b>ListCtrlAutoWidthMixin</b>: | |
136 | <code>listmix.ListCtrlAutoWidthMixin.__init__(self, startcol, endcol)</code> | |
137 | (Look at the general ListCtrl demo for more information about the | |
138 | ListCtrlAutoWidthMixin) | |
139 | ||
140 | <p>Finally, the ListCtrl is automatically scrolled, if needed, when | |
141 | TAB is pressed consecutively (Windows only). | |
142 | ||
143 | </body> | |
144 | </html> | |
145 | """ | |
146 | ||
147 | ||
148 | if __name__ == '__main__': | |
149 | import sys,os | |
150 | import run | |
151 | run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:]) | |
152 |