]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/ScrolledWindow.py
7 #---------------------------------------------------------------------------
9 class MyCanvas(wx
.ScrolledWindow
):
10 def __init__(self
, parent
, id = -1, size
= wx
.DefaultSize
):
11 wx
.ScrolledWindow
.__init
__(self
, parent
, id, (0, 0), size
=size
, style
=wx
.SUNKEN_BORDER
)
20 self
.SetBackgroundColour("WHITE")
21 self
.SetCursor(wx
.StockCursor(wx
.CURSOR_PENCIL
))
22 bmp
= images
.getTest2Bitmap()
23 mask
= wx
.Mask(bmp
, wx
.BLUE
)
27 self
.SetScrollbars(20, 20, self
.maxWidth
/20, self
.maxHeight
/20)
30 # Initialize the buffer bitmap. No real DC is needed at this point.
31 self
.buffer = wx
.EmptyBitmap(self
.maxWidth
, self
.maxHeight
)
32 dc
= wx
.BufferedDC(None, self
.buffer)
33 dc
.SetBackground(wx
.Brush(self
.GetBackgroundColour()))
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
)
50 def OnPaint(self
, event
):
52 # Create a buffered paint DC. It will create the real
53 # wx.PaintDC and then blit the bitmap to it when dc is
54 # deleted. Since we don't need to draw anything else
55 # here that's all there is to it.
56 dc
= wx
.BufferedPaintDC(self
, self
.buffer)
60 # since we're not buffering in this case, we have to
61 # paint the whole window, potentially very time consuming.
65 def DoDrawing(self
, dc
, printing
=False):
67 dc
.SetPen(wx
.Pen('RED'))
68 dc
.DrawRectangle(5, 5, 50, 50)
70 dc
.SetBrush(wx
.LIGHT_GREY_BRUSH
)
71 dc
.SetPen(wx
.Pen('BLUE', 4))
72 dc
.DrawRectangle(15, 15, 50, 50)
74 dc
.SetFont(wx
.Font(14, wx
.SWISS
, wx
.NORMAL
, wx
.NORMAL
))
75 dc
.SetTextForeground(wx
.Colour(0xFF, 0x20, 0xFF))
76 te
= dc
.GetTextExtent("Hello World")
77 dc
.DrawText("Hello World", 60, 65)
79 dc
.SetPen(wx
.Pen('VIOLET', 4))
80 dc
.DrawLine(5, 65+te
[1], 60+te
[0], 65+te
[1])
82 lst
= [(100,110), (150,110), (150,160), (100,160)]
83 dc
.DrawLines(lst
, -60)
84 dc
.SetPen(wx
.GREY_PEN
)
85 dc
.DrawPolygon(lst
, 75)
86 dc
.SetPen(wx
.GREEN_PEN
)
87 dc
.DrawSpline(lst
+[(100,100)])
89 dc
.DrawBitmap(self
.bmp
, 200, 20, True)
90 dc
.SetTextForeground(wx
.Colour(0, 0xFF, 0x80))
91 dc
.DrawText("a bitmap", 200, 85)
93 ## dc.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.NORMAL))
94 ## dc.SetTextForeground("BLACK")
95 ## dc.DrawText("TEST this STRING", 10, 200)
96 ## print dc.GetFullTextExtent("TEST this STRING")
98 font
= wx
.Font(20, wx
.SWISS
, wx
.NORMAL
, wx
.NORMAL
)
100 dc
.SetTextForeground(wx
.BLACK
)
102 for a
in range(0, 360, 45):
103 dc
.DrawRotatedText("Rotated text...", 300, 300, a
)
105 dc
.SetPen(wx
.TRANSPARENT_PEN
)
106 dc
.SetBrush(wx
.BLUE_BRUSH
)
107 dc
.DrawRectangle(50,500, 50,50)
108 dc
.DrawRectangle(100,500, 50,50)
110 dc
.SetPen(wx
.Pen('RED'))
111 dc
.DrawEllipticArc(200,500, 50,75, 0, 90)
114 # This has troubles when used on a print preview in wxGTK,
115 # probably something to do with the pen styles and the scaling
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
)
126 dc
.DrawLine(300,y
, 400,y
)
129 dc
.SetBrush(wx
.TRANSPARENT_BRUSH
)
130 dc
.SetPen(wx
.Pen(wx
.Colour(0xFF, 0x20, 0xFF), 1, wx
.SOLID
))
131 dc
.DrawRectangle(450,50, 100,100)
132 old_pen
= dc
.GetPen()
133 new_pen
= wx
.Pen("BLACK", 5)
135 dc
.DrawRectangle(470,70, 60,60)
137 dc
.DrawRectangle(490,90, 20,20)
139 self
.DrawSavedLines(dc
)
143 def DrawSavedLines(self
, dc
):
144 dc
.SetPen(wx
.Pen('MEDIUM FOREST GREEN', 4))
146 for line
in self
.lines
:
148 apply(dc
.DrawLine
, coords
)
151 def SetXY(self
, event
):
152 self
.x
, self
.y
= self
.ConvertEventCoords(event
)
154 def ConvertEventCoords(self
, event
):
155 xView
, yView
= self
.GetViewStart()
156 xDelta
, yDelta
= self
.GetScrollPixelsPerUnit()
157 return (event
.GetX() + (xView
* xDelta
),
158 event
.GetY() + (yView
* yDelta
))
160 def OnLeftButtonEvent(self
, event
):
168 elif event
.Dragging() and self
.drawing
:
170 # If doing buffered drawing, create the buffered DC, giving it
171 # it a real DC to blit to when done.
172 cdc
= wx
.ClientDC(self
)
174 dc
= wx
.BufferedDC(cdc
, self
.buffer)
176 dc
= wx
.ClientDC(self
)
180 dc
.SetPen(wx
.Pen('MEDIUM FOREST GREEN', 4))
181 coords
= (self
.x
, self
.y
) + self
.ConvertEventCoords(event
)
182 self
.curLine
.append(coords
)
188 elif event
.LeftUp() and self
.drawing
:
189 self
.lines
.append(self
.curLine
)
195 ## This is an example of what to do for the EVT_MOUSEWHEEL event,
196 ## but since wx.ScrolledWindow does this already it's not
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.
201 ## def OnWheel(self, evt):
202 ## delta = evt.GetWheelDelta()
203 ## rot = evt.GetWheelRotation()
204 ## linesPer = evt.GetLinesPerAction()
205 ## print delta, rot, linesPer
206 ## ws = self.wheelScroll
208 ## lines = ws / delta
209 ## ws = ws - lines * delta
210 ## self.wheelScroll = ws
212 ## lines = lines * linesPer
213 ## vsx, vsy = self.GetViewStart()
214 ## scrollTo = vsy - lines
215 ## self.Scroll(-1, scrollTo)
217 #---------------------------------------------------------------------------
219 def runTest(frame
, nb
, log
):
223 #---------------------------------------------------------------------------
230 The wx.ScrolledWindow class manages scrolling for its client area, transforming the
231 coordinates according to the scrollbar positions, and setting the scroll positions,
232 thumb sizes and ranges according to the area in view.
238 if __name__
== '__main__':
241 run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])