1---------------------------------------------------------------------------
2-- A simple button widget.
3-- @usage local button = awful.widget.button()
4-- button:buttons(gears.table.join(
5--     button:buttons(),
6--     awful.button({}, 1, nil, function ()
7--         print("Mouse was clicked")
8--     end)
9-- ))
10-- @author Julien Danjou <julien@danjou.info>
11-- @copyright 2008-2009 Julien Danjou
12-- @classmod awful.widget.button
13---------------------------------------------------------------------------
14
15local setmetatable = setmetatable
16local abutton = require("awful.button")
17local imagebox = require("wibox.widget.imagebox")
18local widget = require("wibox.widget.base")
19local surface = require("gears.surface")
20local cairo = require("lgi").cairo
21
22local button = { mt = {} }
23
24--- Create a button widget. When clicked, the image is deplaced to make it like
25-- a real button.
26--
27-- @param args Widget arguments. "image" is the image to display.
28-- @return A textbox widget configured as a button.
29function button.new(args)
30    if not args or not args.image then
31        return widget.empty_widget()
32    end
33
34    local w = imagebox()
35    local orig_set_image = w.set_image
36    local img_release
37    local img_press
38
39    function w:set_image(image)
40        img_release = surface.load(image)
41        img_press = img_release:create_similar(cairo.Content.COLOR_ALPHA, img_release.width, img_release.height)
42        local cr = cairo.Context(img_press)
43        cr:set_source_surface(img_release, 2, 2)
44        cr:paint()
45        orig_set_image(self, img_release)
46    end
47    w:set_image(args.image)
48    w:buttons(abutton({}, 1, function () orig_set_image(w, img_press) end,
49                             function () orig_set_image(w, img_release) end))
50
51    w:connect_signal("mouse::leave", function(self) orig_set_image(self, img_release) end)
52
53    return w
54end
55
56function button.mt:__call(...)
57    return button.new(...)
58end
59
60--@DOC_widget_COMMON@
61
62--@DOC_object_COMMON@
63
64return setmetatable(button, button.mt)
65
66-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
67