]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/OGL.py
786eae770e159fddfee576dbbd4e79b35a20fa5d
[wxWidgets.git] / wxPython / demo / OGL.py
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 #
10
11 import wx
12 import wx.ogl as ogl
13
14 import images
15
16 ##wx.Trap()
17
18 #----------------------------------------------------------------------
19
20 class DiamondShape(ogl.PolygonShape):
21 def __init__(self, w=0.0, h=0.0):
22 ogl.PolygonShape.__init__(self)
23 if w == 0.0:
24 w = 60.0
25 if h == 0.0:
26 h = 60.0
27
28 # Either ogl.RealPoints or 2-tuples of floats works.
29
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),
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
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)
50
51
52 #----------------------------------------------------------------------
53
54 class DividedShape(ogl.DividedShape):
55 def __init__(self, width, height, canvas):
56 ogl.DividedShape.__init__(self, width, height)
57
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)
63
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)
69
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)
75
76 self.SetRegionSizes()
77 self.ReformatRegions(canvas)
78
79
80 def ReformatRegions(self, canvas=None):
81 rnum = 0
82
83 if canvas is None:
84 canvas = self.GetCanvas()
85
86 dc = wx.ClientDC(canvas) # used for measuring
87
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):
95 print "***", self
96 self.base_OnSizingEndDragLeft(pt, x, y, keys, attch)
97 self.SetRegionSizes()
98 self.ReformatRegions()
99 self.GetCanvas().Refresh()
100
101
102 #----------------------------------------------------------------------
103
104 class MyEvtHandler(ogl.ShapeEvtHandler):
105 def __init__(self, log, frame):
106 ogl.ShapeEvtHandler.__init__(self)
107 self.log = log
108 self.statbarFrame = frame
109
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()
119 print shape.__class__, shape.GetClassName()
120 canvas = shape.GetCanvas()
121 dc = wx.ClientDC(canvas)
122 canvas.PrepareDC(dc)
123
124 if shape.Selected():
125 shape.Select(False, dc)
126 canvas.Redraw(dc)
127 else:
128 redraw = False
129 shapeList = canvas.GetDiagram().GetShapeList()
130 toUnselect = []
131
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
139 shape.Select(True, dc)
140
141 if toUnselect:
142 for s in toUnselect:
143 s.Select(False, dc)
144
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)
153
154 if not shape.Selected():
155 self.OnLeftClick(x, y, keys, attachment)
156
157 self.UpdateStatusBar(shape)
158
159
160 def OnSizingEndDragLeft(self, pt, x, y, keys, attch):
161 self.base_OnSizingEndDragLeft(pt, x, y, keys, attch)
162 self.UpdateStatusBar(self.GetShape())
163
164
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())
168
169
170 def OnRightClick(self, *dontcare):
171 self.log.WriteText("%s\n" % self.GetShape())
172
173
174 #----------------------------------------------------------------------
175
176 class TestWindow(ogl.ShapeCanvas):
177 def __init__(self, parent, log, frame):
178 ogl.ShapeCanvas.__init__(self, parent)
179
180 maxWidth = 1000
181 maxHeight = 1000
182 self.SetScrollbars(20, 20, maxWidth/20, maxHeight/20)
183
184 self.log = log
185 self.frame = frame
186 self.SetBackgroundColour("LIGHT BLUE") #wx.WHITE)
187 self.diagram = ogl.Diagram()
188 self.SetDiagram(self.diagram)
189 self.diagram.SetCanvas(self)
190 self.shapes = []
191 self.save_gdi = []
192
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 )
220
221 bmp = images.getTest2Bitmap()
222 mask = wx.MaskColour(bmp, wx.BLUE)
223 bmp.SetMask(mask)
224
225 s = ogl.BitmapShape()
226 s.SetBitmap(bmp)
227 self.MyAddShape(s, 225, 150, None, None, "Bitmap")
228
229 dc = wx.ClientDC(self)
230 self.PrepareDC(dc)
231
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]
238
239 line = ogl.LineShape()
240 line.SetCanvas(self)
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)
247 line.Show(True)
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
252 wx.EVT_WINDOW_DESTROY(self, self.OnDestroy)
253
254
255 def MyAddShape(self, shape, x, y, pen, brush, text):
256 shape.SetDraggable(True, True)
257 shape.SetCanvas(self)
258 shape.SetX(x)
259 shape.SetY(y)
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)
265 shape.Show(True)
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)
273 return shape
274
275
276 def OnDestroy(self, evt):
277 # Do some cleanup
278 for shape in self.diagram.GetShapeList():
279 if shape.GetParent() == None:
280 shape.SetCanvas(None)
281 shape.Destroy()
282
283 self.diagram.Destroy()
284
285
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
293 #----------------------------------------------------------------------
294
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.
299 ogl.OGLInitialize()
300
301 win = TestWindow(nb, log, frame)
302 return win
303
304 #----------------------------------------------------------------------
305
306 # The OGL library holds some resources that need to be freed before
307 # the app shuts down.
308 class __Cleanup:
309 def __del__(self, cleanup=ogl.OGLCleanUp):
310 cleanup()
311
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.
314 __cu = __Cleanup()
315
316
317 overview = """\
318 The Object Graphics Library is a library supporting the creation and
319 manipulation of simple and complex graphic images on a canvas.
320
321 """
322
323 if __name__ == '__main__':
324 import sys,os
325 import run
326 run.main(['', os.path.basename(sys.argv[0])])
327