]>
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
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 #----------------------------------------------------------------------
19 # This creates some pens and brushes that the OGL library uses.
23 #----------------------------------------------------------------------
25 class DiamondShape(ogl
.PolygonShape
):
26 def __init__(self
, w
=0.0, h
=0.0):
27 ogl
.PolygonShape
.__init
__(self
)
33 # Either ogl.RealPoints or 2-tuples of floats works.
35 #points = [ ogl.RealPoint(0.0, -h/2.0),
36 # ogl.RealPoint(w/2.0, 0.0),
37 # ogl.RealPoint(0.0, h/2.0),
38 # ogl.RealPoint(-w/2.0, 0.0),
40 points
= [ (0.0, -h
/2.0),
49 #----------------------------------------------------------------------
51 class RoundedRectangleShape(ogl
.RectangleShape
):
52 def __init__(self
, w
=0.0, h
=0.0):
53 ogl
.RectangleShape
.__init
__(self
, w
, h
)
54 self
.SetCornerRadius(-0.3)
57 #----------------------------------------------------------------------
59 class DividedShape(ogl
.DividedShape
):
60 def __init__(self
, width
, height
, canvas
):
61 ogl
.DividedShape
.__init
__(self
, width
, height
)
63 region1
= ogl
.ShapeRegion()
64 region1
.SetText('DividedShape')
65 region1
.SetProportions(0.0, 0.2)
66 region1
.SetFormatMode(ogl
.FORMAT_CENTRE_HORIZ
)
67 self
.AddRegion(region1
)
69 region2
= ogl
.ShapeRegion()
70 region2
.SetText('This is Region number two.')
71 region2
.SetProportions(0.0, 0.3)
72 region2
.SetFormatMode(ogl
.FORMAT_CENTRE_HORIZ|ogl
.FORMAT_CENTRE_VERT
)
73 self
.AddRegion(region2
)
75 region3
= ogl
.ShapeRegion()
76 region3
.SetText('Region 3\nwith embedded\nline breaks')
77 region3
.SetProportions(0.0, 0.5)
78 region3
.SetFormatMode(ogl
.FORMAT_NONE
)
79 self
.AddRegion(region3
)
82 self
.ReformatRegions(canvas
)
85 def ReformatRegions(self
, canvas
=None):
89 canvas
= self
.GetCanvas()
91 dc
= wx
.ClientDC(canvas
) # used for measuring
93 for region
in self
.GetRegions():
94 text
= region
.GetText()
95 self
.FormatText(dc
, text
, rnum
)
99 def OnSizingEndDragLeft(self
, pt
, x
, y
, keys
, attch
):
101 self
.base_OnSizingEndDragLeft(pt
, x
, y
, keys
, attch
)
102 self
.SetRegionSizes()
103 self
.ReformatRegions()
104 self
.GetCanvas().Refresh()
107 #----------------------------------------------------------------------
109 class MyEvtHandler(ogl
.ShapeEvtHandler
):
110 def __init__(self
, log
, frame
):
111 ogl
.ShapeEvtHandler
.__init
__(self
)
113 self
.statbarFrame
= frame
115 def UpdateStatusBar(self
, shape
):
116 x
,y
= shape
.GetX(), shape
.GetY()
117 width
, height
= shape
.GetBoundingBoxMax()
118 self
.statbarFrame
.SetStatusText("Pos: (%d,%d) Size: (%d, %d)" %
119 (x
, y
, width
, height
))
122 def OnLeftClick(self
, x
, y
, keys
= 0, attachment
= 0):
123 shape
= self
.GetShape()
124 print shape
.__class
__, shape
.GetClassName()
125 canvas
= shape
.GetCanvas()
126 dc
= wx
.ClientDC(canvas
)
130 shape
.Select(False, dc
)
134 shapeList
= canvas
.GetDiagram().GetShapeList()
139 # If we unselect it now then some of the objects in
140 # shapeList will become invalid (the control points are
141 # shapes too!) and bad things will happen...
144 shape
.Select(True, dc
)
152 self
.UpdateStatusBar(shape
)
155 def OnEndDragLeft(self
, x
, y
, keys
= 0, attachment
= 0):
156 shape
= self
.GetShape()
157 self
.base_OnEndDragLeft(x
, y
, keys
, attachment
)
159 if not shape
.Selected():
160 self
.OnLeftClick(x
, y
, keys
, attachment
)
162 self
.UpdateStatusBar(shape
)
165 def OnSizingEndDragLeft(self
, pt
, x
, y
, keys
, attch
):
166 self
.base_OnSizingEndDragLeft(pt
, x
, y
, keys
, attch
)
167 self
.UpdateStatusBar(self
.GetShape())
170 def OnMovePost(self
, dc
, x
, y
, oldX
, oldY
, display
):
171 self
.base_OnMovePost(dc
, x
, y
, oldX
, oldY
, display
)
172 self
.UpdateStatusBar(self
.GetShape())
175 def OnRightClick(self
, *dontcare
):
176 self
.log
.WriteText("%s\n" % self
.GetShape())
179 #----------------------------------------------------------------------
181 class TestWindow(ogl
.ShapeCanvas
):
182 def __init__(self
, parent
, log
, frame
):
183 ogl
.ShapeCanvas
.__init
__(self
, parent
)
187 self
.SetScrollbars(20, 20, maxWidth
/20, maxHeight
/20)
191 self
.SetBackgroundColour("LIGHT BLUE") #wx.WHITE)
192 self
.diagram
= ogl
.Diagram()
193 self
.SetDiagram(self
.diagram
)
194 self
.diagram
.SetCanvas(self
)
198 rRectBrush
= wx
.Brush("MEDIUM TURQUOISE", wx
.SOLID
)
199 dsBrush
= wx
.Brush("WHEAT", wx
.SOLID
)
203 100, 100, wx
.Pen(wx
.BLUE
, 3), wx
.GREEN_BRUSH
, "Circle"
207 ogl
.RectangleShape(85, 50),
208 305, 60, wx
.BLACK_PEN
, wx
.LIGHT_GREY_BRUSH
, "Rectangle"
211 ds
= self
.MyAddShape(
212 DividedShape(140, 150, self
),
213 495, 145, wx
.BLACK_PEN
, dsBrush
, ''
217 DiamondShape(90, 90),
218 345, 235, wx
.Pen(wx
.BLUE
, 3, wx
.DOT
), wx
.RED_BRUSH
, "Polygon"
222 RoundedRectangleShape(95,70),
223 140, 255, wx
.Pen(wx
.RED
, 2), rRectBrush
, "Rounded Rect"
226 bmp
= images
.getTest2Bitmap()
227 mask
= wx
.MaskColour(bmp
, wx
.BLUE
)
230 s
= ogl
.BitmapShape()
232 self
.MyAddShape(s
, 225, 150, None, None, "Bitmap")
234 dc
= wx
.ClientDC(self
)
237 for x
in range(len(self
.shapes
)):
238 fromShape
= self
.shapes
[x
]
239 if x
+1 == len(self
.shapes
):
240 toShape
= self
.shapes
[0]
242 toShape
= self
.shapes
[x
+1]
244 line
= ogl
.LineShape()
246 line
.SetPen(wx
.BLACK_PEN
)
247 line
.SetBrush(wx
.BLACK_BRUSH
)
248 line
.AddArrow(ogl
.ARROW_ARROW
)
249 line
.MakeLineControlPoints(2)
250 fromShape
.AddLine(line
, toShape
)
251 self
.diagram
.AddShape(line
)
254 # for some reason, the shapes have to be moved for the line to show up...
255 fromShape
.Move(dc
, fromShape
.GetX(), fromShape
.GetY())
257 wx
.EVT_WINDOW_DESTROY(self
, self
.OnDestroy
)
260 def MyAddShape(self
, shape
, x
, y
, pen
, brush
, text
):
261 shape
.SetDraggable(True, True)
262 shape
.SetCanvas(self
)
265 if pen
: shape
.SetPen(pen
)
266 if brush
: shape
.SetBrush(brush
)
267 if text
: shape
.AddText(text
)
268 #shape.SetShadowMode(ogl.SHADOW_RIGHT)
269 self
.diagram
.AddShape(shape
)
272 evthandler
= MyEvtHandler(self
.log
, self
.frame
)
273 evthandler
.SetShape(shape
)
274 evthandler
.SetPreviousHandler(shape
.GetEventHandler())
275 shape
.SetEventHandler(evthandler
)
277 self
.shapes
.append(shape
)
281 def OnDestroy(self
, evt
):
283 for shape
in self
.diagram
.GetShapeList():
284 if shape
.GetParent() == None:
285 shape
.SetCanvas(None)
288 self
.diagram
.Destroy()
291 def OnBeginDragLeft(self
, x
, y
, keys
):
292 self
.log
.write("OnBeginDragLeft: %s, %s, %s\n" % (x
, y
, keys
))
294 def OnEndDragLeft(self
, x
, y
, keys
):
295 self
.log
.write("OnEndDragLeft: %s, %s, %s\n" % (x
, y
, keys
))
298 #----------------------------------------------------------------------
300 def runTest(frame
, nb
, log
):
301 # This creates some pens and brushes that the OGL library uses.
302 # It should be called after the app object has been created, but
303 # before OGL is used.
306 win
= TestWindow(nb
, log
, frame
)
309 #----------------------------------------------------------------------
312 cleanup
= ogl
.OGLCleanUp
316 # when this module gets cleaned up then wx.OGLCleanUp() will get called
321 The Object Graphics Library is a library supporting the creation and
322 manipulation of simple and complex graphic images on a canvas.
326 if __name__
== '__main__':
329 run
.main(['', os
.path
.basename(sys
.argv
[0])])