2003-11-12 16:34:20 -05:00
|
|
|
|
|
|
|
import wx # This module uses the new wx namespace
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
gbsDescription = """\
|
2004-01-12 22:17:17 -05:00
|
|
|
The wx.GridBagSizer is similar to the wx.FlexGridSizer except the items are explicitly positioned
|
2003-11-12 16:34:20 -05:00
|
|
|
in a virtual cell of the layout grid, and column or row spanning is allowed. For example, this
|
|
|
|
static text is positioned at (0,0) and it spans 7 columns.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class TestFrame(wx.Frame):
|
|
|
|
def __init__(self):
|
2004-01-12 22:17:17 -05:00
|
|
|
wx.Frame.__init__(self, None, -1, "wx.GridBagSizer")
|
2004-10-30 17:30:16 -04:00
|
|
|
p = wx.Panel(self, -1, style = wx.TAB_TRAVERSAL
|
|
|
|
| wx.CLIP_CHILDREN
|
|
|
|
| wx.FULL_REPAINT_ON_RESIZE
|
|
|
|
)
|
2003-11-19 01:41:33 -05:00
|
|
|
p.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
|
|
|
|
|
|
|
|
gbs = self.gbs = wx.GridBagSizer(5, 5)
|
2003-11-12 16:34:20 -05:00
|
|
|
|
|
|
|
gbs.Add( wx.StaticText(p, -1, gbsDescription),
|
|
|
|
(0,0), (1,7), wx.ALIGN_CENTER | wx.ALL, 5)
|
|
|
|
|
|
|
|
gbs.Add( wx.TextCtrl(p, -1, "pos(1,0)"), (1,0) )
|
|
|
|
gbs.Add( wx.TextCtrl(p, -1, "pos(1,1)"), (1,1) )
|
|
|
|
gbs.Add( wx.TextCtrl(p, -1, "pos(2,0)"), (2,0) )
|
|
|
|
gbs.Add( wx.TextCtrl(p, -1, "pos(2,1)"), (2,1) )
|
|
|
|
|
|
|
|
gbs.Add( wx.TextCtrl(p, -1, "pos(3,2), span(1,2)\nthis row and col are growable", style=wx.TE_MULTILINE),
|
|
|
|
(3,2), (1,2), flag=wx.EXPAND )
|
|
|
|
|
|
|
|
gbs.Add( wx.TextCtrl(p, -1, "pos(4,3), span(3,1)", style=wx.TE_MULTILINE),
|
|
|
|
(4,3), (3,1), wx.EXPAND)
|
|
|
|
|
|
|
|
gbs.Add( wx.TextCtrl(p, -1, "pos(5,4)"), (5,4), flag=wx.EXPAND )
|
|
|
|
gbs.Add( wx.TextCtrl(p, -1, "pos(6,5)"), (6,5), flag=wx.EXPAND )
|
|
|
|
gbs.Add( wx.TextCtrl(p, -1, "pos(7,6)"), (7,6) )
|
|
|
|
|
|
|
|
moveBtn1 = wx.Button(p, -1, "Move this to (3,6)")
|
|
|
|
moveBtn2 = wx.Button(p, -1, "Move this to (3,6)");
|
|
|
|
gbs.Add( moveBtn1, (10,2) )
|
|
|
|
gbs.Add( moveBtn2, (10,3) )
|
|
|
|
|
|
|
|
hideBtn = wx.Button(p, -1, "Hide this item -->")
|
|
|
|
gbs.Add(hideBtn, (12, 3))
|
|
|
|
|
|
|
|
hideTxt = wx.TextCtrl(p, -1, "pos(12,4), size(150, -1)", size = (150,-1))
|
|
|
|
gbs.Add( hideTxt, (12,4) )
|
|
|
|
|
|
|
|
showBtn = wx.Button(p, -1, "<-- Show it again")
|
|
|
|
gbs.Add(showBtn, (12, 5))
|
|
|
|
showBtn.Disable()
|
|
|
|
self.hideBtn = hideBtn
|
|
|
|
self.showBtn = showBtn
|
|
|
|
self.hideTxt = hideTxt
|
|
|
|
|
|
|
|
self.Bind(wx.EVT_BUTTON, self.OnHideButton, hideBtn)
|
|
|
|
self.Bind(wx.EVT_BUTTON, self.OnShowButton, showBtn)
|
|
|
|
self.Bind(wx.EVT_BUTTON, self.OnMoveButton, moveBtn1)
|
|
|
|
self.Bind(wx.EVT_BUTTON, self.OnMoveButton, moveBtn2)
|
|
|
|
|
|
|
|
# Add a spacer at the end to ensure some extra space at the bottom
|
2003-11-19 01:41:33 -05:00
|
|
|
gbs.Add((10,10), (14,7))
|
2003-11-12 16:34:20 -05:00
|
|
|
|
|
|
|
gbs.AddGrowableRow(3)
|
|
|
|
gbs.AddGrowableCol(2)
|
|
|
|
|
|
|
|
p.SetSizerAndFit(gbs)
|
|
|
|
self.SetClientSize(p.GetSize())
|
|
|
|
|
|
|
|
|
|
|
|
def OnHideButton(self, evt):
|
|
|
|
self.gbs.Hide(self.hideTxt)
|
|
|
|
self.hideBtn.Disable()
|
|
|
|
self.showBtn.Enable()
|
|
|
|
self.gbs.Layout()
|
|
|
|
|
|
|
|
|
|
|
|
def OnShowButton(self, evt):
|
|
|
|
self.gbs.Show(self.hideTxt)
|
|
|
|
self.hideBtn.Enable()
|
|
|
|
self.showBtn.Disable()
|
|
|
|
self.gbs.Layout()
|
|
|
|
|
|
|
|
|
|
|
|
def OnMoveButton(self, evt):
|
|
|
|
btn = evt.GetEventObject()
|
|
|
|
curPos = self.gbs.GetItemPosition(btn)
|
|
|
|
|
|
|
|
# if it's already at the "other" spot then move it back
|
|
|
|
if curPos == (3,6):
|
|
|
|
self.gbs.SetItemPosition(btn, self.lastPos)
|
|
|
|
btn.SetLabel("Move this to (3,6)")
|
|
|
|
else:
|
2004-06-09 14:55:39 -04:00
|
|
|
if self.gbs.CheckForIntersectionPos( (3,6), (1,1) ):
|
2003-11-12 16:34:20 -05:00
|
|
|
wx.MessageBox("""\
|
2004-01-12 22:17:17 -05:00
|
|
|
wx.GridBagSizer will not allow items to be in the same cell as
|
2004-06-09 14:55:39 -04:00
|
|
|
another item, so this operation will fail. You will also get an
|
|
|
|
assert when compiled in debug mode.""",
|
2003-11-12 16:34:20 -05:00
|
|
|
"Warning", wx.OK | wx.ICON_INFORMATION)
|
|
|
|
|
|
|
|
try:
|
|
|
|
if self.gbs.SetItemPosition(btn, (3,6)):
|
|
|
|
self.lastPos = curPos
|
|
|
|
btn.SetLabel("Move it back")
|
|
|
|
except wx.PyAssertionError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
self.gbs.Layout()
|
|
|
|
|
2003-11-19 01:41:33 -05:00
|
|
|
|
|
|
|
def OnLeftDown(self, evt):
|
|
|
|
pt = evt.GetPosition()
|
|
|
|
item = self.gbs.FindItemAtPoint(pt)
|
|
|
|
if item is None:
|
|
|
|
print "no item at", `pt`
|
|
|
|
else:
|
|
|
|
print "item found: ", `item.GetPos()`, "--", `item.GetSpan()`
|
|
|
|
|
2003-11-12 16:34:20 -05:00
|
|
|
|
2004-08-09 21:21:16 -04:00
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class TestPanel(wx.Panel):
|
|
|
|
def __init__(self, parent, log):
|
|
|
|
self.log = log
|
|
|
|
wx.Panel.__init__(self, parent, -1)
|
|
|
|
|
|
|
|
b = wx.Button(self, -1, "Show the GridBagSizer sample", (50,50))
|
|
|
|
self.Bind(wx.EVT_BUTTON, self.OnButton, b)
|
|
|
|
|
|
|
|
|
|
|
|
def OnButton(self, evt):
|
|
|
|
win = TestFrame()
|
|
|
|
win.Show(True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
2003-11-12 16:34:20 -05:00
|
|
|
|
|
|
|
def runTest(frame, nb, log):
|
2004-08-09 21:21:16 -04:00
|
|
|
win = TestPanel(nb, log)
|
|
|
|
return win
|
2003-11-12 16:34:20 -05:00
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
overview = """<html><body>
|
2004-01-12 22:17:17 -05:00
|
|
|
<h2><center>wx.GridBagSizer</center></h2>
|
2003-11-12 16:34:20 -05:00
|
|
|
|
2004-01-12 22:17:17 -05:00
|
|
|
The wx.GridBagSizer is more or less a port of the the RowColSizer (that
|
2003-11-12 16:34:20 -05:00
|
|
|
has been in the wxPython.lib package for quite some time) to C++. It
|
|
|
|
allows items to be placed at specific layout grid cells, and items can
|
|
|
|
span across more than one row or column.
|
|
|
|
</body></html>
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys,os
|
|
|
|
import run
|
2004-03-04 19:06:33 -05:00
|
|
|
run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
|
2003-11-12 16:34:20 -05:00
|
|
|
|