2009-02-27 17:22:03 +01:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
-- @author Julien Danjou <julien@danjou.info>
|
|
|
|
-- @copyright 2008-2009 Julien Danjou
|
|
|
|
-- @release @AWESOME_VERSION@
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- Grab environment we need
|
|
|
|
local math = math
|
|
|
|
local type = type
|
|
|
|
local ipairs = ipairs
|
2009-10-24 14:53:01 +02:00
|
|
|
local pairs = pairs
|
2009-04-11 14:13:20 +02:00
|
|
|
local setmetatable = setmetatable
|
2010-10-06 14:23:36 +02:00
|
|
|
local capi = { button = button }
|
2009-10-24 14:53:01 +02:00
|
|
|
local util = require("awful.util")
|
2010-10-06 14:23:36 +02:00
|
|
|
local imagebox = require("wibox.widget.imagebox")
|
|
|
|
local textbox = require("wibox.widget.textbox")
|
2009-02-27 17:22:03 +01:00
|
|
|
|
2009-10-24 14:53:01 +02:00
|
|
|
local common = {}
|
2009-02-27 17:22:03 +01:00
|
|
|
|
2009-10-24 14:53:01 +02:00
|
|
|
-- Recursively processes a template, replacing the tables representing the icon and
|
|
|
|
-- the title with the widgets ib and tb
|
|
|
|
local function replace_in_template(t, ib, tb)
|
|
|
|
for i, v in ipairs(t) do
|
|
|
|
if type(t[i]) == "table" then
|
|
|
|
if v.item == "icon" then
|
|
|
|
t[i] = ib
|
|
|
|
elseif v.item == "title" then
|
|
|
|
t[i] = tb
|
|
|
|
else
|
|
|
|
replace_in_template(v, ib, tb)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-06 14:23:36 +02:00
|
|
|
function common.list_update(w, buttons, label, data, objects)
|
2009-02-27 17:22:03 +01:00
|
|
|
-- Hack: if it has been registered as a widget in a wibox,
|
|
|
|
-- it's w.len since __len meta does not work on table until Lua 5.2.
|
|
|
|
-- Otherwise it's standard #w.
|
2009-10-24 14:53:01 +02:00
|
|
|
local len = (w.len or #w)
|
2009-02-27 17:22:03 +01:00
|
|
|
|
2009-10-24 14:53:01 +02:00
|
|
|
-- Remove excessive widgets
|
|
|
|
if len > #objects then
|
|
|
|
for i = #objects, len do
|
2009-02-27 17:22:03 +01:00
|
|
|
w[i] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-24 14:53:01 +02:00
|
|
|
-- update the widgets, creating them if needed
|
2010-10-06 14:23:36 +02:00
|
|
|
w:reset()
|
2009-10-24 14:53:01 +02:00
|
|
|
for i, o in ipairs(objects) do
|
2010-10-06 18:45:50 +02:00
|
|
|
local ib = wibox.widget.imagebox()
|
|
|
|
local tb = wibox.widget.textbox()
|
|
|
|
local bgb = wibox.widget.background()
|
2010-10-08 17:22:00 +02:00
|
|
|
local m = wibox.layout.margin(tb, 4, 4)
|
2010-10-06 18:45:50 +02:00
|
|
|
local l = wibox.layout.fixed.horizontal()
|
|
|
|
|
|
|
|
-- All of this is added in a fixed widget
|
2010-10-06 14:23:36 +02:00
|
|
|
l:fill_space(true)
|
|
|
|
l:add(ib)
|
2010-10-06 18:45:50 +02:00
|
|
|
l:add(m)
|
|
|
|
|
|
|
|
-- And all of this gets a background
|
|
|
|
bgb:set_widget(l)
|
|
|
|
w:add(bgb)
|
2009-10-24 14:53:01 +02:00
|
|
|
|
2009-02-27 17:22:03 +01:00
|
|
|
if buttons then
|
2010-09-16 19:02:15 +02:00
|
|
|
-- Use a local variable so that the garbage collector doesn't strike
|
|
|
|
-- between now and the :buttons() call.
|
|
|
|
local btns = data[o]
|
|
|
|
if not btns then
|
|
|
|
btns = {}
|
|
|
|
data[o] = btns
|
2009-02-27 17:22:03 +01:00
|
|
|
for kb, b in ipairs(buttons) do
|
2009-07-09 12:46:06 +02:00
|
|
|
-- Create a proxy button object: it will receive the real
|
|
|
|
-- press and release events, and will propagate them the the
|
|
|
|
-- button object the user provided, but with the object as
|
|
|
|
-- argument.
|
|
|
|
local btn = capi.button { modifiers = b.modifiers, button = b.button }
|
2009-10-09 20:39:55 +02:00
|
|
|
btn:connect_signal("press", function () b:emit_signal("press", o) end)
|
|
|
|
btn:connect_signal("release", function () b:emit_signal("release", o) end)
|
2010-09-16 19:02:15 +02:00
|
|
|
btns[#btns + 1] = btn
|
2009-02-27 17:22:03 +01:00
|
|
|
end
|
|
|
|
end
|
2010-10-06 18:45:50 +02:00
|
|
|
bgb:buttons(btns)
|
2009-02-27 17:22:03 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
local text, bg, bg_image, icon = label(o)
|
2010-10-06 14:23:36 +02:00
|
|
|
tb:set_markup(text)
|
|
|
|
bgb:set_bg(bg)
|
|
|
|
bgb:set_bgimage(bg_image)
|
|
|
|
ib:set_image(icon)
|
2009-02-27 17:22:03 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-24 14:53:01 +02:00
|
|
|
return common
|
|
|
|
|
2009-02-27 17:22:03 +01:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
|