Fix warnings from LDoc
[awesome.git] / lib / awful / widget / common.lua.in
blobf1947162b44a3b8b7ce0df70479fb66043cb116f
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 pcall = pcall
13 local setmetatable = setmetatable
14 local capi = { button = button }
15 local util = require("awful.util")
16 local wibox = require("wibox")
17 local imagebox = require("wibox.widget.imagebox")
18 local textbox = require("wibox.widget.textbox")
20 --- Common utilities for awful widgets
21 local common = {}
23 -- Recursively processes a template, replacing the tables representing the icon and
24 -- the title with the widgets ib and tb
25 local function replace_in_template(t, ib, tb)
26 for i, v in ipairs(t) do
27 if type(t[i]) == "table" then
28 if v.item == "icon" then
29 t[i] = ib
30 elseif v.item == "title" then
31 t[i] = tb
32 else
33 replace_in_template(v, ib, tb)
34 end
35 end
36 end
37 end
39 function common.list_update(w, buttons, label, data, objects)
40 -- update the widgets, creating them if needed
41 w:reset()
42 for i, o in ipairs(objects) do
43 local cache = data[o]
44 local ib, tb, bgb, m, l
45 if cache then
46 ib = cache.ib
47 tb = cache.tb
48 bgb = cache.bgb
49 else
50 ib = wibox.widget.imagebox()
51 tb = wibox.widget.textbox()
52 bgb = wibox.widget.background()
53 m = wibox.layout.margin(tb, 4, 4)
54 l = wibox.layout.fixed.horizontal()
56 -- All of this is added in a fixed widget
57 l:fill_space(true)
58 l:add(ib)
59 l:add(m)
61 -- And all of this gets a background
62 bgb:set_widget(l)
64 if buttons then
65 local btns = {}
66 for kb, b in ipairs(buttons) do
67 -- Create a proxy button object: it will receive the real
68 -- press and release events, and will propagate them the the
69 -- button object the user provided, but with the object as
70 -- argument.
71 local btn = capi.button { modifiers = b.modifiers, button = b.button }
72 btn:connect_signal("press", function () b:emit_signal("press", o) end)
73 btn:connect_signal("release", function () b:emit_signal("release", o) end)
74 btns[#btns + 1] = btn
75 end
76 bgb:buttons(btns)
77 end
79 data[o] = {
80 ib = ib,
81 tb = tb,
82 bgb = bgb
84 end
86 local text, bg, bg_image, icon = label(o)
87 -- The text might be invalid, so use pcall
88 if not pcall(tb.set_markup, tb, text) then
89 tb:set_markup("<i>&lt;Invalid text&gt;</i>")
90 end
91 bgb:set_bg(bg)
92 bgb:set_bgimage(bg_image)
93 ib:set_image(icon)
94 w:add(bgb)
95 end
96 end
98 return common
100 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80