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