]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/SizedControls.py
reformat the overview string
[wxWidgets.git] / wxPython / demo / SizedControls.py
1 import wx
2 import wxaddons.sized_controls as sc
3
4 overview = """\
5 <html><body><h2>Sized Controls</h2>
6 SizedControls is an addon library that attempts to simplify the
7 creation of sizer-based layouts. It adds the following classes:
8
9 <h3>SizedPanel</h3>
10
11 This class automatically creates its own sizer (a vertical box sizer
12 by default) and automatically adds its children to the sizer. You can
13 change the SizedPanel's sizer type by calling
14 panel.SetSizerType(\"type\", [args]), where valid types are
15 \"horizontal\", \"vertical\", \"form\" (a 2-col flex grid sizer), and
16 \"grid\". Args include \"cols\" and \"rows\" attributes for
17 grids. This class also applies control borders that adhere to the
18 native platform's Human Interface Guidelines (HIG) on Win, GTK and
19 Mac.
20
21 <h3>SizedFrame and SizedDialog</h3>
22
23 These classes automatically setup a SizedPanel which is appropriately
24 positioned and given appropriate borders in accordance with the
25 platform's HIGs.
26
27 <p>Since controls are added to the parent's sizer upon creation, you
28 don't need to use sizer.Add or even create sizers yourself. You just
29 use SetSizerType() to change the sizer you want to use, and
30 control.SetSizerProps() to change the sizer properties of the
31 control. So as a result, code that used to look like this:
32
33 <table bgcolor=\"#EFEFEF\"><tr><td><pre>
34 ... wx.Dialog init code...
35
36 panel = wx.Panel(self, -1)
37 b1 = wx.Button(panel, -1)
38 b2 = wx.Button(panel, -1)
39 t1 = wx.TextCtrl(panel, -1)
40 b3 = wx.Button(panel, -1)
41
42 sizer = wx.BoxSizer(wx.HORIZONTAL)
43 sizer.Add(b1, 0, wx.ALL, 6)
44 sizer.Add(b2, 0, wx.ALL, 6)
45 sizer.Add(t1, 0, wx.EXPAND | wx.ALL, 6)
46 sizer.Add(b3, 0, wx.ALL, 6)
47 panel.SetSizer(sizer)
48
49 dlgSizer = wx.BoxSizer()
50 dlgSizer.Add(panel, 1, wx.EXPAND)
51 self.SetSizer(dlgSizer)
52 self.SetAutoLayout(True)
53
54 ... rest of dialog ...</pre>
55 </td></tr></table>
56
57 would now look like this:
58
59 <table bgcolor=\"#EFEFEF\"><tr><td><pre>
60 ... wx.Dialog init code...
61
62 panel = self.GetContentsPane()
63 panel.SetSizerType(\"horizontal\")
64
65 b1 = wx.Button(panel, -1)
66 b2 = wx.Button(panel, -1)
67
68 t1 = wx.TextCtrl(panel, -1)
69 t1.SetSizerProps(expand=True)
70
71 b3 = wx.Button(panel, -1)
72
73 ... rest of dialog ...</pre>
74 </td></tr></table>
75
76 and the latter example will adhere to HIG spacing guidelines on all platforms,
77 unlike the former example.
78
79 Please check the demos for more complete and sophisticated examples of SizedControls
80 in action.
81 """
82
83 class FormDialog(sc.SizedDialog):
84 def __init__(self, parent, id):
85 sc.SizedDialog.__init__(self, None, -1, "SizedForm Dialog",
86 style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER)
87
88 pane = self.GetContentsPane()
89 pane.SetSizerType("form")
90
91 # row 1
92 wx.StaticText(pane, -1, "Name")
93 textCtrl = wx.TextCtrl(pane, -1, "Your name here")
94 textCtrl.SetSizerProps(expand=True)
95
96 # row 2
97 wx.StaticText(pane, -1, "Email")
98 emailCtrl = wx.TextCtrl(pane, -1, "")
99 emailCtrl.SetSizerProps(expand=True)
100
101 # row 3
102 wx.StaticText(pane, -1, "Gender")
103 wx.Choice(pane, -1, choices=["male", "female"])
104
105 # row 4
106 wx.StaticText(pane, -1, "State")
107 wx.TextCtrl(pane, -1, size=(60, -1)) # two chars for state
108
109 # row 5
110 wx.StaticText(pane, -1, "Title")
111
112 # here's how to add a 'nested sizer' using sized_controls
113 radioPane = sc.SizedPanel(pane, -1)
114 radioPane.SetSizerType("horizontal")
115 radioPane.SetSizerProps(expand=True)
116
117 # make these children of the radioPane to have them use
118 # the horizontal layout
119 wx.RadioButton(radioPane, -1, "Mr.")
120 wx.RadioButton(radioPane, -1, "Mrs.")
121 wx.RadioButton(radioPane, -1, "Dr.")
122 # end row 5
123
124 # add dialog buttons
125 self.SetButtonSizer(self.CreateStdDialogButtonSizer(wx.OK | wx.CANCEL))
126
127 # a little trick to make sure that you can't resize the dialog to
128 # less screen space than the controls need
129 self.Fit()
130 self.SetMinSize(self.GetSize())
131
132
133 class ErrorDialog(sc.SizedDialog):
134 def __init__(self, parent, id):
135 sc.SizedDialog.__init__(self, parent, id, "Error log viewer",
136 style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)
137
138 # Always use self.GetContentsPane() - this ensures that your dialog
139 # automatically adheres to HIG spacing requirements on all platforms.
140 # pane here is a sc.SizedPanel with a vertical sizer layout. All children
141 # should be added to this pane, NOT to self.
142 pane = self.GetContentsPane()
143
144 # first row
145 self.listCtrl = wx.ListCtrl(pane, -1, size=(300, -1), style=wx.LC_REPORT)
146 self.listCtrl.SetSizerProps(expand=True, proportion=1)
147 self.ConfigureListCtrl()
148
149 # second row
150 self.lblDetails = wx.StaticText(pane, -1, "Error Details")
151
152 # third row
153 self.details = wx.TextCtrl(pane, -1, style=wx.TE_MULTILINE)
154 self.details.SetSizerProps(expand=True, proportion=1)
155
156 # final row
157 # since we want to use a custom button layout, we won't use the
158 # CreateStdDialogBtnSizer here, we'll just create our own panel with
159 # a horizontal layout and add the buttons to that.
160 btnpane = sc.SizedPanel(pane, -1)
161 btnpane.SetSizerType("horizontal")
162 btnpane.SetSizerProps(expand=True)
163
164 self.saveBtn = wx.Button(btnpane, wx.ID_SAVE)
165 spacer = sc.SizedPanel(btnpane, -1)
166 spacer.SetSizerProps(expand=True, proportion=1)
167
168 self.clearBtn = wx.Button(btnpane, -1, "Clear")
169
170 self.Fit()
171 self.SetMinSize(self.GetSize())
172
173 def ConfigureListCtrl(self):
174 self.listCtrl.InsertColumn(0, "Time")
175 self.listCtrl.InsertColumn(1, "Error Message")
176 self.listCtrl.SetColumnWidth(0, 100)
177 self.listCtrl.SetColumnWidth(1, 280)
178
179 #---------------------------------------------------------------------------
180
181 class TestPanel(wx.Panel):
182 def __init__(self, parent, log):
183 self.log = log
184 self.parent = parent
185 wx.Panel.__init__(self, parent, -1)
186
187 b = wx.Button(self, -1, "Sized Controls Form Dialog", (50,50))
188 self.Bind(wx.EVT_BUTTON, self.OnFormButton, b)
189
190 b2 = wx.Button(self, -1, "Sized Controls Error Dialog", (50,90))
191 self.Bind(wx.EVT_BUTTON, self.OnErrorButton, b2)
192
193
194 def OnFormButton(self, evt):
195 print
196 dlg = FormDialog(self, -1)
197 dlg.CenterOnScreen()
198
199 # this does not return until the dialog is closed.
200 val = dlg.ShowModal()
201
202 if val == wx.ID_OK:
203 self.log.WriteText("You pressed OK\n")
204 else:
205 self.log.WriteText("You pressed Cancel\n")
206
207 dlg.Destroy()
208
209 def OnErrorButton(self, evt):
210
211 dlg = ErrorDialog(self, -1)
212 dlg.CenterOnScreen()
213
214 # this does not return until the dialog is closed.
215 val = dlg.ShowModal()
216
217 if val == wx.ID_OK:
218 self.log.WriteText("You pressed OK\n")
219 else:
220 self.log.WriteText("You pressed Cancel\n")
221
222 dlg.Destroy()
223
224 def runTest(frame, nb, log):
225 win = TestPanel(nb, log)
226 return win
227
228 if __name__ == "__main__":
229 app = wx.PySimpleApp()
230 dlg = FormDialog()
231 dlg.ShowModal()