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