on scrolling how to redraw as little as

Hi,

win98, Py234, wxPy2531

I have nothing to add more to what has been said. Here I submit my
own implementation of a resizable scrolled window. I sometimes
use it to mimic a printer page (1:1). Still to be improved.
With the hope it may help.

*** To Robin ***

If the bitmap size is too? large (25000 x 5000) I get

Traceback (most recent call last):
  File "DessinSurScrolledWindow7.py", line 65, in OnPaint
    dc.DrawBitmap(self.BufferBmp, 0, 0)
  File "C:\Python24\Lib\site-packages\wx-2.5.3-msw-ansi\wx\_gdi.py", line
2843, in DrawBitmap
    return _gdi_.DC_DrawBitmap(*args, **kwargs)
wx._core.PyAssertionError: C++ assertion "wxAssertFailure" failed in
..\..\src\msw\dc.cpp(1055): invalid bitmap in wxDC::DrawBitmap

and my HD is working, working...

Jean-Michel Fauth, Switzerland

PS DessinSurScrolledWindow means Drawing on a ScrolledWindow

#-*- coding: iso-8859-1 -*-

···

#-------------------------------------------------------------------
# DessinSurScrolledWindow7.py
# win98, Py24, wxPy2531
# Jean-Michel Fauth
#-------------------------------------------------------------------

import wx

#-------------------------------------------------------------------

def DrawOnDCVirt(dc, vp1x, vp1y, vp2x, vp2y):
    dc.BeginDrawing()
    p1x, p1y = vp1x, vp1y
    p2x, p2y = vp2x - 1, vp2y - 1
    p2x, p2y = vp2x - 1, vp2y - 1

    dc.SetBrush(wx.Brush(wx.NamedColour('black'), wx.SOLID))
    dc.SetPen(wx.Pen(wx.NamedColour('green'), 1, wx.SOLID))
    dc.DrawRectangle(p1x, p1y, p2x - p1x + 1, p2y - p1y + 1)

    dc.SetPen(wx.Pen(wx.NamedColour('white'), 1, wx.SOLID))
    dx = (p2x - p1x ) / 10.0
    for i in xrange(1, 10):
        dc.DrawLine(int(i * dx), p1y, int(i * dx), p2y)
    dy = (p2y - p1y ) / 10.0
    for i in xrange(1, 10):
        dc.DrawLine(p1x, int(i * dy), p2x, int(i * dy))

    dc.SetPen(wx.Pen(wx.NamedColour('red'), 1, wx.SOLID))
    dc.DrawLine(p1x, p1y, p2x, p2y)
    dc.SetPen(wx.Pen(wx.NamedColour('cyan'), 1, wx.SOLID))
    dc.DrawLine(p2x, p1y, p1x, p2y)

    dc.EndDrawing()

#-------------------------------------------------------------------

class MyScrolledWin(wx.ScrolledWindow):

    def __init__(self, parent, id, pos, size, style):
        wx.ScrolledWindow.__init__(self, parent, id, pos, size, style)
        self.SetBackgroundColour(wx.NamedColour('salmon'))

        self.maxwi = 25000
        self.maxhe = 5000
        self.SetVirtualSize((self.maxwi, self.maxhe))
        self.SetScrollRate(1, 1) #<<<<<<<<<<<<

        #memory bitmap holding the drawing
        self.BufferBmp = wx.EmptyBitmap(self.maxwi, self.maxhe)
        memdc = wx.MemoryDC()
        memdc.SelectObject(self.BufferBmp)
        p1x, p1y = 0, 0
        memdcwi, memdche = memdc.GetSizeTuple()
        #~ print memdcwi, memdche
        DrawOnDCVirt(memdc, 0, 0, memdcwi, memdche)

        self.Bind(wx.EVT_PAINT, self.OnPaint)

    def OnPaint(self, event):
        dc = wx.PaintDC(self)
        self.PrepareDC(dc)
        #~ DrawOnDCVirt(dc, 0, 0, self.maxwi, self.maxhe)
        dc.DrawBitmap(self.BufferBmp, 0, 0)

#-------------------------------------------------------------------

class MyPanel(wx.Panel):

    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, id, wx.DefaultPosition,
wx.DefaultSize)

        cwi, che = parent.GetClientSize()

        sty = wx.SUNKEN_BORDER
        swin = MyScrolledWin(self, -1, (0, 0), (-1, -1), style=sty)

        sizer1 = wx.BoxSizer(wx.VERTICAL)
        sizer1.Add(swin, proportion=1, flag=wx.EXPAND | wx.ALL, border=10)
        self.SetSizerAndFit(sizer1)

#-------------------------------------------------------------------

class MyFrame(wx.Frame):

    def __init__(self, parent, id):
        sty = wx.DEFAULT_FRAME_STYLE
        s = __file__
        wx.Frame.__init__(self, parent, id, s, (0, 0), (300, 200),
style=sty)

        self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)

        self.panel = MyPanel(self, -1)

    def OnCloseWindow(self, event):
        self.Destroy()

#-------------------------------------------------------------------

class MyApp(wx.App):

    def OnInit(self):
        frame = MyFrame(None, -1)
        frame.Show(True)
        self.SetTopWindow(frame)
        return True

#-------------------------------------------------------------------

def main():
    app = MyApp(False)
    app.MainLoop()

#-------------------------------------------------------------------

if __name__ == '__main__' :
    main()

#eof-------------------------------------------------------------------