wx.ScrolledWindow wx.PseudoDC

时间:2023-03-10 05:10:44
wx.ScrolledWindow wx.PseudoDC

# encoding: utf-8

import logging
import random

import wx
import wx.lib.inspection

def GetMyBitmap():
    image = wx.Image("0.png", wx.BITMAP_TYPE_PNG)
    return wx.BitmapFromImage(image)

ID_BTN_SPY = wx.NewId()

W = 2000
H = 2000
SW = 150
SH = 150
SHAPE_COUNT = 2500
hitradius = 5

colours = [
    "BLACK",
    "BLUE",
    "BLUE VIOLET",
    "BROWN",
    "CYAN",]

class MyCanvas(wx.ScrolledWindow):   
    def RandomPen(self):
        c = random.choice(colours)
        t = random.randint(1, 4)
        if not self.pen_cache.has_key( (c, t) ):
            self.pen_cache[(c, t)] = wx.Pen(c, t)
        return self.pen_cache[(c, t)]
    def RandomColor(self):
        return random.choice(colours)
    def RandomBrush(self):
        c = random.choice(colours)
        if not self.brush_cache.has_key(c):
            self.brush_cache[c] = wx.Brush(c)

        return self.brush_cache[c]
   
    def DoDrawing(self, dc):
        random.seed()
        self.objids = []
        self.boundsdict = {}
        dc.BeginDrawing()
               
        id = wx.NewId()
        dc.SetId(id)                       
        w,h = self.bmp.GetSize()
        x = w
        y = h
        dc.DrawBitmap(self.bmp,x,y,True)
        dc.SetIdBounds(id,wx.Rect(x,y,w,h))
        self.objids.append(id)
       
        dc.EndDrawing()
   
   
    def __init__(self, parent, id, log, size = wx.DefaultSize):
        wx.ScrolledWindow.__init__(self, parent, id, (0, 0), size=size, style=wx.SUNKEN_BORDER)

        self.lines = []
        self.maxWidth  = W
        self.maxHeight = H
        self.x = self.y = 0
        self.curLine = []
        self.drawing = False

        self.SetBackgroundColour("WHITE")
        bmp = GetMyBitmap()
        mask = wx.Mask(bmp, wx.BLUE)
        bmp.SetMask(mask)
        self.bmp = bmp
       
        self.SetVirtualSize((self.maxWidth, self.maxHeight))
        self.SetScrollRate(20,20)
       
        # create a PseudoDC to record our drawing
        self.pdc = wx.PseudoDC()
        self.pen_cache = {}
        self.brush_cache = {}
        self.DoDrawing(self.pdc)
       
        log.info('Created PseudoDC draw list with %d operations!'%self.pdc.GetLen())

        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.Bind(wx.EVT_ERASE_BACKGROUND, lambda x:None)
        self.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouse)
       
        # vars for handling mouse clicks
        self.dragid = -1
        self.lastpos = (0,0) 
       
    def OnMouse(self, event):
        pass
   
    def OnPaint(self, event):
        # Create a buffered paint DC.  It will create the real
        # wx.PaintDC and then blit the bitmap to it when dc is
        # deleted. 
        dc = wx.BufferedPaintDC(self)
        # use PrepateDC to set position correctly
        self.PrepareDC(dc)
        # we need to clear the dc BEFORE calling PrepareDC
        bg = wx.Brush(self.GetBackgroundColour())
        dc.SetBackground(bg)
        dc.Clear()
        # create a clipping rect from our position and size
        # and the Update Region
        xv, yv = self.GetViewStart()
        dx, dy = self.GetScrollPixelsPerUnit()
        x, y   = (xv * dx, yv * dy)
        rgn = self.GetUpdateRegion()
        rgn.Offset(x,y)
        r = rgn.GetBox()
        # draw to the dc using the calculated clipping rect
        self.pdc.DrawToDCClipped(dc,r)
       

class MyFrame(wx.Frame):  
    def OnBtnSpy(self, evt):
        print 'click btn spy'
        wx.lib.inspection.InspectionTool().Show()   
   
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "My Frame", size=(300,300))
        self.log = None
        panel0 = wx.Panel(self, -1)
        btn1 = wx.Button(panel0, ID_BTN_SPY, label='spy++')
               
        self.log = logging   
        win = MyCanvas(self, -1, self.log)
       
        sz0 = wx.BoxSizer(wx.VERTICAL)
        sz0.Add(panel0, flag=wx.EXPAND)
        sz0.Add(win, proportion=1, flag=wx.EXPAND)       
        self.SetSizer(sz0)
               
        self.Bind(wx.EVT_BUTTON, self.OnBtnSpy, id=ID_BTN_SPY)

if __name__ == '__main__':
    app = wx.App()
    frame = MyFrame()
    frame.Show(True)
    app.MainLoop()