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.imagebox
|
2010-10-06 12:42:56 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local base = require("wibox.widget.base")
|
2012-05-27 19:20:34 +02:00
|
|
|
local surface = require("gears.surface")
|
2010-10-06 12:42:56 +02:00
|
|
|
local setmetatable = setmetatable
|
|
|
|
local pairs = pairs
|
|
|
|
local type = type
|
|
|
|
local pcall = pcall
|
|
|
|
local print = print
|
|
|
|
|
2012-06-12 15:55:10 +02:00
|
|
|
local imagebox = { mt = {} }
|
2010-10-06 12:42:56 +02:00
|
|
|
|
|
|
|
--- Draw an imagebox with the given cairo context in the given geometry.
|
2012-11-18 20:44:03 +01:00
|
|
|
function imagebox:draw(wibox, cr, width, height)
|
2012-11-27 17:24:54 +01:00
|
|
|
if not self._image then return end
|
2014-04-02 22:48:06 +02:00
|
|
|
if width == 0 or height == 0 then return end
|
2010-10-06 12:42:56 +02:00
|
|
|
|
|
|
|
cr:save()
|
|
|
|
|
2012-11-18 20:44:03 +01:00
|
|
|
if not self.resize_forbidden then
|
2010-10-06 12:42:56 +02:00
|
|
|
-- Let's scale the image so that it fits into (width, height)
|
2012-11-27 17:24:54 +01:00
|
|
|
local w = self._image:get_width()
|
|
|
|
local h = self._image:get_height()
|
2010-10-06 12:42:56 +02:00
|
|
|
local aspect = width / w
|
|
|
|
local aspect_h = height / h
|
|
|
|
if aspect > aspect_h then aspect = aspect_h end
|
|
|
|
|
|
|
|
cr:scale(aspect, aspect)
|
|
|
|
end
|
2012-11-27 17:24:54 +01:00
|
|
|
cr:set_source_surface(self._image, 0, 0)
|
2010-10-06 12:42:56 +02:00
|
|
|
cr:paint()
|
|
|
|
|
|
|
|
cr:restore()
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Fit the imagebox into the given geometry
|
2012-11-18 20:44:03 +01:00
|
|
|
function imagebox:fit(width, height)
|
2012-11-27 17:24:54 +01:00
|
|
|
if not self._image then
|
2010-10-06 12:42:56 +02:00
|
|
|
return 0, 0
|
|
|
|
end
|
|
|
|
|
2012-11-27 17:24:54 +01:00
|
|
|
local w = self._image:get_width()
|
|
|
|
local h = self._image:get_height()
|
2010-10-06 12:42:56 +02:00
|
|
|
|
|
|
|
if w > width then
|
|
|
|
h = h * width / w
|
|
|
|
w = width
|
|
|
|
end
|
|
|
|
if h > height then
|
|
|
|
w = w * height / h
|
|
|
|
h = height
|
|
|
|
end
|
|
|
|
|
2014-03-23 17:39:42 +01:00
|
|
|
if h == 0 or w == 0 then
|
|
|
|
return 0, 0
|
|
|
|
end
|
|
|
|
|
2012-11-18 20:44:03 +01:00
|
|
|
if not self.resize_forbidden then
|
2010-10-06 12:42:56 +02:00
|
|
|
local aspect = width / w
|
|
|
|
local aspect_h = height / h
|
|
|
|
|
|
|
|
-- Use the smaller one of the two aspect ratios.
|
|
|
|
if aspect > aspect_h then aspect = aspect_h end
|
|
|
|
|
|
|
|
w, h = w * aspect, h * aspect
|
|
|
|
end
|
|
|
|
|
|
|
|
return w, h
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Set an imagebox' image
|
|
|
|
-- @param image Either a string or a cairo image surface. A string is
|
|
|
|
-- interpreted as the path to a png image file.
|
2012-11-18 20:44:03 +01:00
|
|
|
function imagebox:set_image(image)
|
2010-10-06 12:42:56 +02:00
|
|
|
local image = image
|
|
|
|
|
|
|
|
if type(image) == "string" then
|
2012-05-27 19:20:34 +02:00
|
|
|
local success, result = pcall(surface.load, image)
|
2010-10-06 12:42:56 +02:00
|
|
|
if not success then
|
|
|
|
print("Error while reading '" .. image .. "': " .. result)
|
2015-02-09 20:20:18 +01:00
|
|
|
print(debug.traceback())
|
2010-10-06 12:42:56 +02:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
image = result
|
|
|
|
end
|
|
|
|
|
2012-05-27 19:20:34 +02:00
|
|
|
image = surface.load(image)
|
|
|
|
|
2011-03-04 22:03:42 +01:00
|
|
|
if image then
|
2012-05-27 19:20:34 +02:00
|
|
|
local w = image.width
|
|
|
|
local h = image.height
|
2011-03-04 22:03:42 +01:00
|
|
|
if w <= 0 or h <= 0 then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-27 17:24:54 +01:00
|
|
|
self._image = image
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2012-11-18 20:44:03 +01:00
|
|
|
self:emit_signal("widget::updated")
|
2010-10-06 12:42:56 +02:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Should the image be resized to fit into the available space?
|
|
|
|
-- @param allowed If false, the image will be clipped, else it will be resized
|
|
|
|
-- to fit into the available space.
|
2012-11-18 20:44:03 +01:00
|
|
|
function imagebox:set_resize(allowed)
|
|
|
|
self.resize_forbidden = not allowed
|
|
|
|
self:emit_signal("widget::updated")
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
2015-02-20 15:45:53 +01:00
|
|
|
--- Returns a new imagebox
|
2012-11-25 19:16:31 +01:00
|
|
|
-- @param image the image to display, may be nil
|
|
|
|
-- @param resize_allowed If false, the image will be clipped, else it will be resized
|
|
|
|
-- to fit into the available space.
|
|
|
|
local function new(image, resize_allowed)
|
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(imagebox) do
|
2010-10-06 12:42:56 +02:00
|
|
|
if type(v) == "function" then
|
|
|
|
ret[k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-25 19:16:31 +01:00
|
|
|
if image then
|
|
|
|
ret:set_image(image)
|
|
|
|
end
|
|
|
|
if resize_allowed ~= nil then
|
|
|
|
ret:set_resize(resize_allowed)
|
|
|
|
end
|
|
|
|
|
2010-10-06 12:42:56 +02:00
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2012-06-12 15:55:10 +02:00
|
|
|
function imagebox.mt:__call(...)
|
|
|
|
return new(...)
|
|
|
|
end
|
|
|
|
|
|
|
|
return setmetatable(imagebox, imagebox.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
|