]> git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/demo/wxOGL.py
Added a demo showing how to use wxPostEvent
[wxWidgets.git] / utils / 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 self.log = log
119 self.frame = frame
120 self.SetBackgroundColour(wxWHITE)
121 self.diagram = wxDiagram()
122 self.SetDiagram(self.diagram)
123 self.diagram.SetCanvas(self)
124 self.shapes = []
125
126 rRectBrush = wxBrush(wxNamedColour("MEDIUM TURQUOISE"), wxSOLID)
127
128 self.MyAddShape(wxCircleShape(80), 100, 100, wxPen(wxBLUE, 3), wxGREEN_BRUSH, "Circle")
129 self.MyAddShape(wxRectangleShape(85, 50), 305, 60, wxBLACK_PEN, wxLIGHT_GREY_BRUSH, "Rectangle")
130 self.MyAddShape(DiamondShape(90, 90), 345, 235, wxPen(wxBLUE, 3, wxDOT), wxRED_BRUSH, "Polygon")
131 self.MyAddShape(RoundedRectangleShape(95,70), 140, 255, wxPen(wxRED, 1), rRectBrush, "Rounded Rect")
132
133 dc = wxClientDC(self)
134 self.PrepareDC(dc)
135 for x in range(len(self.shapes)):
136 fromShape = self.shapes[x]
137 if x+1 == len(self.shapes):
138 toShape = self.shapes[0]
139 else:
140 toShape = self.shapes[x+1]
141 line = wxLineShape()
142 line.SetCanvas(self)
143 line.SetPen(wxBLACK_PEN)
144 line.SetBrush(wxBLACK_BRUSH)
145 line.AddArrow(ARROW_ARROW)
146 line.MakeLineControlPoints(2)
147 fromShape.AddLine(line, toShape)
148 self.diagram.AddShape(line)
149 line.Show(true)
150
151 # for some reason, the shapes have to be moved for the line to show up...
152 fromShape.Move(dc, fromShape.GetX(), fromShape.GetY())
153
154
155
156 def MyAddShape(self, shape, x, y, pen, brush, text):
157 shape.SetDraggable(true)
158 shape.SetCanvas(self)
159 shape.SetX(x)
160 shape.SetY(y)
161 shape.SetPen(pen)
162 shape.SetBrush(brush)
163 shape.AddText(text)
164 #shape.SetShadowMode(SHADOW_RIGHT)
165 self.diagram.AddShape(shape)
166 shape.Show(true)
167
168 evthandler = MyEvtHandler(self.log, self.frame)
169 evthandler.SetShape(shape)
170 evthandler.SetPreviousHandler(shape.GetEventHandler())
171 shape.SetEventHandler(evthandler)
172
173 self.shapes.append(shape)
174
175
176 def __del__(self):
177 for shape in self.diagram.GetShapeList():
178 if shape.GetParent() == None:
179 shape.SetCanvas(None)
180 shape.Destroy()
181
182
183 #----------------------------------------------------------------------
184
185 def runTest(frame, nb, log):
186 win = TestWindow(nb, log, frame)
187 return win
188
189 #----------------------------------------------------------------------
190
191 class __Cleanup:
192 cleanup = wxOGLCleanUp
193 def __del__(self):
194 self.cleanup()
195
196 # when this module gets cleaned up then wxOGLCleanUp() will get called
197 __cu = __Cleanup()
198
199
200
201
202
203
204
205
206 overview = """\
207 The Object Graphics Library is a library supporting the creation and
208 manipulation of simple and complex graphic images on a canvas.
209
210 """
211
212