2010-10-06 12:42:56 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
-- @author Uli Schlachter
|
|
|
|
-- @copyright 2010 Uli Schlachter
|
|
|
|
-- @release @AWESOME_VERSION@
|
2015-02-25 11:18:53 +01:00
|
|
|
-- @classmod wibox.widget.background
|
2010-10-06 12:42:56 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
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
|
|
|
local background = { mt = {} }
|
2010-10-06 12:42:56 +02:00
|
|
|
|
|
|
|
--- Draw this widget
|
2012-11-18 20:44:03 +01:00
|
|
|
function background:draw(wibox, cr, width, height)
|
|
|
|
if not self.widget then
|
2010-10-06 12:42:56 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
cr:save()
|
|
|
|
|
2012-11-18 20:44:03 +01:00
|
|
|
if self.background then
|
|
|
|
cr:set_source(self.background)
|
2010-10-06 12:42:56 +02:00
|
|
|
cr:paint()
|
|
|
|
end
|
2012-11-18 20:44:03 +01:00
|
|
|
if self.bgimage then
|
|
|
|
local pattern = cairo.Pattern.create_for_surface(self.bgimage)
|
2010-10-06 12:42:56 +02:00
|
|
|
cr:set_source(pattern)
|
|
|
|
cr:paint()
|
|
|
|
end
|
|
|
|
|
|
|
|
cr:restore()
|
|
|
|
|
2012-11-18 20:44:03 +01:00
|
|
|
if self.foreground then
|
2011-03-30 04:00:24 +02:00
|
|
|
cr:save()
|
2012-11-18 20:44:03 +01:00
|
|
|
cr:set_source(self.foreground)
|
2011-03-30 04:00:24 +02:00
|
|
|
end
|
2012-11-18 20:44:03 +01:00
|
|
|
layout_base.draw_widget(wibox, cr, self.widget, 0, 0, width, height)
|
|
|
|
if self.foreground then
|
2011-03-30 04:00:24 +02:00
|
|
|
cr:restore()
|
|
|
|
end
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Fit this widget into the given area
|
2012-11-18 20:44:03 +01:00
|
|
|
function background:fit(width, height)
|
|
|
|
if not self.widget then
|
2010-10-06 12:42:56 +02:00
|
|
|
return 0, 0
|
|
|
|
end
|
|
|
|
|
2012-11-18 20:44:03 +01:00
|
|
|
return self.widget:fit(width, height)
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Set the widget that is drawn on top of the background
|
2012-11-18 20:44:03 +01:00
|
|
|
function background:set_widget(widget)
|
|
|
|
if self.widget then
|
|
|
|
self.widget:disconnect_signal("widget::updated", self._emit_updated)
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
if widget then
|
2010-10-06 18:45:13 +02:00
|
|
|
base.check_widget(widget)
|
2012-11-18 20:44:03 +01:00
|
|
|
widget:connect_signal("widget::updated", self._emit_updated)
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
2012-11-18 20:44:03 +01:00
|
|
|
self.widget = widget
|
|
|
|
self._emit_updated()
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Set the background to use
|
2012-11-18 20:44:03 +01:00
|
|
|
function background:set_bg(bg)
|
2010-10-06 12:42:56 +02:00
|
|
|
if bg then
|
2012-11-18 20:44:03 +01:00
|
|
|
self.background = color(bg)
|
2010-10-06 12:42:56 +02:00
|
|
|
else
|
2012-11-18 20:44:03 +01:00
|
|
|
self.background = nil
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
2012-11-18 20:44:03 +01:00
|
|
|
self._emit_updated()
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
2011-03-30 04:00:24 +02:00
|
|
|
--- Set the foreground to use
|
2012-11-18 20:44:03 +01:00
|
|
|
function background:set_fg(fg)
|
2011-03-30 04:00:24 +02:00
|
|
|
if fg then
|
2012-11-18 20:44:03 +01:00
|
|
|
self.foreground = color(fg)
|
2011-03-30 04:00:24 +02:00
|
|
|
else
|
2012-11-18 20:44:03 +01:00
|
|
|
self.foreground = nil
|
2011-03-30 04:00:24 +02:00
|
|
|
end
|
2012-11-18 20:44:03 +01:00
|
|
|
self._emit_updated()
|
2011-03-30 04:00:24 +02:00
|
|
|
end
|
|
|
|
|
2010-10-06 12:42:56 +02:00
|
|
|
--- Set the background image to use
|
2012-11-18 20:44:03 +01:00
|
|
|
function background:set_bgimage(image)
|
|
|
|
self.bgimage = surface.load(image)
|
|
|
|
self._emit_updated()
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
2013-03-10 13:46:28 +01:00
|
|
|
--- Returns a new background layout. A background layout applies a background
|
|
|
|
-- and foreground color to another widget.
|
|
|
|
-- @param widget The widget to display (optional)
|
|
|
|
-- @param bg The background to use for that widget (optional)
|
|
|
|
local function new(widget, bg)
|
2010-10-06 12:42:56 +02:00
|
|
|
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
|
|
|
|
|
2012-11-25 19:16:31 +01:00
|
|
|
ret:set_widget(widget)
|
2013-03-10 13:46:28 +01:00
|
|
|
ret:set_bg(bg)
|
2012-11-25 19:16:31 +01:00
|
|
|
|
2010-10-06 12:42:56 +02:00
|
|
|
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
|