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