[wxPython-users] Re: Bitmap in a wxScrolledWindow
Gavana, Andrea
gavana at kpo.kz
Wed Aug 9 03:48:55 PDT 2006
Hello Andrew,
> Is it possible that you could post the source code of your
> working solution?
Well, at the moment these are the relevant pieces of code I am using in InfinImage (thanks to Will McGugan, Peter Damoc and Simon Clay):
Class declaration and __init__:
class ScrolledImage(wx.ScrolledWindow):
def __init__(self, parent, id=-1, pos=wx.DefaultPosition, size=wx.DefaultSize):
wx.ScrolledWindow.__init__(self, parent, id, pos, size)
self.size = wx.Size(0, 0)
self.SetExtraStyle(0)
self.Bind(wx.EVT_SIZE, self.OnSize)
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
Image Loading:
def ShowImage(self, image):
PIL_bitmap = Image.open(image)
bmp = PilToBitmap(PIL_bitmap)
self.SetScrollbars(1, 1, PIL_bitmap.size[0], PIL_bitmap.size[1], 0, 0)
size = self.GetSize()
xmin = (size.x - PIL_bitmap.size[0])/2
ymin = (size.y - PIL_bitmap.size[1])/2
self.currentbmp = bmp
self.size = wx.Size(self.currentbmp.GetSize().width, self.currentbmp.GetSize().height)
self.Refresh()
OnPaint:
def OnPaint(self, event):
dc = wx.PaintDC(self)
dc.Clear()
self.DoPaint(dc)
def DoPaint(self, dc=None):
if dc is None:
dc = wx.ClientDC(self)
self.PrepareDC(dc)
iw, ih = self.size
xmin = (self.windowsize.x - iw)/2
ymin = (self.windowsize.y - ih)/2
bm_dc = wx.MemoryDC()
bm_dc.SelectObject(self.currentbmp)
if xmin < 0:
xmin = 0
if ymin < 0:
ymin = 0
dc.Blit(xmin, ymin, iw, ih, bm_dc, 0, 0, wx.COPY, True)
bm_dc.SelectObject(wx.NullBitmap)
OnSize:
def OnSize(self, event):
iw, ih = self.size.width, self.size.height
w, h = event.GetSize()
scroll_x = w < iw
scroll_y = h < ih
scroll = scroll_x | scroll_y
x, y = self.GetViewStart()
self.SetScrollbars(scroll,scroll,iw,ih,x,y)
self.windowsize = wx.Size(w, h)
OnEraseBackground:
def OnEraseBackground(self, event):
pass
I don't have at the moment a runnable piece of code, it's been a while since I worked last time on InfinImage. Well, I promised to my girlfriend to finish that image viewer, so I have to force myself to continue in some way. But there are only 24 hours in a day :-D
For the zooming part, at the moment I am using the mouse wheel event to handle it. It still needs some work to be done, but it's almost finished. I'll send some code when it will be ready.
Andrea.
_________________________________________
Andrea Gavana (gavana at kpo.kz)
Reservoir Engineer
KPDL
4, Millbank
SW1P 3JA London
Direct Tel: +44 (0) 20 717 08936
Mobile Tel: +44 (0) 77 487 70534
Fax: +44 (0) 20 717 08900
Web: http://xoomer.virgilio.it/infinity77
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
More information about the wxpython-users
mailing list