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