File: SizedControls.py

package info (click to toggle)
wxpython3.0 3.0.2.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 482,760 kB
  • ctags: 518,293
  • sloc: cpp: 2,127,226; python: 294,045; makefile: 51,942; ansic: 19,033; sh: 3,013; xml: 1,629; perl: 17
file content (400 lines) | stat: -rw-r--r-- 13,418 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import wx
import wx.lib.sized_controls as sc

overview = """\
<html><body><h2>Sized Controls</h2>
SizedControls is an addon library that attempts to simplify the
creation of sizer-based layouts. It adds the following classes:

<h3>SizedPanel</h3>

This class automatically creates its own sizer (a vertical box sizer
by default) and automatically adds its children to the sizer. You can
change the SizedPanel's sizer type by calling
panel.SetSizerType(\"type\", [args]), where valid types are
\"horizontal\", \"vertical\", \"form\" (a 2-col flex grid sizer), and
\"grid\". Args include \"cols\" and \"rows\" attributes for
grids. This class also applies control borders that adhere to the
native platform's Human Interface Guidelines (HIG) on Win, GTK and
Mac.

<h3>SizedScrolledPanel</h3>

This class automatically creates its own sizer (a vertical box sizer
by default) and automatically adds its children to the sizer. You can
change the SizedScrolledPanel's sizer type by calling
panel.SetSizerType(\"type\", [args]), where valid types are
\"horizontal\", \"vertical\", \"form\" (a 2-col flex grid sizer), and
\"grid\". Args include \"cols\" and \"rows\" attributes for
grids. This class also applies control borders that adhere to the
native platform's Human Interface Guidelines (HIG) on Win, GTK and
Mac.

<h3>SizedFrame and SizedDialog</h3>

These classes automatically setup a SizedPanel which is appropriately
positioned and given appropriate borders in accordance with the
platform's HIGs.

<p>Since controls are added to the parent's sizer upon creation, you
don't need to use sizer.Add or even create sizers yourself. You just
use SetSizerType() to change the sizer you want to use, and
control.SetSizerProps() to change the sizer properties of the
control. So as a result, code that used to look like this:

<table bgcolor=\"#EFEFEF\"><tr><td><pre>
... wx.Dialog init code...

panel = wx.Panel(self, -1)
b1 = wx.Button(panel, -1)
b2 = wx.Button(panel, -1)
t1 = wx.TextCtrl(panel, -1)
b3 = wx.Button(panel, -1)

sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(b1, 0, wx.ALL, 6)
sizer.Add(b2, 0, wx.ALL, 6)
sizer.Add(t1, 0, wx.EXPAND | wx.ALL, 6)
sizer.Add(b3, 0, wx.ALL, 6)
panel.SetSizer(sizer)

dlgSizer = wx.BoxSizer()
dlgSizer.Add(panel, 1, wx.EXPAND)
self.SetSizer(dlgSizer)
self.SetAutoLayout(True)

... rest of dialog ...</pre>
</td></tr></table>

would now look like this:

<table bgcolor=\"#EFEFEF\"><tr><td><pre>
... wx.Dialog init code...

panel = self.GetContentsPane()
panel.SetSizerType(\"horizontal\")

b1 = wx.Button(panel, -1)
b2 = wx.Button(panel, -1)

t1 = wx.TextCtrl(panel, -1)
t1.SetSizerProps(expand=True)

b3 = wx.Button(panel, -1)

... rest of dialog ...</pre>
</td></tr></table>

and the latter example will adhere to HIG spacing guidelines on all platforms,
unlike the former example. Please check the demos for more complete and sophisticated examples of SizedControls
in action.

<h3>wx.Window.SetSizerProps Quick Reference</h3>

<p><pre>wx.Window.SetSizerProps(&lt;props&gt;)</pre>

<p>
<table bgcolor=\"#EFEFEF\">
<tr>
<td valign="middle" width="90"><b>Parameter</b></td> <td valign="middle"><b>Values</b></td> <td valign="middle"><b>Summary</b></td>
</tr>

<tr>
<td><i>expand</i></td> <td>True/False</td>
<td>Whether or not the control should grow to fill free space if 
free space is available.</td>
</tr>

<tr>
<td><i>proportion</i></td> <td>Number (typically 0-10)</td> 
<td>How much of the free space the control should take up. Note that this value is 
<i>relative</i> to other controls, so a proportion of 2 means take up 
'twice as much' space as controls with a proportion of 1.</td>
</tr>

<tr>
<td><i>halign</i> <td>"left", "center", "centre", "right"</td>
<td>Determines horizontal alignment of control.</td>
</tr>

<tr>
<td><i>valign</i> <td>"top", "center", "centre", "bottom"</td>
<td>Determines vertical alignment of control.</td>
</tr>

<tr>
<td><i>border</i> <td>Tuple: ([<i>dirs</i>], integer)</td>
<td>Specifies amount of border padding to apply to specified directions. </br>
Example: (["left", "right"], 6) would add six pixels to left and right borders. </br>
Note that, unfortunately,
it is not currently possible to assign different border sizes to each direction.</td>
</tr>

<tr>
<td><i>minsize</i> <td>One of the following string values: "fixed", "adjust"</td>
<td>Determines whether or not the minsize can be updated when the control's best size changes.</td>
</tr>

</table>
"""

