2010-10-06 12:42:56 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
-- @author Uli Schlachter
|
2011-02-27 23:41:45 +01:00
|
|
|
-- @author dodo
|
|
|
|
-- @copyright 2010, 2011 Uli Schlachter, dodo
|
2010-10-06 12:42:56 +02:00
|
|
|
-- @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")
|
|
|
|
|
2010-10-06 15:11:20 +02:00
|
|
|
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 update_and_show(cr, layout)
|
|
|
|
if oopango.cairo_update_layout then
|
|
|
|
oopango.cairo_update_layout(cr, layout)
|
|
|
|
oopango.cairo_show_layout(cr, layout)
|
|
|
|
else
|
|
|
|
oopango.cairo.update_layout(cr, layout)
|
|
|
|
oopango.cairo.show_layout(cr, layout)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-06 12:42:56 +02:00
|
|
|
-- Setup a pango layout for the given textbox and cairo context
|
|
|
|
local function setup_layout(box, cr, width, height)
|
2010-10-06 15:11:20 +02:00
|
|
|
local layout = layout_create(cr)
|
2011-02-27 23:41:45 +01:00
|
|
|
layout:set_alignment(box._align)
|
2010-10-07 14:22:44 +02:00
|
|
|
layout:set_ellipsize(box._ellipsize)
|
|
|
|
layout:set_wrap(box._wrap)
|
2010-10-06 12:42:56 +02:00
|
|
|
layout:set_width(oopango.units_from_number(width))
|
|
|
|
layout:set_height(oopango.units_from_number(height))
|
2011-04-14 07:04:48 +02:00
|
|
|
layout:set_font_description(beautiful.get_font(box._font))
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2010-10-07 14:22:44 +02:00
|
|
|
if box._markup then
|
|
|
|
layout:set_markup(box._text)
|
2010-10-06 12:42:56 +02:00
|
|
|
else
|
2010-10-07 14:22:44 +02:00
|
|
|
layout:set_text(box._text)
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local ink, logical = layout:get_pixel_extents()
|
|
|
|
local offset = 0
|
2010-10-07 14:22:44 +02:00
|
|
|
if box._valign == "center" then
|
2010-10-06 12:42:56 +02:00
|
|
|
offset = (height - logical.height) / 2
|
2010-10-07 14:22:44 +02:00
|
|
|
elseif box._valign == "bottom" then
|
2010-10-06 12:42:56 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2010-10-07 14:22:44 +02:00
|
|
|
if box._text then
|
2010-10-06 12:42:56 +02:00
|
|
|
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)
|
2010-10-07 14:22:44 +02:00
|
|
|
if not box._text then return end
|
2010-10-06 12:42:56 +02:00
|
|
|
|
|
|
|
local layout = setup_layout(box, cr, width, height)
|
|
|
|
|
2010-10-06 15:11:20 +02:00
|
|
|
update_and_show(cr, layout)
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Fit the given textbox
|
|
|
|
function fit(box, width, height)
|
|
|
|
local res = get_size(box, nil, width, height)
|
|
|
|
return res.width, res.height
|
|
|
|
end
|
|
|
|
|
2011-01-08 13:55:36 +01:00
|
|
|
-- 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)
|
|
|
|
local surface = oocairo.image_surface_create("argb32", 1, 1)
|
|
|
|
local cr = oocairo.context_create(surface)
|
|
|
|
local layout = layout_create(cr)
|
|
|
|
|
|
|
|
if markup then
|
|
|
|
layout:set_markup(text)
|
|
|
|
else
|
|
|
|
layout:set_text(text)
|
|
|
|
end
|
2010-10-06 12:42:56 +02:00
|
|
|
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)
|
2011-01-08 13:55:36 +01:00
|
|
|
check_text(text, true)
|
2010-10-07 14:22:44 +02:00
|
|
|
box._text = text
|
|
|
|
box._markup = true
|
2010-10-06 12:42:56 +02:00
|
|
|
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)
|
2011-01-08 13:55:36 +01:00
|
|
|
check_text(text, false)
|
2010-10-07 14:22:44 +02:00
|
|
|
box._text = text
|
|
|
|
box._markup = false
|
2010-10-06 12:42:56 +02:00
|
|
|
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
|
2010-10-07 14:22:44 +02:00
|
|
|
box._ellipsize = mode
|
2010-10-06 12:42:56 +02:00
|
|
|
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
|
2010-10-07 14:22:44 +02:00
|
|
|
box._wrap = mode
|
2010-10-06 12:42:56 +02:00
|
|
|
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
|
2010-10-07 14:22:44 +02:00
|
|
|
box._valign = mode
|
2010-10-06 12:42:56 +02:00
|
|
|
box:emit_signal("widget::updated")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-02-27 23:41:45 +01:00
|
|
|
--- 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._align = mode
|
|
|
|
box:emit_signal("widget::updated")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-04-14 07:04:48 +02:00
|
|
|
--- Set a textbox' font
|
|
|
|
-- @param font The font description as string
|
|
|
|
function set_font(box, font)
|
|
|
|
box._font = beautiful.get_font(font)
|
|
|
|
end
|
|
|
|
|
2010-10-06 12:42:56 +02:00
|
|
|
-- 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
|
|
|
|
|
2010-10-07 14:22:44 +02:00
|
|
|
ret._ellipsize = "end"
|
|
|
|
ret._wrap = "word_char"
|
|
|
|
ret._valign = "center"
|
2011-02-27 23:41:45 +01:00
|
|
|
ret._align = "left"
|
2010-10-06 12:42:56 +02:00
|
|
|
|
|
|
|
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
|