X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/856e03b7538cb74a991ad18a13708583d8712fdd..61940a7061d0b5d5122b1e1e939cfa2d72596999:/wxPython/demo/wxOGL.py diff --git a/wxPython/demo/wxOGL.py b/wxPython/demo/wxOGL.py index a13bee893a..eb7cea9c83 100644 --- a/wxPython/demo/wxOGL.py +++ b/wxPython/demo/wxOGL.py @@ -1,30 +1,41 @@ +# 11/20/2003 - Jeff Grimmett (grimmtooth@softhome.net) +# +# o Updated for wx namespace +# +# 11/30/2003 - Jeff Grimmett (grimmtooth@softhome.net) +# +# o OGL's busted bigtime. Can't even use OGLInitialize() without a +# program error on w2k. +# -from wxPython.wx import * -from wxPython.ogl import * +import wx +import wx.ogl as ogl -import images +import images + +##wx.Trap() #---------------------------------------------------------------------- # This creates some pens and brushes that the OGL library uses. -wxOGLInitialize() +ogl.OGLInitialize() #---------------------------------------------------------------------- -class DiamondShape(wxPolygonShape): +class DiamondShape(ogl.PolygonShape): def __init__(self, w=0.0, h=0.0): - wxPolygonShape.__init__(self) + ogl.PolygonShape.__init__(self) if w == 0.0: w = 60.0 if h == 0.0: h = 60.0 - ## Either wxRealPoints or 2-tuples of floats works. + # Either ogl.RealPoints or 2-tuples of floats works. - #points = [ wxRealPoint(0.0, -h/2.0), - # wxRealPoint(w/2.0, 0.0), - # wxRealPoint(0.0, h/2.0), - # wxRealPoint(-w/2.0, 0.0), + #points = [ ogl.RealPoint(0.0, -h/2.0), + # ogl.RealPoint(w/2.0, 0.0), + # ogl.RealPoint(0.0, h/2.0), + # ogl.RealPoint(-w/2.0, 0.0), # ] points = [ (0.0, -h/2.0), (w/2.0, 0.0), @@ -37,17 +48,67 @@ class DiamondShape(wxPolygonShape): #---------------------------------------------------------------------- -class RoundedRectangleShape(wxRectangleShape): +class RoundedRectangleShape(ogl.RectangleShape): def __init__(self, w=0.0, h=0.0): - wxRectangleShape.__init__(self, w, h) + ogl.RectangleShape.__init__(self, w, h) self.SetCornerRadius(-0.3) #---------------------------------------------------------------------- -class MyEvtHandler(wxShapeEvtHandler): +class DividedShape(ogl.DividedShape): + def __init__(self, width, height, canvas): + ogl.DividedShape.__init__(self, width, height) + + region1 = ogl.ShapeRegion() + region1.SetText('DividedShape') + region1.SetProportions(0.0, 0.2) + region1.SetFormatMode(ogl.FORMAT_CENTRE_HORIZ) + self.AddRegion(region1) + + region2 = ogl.ShapeRegion() + region2.SetText('This is Region number two.') + region2.SetProportions(0.0, 0.3) + region2.SetFormatMode(ogl.FORMAT_CENTRE_HORIZ|ogl.FORMAT_CENTRE_VERT) + self.AddRegion(region2) + + region3 = ogl.ShapeRegion() + region3.SetText('Region 3\nwith embedded\nline breaks') + region3.SetProportions(0.0, 0.5) + region3.SetFormatMode(ogl.FORMAT_NONE) + self.AddRegion(region3) + + self.SetRegionSizes() + self.ReformatRegions(canvas) + + + def ReformatRegions(self, canvas=None): + rnum = 0 + + if canvas is None: + canvas = self.GetCanvas() + + dc = wx.ClientDC(canvas) # used for measuring + + for region in self.GetRegions(): + text = region.GetText() + self.FormatText(dc, text, rnum) + rnum += 1 + + + def OnSizingEndDragLeft(self, pt, x, y, keys, attch): + print "***", self + self.base_OnSizingEndDragLeft(pt, x, y, keys, attch) + self.SetRegionSizes() + self.ReformatRegions() + self.GetCanvas().Refresh() + + +#---------------------------------------------------------------------- + +class MyEvtHandler(ogl.ShapeEvtHandler): def __init__(self, log, frame): - wxShapeEvtHandler.__init__(self) + ogl.ShapeEvtHandler.__init__(self) self.log = log self.statbarFrame = frame @@ -60,17 +121,19 @@ class MyEvtHandler(wxShapeEvtHandler): def OnLeftClick(self, x, y, keys = 0, attachment = 0): shape = self.GetShape() + print shape.__class__, shape.GetClassName() canvas = shape.GetCanvas() - dc = wxClientDC(canvas) + dc = wx.ClientDC(canvas) canvas.PrepareDC(dc) if shape.Selected(): - shape.Select(false, dc) + shape.Select(False, dc) canvas.Redraw(dc) else: - redraw = false + 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 @@ -78,11 +141,12 @@ class MyEvtHandler(wxShapeEvtHandler): # shapes too!) and bad things will happen... toUnselect.append(s) - shape.Select(true, dc) + shape.Select(True, dc) if toUnselect: for s in toUnselect: - s.Select(false, dc) + s.Select(False, dc) + canvas.Redraw(dc) self.UpdateStatusBar(shape) @@ -91,19 +155,21 @@ class MyEvtHandler(wxShapeEvtHandler): def OnEndDragLeft(self, x, y, keys = 0, attachment = 0): shape = self.GetShape() self.base_OnEndDragLeft(x, y, keys, attachment) + if not shape.Selected(): self.OnLeftClick(x, y, keys, attachment) + self.UpdateStatusBar(shape) - def OnSize(self, x, y): - self.base_OnSize(x, y) + def OnSizingEndDragLeft(self, pt, x, y, keys, attch): + self.base_OnSizingEndDragLeft(pt, x, y, keys, attch) self.UpdateStatusBar(self.GetShape()) -# def OnMovePost(self, dc, x, y, oldX, oldY, display): -# self.base_OnMovePost(dc, x, y, oldX, oldY, display) -# self.UpdateStatusBar(self.GetShape()) + def OnMovePost(self, dc, x, y, oldX, oldY, display): + self.base_OnMovePost(dc, x, y, oldX, oldY, display) + self.UpdateStatusBar(self.GetShape()) def OnRightClick(self, *dontcare): @@ -112,9 +178,9 @@ class MyEvtHandler(wxShapeEvtHandler): #---------------------------------------------------------------------- -class TestWindow(wxShapeCanvas): +class TestWindow(ogl.ShapeCanvas): def __init__(self, parent, log, frame): - wxShapeCanvas.__init__(self, parent) + ogl.ShapeCanvas.__init__(self, parent) maxWidth = 1000 maxHeight = 1000 @@ -122,62 +188,86 @@ class TestWindow(wxShapeCanvas): self.log = log self.frame = frame - self.SetBackgroundColour(wxWHITE) - self.diagram = wxDiagram() + self.SetBackgroundColour("LIGHT BLUE") #wx.WHITE) + self.diagram = ogl.Diagram() self.SetDiagram(self.diagram) self.diagram.SetCanvas(self) self.shapes = [] self.save_gdi = [] - rRectBrush = wxBrush(wxNamedColour("MEDIUM TURQUOISE"), wxSOLID) - - self.MyAddShape(wxCircleShape(80), 100, 100, wxPen(wxBLUE, 3), wxGREEN_BRUSH, "Circle") - self.MyAddShape(wxRectangleShape(85, 50), 305, 60, wxBLACK_PEN, wxLIGHT_GREY_BRUSH, "Rectangle") - self.MyAddShape(DiamondShape(90, 90), 345, 235, wxPen(wxBLUE, 3, wxDOT), wxRED_BRUSH, "Polygon") - self.MyAddShape(RoundedRectangleShape(95,70), 140, 255, wxPen(wxRED, 1), rRectBrush, "Rounded Rect") + rRectBrush = wx.Brush("MEDIUM TURQUOISE", wx.SOLID) + dsBrush = wx.Brush("WHEAT", wx.SOLID) + + self.MyAddShape( + ogl.CircleShape(80), + 100, 100, wx.Pen(wx.BLUE, 3), wx.GREEN_BRUSH, "Circle" + ) + + self.MyAddShape( + ogl.RectangleShape(85, 50), + 305, 60, wx.BLACK_PEN, wx.LIGHT_GREY_BRUSH, "Rectangle" + ) + + ds = self.MyAddShape( + DividedShape(140, 150, self), + 495, 145, wx.BLACK_PEN, dsBrush, '' + ) + + self.MyAddShape( + DiamondShape(90, 90), + 345, 235, wx.Pen(wx.BLUE, 3, wx.DOT), wx.RED_BRUSH, "Polygon" + ) + + self.MyAddShape( + RoundedRectangleShape(95,70), + 140, 255, wx.Pen(wx.RED, 2), rRectBrush, "Rounded Rect" + ) bmp = images.getTest2Bitmap() - mask = wxMaskColour(bmp, wxBLUE) + mask = wx.MaskColour(bmp, wx.BLUE) bmp.SetMask(mask) - s = wxBitmapShape() + s = ogl.BitmapShape() s.SetBitmap(bmp) self.MyAddShape(s, 225, 150, None, None, "Bitmap") - dc = wxClientDC(self) + dc = wx.ClientDC(self) self.PrepareDC(dc) + for x in range(len(self.shapes)): fromShape = self.shapes[x] if x+1 == len(self.shapes): toShape = self.shapes[0] else: toShape = self.shapes[x+1] - line = wxLineShape() + + line = ogl.LineShape() line.SetCanvas(self) - line.SetPen(wxBLACK_PEN) - line.SetBrush(wxBLACK_BRUSH) - line.AddArrow(ARROW_ARROW) + line.SetPen(wx.BLACK_PEN) + line.SetBrush(wx.BLACK_BRUSH) + line.AddArrow(ogl.ARROW_ARROW) line.MakeLineControlPoints(2) fromShape.AddLine(line, toShape) self.diagram.AddShape(line) - line.Show(true) + line.Show(True) # for some reason, the shapes have to be moved for the line to show up... fromShape.Move(dc, fromShape.GetX(), fromShape.GetY()) + wx.EVT_WINDOW_DESTROY(self, self.OnDestroy) def MyAddShape(self, shape, x, y, pen, brush, text): - shape.SetDraggable(true, true) + 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: shape.AddText(text) - #shape.SetShadowMode(SHADOW_RIGHT) + #shape.SetShadowMode(ogl.SHADOW_RIGHT) self.diagram.AddShape(shape) - shape.Show(true) + shape.Show(True) evthandler = MyEvtHandler(self.log, self.frame) evthandler.SetShape(shape) @@ -185,27 +275,41 @@ class TestWindow(wxShapeCanvas): shape.SetEventHandler(evthandler) self.shapes.append(shape) + return shape - - def __del__(self): + def OnDestroy(self, evt): + # Do some cleanup for shape in self.diagram.GetShapeList(): if shape.GetParent() == None: shape.SetCanvas(None) shape.Destroy() + self.diagram.Destroy() + 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)) + + #---------------------------------------------------------------------- def runTest(frame, nb, log): + # This creates some pens and brushes that the OGL library uses. + # It should be called after the app object has been created, but + # before OGL is used. + ogl.OGLInitialize() + win = TestWindow(nb, log, frame) return win - + #---------------------------------------------------------------------- class __Cleanup: - cleanup = wxOGLCleanUp + cleanup = ogl.OGLCleanUp def __del__(self): self.cleanup() @@ -213,16 +317,14 @@ class __Cleanup: __cu = __Cleanup() - - - - - - overview = """\ The Object Graphics Library is a library supporting the creation and manipulation of simple and complex graphic images on a canvas. """ +if __name__ == '__main__': + import sys,os + import run + run.main(['', os.path.basename(sys.argv[0])])