]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/wxOGL.py
Another try at fixing this patch so it applies cleanly
[wxWidgets.git] / wxPython / demo / wxOGL.py
CommitLineData
8fa876ca
RD
1# 11/20/2003 - Jeff Grimmett (grimmtooth@softhome.net)
2#
3# o Updated for wx namespace
4#
5# 11/30/2003 - Jeff Grimmett (grimmtooth@softhome.net)
6#
7# o OGL's busted bigtime. Can't even use OGLInitialize() without a
8# program error on w2k.
9#
e91a9dfc 10
8fa876ca
RD
11import wx
12import wx.ogl as ogl
e91a9dfc 13
8fa876ca 14import images
96bfd053 15
8fa876ca 16##wx.Trap()
e91a9dfc
RD
17
18#----------------------------------------------------------------------
8fa876ca 19# This creates some pens and brushes that the OGL library uses.
e91a9dfc 20
8fa876ca
RD
21ogl.OGLInitialize()
22
23#----------------------------------------------------------------------
24
25class DiamondShape(ogl.PolygonShape):
e91a9dfc 26 def __init__(self, w=0.0, h=0.0):
8fa876ca 27 ogl.PolygonShape.__init__(self)
e91a9dfc
RD
28 if w == 0.0:
29 w = 60.0
30 if h == 0.0:
31 h = 60.0
32
8fa876ca 33 # Either ogl.RealPoints or 2-tuples of floats works.
e91a9dfc 34
8fa876ca
RD
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),
e91a9dfc
RD
39 # ]
40 points = [ (0.0, -h/2.0),
41 (w/2.0, 0.0),
42 (0.0, h/2.0),
43 (-w/2.0, 0.0),
44 ]
45
46 self.Create(points)
47
48
49#----------------------------------------------------------------------
50
8fa876ca 51class RoundedRectangleShape(ogl.RectangleShape):
e91a9dfc 52 def __init__(self, w=0.0, h=0.0):
8fa876ca 53 ogl.RectangleShape.__init__(self, w, h)
e91a9dfc
RD
54 self.SetCornerRadius(-0.3)
55
56
1e4a197e
RD
57#----------------------------------------------------------------------
58
8fa876ca 59class DividedShape(ogl.DividedShape):
1e4a197e 60 def __init__(self, width, height, canvas):
8fa876ca 61 ogl.DividedShape.__init__(self, width, height)
1e4a197e 62
8fa876ca
RD
63 region1 = ogl.ShapeRegion()
64 region1.SetText('DividedShape')
1e4a197e 65 region1.SetProportions(0.0, 0.2)
8fa876ca 66 region1.SetFormatMode(ogl.FORMAT_CENTRE_HORIZ)
1e4a197e
RD
67 self.AddRegion(region1)
68
8fa876ca 69 region2 = ogl.ShapeRegion()
1e4a197e
RD
70 region2.SetText('This is Region number two.')
71 region2.SetProportions(0.0, 0.3)
8fa876ca 72 region2.SetFormatMode(ogl.FORMAT_CENTRE_HORIZ|ogl.FORMAT_CENTRE_VERT)
1e4a197e
RD
73 self.AddRegion(region2)
74
8fa876ca 75 region3 = ogl.ShapeRegion()
1e4a197e
RD
76 region3.SetText('Region 3\nwith embedded\nline breaks')
77 region3.SetProportions(0.0, 0.5)
8fa876ca 78 region3.SetFormatMode(ogl.FORMAT_NONE)
1e4a197e
RD
79 self.AddRegion(region3)
80
81 self.SetRegionSizes()
82 self.ReformatRegions(canvas)
83
84
85 def ReformatRegions(self, canvas=None):
86 rnum = 0
8fa876ca 87
1e4a197e
RD
88 if canvas is None:
89 canvas = self.GetCanvas()
8fa876ca
RD
90
91 dc = wx.ClientDC(canvas) # used for measuring
92
1e4a197e
RD
93 for region in self.GetRegions():
94 text = region.GetText()
95 self.FormatText(dc, text, rnum)
96 rnum += 1
97
98
99 def OnSizingEndDragLeft(self, pt, x, y, keys, attch):
d14a1e28 100 print "***", self
1e4a197e
RD
101 self.base_OnSizingEndDragLeft(pt, x, y, keys, attch)
102 self.SetRegionSizes()
103 self.ReformatRegions()
104 self.GetCanvas().Refresh()
105
106
e91a9dfc
RD
107#----------------------------------------------------------------------
108
8fa876ca 109class MyEvtHandler(ogl.ShapeEvtHandler):
e91a9dfc 110 def __init__(self, log, frame):
8fa876ca 111 ogl.ShapeEvtHandler.__init__(self)
e91a9dfc
RD
112 self.log = log
113 self.statbarFrame = frame
114
e91a9dfc
RD
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))
120
121
122 def OnLeftClick(self, x, y, keys = 0, attachment = 0):
123 shape = self.GetShape()
1fded56b 124 print shape.__class__, shape.GetClassName()
e91a9dfc 125 canvas = shape.GetCanvas()
8fa876ca 126 dc = wx.ClientDC(canvas)
e91a9dfc
RD
127 canvas.PrepareDC(dc)
128
129 if shape.Selected():
1e4a197e 130 shape.Select(False, dc)
e91a9dfc
RD
131 canvas.Redraw(dc)
132 else:
1e4a197e 133 redraw = False
e91a9dfc
RD
134 shapeList = canvas.GetDiagram().GetShapeList()
135 toUnselect = []
8fa876ca 136
e91a9dfc
RD
137 for s in shapeList:
138 if s.Selected():
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...
142 toUnselect.append(s)
143
1e4a197e 144 shape.Select(True, dc)
e91a9dfc
RD
145
146 if toUnselect:
147 for s in toUnselect:
1e4a197e 148 s.Select(False, dc)
8fa876ca 149
e91a9dfc
RD
150 canvas.Redraw(dc)
151
152 self.UpdateStatusBar(shape)
153
154
155 def OnEndDragLeft(self, x, y, keys = 0, attachment = 0):
156 shape = self.GetShape()
157 self.base_OnEndDragLeft(x, y, keys, attachment)
8fa876ca 158
e91a9dfc
RD
159 if not shape.Selected():
160 self.OnLeftClick(x, y, keys, attachment)
8fa876ca 161
e91a9dfc
RD
162 self.UpdateStatusBar(shape)
163
164
1e4a197e
RD
165 def OnSizingEndDragLeft(self, pt, x, y, keys, attch):
166 self.base_OnSizingEndDragLeft(pt, x, y, keys, attch)
e91a9dfc
RD
167 self.UpdateStatusBar(self.GetShape())
168
169
1e4a197e
RD
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())
e91a9dfc
RD
173
174
175 def OnRightClick(self, *dontcare):
176 self.log.WriteText("%s\n" % self.GetShape())
177
178
179#----------------------------------------------------------------------
180
8fa876ca 181class TestWindow(ogl.ShapeCanvas):
e91a9dfc 182 def __init__(self, parent, log, frame):
8fa876ca 183 ogl.ShapeCanvas.__init__(self, parent)
e91a9dfc 184
f6bcfd97
BP
185 maxWidth = 1000
186 maxHeight = 1000
187 self.SetScrollbars(20, 20, maxWidth/20, maxHeight/20)
188
e91a9dfc
RD
189 self.log = log
190 self.frame = frame
8fa876ca
RD
191 self.SetBackgroundColour("LIGHT BLUE") #wx.WHITE)
192 self.diagram = ogl.Diagram()
e91a9dfc
RD
193 self.SetDiagram(self.diagram)
194 self.diagram.SetCanvas(self)
195 self.shapes = []
f3d9dc1d 196 self.save_gdi = []
e91a9dfc 197
8fa876ca
RD
198 rRectBrush = wx.Brush("MEDIUM TURQUOISE", wx.SOLID)
199 dsBrush = wx.Brush("WHEAT", wx.SOLID)
200
201 self.MyAddShape(
202 ogl.CircleShape(80),
203 100, 100, wx.Pen(wx.BLUE, 3), wx.GREEN_BRUSH, "Circle"
204 )
205
206 self.MyAddShape(
207 ogl.RectangleShape(85, 50),
208 305, 60, wx.BLACK_PEN, wx.LIGHT_GREY_BRUSH, "Rectangle"
209 )
210
211 ds = self.MyAddShape(
212 DividedShape(140, 150, self),
213 495, 145, wx.BLACK_PEN, dsBrush, ''
214 )
215
216 self.MyAddShape(
217 DiamondShape(90, 90),
218 345, 235, wx.Pen(wx.BLUE, 3, wx.DOT), wx.RED_BRUSH, "Polygon"
219 )
220
221 self.MyAddShape(
222 RoundedRectangleShape(95,70),
223 140, 255, wx.Pen(wx.RED, 2), rRectBrush, "Rounded Rect"
224 )
e91a9dfc 225
96bfd053 226 bmp = images.getTest2Bitmap()
8fa876ca 227 mask = wx.MaskColour(bmp, wx.BLUE)
f3d9dc1d 228 bmp.SetMask(mask)
f3d9dc1d 229
8fa876ca 230 s = ogl.BitmapShape()
f3d9dc1d
RD
231 s.SetBitmap(bmp)
232 self.MyAddShape(s, 225, 150, None, None, "Bitmap")
233
8fa876ca 234 dc = wx.ClientDC(self)
e91a9dfc 235 self.PrepareDC(dc)
8fa876ca 236
e91a9dfc
RD
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]
241 else:
242 toShape = self.shapes[x+1]
8fa876ca
RD
243
244 line = ogl.LineShape()
e91a9dfc 245 line.SetCanvas(self)
8fa876ca
RD
246 line.SetPen(wx.BLACK_PEN)
247 line.SetBrush(wx.BLACK_BRUSH)
248 line.AddArrow(ogl.ARROW_ARROW)
e91a9dfc
RD
249 line.MakeLineControlPoints(2)
250 fromShape.AddLine(line, toShape)
251 self.diagram.AddShape(line)
1e4a197e 252 line.Show(True)
e91a9dfc
RD
253
254 # for some reason, the shapes have to be moved for the line to show up...
255 fromShape.Move(dc, fromShape.GetX(), fromShape.GetY())
256
8fa876ca 257 wx.EVT_WINDOW_DESTROY(self, self.OnDestroy)
e91a9dfc
RD
258
259
2f90df85 260 def MyAddShape(self, shape, x, y, pen, brush, text):
1e4a197e 261 shape.SetDraggable(True, True)
e91a9dfc
RD
262 shape.SetCanvas(self)
263 shape.SetX(x)
264 shape.SetY(y)
f3d9dc1d
RD
265 if pen: shape.SetPen(pen)
266 if brush: shape.SetBrush(brush)
267 if text: shape.AddText(text)
8fa876ca 268 #shape.SetShadowMode(ogl.SHADOW_RIGHT)
e91a9dfc 269 self.diagram.AddShape(shape)
1e4a197e 270 shape.Show(True)
e91a9dfc
RD
271
272 evthandler = MyEvtHandler(self.log, self.frame)
273 evthandler.SetShape(shape)
274 evthandler.SetPreviousHandler(shape.GetEventHandler())
275 shape.SetEventHandler(evthandler)
276
277 self.shapes.append(shape)
1e4a197e 278 return shape
e91a9dfc 279
f3d9dc1d 280
0b85cc38
RD
281 def OnDestroy(self, evt):
282 # Do some cleanup
e91a9dfc
RD
283 for shape in self.diagram.GetShapeList():
284 if shape.GetParent() == None:
285 shape.SetCanvas(None)
286 shape.Destroy()
8fa876ca 287
856e03b7 288 self.diagram.Destroy()
e91a9dfc
RD
289
290
ecc08ead
RD
291 def OnBeginDragLeft(self, x, y, keys):
292 self.log.write("OnBeginDragLeft: %s, %s, %s\n" % (x, y, keys))
293
294 def OnEndDragLeft(self, x, y, keys):
295 self.log.write("OnEndDragLeft: %s, %s, %s\n" % (x, y, keys))
296
297
e91a9dfc
RD
298#----------------------------------------------------------------------
299
300def runTest(frame, nb, log):
d14a1e28
RD
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.
8fa876ca 304 wx.OGLInitialize()
d14a1e28 305
e91a9dfc
RD
306 win = TestWindow(nb, log, frame)
307 return win
8fa876ca 308
e91a9dfc
RD
309#----------------------------------------------------------------------
310
311class __Cleanup:
8fa876ca 312 cleanup = ogl.OGLCleanUp
e91a9dfc
RD
313 def __del__(self):
314 self.cleanup()
315
316# when this module gets cleaned up then wxOGLCleanUp() will get called
317__cu = __Cleanup()
318
319
e91a9dfc
RD
320overview = """\
321The Object Graphics Library is a library supporting the creation and
322manipulation of simple and complex graphic images on a canvas.
323
324"""
325
1e4a197e
RD
326if __name__ == '__main__':
327 import sys,os
328 import run
329 run.main(['', os.path.basename(sys.argv[0])])
330