awesome/lib/wibox/widget/textbox.lua.in

168 lines
4.7 KiB
Lua
Raw Normal View History

---------------------------------------------------------------------------
-- @author Uli Schlachter
-- @copyright 2010 Uli Schlachter
-- @release @AWESOME_VERSION@
---------------------------------------------------------------------------
local oopango = require("oopango")
local oocairo = require("oocairo")
local base = require("wibox.widget.base")
local beautiful = require("beautiful")
local type = type
local unpack = unpack
local setmetatable = setmetatable
local pairs = pairs
module("wibox.widget.textbox")
-- Setup a pango layout for the given textbox and cairo context
local function setup_layout(box, cr, width, height)
local layout = oopango.cairo.layout_create(cr)
layout:set_ellipsize(box.ellipsize)
layout:set_wrap(box.wrap)
layout:set_width(oopango.units_from_number(width))
layout:set_height(oopango.units_from_number(height))
if box.markup then
layout:set_markup(box.text)
else
layout:set_text(box.text)
end
if box.font then
layout:set_font_description(box.font)
else
layout:set_font_description(beautiful.get_font())
end
local ink, logical = layout:get_pixel_extents()
local offset = 0
if box.valign == "center" then
offset = (height - logical.height) / 2
elseif box.valign == "bottom" then
offset = height - logical.height
end
if offset > 0 then
cr:move_to(0, offset)
end
return layout
end
-- Get the size that the given textbox covers.
-- If layout is given, it's :get_width()/get_height() is honoured.
local function get_size(box, layout, width, height)
local ret = {
width = 0,
height = 0
}
if box.text then
local layout = layout
if not layout then
-- Create a temporary surface that we need for computing the extents :(
local surface = oocairo.image_surface_create("argb32", 1, 1)
local cr = oocairo.context_create(surface)
layout = setup_layout(box, cr, width, height)
end
local ink, logical = layout:get_pixel_extents()
ret.width = logical.width
ret.height = logical.height
end
return ret
end
--- Draw the given textbox on the given cairo context in the given geometry
function draw(box, wibox, cr, width, height)
if not box.text then return end
local layout = setup_layout(box, cr, width, height)
oopango.cairo.update_layout(cr, layout)
oopango.cairo.show_layout(cr, layout)
end
--- Fit the given textbox
function fit(box, width, height)
local res = get_size(box, nil, width, height)
return res.width, res.height
end
--- Test if a textbox' text is valid. If it isn't, a lua error will be thrown.
function check(box)
-- This creates a pango layout for the string and so forces pango to verify
-- whether we have some sane input for it
get_size(box, nil, -1, -1)
end
--- Set a textbox' text.
-- @param text The text to set. This can contain pango markup (e.g. <b>bold</b>)
function set_markup(box, text)
box.text = text
box.markup = true
box:emit_signal("widget::updated")
end
--- Set a textbox' text.
-- @param text The text to display. Pango markup is ignored and shown as-is.
function set_text(box, text)
box.text = text
box.markup = false
box:emit_signal("widget::updated")
end
--- Set a textbox' ellipsize mode.
-- @param mode Where should long lines be shortened? "start", "middle" or "end"
function set_ellipsize(box, mode)
local allowed = { none = true, start = true, middle = true, ["end"] = true }
if allowed[mode] then
box.ellipsize = mode
box:emit_signal("widget::updated")
end
end
--- Set a textbox' wrap mode.
-- @param mode Where to wrap? After "word", "char" or "word_char"
function set_wrap(box, mode)
local allowed = { word = true, char = true, word_char = true }
if allowed[mode] then
box.wrap = mode
box:emit_signal("widget::updated")
end
end
--- Set a textbox' vertical alignment
-- @param mode Where should the textbox be drawn? "top", "center" or "bottom"
function set_valign(box, mode)
local allowed = { top = true, center = true, bottom = true }
if allowed[mode] then
box.valign = mode
box:emit_signal("widget::updated")
end
end
-- Returns a new textbox
local function new()
local ret = base.make_widget()
for k, v in pairs(_M) do
if type(v) == "function" then
ret[k] = v
end
end
ret.ellipsize = "end"
ret.wrap = "word_char"
ret.valign = "center"
return ret
end
setmetatable(_M, { __call = function (_, ...) return new(...) end })
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80