class FormDialog(sc.SizedDialog):
    def __init__(self, parent, id):
        sc.SizedDialog.__init__(self, None, -1, "SizedForm Dialog", 
                        style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER)
        
        pane = self.GetContentsPane()
        pane.SetSizerType("form")
        
        # row 1
        wx.StaticText(pane, -1, "Name")
        textCtrl = wx.TextCtrl(pane, -1, "Your name here")
        textCtrl.SetSizerProps(expand=True)
        
        # row 2
        wx.StaticText(pane, -1, "Email")
        emailCtrl = wx.TextCtrl(pane, -1, "")
        emailCtrl.SetSizerProps(expand=True)
        
        # row 3
        wx.StaticText(pane, -1, "Gender")
        wx.Choice(pane, -1, choices=["male", "female"])
        
        # row 4
        wx.StaticText(pane, -1, "State")
        wx.TextCtrl(pane, -1, size=(60, -1)) # two chars for state
        
        # row 5
        wx.StaticText(pane, -1, "Title")
        
        # here's how to add a 'nested sizer' using sized_controls
        radioPane = sc.SizedPanel(pane, -1)
        radioPane.SetSizerType("horizontal")
        radioPane.SetSizerProps(expand=True)
        
        # make these children of the radioPane to have them use
        # the horizontal layout
        wx.RadioButton(radioPane, -1, "Mr.")
        wx.RadioButton(radioPane, -1, "Mrs.")
        wx.RadioButton(radioPane, -1, "Dr.")
        # end row 5
        
        # add dialog buttons
        self.SetButtonSizer(self.CreateStdDialogButtonSizer(wx.OK | wx.CANCEL))
        
        # a little trick to make sure that you can't resize the dialog to
        # less screen space than the controls need
        self.Fit()
        self.SetMinSize(self.GetSize())


class ScrolledFormDialog(sc.SizedDialog):
    def __init__(self, parent, id):
        sc.SizedDialog.__init__(self, None, -1, "SizedForm Dialog with a scolled panel", 
                        style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER,
                        size=wx.Size(-1, 220))
        
        cPane = self.GetContentsPane()
        pane = sc.SizedScrolledPanel(cPane, wx.ID_ANY)
        pane.SetSizerProps(expand=True, proportion=1)
        pane.SetSizerType("form")
        
        # row 1
        wx.StaticText(pane, -1, "Name")
        textCtrl = wx.TextCtrl(pane, -1, "Your name here")
        textCtrl.SetSizerProps(expand=True)
        
        # row 2
        wx.StaticText(pane, -1, "Email")
        emailCtrl = wx.TextCtrl(pane, -1, "")
        emailCtrl.SetSizerProps(expand=True)
        
        # row 3
        wx.StaticText(pane, -1, "Gender")
        wx.Choice(pane, -1, choices=["male", "female"])
        
        # row 4
        wx.StaticText(pane, -1, "State")
        wx.TextCtrl(pane, -1, size=(60, -1)) # two chars for state
        
        # row 5
        wx.StaticText(pane, -1, "Title")
        
        # here's how to add a 'nested sizer' using sized_controls
        radioPane = sc.SizedPanel(pane, -1)
        radioPane.SetSizerType("horizontal")
        radioPane.SetSizerProps(expand=True)
        
        # make these children of the radioPane to have them use
        # the horizontal layout
        wx.RadioButton(radioPane, -1, "Mr.")
        wx.RadioButton(radioPane, -1, "Mrs.")
        wx.RadioButton(radioPane, -1, "Dr.")
        # end row 5
        
        # add dialog buttons
        self.SetButtonSizer(self.CreateStdDialogButtonSizer(wx.OK | wx.CANCEL))
        

