2010-10-06 12:42:56 +02:00
|
|
|
---------------------------------------------------------------------------
|
2016-05-25 23:56:23 +02:00
|
|
|
--
|
|
|
|
--@DOC_wibox_widget_defaults_imagebox_EXAMPLE@
|
2010-10-06 12:42:56 +02:00
|
|
|
-- @author Uli Schlachter
|
|
|
|
-- @copyright 2010 Uli Schlachter
|
2019-06-06 08:15:53 +02:00
|
|
|
-- @widgetmod 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")
|
2017-03-08 21:18:33 +01:00
|
|
|
local gtable = require("gears.table")
|
2010-10-06 12:42:56 +02:00
|
|
|
local setmetatable = setmetatable
|
|
|
|
local type = type
|
|
|
|
local print = print
|
2016-02-07 14:13:43 +01:00
|
|
|
local unpack = unpack or table.unpack -- luacheck: globals unpack (compatibility with Lua 5.1)
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2012-06-12 15:55:10 +02:00
|
|
|
local imagebox = { mt = {} }
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2016-05-23 09:02:11 +02:00
|
|
|
-- Draw an imagebox with the given cairo context in the given geometry.
|
2016-02-07 14:13:43 +01:00
|
|
|
function imagebox:draw(_, cr, width, height)
|
2016-05-26 23:39:27 +02:00
|
|
|
if not self._private.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
|
|
|
|
2019-06-13 01:39:01 +02:00
|
|
|
-- Set the clip
|
|
|
|
if self._private.clip_shape then
|
|
|
|
cr:clip(self._private.clip_shape(cr, width, height, unpack(self._private.clip_args)))
|
|
|
|
end
|
|
|
|
|
2016-05-26 23:39:27 +02:00
|
|
|
if not self._private.resize_forbidden then
|
2010-10-06 12:42:56 +02:00
|
|
|
-- Let's scale the image so that it fits into (width, height)
|
2016-05-26 23:39:27 +02:00
|
|
|
local w = self._private.image:get_width()
|
|
|
|
local h = self._private.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
|
2016-01-20 08:09:07 +01:00
|
|
|
|
2016-05-26 23:39:27 +02:00
|
|
|
cr:set_source_surface(self._private.image, 0, 0)
|
2010-10-06 12:42:56 +02:00
|
|
|
cr:paint()
|
|
|
|
end
|
|
|
|
|
2016-05-23 09:02:11 +02:00
|
|
|
-- Fit the imagebox into the given geometry
|
2016-02-07 14:13:43 +01:00
|
|
|
function imagebox:fit(_, width, height)
|
2016-05-26 23:39:27 +02:00
|
|
|
if not self._private.image then
|
2010-10-06 12:42:56 +02:00
|
|
|
return 0, 0
|
|
|
|
end
|
|
|
|
|
2016-05-26 23:39:27 +02:00
|
|
|
local w = self._private.image:get_width()
|
|
|
|
local h = self._private.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
|
|
|
|
|
2016-05-26 23:39:27 +02:00
|
|
|
if not self._private.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
|
2016-05-26 23:39:27 +02:00
|
|
|
-- @property image
|
2010-10-06 12:42:56 +02:00
|
|
|
-- @param image Either a string or a cairo image surface. A string is
|
2015-10-14 00:21:29 +02:00
|
|
|
-- interpreted as the path to a png image file.
|
2016-01-15 18:47:09 +01:00
|
|
|
-- @return true on success, false if the image cannot be used
|
2016-05-26 23:39:27 +02:00
|
|
|
|
2012-11-18 20:44:03 +01:00
|
|
|
function imagebox:set_image(image)
|
2010-10-06 12:42:56 +02:00
|
|
|
if type(image) == "string" then
|
2016-01-15 18:47:09 +01:00
|
|
|
image = surface.load(image)
|
|
|
|
if not image then
|
2015-02-09 20:20:18 +01:00
|
|
|
print(debug.traceback())
|
2010-10-06 12:42:56 +02:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-25 06:07:55 +01:00
|
|
|
image = image and surface.load(image)
|
2012-05-27 19:20:34 +02:00
|
|
|
|
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
|
|
|
|
|
2016-05-26 23:39:27 +02:00
|
|
|
if self._private.image == image then
|
2015-09-27 16:48:54 +02:00
|
|
|
-- The image could have been modified, so better redraw
|
|
|
|
self:emit_signal("widget::redraw_needed")
|
2017-04-06 20:33:14 +02:00
|
|
|
return true
|
2015-09-06 13:18:59 +02:00
|
|
|
end
|
|
|
|
|
2016-05-26 23:39:27 +02:00
|
|
|
self._private.image = image
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2015-06-14 16:36:00 +02:00
|
|
|
self:emit_signal("widget::redraw_needed")
|
|
|
|
self:emit_signal("widget::layout_changed")
|
2010-10-06 12:42:56 +02:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2016-05-26 23:39:27 +02:00
|
|
|
--- Set a clip shape for this imagebox
|
|
|
|
-- A clip shape define an area where the content is displayed and one where it
|
|
|
|
-- is trimmed.
|
|
|
|
--
|
|
|
|
-- @property clip_shape
|
2019-06-08 06:15:59 +02:00
|
|
|
-- @tparam gears.shape clip_shape A `gears_shape` compatible shape function
|
2016-05-26 23:39:27 +02:00
|
|
|
-- @see gears.shape
|
|
|
|
-- @see set_clip_shape
|
|
|
|
|
2016-01-20 08:09:07 +01:00
|
|
|
--- Set a clip shape for this imagebox
|
|
|
|
-- A clip shape define an area where the content is displayed and one where it
|
|
|
|
-- is trimmed.
|
|
|
|
--
|
|
|
|
-- Any other parameters will be passed to the clip shape function
|
|
|
|
--
|
2019-06-06 22:32:53 +02:00
|
|
|
-- @tparam function clip_shape A `gears_shape` compatible shape function.
|
|
|
|
-- @method set_clip_shape
|
2016-05-26 23:39:27 +02:00
|
|
|
-- @see gears.shape
|
|
|
|
-- @see clip_shape
|
2016-01-20 08:09:07 +01:00
|
|
|
function imagebox:set_clip_shape(clip_shape, ...)
|
2016-05-26 23:39:27 +02:00
|
|
|
self._private.clip_shape = clip_shape
|
|
|
|
self._private.clip_args = {...}
|
2016-01-20 08:09:07 +01:00
|
|
|
self:emit_signal("widget::redraw_needed")
|
|
|
|
end
|
|
|
|
|
2010-10-06 12:42:56 +02:00
|
|
|
--- Should the image be resized to fit into the available space?
|
2016-05-26 23:39:27 +02:00
|
|
|
-- @property resize
|
2010-10-06 12:42:56 +02:00
|
|
|
-- @param allowed If false, the image will be clipped, else it will be resized
|
2015-10-14 00:21:29 +02:00
|
|
|
-- to fit into the available space.
|
2016-05-26 23:39:27 +02:00
|
|
|
|
2012-11-18 20:44:03 +01:00
|
|
|
function imagebox:set_resize(allowed)
|
2016-05-26 23:39:27 +02:00
|
|
|
self._private.resize_forbidden = not allowed
|
2015-06-14 16:36:00 +02:00
|
|
|
self:emit_signal("widget::redraw_needed")
|
|
|
|
self:emit_signal("widget::layout_changed")
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
2016-05-23 08:54:01 +02:00
|
|
|
--- Returns a new imagebox.
|
2016-01-20 08:09:07 +01:00
|
|
|
-- Any other arguments will be passed to the clip shape function
|
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
|
2015-10-14 00:21:29 +02:00
|
|
|
-- to fit into the available space.
|
2016-01-20 08:09:07 +01:00
|
|
|
-- @param clip_shape A `gears.shape` compatible function
|
2016-05-23 08:54:01 +02:00
|
|
|
-- @treturn table A new `imagebox`
|
2019-06-07 20:59:34 +02:00
|
|
|
-- @constructorfct wibox.widget.imagebox
|
2016-01-20 08:09:07 +01:00
|
|
|
local function new(image, resize_allowed, clip_shape)
|
2016-05-26 23:39:27 +02:00
|
|
|
local ret = base.make_widget(nil, nil, {enable_properties = true})
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2017-03-08 21:18:33 +01:00
|
|
|
gtable.crush(ret, imagebox, true)
|
2010-10-06 12:42:56 +02:00
|
|
|
|
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
|
|
|
|
|
2016-05-26 23:39:27 +02:00
|
|
|
ret._private.clip_shape = clip_shape
|
|
|
|
ret._private.clip_args = {}
|
2016-01-20 08:09:07 +01:00
|
|
|
|
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
|
|
|
|
|
2016-05-26 23:39:27 +02:00
|
|
|
--@DOC_widget_COMMON@
|
|
|
|
|
|
|
|
--@DOC_object_COMMON@
|
|
|
|
|
2012-06-12 15:55:10 +02:00
|
|
|
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
|