]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/OGL.py
   1 # 11/20/2003 - Jeff Grimmett (grimmtooth@softhome.net) 
   3 # o Updated for wx namespace 
  13 #---------------------------------------------------------------------- 
  15 class DiamondShape(ogl
.PolygonShape
): 
  16     def __init__(self
, w
=0.0, h
=0.0): 
  17         ogl
.PolygonShape
.__init
__(self
) 
  23         # Either ogl.RealPoints or 2-tuples of floats  works. 
  25         #points = [ ogl.RealPoint(0.0,    -h/2.0), 
  26         #          ogl.RealPoint(w/2.0,  0.0), 
  27         #          ogl.RealPoint(0.0,    h/2.0), 
  28         #          ogl.RealPoint(-w/2.0, 0.0), 
  30         points 
= [ (0.0,    -h
/2.0), 
  39 #---------------------------------------------------------------------- 
  41 class RoundedRectangleShape(ogl
.RectangleShape
): 
  42     def __init__(self
, w
=0.0, h
=0.0): 
  43         ogl
.RectangleShape
.__init
__(self
, w
, h
) 
  44         self
.SetCornerRadius(-0.3) 
  47 #---------------------------------------------------------------------- 
  49 class DividedShape(ogl
.DividedShape
): 
  50     def __init__(self
, width
, height
, canvas
): 
  51         ogl
.DividedShape
.__init
__(self
, width
, height
) 
  53         region1 
= ogl
.ShapeRegion() 
  54         region1
.SetText('DividedShape') 
  55         region1
.SetProportions(0.0, 0.2) 
  56         region1
.SetFormatMode(ogl
.FORMAT_CENTRE_HORIZ
) 
  57         self
.AddRegion(region1
) 
  59         region2 
= ogl
.ShapeRegion() 
  60         region2
.SetText('This is Region number two.') 
  61         region2
.SetProportions(0.0, 0.3) 
  62         region2
.SetFormatMode(ogl
.FORMAT_CENTRE_HORIZ|ogl
.FORMAT_CENTRE_VERT
) 
  63         self
.AddRegion(region2
) 
  65         region3 
= ogl
.ShapeRegion() 
  66         region3
.SetText('Region 3\nwith embedded\nline breaks') 
  67         region3
.SetProportions(0.0, 0.5) 
  68         region3
.SetFormatMode(ogl
.FORMAT_NONE
) 
  69         self
.AddRegion(region3
) 
  72         self
.ReformatRegions(canvas
) 
  75     def ReformatRegions(self
, canvas
=None): 
  79             canvas 
= self
.GetCanvas() 
  81         dc 
= wx
.ClientDC(canvas
)  # used for measuring 
  83         for region 
in self
.GetRegions(): 
  84             text 
= region
.GetText() 
  85             self
.FormatText(dc
, text
, rnum
) 
  89     def OnSizingEndDragLeft(self
, pt
, x
, y
, keys
, attch
): 
  91         self
.base_OnSizingEndDragLeft(pt
, x
, y
, keys
, attch
) 
  93         self
.ReformatRegions() 
  94         self
.GetCanvas().Refresh() 
  97 #---------------------------------------------------------------------- 
  99 class MyEvtHandler(ogl
.ShapeEvtHandler
): 
 100     def __init__(self
, log
, frame
): 
 101         ogl
.ShapeEvtHandler
.__init
__(self
) 
 103         self
.statbarFrame 
= frame
 
 105     def UpdateStatusBar(self
, shape
): 
 106         x
,y 
= shape
.GetX(), shape
.GetY() 
 107         width
, height 
= shape
.GetBoundingBoxMax() 
 108         self
.statbarFrame
.SetStatusText("Pos: (%d,%d)  Size: (%d, %d)" % 
 109                                         (x
, y
, width
, height
)) 
 112     def OnLeftClick(self
, x
, y
, keys 
= 0, attachment 
= 0): 
 113         shape 
= self
.GetShape() 
 114         print shape
.__class
__, shape
.GetClassName() 
 115         canvas 
= shape
.GetCanvas() 
 116         dc 
= wx
.ClientDC(canvas
) 
 120             shape
.Select(False, dc
) 
 124             shapeList 
= canvas
.GetDiagram().GetShapeList() 
 129                     # If we unselect it now then some of the objects in 
 130                     # shapeList will become invalid (the control points are 
 131                     # shapes too!) and bad things will happen... 
 134             shape
.Select(True, dc
) 
 142         self
.UpdateStatusBar(shape
) 
 145     def OnEndDragLeft(self
, x
, y
, keys 
= 0, attachment 
= 0): 
 146         shape 
= self
.GetShape() 
 147         self
.base_OnEndDragLeft(x
, y
, keys
, attachment
) 
 149         if not shape
.Selected(): 
 150             self
.OnLeftClick(x
, y
, keys
, attachment
) 
 152         self
.UpdateStatusBar(shape
) 
 155     def OnSizingEndDragLeft(self
, pt
, x
, y
, keys
, attch
): 
 156         self
.base_OnSizingEndDragLeft(pt
, x
, y
, keys
, attch
) 
 157         self
.UpdateStatusBar(self
.GetShape()) 
 160     def OnMovePost(self
, dc
, x
, y
, oldX
, oldY
, display
): 
 161         self
.base_OnMovePost(dc
, x
, y
, oldX
, oldY
, display
) 
 162         self
.UpdateStatusBar(self
.GetShape()) 
 165     def OnRightClick(self
, *dontcare
): 
 166         self
.log
.WriteText("%s\n" % self
.GetShape()) 
 169 #---------------------------------------------------------------------- 
 171 class TestWindow(ogl
.ShapeCanvas
): 
 172     def __init__(self
, parent
, log
, frame
): 
 173         ogl
.ShapeCanvas
.__init
__(self
, parent
) 
 177         self
.SetScrollbars(20, 20, maxWidth
/20, maxHeight
/20) 
 181         self
.SetBackgroundColour("LIGHT BLUE") #wx.WHITE) 
 182         self
.diagram 
= ogl
.Diagram() 
 183         self
.SetDiagram(self
.diagram
) 
 184         self
.diagram
.SetCanvas(self
) 
 188         rRectBrush 
= wx
.Brush("MEDIUM TURQUOISE", wx
.SOLID
) 
 189         dsBrush 
= wx
.Brush("WHEAT", wx
.SOLID
) 
 193             100, 100, wx
.Pen(wx
.BLUE
, 3), wx
.GREEN_BRUSH
, "Circle" 
 197             ogl
.RectangleShape(85, 50),  
 198             305, 60, wx
.BLACK_PEN
, wx
.LIGHT_GREY_BRUSH
, "Rectangle" 
 201         ds 
= self
.MyAddShape( 
 202                     DividedShape(140, 150, self
),  
 203                     495, 145, wx
.BLACK_PEN
, dsBrush
, '' 
 207             DiamondShape(90, 90),  
 208             345, 235, wx
.Pen(wx
.BLUE
, 3, wx
.DOT
), wx
.RED_BRUSH
, "Polygon" 
 212             RoundedRectangleShape(95,70),  
 213             140, 255, wx
.Pen(wx
.RED
, 2), rRectBrush
, "Rounded Rect" 
 216         bmp 
= images
.getTest2Bitmap() 
 217         mask 
= wx
.MaskColour(bmp
, wx
.BLUE
) 
 220         s 
= ogl
.BitmapShape() 
 222         self
.MyAddShape(s
, 225, 150, None, None, "Bitmap") 
 224         dc 
= wx
.ClientDC(self
) 
 227         for x 
in range(len(self
.shapes
)): 
 228             fromShape 
= self
.shapes
[x
] 
 229             if x
+1 == len(self
.shapes
): 
 230                 toShape 
= self
.shapes
[0] 
 232                 toShape 
= self
.shapes
[x
+1] 
 234             line 
= ogl
.LineShape() 
 236             line
.SetPen(wx
.BLACK_PEN
) 
 237             line
.SetBrush(wx
.BLACK_BRUSH
) 
 238             line
.AddArrow(ogl
.ARROW_ARROW
) 
 239             line
.MakeLineControlPoints(2) 
 240             fromShape
.AddLine(line
, toShape
) 
 241             self
.diagram
.AddShape(line
) 
 244             # for some reason, the shapes have to be moved for the line to show up... 
 245             fromShape
.Move(dc
, fromShape
.GetX(), fromShape
.GetY()) 
 247         self
.Bind(wx
.EVT_WINDOW_DESTROY
, self
.OnDestroy
) 
 250     def MyAddShape(self
, shape
, x
, y
, pen
, brush
, text
): 
 251         shape
.SetDraggable(True, True) 
 252         shape
.SetCanvas(self
) 
 255         if pen
:    shape
.SetPen(pen
) 
 256         if brush
:  shape
.SetBrush(brush
) 
 257         if text
:   shape
.AddText(text
) 
 258         #shape.SetShadowMode(ogl.SHADOW_RIGHT) 
 259         self
.diagram
.AddShape(shape
) 
 262         evthandler 
= MyEvtHandler(self
.log
, self
.frame
) 
 263         evthandler
.SetShape(shape
) 
 264         evthandler
.SetPreviousHandler(shape
.GetEventHandler()) 
 265         shape
.SetEventHandler(evthandler
) 
 267         self
.shapes
.append(shape
) 
 271     def OnDestroy(self
, evt
): 
 273         for shape 
in self
.diagram
.GetShapeList(): 
 274             if shape
.GetParent() == None: 
 275                 shape
.SetCanvas(None) 
 278         self
.diagram
.Destroy() 
 281     def OnBeginDragLeft(self
, x
, y
, keys
): 
 282         self
.log
.write("OnBeginDragLeft: %s, %s, %s\n" % (x
, y
, keys
)) 
 284     def OnEndDragLeft(self
, x
, y
, keys
): 
 285         self
.log
.write("OnEndDragLeft: %s, %s, %s\n" % (x
, y
, keys
)) 
 288 #---------------------------------------------------------------------- 
 290 def runTest(frame
, nb
, log
): 
 291     # This creates some pens and brushes that the OGL library uses. 
 292     # It should be called after the app object has been created, but 
 293     # before OGL is used. 
 296     win 
= TestWindow(nb
, log
, frame
) 
 299 #---------------------------------------------------------------------- 
 301 # The OGL library holds some resources that need to be freed before 
 302 # the app shuts down. 
 304     def __del__(self
, cleanup
=ogl
.OGLCleanUp
): 
 307 # When this module gets cleaned up by Python then __cu will be cleaned 
 308 # up and it's __dell__ is called, which will then call ogl.OGLCleanUp. 
 313 The Object Graphics Library is a library supporting the creation and 
 314 manipulation of simple and complex graphic images on a canvas. 
 318 if __name__ 
== '__main__': 
 321     run
.main(['', os
.path
.basename(sys
.argv
[0])])