]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxOGL.py
Some additions to the 12-bit patch.
[wxWidgets.git] / wxPython / demo / wxOGL.py
1
2 from wxPython.wx import *
3 from wxPython.ogl import *
4
5 import images
6
7 #----------------------------------------------------------------------
8 # This creates some pens and brushes that the OGL library uses.
9
10 wxOGLInitialize()
11
12 #----------------------------------------------------------------------
13
14 class DiamondShape(wxPolygonShape):
15 def __init__(self, w=0.0, h=0.0):
16 wxPolygonShape.__init__(self)
17 if w == 0.0:
18 w = 60.0
19 if h == 0.0:
20 h = 60.0
21
22 ## Either wxRealPoints or 2-tuples of floats works.
23
24 #points = [ wxRealPoint(0.0, -h/2.0),
25 # wxRealPoint(w/2.0, 0.0),
26 # wxRealPoint(0.0, h/2.0),
27 # wxRealPoint(-w/2.0, 0.0),
28 # ]
29 points = [ (0.0, -h/2.0),
30 (w/2.0, 0.0),
31 (0.0, h/2.0),
32 (-w/2.0, 0.0),
33 ]
34
35 self.Create(points)
36
37
38 #----------------------------------------------------------------------
39
40 class RoundedRectangleShape(wxRectangleShape):
41 def __init__(self, w=0.0, h=0.0):
42 wxRectangleShape.__init__(self, w, h)
43 self.SetCornerRadius(-0.3)
44
45
46 #----------------------------------------------------------------------
47
48 class MyEvtHandler(wxShapeEvtHandler):
49 def __init__(self, log, frame):
50 wxShapeEvtHandler.__init__(self)
51 self.log = log
52 self.statbarFrame = frame
53
54 def UpdateStatusBar(self, shape):
55 x,y = shape.GetX(), shape.GetY()
56 width, height = shape.GetBoundingBoxMax()
57 self.statbarFrame.SetStatusText("Pos: (%d,%d) Size: (%d, %d)" %
58 (x, y, width, height))
59
60
61 def OnLeftClick(self, x, y, keys = 0, attachment = 0):
62 shape = self.GetShape()
63 print shape.__class__
64 canvas = shape.GetCanvas()
65 dc = wxClientDC(canvas)
66 canvas.PrepareDC(dc)
67
68 if shape.Selected():
69 shape.Select(false, dc)
70 canvas.Redraw(dc)
71 else:
72 redraw = false
73 shapeList = canvas.GetDiagram().GetShapeList()
74 toUnselect = []
75 for s in shapeList:
76 if s.Selected():
77 # If we unselect it now then some of the objects in
78 # shapeList will become invalid (the control points are
79 # shapes too!) and bad things will happen...
80 toUnselect.append(s)
81
82 shape.Select(true, dc)
83
84 if toUnselect:
85 for s in toUnselect:
86 s.Select(false, dc)
87 canvas.Redraw(dc)
88
89 self.UpdateStatusBar(shape)
90
91
92 def OnEndDragLeft(self, x, y, keys = 0, attachment = 0):
93 shape = self.GetShape()
94 self.base_OnEndDragLeft(x, y, keys, attachment)
95 if not shape.Selected():
96 self.OnLeftClick(x, y, keys, attachment)
97 self.UpdateStatusBar(shape)
98
99
100 def OnSize(self, x, y):
101 self.base_OnSize(x, y)
102 self.UpdateStatusBar(self.GetShape())
103
104
105 # def OnMovePost(self, dc, x, y, oldX, oldY, display):
106 # self.base_OnMovePost(dc, x, y, oldX, oldY, display)
107 # self.UpdateStatusBar(self.GetShape())
108
109
110 def OnRightClick(self, *dontcare):
111 self.log.WriteText("%s\n" % self.GetShape())
112
113
114 #----------------------------------------------------------------------
115
116 class TestWindow(wxShapeCanvas):
117 def __init__(self, parent, log, frame):
118 wxShapeCanvas.__init__(self, parent)
119
120 maxWidth = 1000
121 maxHeight = 1000
122 self.SetScrollbars(20, 20, maxWidth/20, maxHeight/20)
123
124 self.log = log
125 self.frame = frame
126 self.SetBackgroundColour("LIGHT BLUE") #wxWHITE)
127 self.diagram = wxDiagram()
128 self.SetDiagram(self.diagram)
129 self.diagram.SetCanvas(self)
130 self.shapes = []
131 self.save_gdi = []
132
133 rRectBrush = wxBrush(wxNamedColour("MEDIUM TURQUOISE"), wxSOLID)
134
135 self.MyAddShape(wxCircleShape(80), 100, 100, wxPen(wxBLUE, 3), wxGREEN_BRUSH, "Circle")
136 self.MyAddShape(wxRectangleShape(85, 50), 305, 60, wxBLACK_PEN, wxLIGHT_GREY_BRUSH, "Rectangle")
137 self.MyAddShape(DiamondShape(90, 90), 345, 235, wxPen(wxBLUE, 3, wxDOT), wxRED_BRUSH, "Polygon")
138 self.MyAddShape(RoundedRectangleShape(95,70), 140, 255, wxPen(wxRED, 1), rRectBrush, "Rounded Rect")
139
140 bmp = images.getTest2Bitmap()
141 mask = wxMaskColour(bmp, wxBLUE)
142 bmp.SetMask(mask)
143
144 s = wxBitmapShape()
145 s.SetBitmap(bmp)
146 self.MyAddShape(s, 225, 150, None, None, "Bitmap")
147
148 dc = wxClientDC(self)
149 self.PrepareDC(dc)
150 for x in range(len(self.shapes)):
151 fromShape = self.shapes[x]
152 if x+1 == len(self.shapes):
153 toShape = self.shapes[0]
154 else:
155 toShape = self.shapes[x+1]
156 line = wxLineShape()
157 line.SetCanvas(self)
158 line.SetPen(wxBLACK_PEN)
159 line.SetBrush(wxBLACK_BRUSH)
160 line.AddArrow(ARROW_ARROW)
161 line.MakeLineControlPoints(2)
162 fromShape.AddLine(line, toShape)
163 self.diagram.AddShape(line)
164 line.Show(true)
165
166 # for some reason, the shapes have to be moved for the line to show up...
167 fromShape.Move(dc, fromShape.GetX(), fromShape.GetY())
168
169 ## EVT_PAINT(self, self.OnPaint)
170
171 ## def OnPaint(self, evt):
172 ## evt.Skip()
173 ## print "TheLists:", wxThePenList.GetCount(), wxTheBrushList.GetCount(), wxTheFontList.GetCount()
174
175
176 def MyAddShape(self, shape, x, y, pen, brush, text):
177 shape.SetDraggable(true, true)
178 shape.SetCanvas(self)
179 shape.SetX(x)
180 shape.SetY(y)
181 if pen: shape.SetPen(pen)
182 if brush: shape.SetBrush(brush)
183 if text: shape.AddText(text)
184 #shape.SetShadowMode(SHADOW_RIGHT)
185 self.diagram.AddShape(shape)
186 shape.Show(true)
187
188 evthandler = MyEvtHandler(self.log, self.frame)
189 evthandler.SetShape(shape)
190 evthandler.SetPreviousHandler(shape.GetEventHandler())
191 shape.SetEventHandler(evthandler)
192
193 self.shapes.append(shape)
194
195
196
197 def __del__(self):
198 for shape in self.diagram.GetShapeList():
199 if shape.GetParent() == None:
200 shape.SetCanvas(None)
201 shape.Destroy()
202 self.diagram.Destroy()
203
204
205 def OnBeginDragLeft(self, x, y, keys):
206 self.log.write("OnBeginDragLeft: %s, %s, %s\n" % (x, y, keys))
207
208 def OnEndDragLeft(self, x, y, keys):
209 self.log.write("OnEndDragLeft: %s, %s, %s\n" % (x, y, keys))
210
211
212 #----------------------------------------------------------------------
213
214 def runTest(frame, nb, log):
215 win = TestWindow(nb, log, frame)
216 return win
217
218 #----------------------------------------------------------------------
219
220 class __Cleanup:
221 cleanup = wxOGLCleanUp
222 def __del__(self):
223 self.cleanup()
224
225 # when this module gets cleaned up then wxOGLCleanUp() will get called
226 __cu = __Cleanup()
227
228
229
230
231
232
233
234
235 overview = """\
236 The Object Graphics Library is a library supporting the creation and
237 manipulation of simple and complex graphic images on a canvas.
238
239 """
240
241