]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxScrolledWindow.py
2 from wxPython
.wx
import *
8 #---------------------------------------------------------------------------
10 class MyCanvas(wxScrolledWindow
):
11 def __init__(self
, parent
, id = -1, size
= wxDefaultSize
):
12 wxScrolledWindow
.__init
__(self
, parent
, id, wxPoint(0, 0), size
, wxSUNKEN_BORDER
)
21 self
.SetBackgroundColour("WHITE")
22 self
.SetCursor(wxStockCursor(wxCURSOR_PENCIL
))
23 bmp
= images
.getTest2Bitmap()
24 mask
= wxMaskColour(bmp
, wxBLUE
)
28 self
.SetScrollbars(20, 20, self
.maxWidth
/20, self
.maxHeight
/20)
31 # Initialize the buffer bitmap. No real DC is needed at this point.
32 self
.buffer = wxEmptyBitmap(self
.maxWidth
, self
.maxHeight
)
33 dc
= wxBufferedDC(None, self
.buffer)
34 dc
.SetBackground(wxBrush(self
.GetBackgroundColour()))
38 EVT_LEFT_DOWN(self
, self
.OnLeftButtonEvent
)
39 EVT_LEFT_UP(self
, self
.OnLeftButtonEvent
)
40 EVT_MOTION(self
, self
.OnLeftButtonEvent
)
41 EVT_PAINT(self
, self
.OnPaint
)
51 def OnPaint(self
, event
):
53 # Create a buffered paint DC. It will create the real
54 # wxPaintDC 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
= wxBufferedPaintDC(self
, self
.buffer)
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
):
68 dc
.SetPen(wxPen('RED'))
69 dc
.DrawRectangle(5, 5, 50, 50)
71 dc
.SetBrush(wxLIGHT_GREY_BRUSH
)
72 dc
.SetPen(wxPen('BLUE', 4))
73 dc
.DrawRectangle(15, 15, 50, 50)
75 dc
.SetFont(wxFont(14, wxSWISS
, wxNORMAL
, wxNORMAL
))
76 dc
.SetTextForeground(wxColour(0xFF, 0x20, 0xFF))
77 te
= dc
.GetTextExtent("Hello World")
78 dc
.DrawText("Hello World", 60, 65)
80 dc
.SetPen(wxPen('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)
86 dc
.DrawPolygon(lst
, 75)
87 dc
.SetPen(wxGREEN_PEN
)
88 dc
.DrawSpline(lst
+[(100,100)])
90 dc
.DrawBitmap(self
.bmp
, 200, 20, true
)
91 dc
.SetTextForeground(wxColour(0, 0xFF, 0x80))
92 dc
.DrawText("a bitmap", 200, 85)
94 font
= wxFont(20, wxSWISS
, wxNORMAL
, wxNORMAL
)
96 dc
.SetTextForeground(wxBLACK
)
97 for a
in range(0, 360, 45):
98 dc
.DrawRotatedText("Rotated text...", 300, 300, a
)
100 dc
.SetPen(wxTRANSPARENT_PEN
)
101 dc
.SetBrush(wxBLUE_BRUSH
)
102 dc
.DrawRectangle(50,500,50,50)
103 dc
.DrawRectangle(100,500,50,50)
105 dc
.SetPen(wxPen('RED'))
106 dc
.DrawEllipticArc(200, 500, 50, 75, 0, 90)
109 for style
in [wxDOT
, wxLONG_DASH
, wxSHORT_DASH
, wxDOT_DASH
, wxUSER_DASH
]:
110 pen
= wxPen("DARK ORCHID", 1, style
)
111 if style
== wxUSER_DASH
:
112 pen
.SetDashes([1, 2, 3, 4, 5, 6, 7, 8])
115 dc
.DrawLine(300, y
, 400, y
)
118 dc
.SetBrush(wxTRANSPARENT_BRUSH
)
119 dc
.SetPen(wxPen(wxColour(0xFF, 0x20, 0xFF), 1, wxSOLID
))
120 dc
.DrawRectangle(450, 50, 100, 100)
121 old_pen
= dc
.GetPen()
122 new_pen
= wxPen("BLACK", 5)
124 dc
.DrawRectangle(470, 70, 60, 60)
126 dc
.DrawRectangle(490, 90, 20, 20)
128 self
.DrawSavedLines(dc
)
132 def DrawSavedLines(self
, dc
):
133 dc
.SetPen(wxPen('MEDIUM FOREST GREEN', 4))
134 for line
in self
.lines
:
136 apply(dc
.DrawLine
, coords
)
139 def SetXY(self
, event
):
140 self
.x
, self
.y
= self
.ConvertEventCoords(event
)
142 def ConvertEventCoords(self
, event
):
143 xView
, yView
= self
.GetViewStart()
144 xDelta
, yDelta
= self
.GetScrollPixelsPerUnit()
145 return (event
.GetX() + (xView
* xDelta
),
146 event
.GetY() + (yView
* yDelta
))
148 def OnLeftButtonEvent(self
, event
):
155 elif event
.Dragging() and self
.drawing
:
157 # If doing buffered drawing, create the buffered DC, giving it
158 # it a real DC to blit to when done.
159 cdc
= wxClientDC(self
)
161 dc
= wxBufferedDC(cdc
, self
.buffer)
163 dc
= wxClientDC(self
)
167 dc
.SetPen(wxPen('MEDIUM FOREST GREEN', 4))
168 coords
= (self
.x
, self
.y
) + self
.ConvertEventCoords(event
)
169 self
.curLine
.append(coords
)
170 apply(dc
.DrawLine
, coords
)
175 elif event
.LeftUp() and self
.drawing
:
176 self
.lines
.append(self
.curLine
)
182 ## This is an example of what to do for the EVT_MOUSEWHEEL event,
183 ## but since wxScrolledWindow does this already it's not
184 ## necessary to do it ourselves.
187 ## def OnWheel(self, evt):
188 ## delta = evt.GetWheelDelta()
189 ## rot = evt.GetWheelRotation()
190 ## linesPer = evt.GetLinesPerAction()
191 ## ws = self.wheelScroll
193 ## lines = ws / delta
194 ## ws = ws - lines * delta
195 ## self.wheelScroll = ws
197 ## lines = lines * linesPer
198 ## vsx, vsy = self.GetViewStart()
199 ## scrollTo = vsy - lines
200 ## self.Scroll(-1, scrollTo)
202 #---------------------------------------------------------------------------
204 def runTest(frame
, nb
, log
):
208 #---------------------------------------------------------------------------
220 if __name__
== '__main__':
223 run
.main(['', os
.path
.basename(sys
.argv
[0])])