]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/OGL.py
786eae770e159fddfee576dbbd4e79b35a20fa5d
1 # 11/20/2003 - Jeff Grimmett (grimmtooth@softhome.net)
3 # o Updated for wx namespace
5 # 11/30/2003 - Jeff Grimmett (grimmtooth@softhome.net)
7 # o OGL's busted bigtime. Can't even use OGLInitialize() without a
8 # program error on w2k.
18 #----------------------------------------------------------------------
20 class DiamondShape(ogl
.PolygonShape
):
21 def __init__(self
, w
=0.0, h
=0.0):
22 ogl
.PolygonShape
.__init
__(self
)
28 # Either ogl.RealPoints or 2-tuples of floats works.
30 #points = [ ogl.RealPoint(0.0, -h/2.0),
31 # ogl.RealPoint(w/2.0, 0.0),
32 # ogl.RealPoint(0.0, h/2.0),
33 # ogl.RealPoint(-w/2.0, 0.0),
35 points
= [ (0.0, -h
/2.0),
44 #----------------------------------------------------------------------
46 class RoundedRectangleShape(ogl
.RectangleShape
):
47 def __init__(self
, w
=0.0, h
=0.0):
48 ogl
.RectangleShape
.__init
__(self
, w
, h
)
49 self
.SetCornerRadius(-0.3)
52 #----------------------------------------------------------------------
54 class DividedShape(ogl
.DividedShape
):
55 def __init__(self
, width
, height
, canvas
):
56 ogl
.DividedShape
.__init
__(self
, width
, height
)
58 region1
= ogl
.ShapeRegion()
59 region1
.SetText('DividedShape')
60 region1
.SetProportions(0.0, 0.2)
61 region1
.SetFormatMode(ogl
.FORMAT_CENTRE_HORIZ
)
62 self
.AddRegion(region1
)
64 region2
= ogl
.ShapeRegion()
65 region2
.SetText('This is Region number two.')
66 region2
.SetProportions(0.0, 0.3)
67 region2
.SetFormatMode(ogl
.FORMAT_CENTRE_HORIZ|ogl
.FORMAT_CENTRE_VERT
)
68 self
.AddRegion(region2
)
70 region3
= ogl
.ShapeRegion()
71 region3
.SetText('Region 3\nwith embedded\nline breaks')
72 region3
.SetProportions(0.0, 0.5)
73 region3
.SetFormatMode(ogl
.FORMAT_NONE
)
74 self
.AddRegion(region3
)
77 self
.ReformatRegions(canvas
)
80 def ReformatRegions(self
, canvas
=None):
84 canvas
= self
.GetCanvas()
86 dc
= wx
.ClientDC(canvas
) # used for measuring
88 for region
in self
.GetRegions():
89 text
= region
.GetText()
90 self
.FormatText(dc
, text
, rnum
)
94 def OnSizingEndDragLeft(self
, pt
, x
, y
, keys
, attch
):
96 self
.base_OnSizingEndDragLeft(pt
, x
, y
, keys
, attch
)
98 self
.ReformatRegions()
99 self
.GetCanvas().Refresh()
102 #----------------------------------------------------------------------
104 class MyEvtHandler(ogl
.ShapeEvtHandler
):
105 def __init__(self
, log
, frame
):
106 ogl
.ShapeEvtHandler
.__init
__(self
)
108 self
.statbarFrame
= frame
110 def UpdateStatusBar(self
, shape
):
111 x
,y
= shape
.GetX(), shape
.GetY()
112 width
, height
= shape
.GetBoundingBoxMax()
113 self
.statbarFrame
.SetStatusText("Pos: (%d,%d) Size: (%d, %d)" %
114 (x
, y
, width
, height
))
117 def OnLeftClick(self
, x
, y
, keys
= 0, attachment
= 0):
118 shape
= self
.GetShape()
119 print shape
.__class
__, shape
.GetClassName()
120 canvas
= shape
.GetCanvas()
121 dc
= wx
.ClientDC(canvas
)
125 shape
.Select(False, dc
)
129 shapeList
= canvas
.GetDiagram().GetShapeList()
134 # If we unselect it now then some of the objects in
135 # shapeList will become invalid (the control points are
136 # shapes too!) and bad things will happen...
139 shape
.Select(True, dc
)
147 self
.UpdateStatusBar(shape
)
150 def OnEndDragLeft(self
, x
, y
, keys
= 0, attachment
= 0):
151 shape
= self
.GetShape()
152 self
.base_OnEndDragLeft(x
, y
, keys
, attachment
)
154 if not shape
.Selected():
155 self
.OnLeftClick(x
, y
, keys
, attachment
)
157 self
.UpdateStatusBar(shape
)
160 def OnSizingEndDragLeft(self
, pt
, x
, y
, keys
, attch
):
161 self
.base_OnSizingEndDragLeft(pt
, x
, y
, keys
, attch
)
162 self
.UpdateStatusBar(self
.GetShape())
165 def OnMovePost(self
, dc
, x
, y
, oldX
, oldY
, display
):
166 self
.base_OnMovePost(dc
, x
, y
, oldX
, oldY
, display
)
167 self
.UpdateStatusBar(self
.GetShape())
170 def OnRightClick(self
, *dontcare
):
171 self
.log
.WriteText("%s\n" % self
.GetShape())
174 #----------------------------------------------------------------------
176 class TestWindow(ogl
.ShapeCanvas
):
177 def __init__(self
, parent
, log
, frame
):
178 ogl
.ShapeCanvas
.__init
__(self
, parent
)
182 self
.SetScrollbars(20, 20, maxWidth
/20, maxHeight
/20)
186 self
.SetBackgroundColour("LIGHT BLUE") #wx.WHITE)
187 self
.diagram
= ogl
.Diagram()
188 self
.SetDiagram(self
.diagram
)
189 self
.diagram
.SetCanvas(self
)
193 rRectBrush
= wx
.Brush("MEDIUM TURQUOISE", wx
.SOLID
)
194 dsBrush
= wx
.Brush("WHEAT", wx
.SOLID
)
198 100, 100, wx
.Pen(wx
.BLUE
, 3), wx
.GREEN_BRUSH
, "Circle"
202 ogl
.RectangleShape(85, 50),
203 305, 60, wx
.BLACK_PEN
, wx
.LIGHT_GREY_BRUSH
, "Rectangle"
206 ds
= self
.MyAddShape(
207 DividedShape(140, 150, self
),
208 495, 145, wx
.BLACK_PEN
, dsBrush
, ''
212 DiamondShape(90, 90),
213 345, 235, wx
.Pen(wx
.BLUE
, 3, wx
.DOT
), wx
.RED_BRUSH
, "Polygon"
217 RoundedRectangleShape(95,70),
218 140, 255, wx
.Pen(wx
.RED
, 2), rRectBrush
, "Rounded Rect"
221 bmp
= images
.getTest2Bitmap()
222 mask
= wx
.MaskColour(bmp
, wx
.BLUE
)
225 s
= ogl
.BitmapShape()
227 self
.MyAddShape(s
, 225, 150, None, None, "Bitmap")
229 dc
= wx
.ClientDC(self
)
232 for x
in range(len(self
.shapes
)):
233 fromShape
= self
.shapes
[x
]
234 if x
+1 == len(self
.shapes
):
235 toShape
= self
.shapes
[0]
237 toShape
= self
.shapes
[x
+1]
239 line
= ogl
.LineShape()
241 line
.SetPen(wx
.BLACK_PEN
)
242 line
.SetBrush(wx
.BLACK_BRUSH
)
243 line
.AddArrow(ogl
.ARROW_ARROW
)
244 line
.MakeLineControlPoints(2)
245 fromShape
.AddLine(line
, toShape
)
246 self
.diagram
.AddShape(line
)
249 # for some reason, the shapes have to be moved for the line to show up...
250 fromShape
.Move(dc
, fromShape
.GetX(), fromShape
.GetY())
252 wx
.EVT_WINDOW_DESTROY(self
, self
.OnDestroy
)
255 def MyAddShape(self
, shape
, x
, y
, pen
, brush
, text
):
256 shape
.SetDraggable(True, True)
257 shape
.SetCanvas(self
)
260 if pen
: shape
.SetPen(pen
)
261 if brush
: shape
.SetBrush(brush
)
262 if text
: shape
.AddText(text
)
263 #shape.SetShadowMode(ogl.SHADOW_RIGHT)
264 self
.diagram
.AddShape(shape
)
267 evthandler
= MyEvtHandler(self
.log
, self
.frame
)
268 evthandler
.SetShape(shape
)
269 evthandler
.SetPreviousHandler(shape
.GetEventHandler())
270 shape
.SetEventHandler(evthandler
)
272 self
.shapes
.append(shape
)
276 def OnDestroy(self
, evt
):
278 for shape
in self
.diagram
.GetShapeList():
279 if shape
.GetParent() == None:
280 shape
.SetCanvas(None)
283 self
.diagram
.Destroy()
286 def OnBeginDragLeft(self
, x
, y
, keys
):
287 self
.log
.write("OnBeginDragLeft: %s, %s, %s\n" % (x
, y
, keys
))
289 def OnEndDragLeft(self
, x
, y
, keys
):
290 self
.log
.write("OnEndDragLeft: %s, %s, %s\n" % (x
, y
, keys
))
293 #----------------------------------------------------------------------
295 def runTest(frame
, nb
, log
):
296 # This creates some pens and brushes that the OGL library uses.
297 # It should be called after the app object has been created, but
298 # before OGL is used.
301 win
= TestWindow(nb
, log
, frame
)
304 #----------------------------------------------------------------------
306 # The OGL library holds some resources that need to be freed before
307 # the app shuts down.
309 def __del__(self
, cleanup
=ogl
.OGLCleanUp
):
312 # When this module gets cleaned up by Python then __cu will be cleaned
313 # up and it's __dell__ is called, which will then call ogl.OGLCleanUp.
318 The Object Graphics Library is a library supporting the creation and
319 manipulation of simple and complex graphic images on a canvas.
323 if __name__
== '__main__':
326 run
.main(['', os
.path
.basename(sys
.argv
[0])])