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
|
2009-07-09 12:46:06 +02:00
|
|
|
local capi = { widget = widget, button = button }
|
2009-10-24 14:53:01 +02:00
|
|
|
local layout = require("awful.widget.layout")
|
|
|
|
local util = require("awful.util")
|
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
|
|
|
|
|
|
|
-- Private structures
|
2009-04-11 14:13:20 +02:00
|
|
|
tagwidgets = setmetatable({}, { __mode = 'k' })
|
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
|
|
|
|
tb.align = v.align or "left"
|
|
|
|
tb:margin(v.margin or {})
|
|
|
|
tb.bg_resize = v.bg_resize or false
|
|
|
|
tb.bg_align = v.bg_align or ""
|
|
|
|
t[i] = tb
|
|
|
|
else
|
|
|
|
replace_in_template(v, ib, tb)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function common.list_update(w, buttons, label, data, template, 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
|
|
|
|
for i, o in ipairs(objects) do
|
|
|
|
local ib, tb
|
|
|
|
if w[i] then
|
|
|
|
ib = w[i].icon
|
|
|
|
tb = w[i].title
|
|
|
|
else
|
|
|
|
ib = capi.widget({ type = "imagebox" })
|
|
|
|
tb = capi.widget({ type = "textbox" })
|
|
|
|
w[i] = util.table.clone(template)
|
|
|
|
replace_in_template(w[i], ib, tb)
|
|
|
|
w[i].icon = ib
|
|
|
|
w[i].title = tb
|
|
|
|
|
|
|
|
if type(o) == "tag" then
|
|
|
|
tagwidgets[ib] = o
|
|
|
|
tagwidgets[tb] = o
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-02-27 17:22:03 +01:00
|
|
|
if buttons then
|
|
|
|
if not data[o] then
|
|
|
|
data[o] = { }
|
|
|
|
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 }
|
|
|
|
btn:add_signal("press", function () b:emit_signal("press", o) end)
|
|
|
|
btn:add_signal("release", function () b:emit_signal("release", o) end)
|
|
|
|
data[o][#data[o] + 1] = btn
|
2009-02-27 17:22:03 +01:00
|
|
|
end
|
|
|
|
end
|
2009-10-24 14:53:01 +02:00
|
|
|
ib:buttons(data[o])
|
|
|
|
tb:buttons(data[o])
|
2009-02-27 17:22:03 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
local text, bg, bg_image, icon = label(o)
|
2009-10-24 14:53:01 +02:00
|
|
|
tb.text, tb.bg, tb.bg_image = text, bg, bg_image
|
|
|
|
ib.bg, ib.image = bg, icon
|
|
|
|
if not tb.text then
|
|
|
|
tb.visible = false
|
2009-02-27 17:22:03 +01:00
|
|
|
else
|
2009-10-24 14:53:01 +02:00
|
|
|
tb.visible = true
|
2009-02-27 17:22:03 +01:00
|
|
|
end
|
2009-10-24 14:53:01 +02:00
|
|
|
if not ib.image then
|
|
|
|
ib.visible = false
|
2009-02-27 17:22:03 +01:00
|
|
|
else
|
2009-10-24 14:53:01 +02:00
|
|
|
ib.visible = true
|
2009-02-27 17:22:03 +01:00
|
|
|
end
|
|
|
|
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
|