awful.widget.common: Add background to the icon
[awesome.git] / lib / awful / widget / common.lua.in
blobf162b8843d79894b6661614ee212bbd92d007daf
1 ---------------------------------------------------------------------------
2 -- @author Julien Danjou <julien@danjou.info>
3 -- @copyright 2008-2009 Julien Danjou
4 -- @release @AWESOME_VERSION@
5 ---------------------------------------------------------------------------
7 -- Grab environment we need
8 local math = math
9 local type = type
10 local ipairs = ipairs
11 local pairs = pairs
12 local setmetatable = setmetatable
13 local capi = { button = button }
14 local layout = require("awful.widget.layout")
15 local util = require("awful.util")
16 local imagebox = require("wibox.widget.imagebox")
17 local textbox = require("wibox.widget.textbox")
19 local common = {}
21 -- Recursively processes a template, replacing the tables representing the icon and
22 -- the title with the widgets ib and tb
23 local function replace_in_template(t, ib, tb)
24 for i, v in ipairs(t) do
25 if type(t[i]) == "table" then
26 if v.item == "icon" then
27 t[i] = ib
28 elseif v.item == "title" then
29 t[i] = tb
30 else
31 replace_in_template(v, ib, tb)
32 end
33 end
34 end
35 end
37 function common.list_update(w, buttons, label, data, objects)
38 -- Hack: if it has been registered as a widget in a wibox,
39 -- it's w.len since __len meta does not work on table until Lua 5.2.
40 -- Otherwise it's standard #w.
41 local len = (w.len or #w)
43 -- Remove excessive widgets
44 if len > #objects then
45 for i = #objects, len do
46 w[i] = nil
47 end
48 end
50 -- update the widgets, creating them if needed
51 w:reset()
52 for i, o in ipairs(objects) do
53 local ib = wibox.widget.imagebox()
54 local tb = wibox.widget.textbox()
55 local bgb = wibox.widget.background()
56 local m = wibox.layout.margin()
57 local l = wibox.layout.fixed.horizontal()
59 -- The textbox gets a margin to look better
60 m:set_left(4)
61 m:set_right(4)
62 m:set_widget(tb)
64 -- All of this is added in a fixed widget
65 l:fill_space(true)
66 l:add(ib)
67 l:add(m)
69 -- And all of this gets a background
70 bgb:set_widget(l)
71 w:add(bgb)
73 if buttons then
74 -- Use a local variable so that the garbage collector doesn't strike
75 -- between now and the :buttons() call.
76 local btns = data[o]
77 if not btns then
78 btns = {}
79 data[o] = btns
80 for kb, b in ipairs(buttons) do
81 -- Create a proxy button object: it will receive the real
82 -- press and release events, and will propagate them the the
83 -- button object the user provided, but with the object as
84 -- argument.
85 local btn = capi.button { modifiers = b.modifiers, button = b.button }
86 btn:connect_signal("press", function () b:emit_signal("press", o) end)
87 btn:connect_signal("release", function () b:emit_signal("release", o) end)
88 btns[#btns + 1] = btn
89 end
90 end
91 bgb:buttons(btns)
92 end
94 local text, bg, bg_image, icon = label(o)
95 tb:set_markup(text)
96 bgb:set_bg(bg)
97 bgb:set_bgimage(bg_image)
98 ib:set_image(icon)
99 end
102 return common
104 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80