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