2010-10-06 12:42:56 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
-- @author Uli Schlachter
|
|
|
|
-- @copyright 2010 Uli Schlachter
|
|
|
|
-- @release @AWESOME_VERSION@
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
2010-10-22 14:50:45 +02:00
|
|
|
local debug = require("gears.debug")
|
2010-10-06 12:42:56 +02:00
|
|
|
local object = require("gears.object")
|
|
|
|
local pairs = pairs
|
|
|
|
local type = type
|
|
|
|
local table = table
|
|
|
|
|
2012-06-12 15:55:10 +02:00
|
|
|
-- wibox.widget.base
|
|
|
|
local base = {}
|
2010-10-06 12:42:56 +02:00
|
|
|
|
|
|
|
--- Set/get a widget's buttons
|
2012-11-18 20:44:03 +01:00
|
|
|
function base:buttons(_buttons)
|
2012-06-12 15:55:10 +02:00
|
|
|
if _buttons then
|
2012-11-18 20:44:03 +01:00
|
|
|
self.widget_buttons = _buttons
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
2012-11-18 20:44:03 +01:00
|
|
|
return self.widget_buttons
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Handle a button event on a widget. This is used internally.
|
2012-06-12 15:55:10 +02:00
|
|
|
function base.handle_button(event, widget, x, y, button, modifiers)
|
2010-10-06 12:42:56 +02:00
|
|
|
local function is_any(mod)
|
2012-05-13 00:23:23 +02:00
|
|
|
return #mod == 1 and mod[1] == "Any"
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function tables_equal(a, b)
|
|
|
|
if #a ~= #b then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
for k, v in pairs(b) do
|
|
|
|
if a[k] ~= v then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Find all matching button objects
|
|
|
|
local matches = {}
|
|
|
|
for k, v in pairs(widget.widget_buttons) do
|
|
|
|
local match = true
|
|
|
|
-- Is it the right button?
|
|
|
|
if v.button ~= 0 and v.button ~= button then match = false end
|
|
|
|
-- Are the correct modifiers pressed?
|
|
|
|
if (not is_any(v.modifiers)) and (not tables_equal(v.modifiers, modifiers)) then match = false end
|
|
|
|
if match then
|
|
|
|
table.insert(matches, v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Emit the signals
|
|
|
|
for k, v in pairs(matches) do
|
|
|
|
v:emit_signal(event)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Create a new widget. All widgets have to be generated via this function so
|
|
|
|
-- that the needed signals are added and mouse input handling is set up.
|
|
|
|
-- @param proxy If this is set, the returned widget will be a proxy for this
|
|
|
|
-- widget. It will be equivalent to this widget.
|
2012-06-12 15:55:10 +02:00
|
|
|
function base.make_widget(proxy)
|
2010-10-06 12:42:56 +02:00
|
|
|
local ret = object()
|
|
|
|
|
|
|
|
-- This signal is used by layouts to find out when they have to update.
|
|
|
|
ret:add_signal("widget::updated")
|
|
|
|
-- Mouse input, oh noes!
|
|
|
|
ret:add_signal("button::press")
|
|
|
|
ret:add_signal("button::release")
|
2010-10-06 21:17:05 +02:00
|
|
|
ret:add_signal("mouse::enter")
|
|
|
|
ret:add_signal("mouse::leave")
|
2010-10-06 12:42:56 +02:00
|
|
|
|
|
|
|
-- No buttons yet
|
|
|
|
ret.widget_buttons = {}
|
2012-06-12 15:55:10 +02:00
|
|
|
ret.buttons = base.buttons
|
2010-10-06 12:42:56 +02:00
|
|
|
|
|
|
|
-- Make buttons work
|
|
|
|
ret:connect_signal("button::press", function(...)
|
2012-06-12 15:55:10 +02:00
|
|
|
return base.handle_button("press", ...)
|
2010-10-06 12:42:56 +02:00
|
|
|
end)
|
|
|
|
ret:connect_signal("button::release", function(...)
|
2012-06-12 15:55:10 +02:00
|
|
|
return base.handle_button("release", ...)
|
2010-10-06 12:42:56 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
if proxy then
|
|
|
|
ret.draw = function(_, ...) return proxy:draw(...) end
|
|
|
|
ret.fit = function(_, ...) return proxy:fit(...) end
|
|
|
|
proxy:connect_signal("widget::updated", function()
|
|
|
|
ret:emit_signal("widget::updated")
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2012-11-27 22:55:42 +01:00
|
|
|
--- Generate an empty widget which takes no space and displays nothing
|
|
|
|
function base.empty_widget()
|
|
|
|
local widget = base.make_widget()
|
|
|
|
widget.draw = function() end
|
|
|
|
widget.fit = function() return 0, 0 end
|
|
|
|
return widget
|
|
|
|
end
|
|
|
|
|
2010-10-06 12:42:56 +02:00
|
|
|
--- Do some sanity checking on widget. This function raises a lua error if
|
|
|
|
-- widget is not a valid widget.
|
2012-06-12 15:55:10 +02:00
|
|
|
function base.check_widget(widget)
|
2010-10-22 14:50:45 +02:00
|
|
|
debug.assert(type(widget) == "table")
|
2010-10-06 12:42:56 +02:00
|
|
|
for k, func in pairs({ "draw", "fit", "add_signal", "connect_signal", "disconnect_signal" }) do
|
2010-10-22 14:50:45 +02:00
|
|
|
debug.assert(type(widget[func]) == "function", func .. " is not a function")
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local width, height = widget:fit(0, 0)
|
2010-10-22 14:50:45 +02:00
|
|
|
debug.assert(type(width) == "number")
|
|
|
|
debug.assert(type(height) == "number")
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
2012-06-12 15:55:10 +02:00
|
|
|
return base
|
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|