Hello,
I am trying to use the OGL demo code to display a circle shape. I placed the
TestWindow class derived from ogl.shapecanvas inside a wx.Frame, and it worked
fine.
But I wish to use a SplitterWindow in the frame and place the ogl.shapecanvas
class (TestWindow) on Window2. Now, the shapecanvas does not show up. It is
displayed as a small square on the top left corner of the 2nd window.
Please see my code below. Any help will be very appriciated.
Thanks.
Lokesh
#Boa:Frame:Frame1
import wx
import wx.lib.ogl as ogl
def create(parent):
return Frame1(parent)
[wxID_FRAME1, wxID_FRAME1SPLITTERWINDOW1, wxID_FRAME1WINDOW1,
wxID_FRAME1WINDOW2,
] = [wx.NewId() for _init_ctrls in range(4)]
class MyEvtHandler(ogl.ShapeEvtHandler):
#def __init__(self, log, frame):
def __init__(self, frame):
ogl.ShapeEvtHandler.__init__(self)
def OnLeftClick(self, x, y, keys=0, attachment=0):
shape = self.GetShape()
print 'Shape selected: ', shape
canvas = shape.GetCanvas()
dc = wx.ClientDC(canvas)
canvas.PrepareDC(dc)
if shape.Selected():
shape.Select(False, dc)
#canvas.Redraw(dc)
canvas.Refresh(False)
else:
redraw = False
shapeList = canvas.GetDiagram().GetShapeList()
toUnselect = []
for s in shapeList:
if s.Selected():
# If we unselect it now then some of the objects in
# shapeList will become invalid (the control points are
# shapes too!) and bad things will happen...
toUnselect.append(s)
shape.Select(True, dc)
if toUnselect:
for s in toUnselect:
s.Select(False, dc)
canvas.Redraw(dc)
canvas.Refresh(False)
def OnEndDragLeft(self, x, y, keys=0, attachment=0):
shape = self.GetShape()
ogl.ShapeEvtHandler.OnEndDragLeft(self, x, y, keys, attachment)
if not shape.Selected():
self.OnLeftClick(x, y, keys, attachment)
def OnSizingEndDragLeft(self, pt, x, y, keys, attch):
ogl.ShapeEvtHandler.OnSizingEndDragLeft(self, pt, x, y, keys, attch)
def OnMovePost(self, dc, x, y, oldX, oldY, display):
shape = self.GetShape()
ogl.ShapeEvtHandler.OnMovePost(self, dc, x, y, oldX, oldY, display)
if "wxMac" in wx.PlatformInfo:
shape.GetCanvas().Refresh(False)
class TestWindow(ogl.ShapeCanvas):
#def __init__(self, parent, log, frame):
def __init__(self, parent, frame):
ogl.ShapeCanvas.__init__(self, parent)
self.frame = frame
self.SetBackgroundColour("LIGHT BLUE") #wx.WHITE)
self.diagram = ogl.Diagram()
self.SetDiagram(self.diagram)
self.diagram.SetCanvas(self)
self.shapes = []
self.save_gdi = []
self.MyAddShape(
ogl.CircleShape(80),
75, 110, wx.Pen(wx.BLUE, 3), wx.GREEN_BRUSH, "Circle"
)
def MyAddShape(self, shape, x, y, pen, brush, text):
# Composites have to be moved for all children to get in place
if isinstance(shape, ogl.CompositeShape):
dc = wx.ClientDC(self)
self.PrepareDC(dc)
shape.Move(dc, x, y)
else:
shape.SetDraggable(True, True)
shape.SetCanvas(self)
shape.SetX(x)
shape.SetY(y)
if pen: shape.SetPen(pen)
if brush: shape.SetBrush(brush)
if text:
for line in text.split('\n'):
shape.AddText(line)
#shape.SetShadowMode(ogl.SHADOW_RIGHT)
self.diagram.AddShape(shape)
shape.Show(True)
evthandler = MyEvtHandler(self.frame)
evthandler.SetShape(shape)
evthandler.SetPreviousHandler(shape.GetEventHandler())
shape.SetEventHandler(evthandler)
self.shapes.append(shape)
return shape
def OnBeginDragLeft(self, x, y, keys):
self.log.write("OnBeginDragLeft: %s, %s, %s\n" % (x, y, keys))
def OnEndDragLeft(self, x, y, keys):
self.log.write("OnEndDragLeft: %s, %s, %s\n" % (x, y, keys))
class Frame1(wx.Frame):
def _init_ctrls(self, prnt):
# generated method, don't edit
wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
pos=wx.Point(482, 193), size=wx.Size(555, 417),
style=wx.DEFAULT_FRAME_STYLE, title='Frame1')
self.SetClientSize(wx.Size(539, 381))
self.splitterWindow1 = wx.SplitterWindow(id=wxID_FRAME1SPLITTERWINDOW1,
name='splitterWindow1', parent=self, pos=wx.Point(0, 0),
size=wx.Size(539, 381), style=wx.SP_3D)
self.window1 = wx.Window(id=wxID_FRAME1WINDOW1, name='window1',
parent=self.splitterWindow1, pos=wx.Point(0, 0), size=wx.Size(200,
381), style=0)
self.window1.SetBackgroundColour(wx.Colour(255, 255, 128))
self.window2 = wx.Window(id=wxID_FRAME1WINDOW2, name='window2',
parent=self.splitterWindow1, pos=wx.Point(204, 0),
size=wx.Size(335, 381), style=0)
self.window2.SetBackgroundColour(wx.Colour(128, 255, 255))
self.splitterWindow1.SplitVertically(self.window1, self.window2, 200)
def __init__(self, parent):
self._init_ctrls(parent)
ogl.OGLInitialize()
win = TestWindow(self.window2, self.window2)
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = create(None)
frame.Show()
app.MainLoop()