2010-10-06 12:42:56 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
-- @author Uli Schlachter
|
|
|
|
-- @copyright 2010 Uli Schlachter
|
|
|
|
-- @release @AWESOME_VERSION@
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local base = require("wibox.widget.base")
|
|
|
|
local color = require("gears.color")
|
2010-10-06 18:45:13 +02:00
|
|
|
local layout_base = require("wibox.layout.base")
|
2012-05-27 19:20:34 +02:00
|
|
|
local surface = require("gears.surface")
|
|
|
|
local cairo = require("lgi").cairo
|
2010-10-06 12:42:56 +02:00
|
|
|
local setmetatable = setmetatable
|
|
|
|
local pairs = pairs
|
|
|
|
local type = type
|
|
|
|
|
2012-06-12 15:55:10 +02:00
|
|
|
-- wibox.widget.background
|
|
|
|
local background = { mt = {} }
|
2010-10-06 12:42:56 +02:00
|
|
|
|
|
|
|
--- Draw this widget
|
2012-06-12 15:55:10 +02:00
|
|
|
function background.draw(box, wibox, cr, width, height)
|
2010-10-06 12:42:56 +02:00
|
|
|
if not box.widget then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
cr:save()
|
|
|
|
|
|
|
|
if box.background then
|
|
|
|
cr:set_source(box.background)
|
|
|
|
cr:paint()
|
|
|
|
end
|
|
|
|
if box.bgimage then
|
2012-05-27 19:20:34 +02:00
|
|
|
local pattern = cairo.Pattern.create_for_surface(box.bgimage)
|
2010-10-06 12:42:56 +02:00
|
|
|
cr:set_source(pattern)
|
|
|
|
cr:paint()
|
|
|
|
end
|
|
|
|
|
|
|
|
cr:restore()
|
|
|
|
|
2011-03-30 04:00:24 +02:00
|
|
|
if box.foreground then
|
|
|
|
cr:save()
|
|
|
|
cr:set_source(box.foreground)
|
|
|
|
end
|
2010-10-06 18:45:13 +02:00
|
|
|
layout_base.draw_widget(wibox, cr, box.widget, 0, 0, width, height)
|
2011-03-30 04:00:24 +02:00
|
|
|
if box.foreground then
|
|
|
|
cr:restore()
|
|
|
|
end
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Fit this widget into the given area
|
2012-06-12 15:55:10 +02:00
|
|
|
function background.fit(box, width, height)
|
2010-10-06 12:42:56 +02:00
|
|
|
if not box.widget then
|
|
|
|
return 0, 0
|
|
|
|
end
|
|
|
|
|
|
|
|
return box.widget:fit(width, height)
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Set the widget that is drawn on top of the background
|
2012-06-12 15:55:10 +02:00
|
|
|
function background.set_widget(box, widget)
|
2010-10-06 12:42:56 +02:00
|
|
|
if box.widget then
|
|
|
|
box.widget:disconnect_signal("widget::updated", box._emit_updated)
|
|
|
|
end
|
|
|
|
if widget then
|
2010-10-06 18:45:13 +02:00
|
|
|
base.check_widget(widget)
|
2010-10-06 12:42:56 +02:00
|
|
|
widget:connect_signal("widget::updated", box._emit_updated)
|
|
|
|
end
|
|
|
|
box.widget = widget
|
|
|
|
box._emit_updated()
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Set the background to use
|
2012-06-12 15:55:10 +02:00
|
|
|
function background.set_bg(box, bg)
|
2010-10-06 12:42:56 +02:00
|
|
|
if bg then
|
|
|
|
box.background = color(bg)
|
|
|
|
else
|
|
|
|
box.background = nil
|
|
|
|
end
|
|
|
|
box._emit_updated()
|
|
|
|
end
|
|
|
|
|
2011-03-30 04:00:24 +02:00
|
|
|
--- Set the foreground to use
|
2012-06-12 15:55:10 +02:00
|
|
|
function background.set_fg(box, fg)
|
2011-03-30 04:00:24 +02:00
|
|
|
if fg then
|
|
|
|
box.foreground = color(fg)
|
|
|
|
else
|
|
|
|
box.foreground = nil
|
|
|
|
end
|
|
|
|
box._emit_updated()
|
|
|
|
end
|
|
|
|
|
2010-10-06 12:42:56 +02:00
|
|
|
--- Set the background image to use
|
2012-06-12 15:55:10 +02:00
|
|
|
function background.set_bgimage(box, image)
|
2012-05-27 19:20:34 +02:00
|
|
|
box.bgimage = surface.load(image)
|
2010-10-06 12:42:56 +02:00
|
|
|
box._emit_updated()
|
|
|
|
end
|
|
|
|
|
|
|
|
local function new()
|
|
|
|
local ret = base.make_widget()
|
|
|
|
|
2012-06-12 15:55:10 +02:00
|
|
|
for k, v in pairs(background) do
|
2010-10-06 12:42:56 +02:00
|
|
|
if type(v) == "function" then
|
|
|
|
ret[k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
ret._emit_updated = function()
|
|
|
|
ret:emit_signal("widget::updated")
|
|
|
|
end
|
|
|
|
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2012-06-12 15:55:10 +02:00
|
|
|
function background.mt:__call(...)
|
|
|
|
return new(...)
|
|
|
|
end
|
|
|
|
|
|
|
|
return setmetatable(background, background.mt)
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|