]>
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
.SetVirtualSize((self
.maxWidth
, self
.maxHeight
)) 
  28         self
.SetScrollRate(20,20) 
  31             # Initialize the buffer bitmap.  No real DC is needed at this point. 
  32             self
.buffer = wx
.EmptyBitmap(self
.maxWidth
, self
.maxHeight
) 
  33             dc 
= wx
.BufferedDC(None, self
.buffer) 
  34             dc
.SetBackground(wx
.Brush(self
.GetBackgroundColour())) 
  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
) 
  51     def OnPaint(self
, event
): 
  53             # Create a buffered paint DC.  It will create the real 
  54             # wx.PaintDC and then blit the bitmap to it when dc is 
  55             # deleted.  Since we don't need to draw anything else 
  56             # here that's all there is to it. 
  57             dc 
= wx
.BufferedPaintDC(self
, self
.buffer, wx
.BUFFER_VIRTUAL_AREA
) 
  61             # since we're not buffering in this case, we have to 
  62             # paint the whole window, potentially very time consuming. 
  66     def DoDrawing(self
, dc
, printing
=False): 
  68         dc
.SetPen(wx
.Pen('RED')) 
  69         dc
.DrawRectangle(5, 5, 50, 50) 
  71         dc
.SetBrush(wx
.LIGHT_GREY_BRUSH
) 
  72         dc
.SetPen(wx
.Pen('BLUE', 4)) 
  73         dc
.DrawRectangle(15, 15, 50, 50) 
  75         dc
.SetFont(wx
.Font(14, wx
.SWISS
, wx
.NORMAL
, wx
.NORMAL
)) 
  76         dc
.SetTextForeground(wx
.Colour(0xFF, 0x20, 0xFF)) 
  77         te 
= dc
.GetTextExtent("Hello World") 
  78         dc
.DrawText("Hello World", 60, 65) 
  80         dc
.SetPen(wx
.Pen('VIOLET', 4)) 
  81         dc
.DrawLine(5, 65+te
[1], 60+te
[0], 65+te
[1]) 
  83         lst 
= [(100,110), (150,110), (150,160), (100,160)] 
  84         dc
.DrawLines(lst
, -60) 
  85         dc
.SetPen(wx
.GREY_PEN
) 
  86         dc
.DrawPolygon(lst
, 75) 
  87         dc
.SetPen(wx
.GREEN_PEN
) 
  88         dc
.DrawSpline(lst
+[(100,100)]) 
  90         dc
.DrawBitmap(self
.bmp
, 200, 20, True) 
  91         dc
.SetTextForeground(wx
.Colour(0, 0xFF, 0x80)) 
  92         dc
.DrawText("a bitmap", 200, 85) 
  94 ##         dc.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.NORMAL)) 
  95 ##         dc.SetTextForeground("BLACK") 
  96 ##         dc.DrawText("TEST this STRING", 10, 200) 
  97 ##         print dc.GetFullTextExtent("TEST this STRING") 
  99         font 
= wx
.Font(20, wx
.SWISS
, wx
.NORMAL
, wx
.NORMAL
) 
 101         dc
.SetTextForeground(wx
.BLACK
) 
 103         for a 
in range(0, 360, 45): 
 104             dc
.DrawRotatedText("Rotated text...", 300, 300, a
) 
 106         dc
.SetPen(wx
.TRANSPARENT_PEN
) 
 107         dc
.SetBrush(wx
.BLUE_BRUSH
) 
 108         dc
.DrawRectangle(50,500, 50,50) 
 109         dc
.DrawRectangle(100,500, 50,50) 
 111         dc
.SetPen(wx
.Pen('RED')) 
 112         dc
.DrawEllipticArc(200,500, 50,75, 0, 90) 
 115             # This has troubles when used on a print preview in wxGTK, 
 116             # probably something to do with the pen styles and the scaling 
 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
) 
 127                 dc
.DrawLine(300,y
, 400,y
) 
 130         dc
.SetBrush(wx
.TRANSPARENT_BRUSH
) 
 131         dc
.SetPen(wx
.Pen(wx
.Colour(0xFF, 0x20, 0xFF), 1, wx
.SOLID
)) 
 132         dc
.DrawRectangle(450,50,  100,100) 
 133         old_pen 
= dc
.GetPen() 
 134         new_pen 
= wx
.Pen("BLACK", 5) 
 136         dc
.DrawRectangle(470,70,  60,60) 
 138         dc
.DrawRectangle(490,90, 20,20) 
 140         dc
.GradientFillLinear((20, 260, 50, 50), 
 142         dc
.GradientFillConcentric((20, 325, 50, 50), 
 143                                   "red", "blue", (25,25)) 
 144         self
.DrawSavedLines(dc
) 
 148     def DrawSavedLines(self
, dc
): 
 149         dc
.SetPen(wx
.Pen('MEDIUM FOREST GREEN', 4)) 
 151         for line 
in self
.lines
: 
 153                 apply(dc
.DrawLine
, coords
) 
 156     def SetXY(self
, event
): 
 157         self
.x
, self
.y 
= self
.ConvertEventCoords(event
) 
 159     def ConvertEventCoords(self
, event
): 
 160         xView
, yView 
= self
.GetViewStart() 
 161         xDelta
, yDelta 
= self
.GetScrollPixelsPerUnit() 
 162         return (event
.GetX() + (xView 
* xDelta
), 
 163                 event
.GetY() + (yView 
* yDelta
)) 
 165     def OnLeftButtonEvent(self
, event
): 
 173         elif event
.Dragging() and self
.drawing
: 
 175                 # If doing buffered drawing, create the buffered DC, giving it 
 176                 # it a real DC to blit to when done. 
 177                 cdc 
= wx
.ClientDC(self
) 
 179                 dc 
= wx
.BufferedDC(cdc
, self
.buffer) 
 181                 dc 
= wx
.ClientDC(self
) 
 185             dc
.SetPen(wx
.Pen('MEDIUM FOREST GREEN', 4)) 
 186             coords 
= (self
.x
, self
.y
) + self
.ConvertEventCoords(event
) 
 187             self
.curLine
.append(coords
) 
 193         elif event
.LeftUp() and self
.drawing
: 
 194             self
.lines
.append(self
.curLine
) 
 200 ## This is an example of what to do for the EVT_MOUSEWHEEL event, 
 201 ## but since wx.ScrolledWindow does this already it's not 
 202 ## necessary to do it ourselves. You would need to add an event table  
 203 ## entry to __init__() to direct wheelmouse events to this handler. 
 206 ##     def OnWheel(self, evt): 
 207 ##         delta = evt.GetWheelDelta() 
 208 ##         rot = evt.GetWheelRotation() 
 209 ##         linesPer = evt.GetLinesPerAction() 
 210 ##         print delta, rot, linesPer 
 211 ##         ws = self.wheelScroll 
 213 ##         lines = ws / delta 
 214 ##         ws = ws - lines * delta 
 215 ##         self.wheelScroll = ws 
 217 ##             lines = lines * linesPer 
 218 ##             vsx, vsy = self.GetViewStart() 
 219 ##             scrollTo = vsy - lines 
 220 ##             self.Scroll(-1, scrollTo) 
 222 #--------------------------------------------------------------------------- 
 224 def runTest(frame
, nb
, log
): 
 228 #--------------------------------------------------------------------------- 
 235 The wx.ScrolledWindow class manages scrolling for its client area, transforming the  
 236 coordinates according to the scrollbar positions, and setting the scroll positions,  
 237 thumb sizes and ranges according to the area in view. 
 243 if __name__ 
== '__main__': 
 246     run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])