]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/ScrolledWindow.py
use wxString in wxRegKey methods; make it compile without implicit wxString->char...
[wxWidgets.git] / wxPython / demo / ScrolledWindow.py
CommitLineData
cf694132 1
8fa876ca 2import wx
8fa876ca 3import images
96bfd053 4
d3bfec74
RD
5BUFFERED = 1
6
cf694132
RD
7#---------------------------------------------------------------------------
8
8fa876ca 9class MyCanvas(wx.ScrolledWindow):
372bde9b 10 def __init__(self, parent, id = -1, size = wx.DefaultSize):
8fa876ca 11 wx.ScrolledWindow.__init__(self, parent, id, (0, 0), size=size, style=wx.SUNKEN_BORDER)
cf694132
RD
12
13 self.lines = []
bb0054cd
RD
14 self.maxWidth = 1000
15 self.maxHeight = 1000
d3bfec74
RD
16 self.x = self.y = 0
17 self.curLine = []
1e4a197e 18 self.drawing = False
cf694132 19
d43d449b 20 self.SetBackgroundColour("WHITE")
8fa876ca 21 self.SetCursor(wx.StockCursor(wx.CURSOR_PENCIL))
96bfd053 22 bmp = images.getTest2Bitmap()
d7403ad2 23 mask = wx.Mask(bmp, wx.BLUE)
5ed3dab2 24 bmp.SetMask(mask)
bb0054cd 25 self.bmp = bmp
cf694132 26
8793725c
RD
27 self.SetVirtualSize((self.maxWidth, self.maxHeight))
28 self.SetScrollRate(20,20)
bb0054cd 29
d3bfec74
RD
30 if BUFFERED:
31 # Initialize the buffer bitmap. No real DC is needed at this point.
8fa876ca
RD
32 self.buffer = wx.EmptyBitmap(self.maxWidth, self.maxHeight)
33 dc = wx.BufferedDC(None, self.buffer)
34 dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
d3bfec74
RD
35 dc.Clear()
36 self.DoDrawing(dc)
37
8fa876ca
RD
38 self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftButtonEvent)
39 self.Bind(wx.EVT_LEFT_UP, self.OnLeftButtonEvent)
40 self.Bind(wx.EVT_MOTION, self.OnLeftButtonEvent)
41 self.Bind(wx.EVT_PAINT, self.OnPaint)
0af45411 42
a3015b16 43
bb0054cd
RD
44 def getWidth(self):
45 return self.maxWidth
46
47 def getHeight(self):
48 return self.maxHeight
cf694132
RD
49
50
51 def OnPaint(self, event):
d3bfec74
RD
52 if BUFFERED:
53 # Create a buffered paint DC. It will create the real
8fa876ca 54 # wx.PaintDC and then blit the bitmap to it when dc is
d3bfec74
RD
55 # deleted. Since we don't need to draw anything else
56 # here that's all there is to it.
67a5217c 57 dc = wx.BufferedPaintDC(self, self.buffer, wx.BUFFER_VIRTUAL_AREA)
d3bfec74 58 else:
8fa876ca 59 dc = wx.PaintDC(self)
d3bfec74
RD
60 self.PrepareDC(dc)
61 # since we're not buffering in this case, we have to
62 # paint the whole window, potentially very time consuming.
63 self.DoDrawing(dc)
cf694132
RD
64
65
1fded56b 66 def DoDrawing(self, dc, printing=False):
cf694132 67 dc.BeginDrawing()
8fa876ca 68 dc.SetPen(wx.Pen('RED'))
d7403ad2 69 dc.DrawRectangle(5, 5, 50, 50)
cf694132 70
8fa876ca
RD
71 dc.SetBrush(wx.LIGHT_GREY_BRUSH)
72 dc.SetPen(wx.Pen('BLUE', 4))
d7403ad2 73 dc.DrawRectangle(15, 15, 50, 50)
cf694132 74
8fa876ca
RD
75 dc.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.NORMAL))
76 dc.SetTextForeground(wx.Colour(0xFF, 0x20, 0xFF))
cf694132 77 te = dc.GetTextExtent("Hello World")
d7403ad2 78 dc.DrawText("Hello World", 60, 65)
cf694132 79
8fa876ca 80 dc.SetPen(wx.Pen('VIOLET', 4))
d7403ad2 81 dc.DrawLine(5, 65+te[1], 60+te[0], 65+te[1])
cf694132
RD
82
83 lst = [(100,110), (150,110), (150,160), (100,160)]
84 dc.DrawLines(lst, -60)
8fa876ca 85 dc.SetPen(wx.GREY_PEN)
cf694132 86 dc.DrawPolygon(lst, 75)
8fa876ca 87 dc.SetPen(wx.GREEN_PEN)
cf694132
RD
88 dc.DrawSpline(lst+[(100,100)])
89
d7403ad2 90 dc.DrawBitmap(self.bmp, 200, 20, True)
8fa876ca 91 dc.SetTextForeground(wx.Colour(0, 0xFF, 0x80))
d7403ad2 92 dc.DrawText("a bitmap", 200, 85)
cf694132 93
8fa876ca 94## dc.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.NORMAL))
1fded56b 95## dc.SetTextForeground("BLACK")
d7403ad2 96## dc.DrawText("TEST this STRING", 10, 200)
1fded56b
RD
97## print dc.GetFullTextExtent("TEST this STRING")
98
8fa876ca 99 font = wx.Font(20, wx.SWISS, wx.NORMAL, wx.NORMAL)
6999b0d8 100 dc.SetFont(font)
8fa876ca
RD
101 dc.SetTextForeground(wx.BLACK)
102
6999b0d8 103 for a in range(0, 360, 45):
d7403ad2 104 dc.DrawRotatedText("Rotated text...", 300, 300, a)
6999b0d8 105
8fa876ca
RD
106 dc.SetPen(wx.TRANSPARENT_PEN)
107 dc.SetBrush(wx.BLUE_BRUSH)
d7403ad2
RD
108 dc.DrawRectangle(50,500, 50,50)
109 dc.DrawRectangle(100,500, 50,50)
eec92d76 110
8fa876ca 111 dc.SetPen(wx.Pen('RED'))
d7403ad2 112 dc.DrawEllipticArc(200,500, 50,75, 0, 90)
c368d904 113
1fded56b
RD
114 if not printing:
115 # This has troubles when used on a print preview in wxGTK,
116 # probably something to do with the pen styles and the scaling
117 # it does...
118 y = 20
8fa876ca
RD
119
120 for style in [wx.DOT, wx.LONG_DASH, wx.SHORT_DASH, wx.DOT_DASH, wx.USER_DASH]:
121 pen = wx.Pen("DARK ORCHID", 1, style)
122 if style == wx.USER_DASH:
123 pen.SetCap(wx.CAP_BUTT)
1fded56b
RD
124 pen.SetDashes([1,2])
125 pen.SetColour("RED")
126 dc.SetPen(pen)
d7403ad2 127 dc.DrawLine(300,y, 400,y)
1fded56b 128 y = y + 10
217cb2fa 129
8fa876ca
RD
130 dc.SetBrush(wx.TRANSPARENT_BRUSH)
131 dc.SetPen(wx.Pen(wx.Colour(0xFF, 0x20, 0xFF), 1, wx.SOLID))
d7403ad2 132 dc.DrawRectangle(450,50, 100,100)
c5943253 133 old_pen = dc.GetPen()
8fa876ca 134 new_pen = wx.Pen("BLACK", 5)
c5943253 135 dc.SetPen(new_pen)
d7403ad2 136 dc.DrawRectangle(470,70, 60,60)
c5943253 137 dc.SetPen(old_pen)
d7403ad2 138 dc.DrawRectangle(490,90, 20,20)
c5943253 139
aa5644ae
RD
140 dc.GradientFillLinear((20, 260, 50, 50),
141 "red", "blue")
142 dc.GradientFillConcentric((20, 325, 50, 50),
143 "red", "blue", (25,25))
cf694132
RD
144 self.DrawSavedLines(dc)
145 dc.EndDrawing()
146
147
148 def DrawSavedLines(self, dc):
8fa876ca
RD
149 dc.SetPen(wx.Pen('MEDIUM FOREST GREEN', 4))
150
cf694132
RD
151 for line in self.lines:
152 for coords in line:
8fa876ca 153 apply(dc.DrawLine, coords)
cf694132
RD
154
155
156 def SetXY(self, event):
157 self.x, self.y = self.ConvertEventCoords(event)
158
159 def ConvertEventCoords(self, event):
cbfc9df6
RD
160 newpos = self.CalcUnscrolledPosition(event.GetX(), event.GetY())
161 return newpos
cf694132
RD
162
163 def OnLeftButtonEvent(self, event):
164 if event.LeftDown():
1e4a197e 165 self.SetFocus()
cf694132
RD
166 self.SetXY(event)
167 self.curLine = []
714d23b4 168 self.CaptureMouse()
1e4a197e 169 self.drawing = True
d3bfec74
RD
170
171 elif event.Dragging() and self.drawing:
172 if BUFFERED:
cbfc9df6
RD
173 # If doing buffered drawing we'll just update the
174 # buffer here and then refresh that portion of the
175 # window, then that portion of the buffer will be
176 # redrawn in the EVT_PAINT handler.
177 dc = wx.BufferedDC(None, self.buffer)
d3bfec74 178 else:
cbfc9df6 179 # otherwise we'll draw directly to a wx.ClientDC
8fa876ca 180 dc = wx.ClientDC(self)
d3bfec74 181 self.PrepareDC(dc)
cf694132 182
8fa876ca 183 dc.SetPen(wx.Pen('MEDIUM FOREST GREEN', 4))
d7403ad2 184 coords = (self.x, self.y) + self.ConvertEventCoords(event)
cf694132 185 self.curLine.append(coords)
d7403ad2 186 dc.DrawLine(*coords)
cf694132 187 self.SetXY(event)
cbfc9df6
RD
188
189 if BUFFERED:
190 # figure out what part of the window to refresh
191 x1,y1, x2,y2 = dc.GetBoundingBox()
192 x1,y1 = self.CalcScrolledPosition(x1, y1)
193 x2,y2 = self.CalcScrolledPosition(x2, y2)
194 # make a rectangle
195 rect = wx.Rect()
196 rect.SetTopLeft((x1,y1))
197 rect.SetBottomRight((x2,y2))
198 rect.Inflate(2,2)
199 # refresh it
200 self.RefreshRect(rect)
d3bfec74
RD
201
202 elif event.LeftUp() and self.drawing:
cf694132
RD
203 self.lines.append(self.curLine)
204 self.curLine = []
714d23b4 205 self.ReleaseMouse()
1e4a197e 206 self.drawing = False
cf694132 207
d1679124
RD
208
209## This is an example of what to do for the EVT_MOUSEWHEEL event,
299647ac 210## but since wx.ScrolledWindow does this already it's not
8fa876ca
RD
211## necessary to do it ourselves. You would need to add an event table
212## entry to __init__() to direct wheelmouse events to this handler.
d1679124
RD
213
214## wheelScroll = 0
215## def OnWheel(self, evt):
216## delta = evt.GetWheelDelta()
217## rot = evt.GetWheelRotation()
218## linesPer = evt.GetLinesPerAction()
3b5ccda1 219## print delta, rot, linesPer
d1679124
RD
220## ws = self.wheelScroll
221## ws = ws + rot
222## lines = ws / delta
223## ws = ws - lines * delta
224## self.wheelScroll = ws
225## if lines != 0:
226## lines = lines * linesPer
227## vsx, vsy = self.GetViewStart()
228## scrollTo = vsy - lines
229## self.Scroll(-1, scrollTo)
230
cf694132
RD
231#---------------------------------------------------------------------------
232
233def runTest(frame, nb, log):
234 win = MyCanvas(nb)
235 return win
236
237#---------------------------------------------------------------------------
238
239
240
8fa876ca
RD
241overview = """
242<html>
243<body>
244The wx.ScrolledWindow class manages scrolling for its client area, transforming the
245coordinates according to the scrollbar positions, and setting the scroll positions,
246thumb sizes and ranges according to the area in view.
247</body>
248</html>
f9b24f07 249"""
cf694132
RD
250
251
f9b24f07
RD
252if __name__ == '__main__':
253 import sys,os
254 import run
8eca4fef 255 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
cf694132 256