class ErrorDialog(sc.SizedDialog):
    def __init__(self, parent, id):
        sc.SizedDialog.__init__(self, parent, id, "Error log viewer", 
                                style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)
        
        # Always use self.GetContentsPane() - this ensures that your dialog
        # automatically adheres to HIG spacing requirements on all platforms.
        # pane here is a sc.SizedPanel with a vertical sizer layout. All children
        # should be added to this pane, NOT to self.
        pane = self.GetContentsPane()
        
        # first row
        self.listCtrl = wx.ListCtrl(pane, -1, size=(300, -1), style=wx.LC_REPORT)
        self.listCtrl.SetSizerProps(expand=True, proportion=1)
        self.ConfigureListCtrl()
        
        # second row
        self.lblDetails = wx.StaticText(pane, -1, "Error Details")
        
        # third row
        self.details = wx.TextCtrl(pane, -1, style=wx.TE_MULTILINE)
        self.details.SetSizerProps(expand=True, proportion=1)

        # final row
        # since we want to use a custom button layout, we won't use the 
        # CreateStdDialogBtnSizer here, we'll just create our own panel with
        # a horizontal layout and add the buttons to that.
        btnpane = sc.SizedPanel(pane, -1)
        btnpane.SetSizerType("horizontal")
        btnpane.SetSizerProps(expand=True)

        self.saveBtn = wx.Button(btnpane, wx.ID_SAVE)
        spacer = sc.SizedPanel(btnpane, -1)
        spacer.SetSizerProps(expand=True, proportion=1)

        self.clearBtn = wx.Button(btnpane, -1, "Clear")

        self.Fit()
        self.SetMinSize(self.GetSize())
        
    def ConfigureListCtrl(self):
        self.listCtrl.InsertColumn(0, "Time")
        self.listCtrl.InsertColumn(1, "Error Message")
        self.listCtrl.SetColumnWidth(0, 100)
        self.listCtrl.SetColumnWidth(1, 280)
        

class GridFrame(sc.SizedFrame):
    def __init__(self, parent, id):
        sc.SizedFrame.__init__(self, parent, id, "Grid Layout Demo Frame")
        
        pane = self.GetContentsPane()
        pane.SetSizerType("grid", {"cols":3}) # 3-column grid layout
        
        # row 1
        wx.TextCtrl(pane, -1).SetSizerProps(halign="left")
        wx.TextCtrl(pane, -1).SetSizerProps(halign="center")
        wx.TextCtrl(pane, -1).SetSizerProps(halign="right")
        
        # row 2
        wx.TextCtrl(pane, -1).SetSizerProps(valign="center")
        wx.TextCtrl(pane, -1).SetSizerProps(expand=True, proportion=1)
        wx.TextCtrl(pane, -1).SetSizerProps(valign="center")
        
        # row 3
        wx.TextCtrl(pane, -1).SetSizerProps(halign="left")
        wx.TextCtrl(pane, -1).SetSizerProps(halign="center")
        wx.TextCtrl(pane, -1).SetSizerProps(halign="right")
        
        self.CreateStatusBar() # should always do this when there's a resize border
        
        self.Fit()
        self.SetMinSize(self.GetSize())
        
        
#---------------------------------------------------------------------------

class TestPanel(sc.SizedScrolledPanel):
    def __init__(self, parent, log):
        self.log = log
        self.parent = parent
        sc.SizedScrolledPanel.__init__(self, parent, -1)

        b = wx.Button(self, -1, "Sized Controls Form Dialog")
        b.SetSizerProps({'halign': 'center', 'border': ('all', 15)})
        self.Bind(wx.EVT_BUTTON, self.OnFormButton, b)

        b1 = wx.Button(self, -1, "Sized Controls Form Dialog with scrolled panel")
        b1.SetSizerProps({'halign': 'center', 'border': ('all', 15)})
        self.Bind(wx.EVT_BUTTON, self.OnFormScrolledButton, b1)

        b2 = wx.Button(self, -1, "Sized Controls Error Dialog")
        b2.SetSizerProps({'halign': 'center', 'border': ('all', 15)})
        self.Bind(wx.EVT_BUTTON, self.OnErrorButton, b2)
        
        b3 = wx.Button(self, -1, "Sized Controls Grid Layout Demo")
        b3.SetSizerProps({'halign': 'center', 'border': ('all', 15)})
        self.Bind(wx.EVT_BUTTON, self.OnGridButton, b3)    
        

    def OnFormButton(self, evt):

        dlg = FormDialog(self, -1)
        dlg.CenterOnScreen()

        # this does not return until the dialog is closed.
        val = dlg.ShowModal()
    
        if val == wx.ID_OK:
            self.log.WriteText("You pressed OK\n")
        else:
            self.log.WriteText("You pressed Cancel\n")

        dlg.Destroy()
        
    def OnFormScrolledButton(self, evt):

        dlg = ScrolledFormDialog(self, -1)
        dlg.CenterOnScreen()

        # this does not return until the dialog is closed.
        val = dlg.ShowModal()
    
        if val == wx.ID_OK:
            self.log.WriteText("You pressed OK\n")
        else:
            self.log.WriteText("You pressed Cancel\n")

        dlg.Destroy()

    def OnErrorButton(self, evt):
            
        dlg = ErrorDialog(self, -1)
        dlg.CenterOnScreen()

        # this does not return until the dialog is closed.
        val = dlg.ShowModal()
    
        if val == wx.ID_OK:
            self.log.WriteText("You pressed OK\n")
        else:
            self.log.WriteText("You pressed Cancel\n")

        dlg.Destroy()
        
    def OnGridButton(self, evt):
            
        dlg = GridFrame(self, -1)
        dlg.CenterOnScreen()

        dlg.Show()
        
def runTest(frame, nb, log):
    win = TestPanel(nb, log)
    return win


if __name__ == '__main__':
    import sys,os
    import run
    run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])