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

178 lines
5.1 KiB
Lua

---------------------------------------------------------------------------
-- @author Uli Schlachter
-- @author dodo
-- @copyright 2010, 2011 Uli Schlachter, dodo
-- @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")
local function layout_create(cr)
if oopango.cairo_layout_create then
return oopango.cairo_layout_create(cr)
end
return oopango.cairo.layout_create(cr)
end
local function layout_update(cr, layout)
if oopango.cairo_update_layout then
oopango.cairo_update_layout(cr, layout)
else
oopango.cairo.update_layout(cr, layout)
end
end
local function layout_show(cr, layout)
if oopango.cairo_show_layout then
oopango.cairo_show_layout(cr, layout)
else
oopango.cairo.show_layout(cr, layout)
end
end
-- Setup a pango layout for the given textbox and cairo context
local function setup_layout(box, width, height)
local layout = box._layout
layout:set_width(oopango.units_from_number(width))
layout:set_height(oopango.units_from_number(height))
end
--- Draw the given textbox on the given cairo context in the given geometry
function draw(box, wibox, cr, width, height)
layout_update(cr, box._layout)
setup_layout(box, width, height)
local ink, logical = box._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
cr:move_to(0, offset)
layout_show(cr, box._layout)
end
--- Fit the given textbox
function fit(box, width, height)
setup_layout(box, width, height)
local ink, logical = box._layout:get_pixel_extents()
return logical.width, logical.height
end
-- Return a pango layout which can be used if no cairo context is available
local function get_temp_layout()
local surface = oocairo.image_surface_create("argb32", 0, 0)
local cr = oocairo.context_create(surface)
return layout_create(cr)
end
local temp_layout
-- Test if a text is valid for a textbox. If it isn't, a lua error will be thrown.
local function check_text(text, markup)
if not temp_layout then
temp_layout = get_temp_layout()
end
if markup then
temp_layout:set_markup(text)
else
temp_layout:set_text(text)
end
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)
check_text(text, true)
box._layout:set_markup(text)
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)
check_text(text, false)
box._layout:set_text(text)
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._layout:set_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._layout:set_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
--- Set a textbox' horizontal alignment
-- @param mode Where should the textbox be drawn? "left", "center" or "right"
function set_align(box, mode)
local allowed = { left = true, center = true, right = true }
if allowed[mode] then
box._layout:set_alignment(mode)
box:emit_signal("widget::updated")
end
end
--- Set a textbox' font
-- @param font The font description as string
function set_font(box, font)
box._layout:set_font_description(beautiful.get_font(font))
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
-- Spot the hack: Temporary surface to make pango happy
ret._layout = get_temp_layout()
ret:set_ellipsize("end")
ret:set_wrap("word_char")
ret:set_valign("center")
ret:set_align("left")
ret:set_font()
return ret
end
setmetatable(_M, { __call = function (_, ...) return new(...) end })
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80