Hi.

In attach some example.
But "but something went wrong" ...
Maybe it was a bad idea to make a call Refresh() within action() cb?
I not experienced in messages processing.
require "cdlua"
require "iuplua"
require "iupluacd"
--require "iupluacontrols"

local function hex2cdcolor(s)
    local r,g,b = s:match("%s*#(%x%x)(%x%x)(%x%x)%s*")
    return cd.EncodeColor(tonumber(r,16), tonumber(g,16), tonumber(b,16))
end

local function CanvasButton(t)

    local cx, cy
    local canvas
    local cnv = iup.canvas(t)
    cnv.border = "NO"
    cnv.expand = "NO"
    cnv.text = t.text or ""

    function cnv:map_cb()
        -- store the CD canvas
        canvas = cd.CreateCanvas(cd.IUP, self)
    end

    function cnv:unmap_cb()
        canvas:KillCanvas()
    end

    function cnv:action(posx, posy)
        canvas:Activate()
        self:fill(self.bgcolor)
        self:print(self.text, "#000000")
        self:leavewindow_cb()
    end

    function cnv:button_cb (but, pressed, x, y, status)
        --print("button_cb:", but, pressed, x, y, status)
        if but == iup.BUTTON1 and pressed == 1 then
            cx, cy = x, y
            self:print(self.text, "#FF00FF")
        elseif but == iup.BUTTON1 and pressed == 0 then
            self:print(self.text, "#000000")
        end
    end

    function cnv:motion_cb(x, y, status)
        if iup.isbutton1(status) then
            local dx, dy = x-cx, y-cy
            cx,cy = x,y
            self.cx = self.cx + dx
            self.cy = self.cy + dy
            if dx ~= 0 or dy ~= 0 then
                iup.Refresh(dlg, 1)
            end
        end
    end

    function cnv:print(text, color)
        canvas:TextAlignment(cd.CENTER)
        canvas:SetForeground(hex2cdcolor(color))
        local w,h = canvas:GetSize()
        canvas:Text(w/2, h/2, text)
    end

    function cnv:fill(color)
        canvas:SetForeground(hex2cdcolor(color))
        local w,h = canvas:GetSize()
        canvas:Box(0,w,0,h)
    end


    function cnv:draw_border(color, tick)
        --canvas:Activate()

        --local bg_color = canvas:Background(cd.QUERY)
        local old_fg_color = canvas:Foreground(color)
        canvas:LineWidth(tick)

        --local old_mode = canvas:WriteMode(cd.XOR)
        local old_mode = canvas:WriteMode(cd.REPLACE)
        --canvas:LineJoin(cd.ROUND)

        local w,h = canvas:GetSize()
        local ht = math.modf(tick/2)
        local mt = tick % 2
        local x1,x2,y1,y2 = ht, w-ht-mt, ht+mt-1, h-ht-1
        canvas:Rect(ht,x2,y1,y2)

        canvas:LineStyle(cd.DOTTED)
        canvas:SetForeground(old_fg_color)
        canvas:WriteMode(old_mode)
        --canvas:Deactivate()
    end
    
    function cnv:enterwindow_cb()
        self:draw_border(cd.WHITE, self.borderwidth)
    end

    function cnv:leavewindow_cb()
        self:draw_border(cd.BLACK, self.borderwidth)
    end

    return cnv
end



dlg = iup.dialog{
    iup.frame{
        title = "Canvas Buttons",
        iup.backgroundbox{
            bgcolor = "#FFFFFF",
            iup.cbox{
                rastersize = "300x300",
                CanvasButton{ rastersize="120x40", bgcolor="#FF0000", 
borderwidth=4,
                    text = "Big Red",
                    cx = 10, cy = 10,
                },
                CanvasButton{ rastersize="100x30", bgcolor="#00FF00", 
borderwidth=2,
                    text = "Medium Green",
                    cx = 150, cy = 100,
                },
                CanvasButton{ rastersize="80x20", bgcolor="#0000FF", 
borderwidth=1,
                    text = "Small Blue",
                    cx = 80, cy = 200,
                },
            },
        },
    },
}


dlg:showxy(iup.CENTER, iup.CENTER)
dlg.rastersize = "400x400"
dlg.minsize = "400x400"
iup.Refresh(dlg)
iup.MainLoop()

------------------------------------------------------------------------------
_______________________________________________
Iup-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/iup-users

Reply via email to