loadmodule "luaiup"
loadmodule "luahandle"
loadmodule "lualibs"

if not iupep then iupep = {} end

iupep.dbuffer = {
    map_cb = function (self)
        self.cv = cd.CreateCanvas(cd.IUP, self)
        if not self.cv then return iup.DEFAULT end
        local cvdb = cd.CreateCanvas(cd.DBUFFER, self.cv)
        self.cvdb = cvdb
        return iup.DEFAULT
    end,
    
    unmap_cb = function(self)
        if self.cv then cd.KillCanvas(self.cv) end
        if self.cvdb then cd.KillCanvas(self.cvdb) end
    end,

    resize_cb = function(self, width, height)
        local cv = self.cv
        local cvdb = self.cvdb
        if not cvdb then
            cv:Activate()
            self.cvdb = cd.CreateCanvas(cd.DBUFFER, cv)
            cvdb = self.cvdb
        end
        self.width = width
        self.height = height
        if not cvdb then return iup.DEFAULT end
        cvdb:Activate()
        return self:draw()
    end,

    action = function(self, posx, posy)
        local cvdb = self.cvdb
        if not cvdb then return end
        cvdb:Flush()
        
        return iup.DEFAULT
    end,
}