]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxScrolledWindow.py
1 # 11/12/2003 - Jeff Grimmett (grimmtooth@softhome.net)
3 # o Updated for wx namespace
4 # o Rudimentary overview doc added.
13 #---------------------------------------------------------------------------
15 class MyCanvas(wx
.ScrolledWindow
):
16 def __init__(self
, parent
, id = -1, size
= wx
.DefaultSize
):
17 wx
.ScrolledWindow
.__init
__(self
, parent
, id, (0, 0), size
=size
, style
=wx
.SUNKEN_BORDER
)
26 self
.SetBackgroundColour("WHITE")
27 self
.SetCursor(wx
.StockCursor(wx
.CURSOR_PENCIL
))
28 bmp
= images
.getTest2Bitmap()
29 mask
= wx
.MaskColour(bmp
, wx
.BLUE
)
33 self
.SetScrollbars(20, 20, self
.maxWidth
/20, self
.maxHeight
/20)
36 # Initialize the buffer bitmap. No real DC is needed at this point.
37 self
.buffer = wx
.EmptyBitmap(self
.maxWidth
, self
.maxHeight
)
38 dc
= wx
.BufferedDC(None, self
.buffer)
39 dc
.SetBackground(wx
.Brush(self
.GetBackgroundColour()))
43 self
.Bind(wx
.EVT_LEFT_DOWN
, self
.OnLeftButtonEvent
)
44 self
.Bind(wx
.EVT_LEFT_UP
, self
.OnLeftButtonEvent
)
45 self
.Bind(wx
.EVT_MOTION
, self
.OnLeftButtonEvent
)
46 self
.Bind(wx
.EVT_PAINT
, self
.OnPaint
)
56 def OnPaint(self
, event
):
58 # Create a buffered paint DC. It will create the real
59 # wx.PaintDC and then blit the bitmap to it when dc is
60 # deleted. Since we don't need to draw anything else
61 # here that's all there is to it.
62 dc
= wx
.BufferedPaintDC(self
, self
.buffer)
66 # since we're not buffering in this case, we have to
67 # paint the whole window, potentially very time consuming.
71 def DoDrawing(self
, dc
, printing
=False):
73 dc
.SetPen(wx
.Pen('RED'))
74 dc
.DrawRectangle((5, 5), (50, 50))
76 dc
.SetBrush(wx
.LIGHT_GREY_BRUSH
)
77 dc
.SetPen(wx
.Pen('BLUE', 4))
78 dc
.DrawRectangle((15, 15), (50, 50))
80 dc
.SetFont(wx
.Font(14, wx
.SWISS
, wx
.NORMAL
, wx
.NORMAL
))
81 dc
.SetTextForeground(wx
.Colour(0xFF, 0x20, 0xFF))
82 te
= dc
.GetTextExtent("Hello World")
83 dc
.DrawText("Hello World", (60, 65))
85 dc
.SetPen(wx
.Pen('VIOLET', 4))
86 dc
.DrawLine((5, 65+te
[1]), (60+te
[0], 65+te
[1]))
88 lst
= [(100,110), (150,110), (150,160), (100,160)]
89 dc
.DrawLines(lst
, -60)
90 dc
.SetPen(wx
.GREY_PEN
)
91 dc
.DrawPolygon(lst
, 75)
92 dc
.SetPen(wx
.GREEN_PEN
)
93 dc
.DrawSpline(lst
+[(100,100)])
95 dc
.DrawBitmap(self
.bmp
, (200, 20), True)
96 dc
.SetTextForeground(wx
.Colour(0, 0xFF, 0x80))
97 dc
.DrawText("a bitmap", (200, 85))
99 ## dc.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.NORMAL))
100 ## dc.SetTextForeground("BLACK")
101 ## dc.DrawText("TEST this STRING", (10, 200))
102 ## print dc.GetFullTextExtent("TEST this STRING")
104 font
= wx
.Font(20, wx
.SWISS
, wx
.NORMAL
, wx
.NORMAL
)
106 dc
.SetTextForeground(wx
.BLACK
)
108 for a
in range(0, 360, 45):
109 dc
.DrawRotatedText("Rotated text...", (300, 300), a
)
111 dc
.SetPen(wx
.TRANSPARENT_PEN
)
112 dc
.SetBrush(wx
.BLUE_BRUSH
)
113 dc
.DrawRectangle((50,500),(50,50))
114 dc
.DrawRectangle((100,500),(50,50))
116 dc
.SetPen(wx
.Pen('RED'))
117 dc
.DrawEllipticArc((200, 500), (50, 75), 0, 90)
120 # This has troubles when used on a print preview in wxGTK,
121 # probably something to do with the pen styles and the scaling
125 for style
in [wx
.DOT
, wx
.LONG_DASH
, wx
.SHORT_DASH
, wx
.DOT_DASH
, wx
.USER_DASH
]:
126 pen
= wx
.Pen("DARK ORCHID", 1, style
)
127 if style
== wx
.USER_DASH
:
128 pen
.SetCap(wx
.CAP_BUTT
)
132 dc
.DrawLine((300, y
), (400, y
))
135 dc
.SetBrush(wx
.TRANSPARENT_BRUSH
)
136 dc
.SetPen(wx
.Pen(wx
.Colour(0xFF, 0x20, 0xFF), 1, wx
.SOLID
))
137 dc
.DrawRectangle((450, 50), (100, 100))
138 old_pen
= dc
.GetPen()
139 new_pen
= wx
.Pen("BLACK", 5)
141 dc
.DrawRectangle((470, 70), (60, 60))
143 dc
.DrawRectangle((490, 90), (20, 20))
145 self
.DrawSavedLines(dc
)
149 def DrawSavedLines(self
, dc
):
150 dc
.SetPen(wx
.Pen('MEDIUM FOREST GREEN', 4))
152 for line
in self
.lines
:
154 apply(dc
.DrawLine
, coords
)
157 def SetXY(self
, event
):
158 self
.x
, self
.y
= self
.ConvertEventCoords(event
)
160 def ConvertEventCoords(self
, event
):
161 xView
, yView
= self
.GetViewStart()
162 xDelta
, yDelta
= self
.GetScrollPixelsPerUnit()
163 return (event
.GetX() + (xView
* xDelta
),
164 event
.GetY() + (yView
* yDelta
))
166 def OnLeftButtonEvent(self
, event
):
174 elif event
.Dragging() and self
.drawing
:
176 # If doing buffered drawing, create the buffered DC, giving it
177 # it a real DC to blit to when done.
178 cdc
= wx
.ClientDC(self
)
180 dc
= wx
.BufferedDC(cdc
, self
.buffer)
182 dc
= wx
.ClientDC(self
)
186 dc
.SetPen(wx
.Pen('MEDIUM FOREST GREEN', 4))
187 coords
= [(self
.x
, self
.y
) , self
.ConvertEventCoords(event
)]
188 self
.curLine
.append(coords
)
189 apply(dc
.DrawLine
, coords
)
194 elif event
.LeftUp() and self
.drawing
:
195 self
.lines
.append(self
.curLine
)
201 ## This is an example of what to do for the EVT_MOUSEWHEEL event,
202 ## but since wxScrolledWindow does this already it's not
203 ## necessary to do it ourselves. You would need to add an event table
204 ## entry to __init__() to direct wheelmouse events to this handler.
207 ## def OnWheel(self, evt):
208 ## delta = evt.GetWheelDelta()
209 ## rot = evt.GetWheelRotation()
210 ## linesPer = evt.GetLinesPerAction()
211 ## print delta, rot, linesPer
212 ## ws = self.wheelScroll
214 ## lines = ws / delta
215 ## ws = ws - lines * delta
216 ## self.wheelScroll = ws
218 ## lines = lines * linesPer
219 ## vsx, vsy = self.GetViewStart()
220 ## scrollTo = vsy - lines
221 ## self.Scroll(-1, scrollTo)
223 #---------------------------------------------------------------------------
225 def runTest(frame
, nb
, log
):
229 #---------------------------------------------------------------------------
236 The wx.ScrolledWindow class manages scrolling for its client area, transforming the
237 coordinates according to the scrollbar positions, and setting the scroll positions,
238 thumb sizes and ranges according to the area in view.
244 if __name__
== '__main__':
247 run
.main(['', os
.path
.basename(sys
.argv
[0])])