2010-10-06 12:42:56 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
-- @author Uli Schlachter
|
|
|
|
-- @copyright 2010 Uli Schlachter
|
|
|
|
-- @release @AWESOME_VERSION@
|
2014-05-20 13:02:13 +02:00
|
|
|
-- @module wibox.layout.base
|
2010-10-06 12:42:56 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local pairs = pairs
|
|
|
|
local pcall = pcall
|
|
|
|
local print = print
|
2013-01-05 20:50:17 +01:00
|
|
|
local min = math.min
|
|
|
|
local max = math.max
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2012-06-12 15:29:52 +02:00
|
|
|
local base = {}
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2014-10-05 10:47:39 +02:00
|
|
|
--- Figure out the geometry in device coordinate space. This gives only tight
|
|
|
|
-- bounds if no rotations by non-multiples of 90° are used.
|
2012-06-12 15:29:52 +02:00
|
|
|
function base.rect_to_device_geometry(cr, x, y, width, height)
|
2010-10-06 12:42:56 +02:00
|
|
|
local x1, y1 = cr:user_to_device(x, y)
|
2014-10-05 10:47:39 +02:00
|
|
|
local x2, y2 = cr:user_to_device(x, y + height)
|
|
|
|
local x3, y3 = cr:user_to_device(x + width, y + height)
|
|
|
|
local x4, y4 = cr:user_to_device(x + width, y)
|
|
|
|
local x = min(x1, x2, x3, x4)
|
|
|
|
local y = min(y1, y2, y3, y4)
|
|
|
|
local width = max(x1, x2, x3, x4) - x
|
|
|
|
local height = max(y1, y2, y3, y4) - y
|
2010-10-06 12:42:56 +02:00
|
|
|
|
|
|
|
return x, y, width, height
|
|
|
|
end
|
|
|
|
|
2013-08-21 12:26:47 +02:00
|
|
|
--- Fit a widget for the given available width and height
|
|
|
|
-- @param widget The widget to fit (this uses widget:fit(width, height)).
|
|
|
|
-- @param width The available width for the widget
|
|
|
|
-- @param height The available height for the widget
|
|
|
|
-- @return The width and height that the widget wants to use
|
|
|
|
function base.fit_widget(widget, width, height)
|
2014-03-23 17:48:26 +01:00
|
|
|
-- Sanitize the input. This also filters out e.g. NaN.
|
|
|
|
local width = math.max(0, width)
|
|
|
|
local height = math.max(0, height)
|
|
|
|
|
|
|
|
-- Since the geometry cache is a weak table, we have to be careful when
|
|
|
|
-- doing lookups. We can't do "if cache[width] ~= nil then"!
|
2013-08-21 12:26:47 +02:00
|
|
|
local cache = widget._fit_geometry_cache
|
|
|
|
local result = cache[width]
|
|
|
|
if not result then
|
|
|
|
result = {}
|
|
|
|
cache[width] = result
|
|
|
|
end
|
|
|
|
cache, result = result, result[height]
|
|
|
|
if not result then
|
|
|
|
local w, h = widget:fit(width, height)
|
|
|
|
result = { width = w, height = h }
|
|
|
|
cache[height] = result
|
|
|
|
end
|
|
|
|
return result.width, result.height
|
|
|
|
end
|
|
|
|
|
2010-10-06 12:42:56 +02:00
|
|
|
--- Draw a widget via a cairo context
|
|
|
|
-- @param wibox The wibox on which we are drawing
|
|
|
|
-- @param cr The cairo context used
|
|
|
|
-- @param widget The widget to draw (this uses widget:draw(cr, width, height)).
|
|
|
|
-- @param x The position that the widget should get
|
|
|
|
-- @param y The position that the widget should get
|
|
|
|
-- @param width The widget's width
|
|
|
|
-- @param height The widget's height
|
2012-06-12 15:29:52 +02:00
|
|
|
function base.draw_widget(wibox, cr, widget, x, y, width, height)
|
2010-10-06 12:42:56 +02:00
|
|
|
-- Use save() / restore() so that our modifications aren't permanent
|
|
|
|
cr:save()
|
|
|
|
|
|
|
|
-- Move (0, 0) to the place where the widget should show up
|
|
|
|
cr:translate(x, y)
|
|
|
|
|
|
|
|
-- Make sure the widget cannot draw outside of the allowed area
|
|
|
|
cr:rectangle(0, 0, width, height)
|
|
|
|
cr:clip()
|
|
|
|
|
|
|
|
-- Let the widget draw itself
|
|
|
|
local success, msg = pcall(widget.draw, widget, wibox, cr, width, height)
|
|
|
|
if not success then
|
|
|
|
print("Error while drawing widget: " .. msg)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Register the widget for input handling
|
2012-06-12 15:29:52 +02:00
|
|
|
wibox:widget_at(widget, base.rect_to_device_geometry(cr, 0, 0, width, height))
|
2010-10-06 12:42:56 +02:00
|
|
|
|
|
|
|
cr:restore()
|
|
|
|
end
|
|
|
|
|
2012-06-12 15:29:52 +02:00
|
|
|
return base
|
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|