]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/OGL.py
new DC API fix
[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 # This creates some pens and brushes that the OGL library uses.
20
21 ogl.OGLInitialize()
22
23 #----------------------------------------------------------------------
24
25 class DiamondShape(ogl.PolygonShape):
26 def __init__(self, w=0.0, h=0.0):
27 ogl.PolygonShape.__init__(self)
28 if w == 0.0:
29 w = 60.0
30 if h == 0.0:
31 h = 60.0
32
33 # Either ogl.RealPoints or 2-tuples of floats works.
34
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),
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
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)
55
56
57 #----------------------------------------------------------------------
58
59 class DividedShape(ogl.DividedShape):
60 def __init__(self, width, height, canvas):
61 ogl.DividedShape.__init__(self, width, height)
62
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)
68
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)
74
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)
80
81 self.SetRegionSizes()
82 self.ReformatRegions(canvas)
83
84
85 def ReformatRegions(self, canvas=None):
86 rnum = 0
87
88 if canvas is None:
89 canvas = self.GetCanvas()
90
91 dc = wx.ClientDC(canvas) # used for measuring
92
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):
100 print "***", self
101 self.base_OnSizingEndDragLeft(pt, x, y, keys, attch)
102 self.SetRegionSizes()
103 self.ReformatRegions()
104 self.GetCanvas().Refresh()
105
106
107 #----------------------------------------------------------------------
108
109 class MyEvtHandler(ogl.ShapeEvtHandler):
110 def __init__(self, log, frame):
111 ogl.ShapeEvtHandler.__init__(self)
112 self.log = log
113 self.statbarFrame = frame
114
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()
124 print shape.__class__, shape.GetClassName()
125 canvas = shape.GetCanvas()
126 dc = wx.ClientDC(canvas)
127 canvas.PrepareDC(dc)
128
129 if shape.Selected():
130 shape.Select(False, dc)
131 canvas.Redraw(dc)
132 else:
133 redraw = False
134 shapeList = canvas.GetDiagram().GetShapeList()
135 toUnselect = []
136
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
144 shape.Select(True, dc)
145
146 if toUnselect:
147 for s in toUnselect:
148 s.Select(False, dc)
149
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)
158
159 if not shape.Selected():
160 self.OnLeftClick(x, y, keys, attachment)
161
162 self.UpdateStatusBar(shape)
163
164
165 def OnSizingEndDragLeft(self, pt, x, y, keys, attch):
166 self.base_OnSizingEndDragLeft(pt, x, y, keys, attch)
167 self.UpdateStatusBar(self.GetShape())
168
169
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())
173
174
175 def OnRightClick(self, *dontcare):
176 self.log.WriteText("%s\n" % self.GetShape())
177
178
179 #----------------------------------------------------------------------
180
181 class TestWindow(ogl.ShapeCanvas):
182 def __init__(self, parent, log, frame):
183 ogl.ShapeCanvas.__init__(self, parent)
184
185 maxWidth = 1000
186 maxHeight = 1000
187 self.SetScrollbars(20, 20, maxWidth/20, maxHeight/20)
188
189 self.log = log
190 self.frame = frame
191 self.SetBackgroundColour("LIGHT BLUE") #wx.WHITE)
192 self.diagram = ogl.Diagram()
193 self.SetDiagram(self.diagram)
194 self.diagram.SetCanvas(self)
195 self.shapes = []
196 self.save_gdi = []
197
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 )
225
226 bmp = images.getTest2Bitmap()
227 mask = wx.MaskColour(bmp, wx.BLUE)
228 bmp.SetMask(mask)
229
230 s = ogl.BitmapShape()
231 s.SetBitmap(bmp)
232 self.MyAddShape(s, 225, 150, None, None, "Bitmap")
233
234 dc = wx.ClientDC(self)
235 self.PrepareDC(dc)
236
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]
243
244 line = ogl.LineShape()
245 line.SetCanvas(self)
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)
252 line.Show(True)
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
257 wx.EVT_WINDOW_DESTROY(self, self.OnDestroy)
258
259
260 def MyAddShape(self, shape, x, y, pen, brush, text):
261 shape.SetDraggable(True, True)
262 shape.SetCanvas(self)
263 shape.SetX(x)
264 shape.SetY(y)
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)
270 shape.Show(True)
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)
278 return shape
279
280
281 def OnDestroy(self, evt):
282 # Do some cleanup
283 for shape in self.diagram.GetShapeList():
284 if shape.GetParent() == None:
285 shape.SetCanvas(None)
286 shape.Destroy()
287
288 self.diagram.Destroy()
289
290
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
298 #----------------------------------------------------------------------
299
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.
304 ogl.OGLInitialize()
305
306 win = TestWindow(nb, log, frame)
307 return win
308
309 #----------------------------------------------------------------------
310
311 class __Cleanup:
312 cleanup = ogl.OGLCleanUp
313 def __del__(self):
314 self.cleanup()
315
316 # when this module gets cleaned up then wx.OGLCleanUp() will get called
317 __cu = __Cleanup()
318
319
320 overview = """\
321 The Object Graphics Library is a library supporting the creation and
322 manipulation of simple and complex graphic images on a canvas.
323
324 """
325
326 if __name__ == '__main__':
327 import sys,os
328 import run
329 run.main(['', os.path.basename(sys.argv[0])])
330