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@
|
2015-02-25 11:18:53 +01:00
|
|
|
-- @classmod wibox.widget.textbox
|
2010-10-06 12:42:56 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local base = require("wibox.widget.base")
|
|
|
|
local beautiful = require("beautiful")
|
2012-05-27 19:20:34 +02:00
|
|
|
local lgi = require("lgi")
|
|
|
|
local cairo = lgi.cairo
|
|
|
|
local Pango = lgi.Pango
|
|
|
|
local PangoCairo = lgi.PangoCairo
|
2010-10-06 12:42:56 +02:00
|
|
|
local type = type
|
|
|
|
local setmetatable = setmetatable
|
|
|
|
local pairs = pairs
|
2012-05-27 19:20:34 +02:00
|
|
|
local error = error
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2012-06-12 15:55:10 +02:00
|
|
|
local textbox = { mt = {} }
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2015-02-20 15:45:53 +01:00
|
|
|
--- Setup a pango layout for the given textbox and cairo context
|
2011-09-07 21:37:23 +02:00
|
|
|
local function setup_layout(box, width, height)
|
|
|
|
local layout = box._layout
|
2012-05-27 19:20:34 +02:00
|
|
|
layout.width = Pango.units_from_double(width)
|
|
|
|
layout.height = Pango.units_from_double(height)
|
2011-09-07 21:37:23 +02:00
|
|
|
end
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2011-09-07 21:37:23 +02:00
|
|
|
--- Draw the given textbox on the given cairo context in the given geometry
|
2015-08-08 13:18:54 +02:00
|
|
|
function textbox:draw(context, cr, width, height)
|
2012-11-18 20:44:03 +01:00
|
|
|
cr:update_layout(self._layout)
|
|
|
|
setup_layout(self, width, height)
|
|
|
|
local ink, logical = self._layout:get_pixel_extents()
|
2010-10-06 12:42:56 +02:00
|
|
|
local offset = 0
|
2012-11-18 20:44:03 +01:00
|
|
|
if self._valign == "center" then
|
2010-10-06 12:42:56 +02:00
|
|
|
offset = (height - logical.height) / 2
|
2012-11-18 20:44:03 +01:00
|
|
|
elseif self._valign == "bottom" then
|
2010-10-06 12:42:56 +02:00
|
|
|
offset = height - logical.height
|
|
|
|
end
|
2011-09-07 21:37:23 +02:00
|
|
|
cr:move_to(0, offset)
|
2012-11-18 20:44:03 +01:00
|
|
|
cr:show_layout(self._layout)
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Fit the given textbox
|
2015-08-08 13:43:35 +02:00
|
|
|
function textbox:fit(context, width, height)
|
2012-11-18 20:44:03 +01:00
|
|
|
setup_layout(self, width, height)
|
|
|
|
local ink, logical = self._layout:get_pixel_extents()
|
2013-01-20 15:00:29 +01:00
|
|
|
|
|
|
|
if logical.width == 0 or logical.height == 0 then
|
|
|
|
return 0, 0
|
|
|
|
end
|
|
|
|
|
2011-09-07 21:37:23 +02:00
|
|
|
return logical.width, logical.height
|
|
|
|
end
|
|
|
|
|
2015-08-13 16:12:29 +02:00
|
|
|
--- Set the text of the textbox (with
|
|
|
|
-- [Pango markup](https://developer.gnome.org/pango/stable/PangoMarkupFormat.html)).
|
|
|
|
-- @tparam string text The text to set. This can contain pango markup (e.g.
|
|
|
|
-- `<b>bold</b>`). You can use `awful.util.escape` to escape
|
2015-02-26 16:08:21 +01:00
|
|
|
-- parts of it.
|
2012-11-18 20:44:03 +01:00
|
|
|
function textbox:set_markup(text)
|
2015-07-22 03:03:53 +02:00
|
|
|
if self._markup == text then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2012-05-27 19:20:34 +02:00
|
|
|
local attr, parsed = Pango.parse_markup(text, -1, 0)
|
|
|
|
-- In case of error, attr is false and parsed is an error message
|
|
|
|
if not attr then error(parsed) end
|
|
|
|
|
2015-07-22 03:03:53 +02:00
|
|
|
self._markup = text
|
2012-11-18 20:44:03 +01:00
|
|
|
self._layout.text = parsed
|
|
|
|
self._layout.attributes = attr
|
2015-06-14 16:37:28 +02:00
|
|
|
self:emit_signal("widget::redraw_needed")
|
|
|
|
self:emit_signal("widget::layout_changed")
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Set a textbox' text.
|
|
|
|
-- @param text The text to display. Pango markup is ignored and shown as-is.
|
2012-11-18 20:44:03 +01:00
|
|
|
function textbox:set_text(text)
|
2015-07-22 03:03:53 +02:00
|
|
|
if self._layout.text == text and self._layout.attributes == nil then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
self._markup = nil
|
2012-11-18 20:44:03 +01:00
|
|
|
self._layout.text = text
|
|
|
|
self._layout.attributes = nil
|
2015-06-14 16:37:28 +02:00
|
|
|
self:emit_signal("widget::redraw_needed")
|
|
|
|
self:emit_signal("widget::layout_changed")
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Set a textbox' ellipsize mode.
|
|
|
|
-- @param mode Where should long lines be shortened? "start", "middle" or "end"
|
2012-11-18 20:44:03 +01:00
|
|
|
function textbox:set_ellipsize(mode)
|
2012-05-27 19:20:34 +02:00
|
|
|
local allowed = { none = "NONE", start = "START", middle = "MIDDLE", ["end"] = "END" }
|
2010-10-06 12:42:56 +02:00
|
|
|
if allowed[mode] then
|
2015-07-22 03:03:53 +02:00
|
|
|
if self._layout:get_ellipsize() == allowed[mode] then
|
|
|
|
return
|
|
|
|
end
|
2012-11-18 20:44:03 +01:00
|
|
|
self._layout:set_ellipsize(allowed[mode])
|
2015-06-14 16:37:28 +02:00
|
|
|
self:emit_signal("widget::redraw_needed")
|
|
|
|
self:emit_signal("widget::layout_changed")
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Set a textbox' wrap mode.
|
|
|
|
-- @param mode Where to wrap? After "word", "char" or "word_char"
|
2012-11-18 20:44:03 +01:00
|
|
|
function textbox:set_wrap(mode)
|
2012-05-27 19:20:34 +02:00
|
|
|
local allowed = { word = "WORD", char = "CHAR", word_char = "WORD_CHAR" }
|
2010-10-06 12:42:56 +02:00
|
|
|
if allowed[mode] then
|
2015-07-22 03:03:53 +02:00
|
|
|
if self._layout:get_wrap() == allowed[mode] then
|
|
|
|
return
|
|
|
|
end
|
2012-11-18 20:44:03 +01:00
|
|
|
self._layout:set_wrap(allowed[mode])
|
2015-06-14 16:37:28 +02:00
|
|
|
self:emit_signal("widget::redraw_needed")
|
|
|
|
self:emit_signal("widget::layout_changed")
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Set a textbox' vertical alignment
|
|
|
|
-- @param mode Where should the textbox be drawn? "top", "center" or "bottom"
|
2012-11-18 20:44:03 +01:00
|
|
|
function textbox:set_valign(mode)
|
2010-10-06 12:42:56 +02:00
|
|
|
local allowed = { top = true, center = true, bottom = true }
|
|
|
|
if allowed[mode] then
|
2015-07-22 03:03:53 +02:00
|
|
|
if self._valign == mode then
|
|
|
|
return
|
|
|
|
end
|
2012-11-18 20:44:03 +01:00
|
|
|
self._valign = mode
|
2015-06-14 16:37:28 +02:00
|
|
|
self:emit_signal("widget::redraw_needed")
|
|
|
|
self:emit_signal("widget::layout_changed")
|
2010-10-06 12:42:56 +02:00
|
|
|
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"
|
2012-11-18 20:44:03 +01:00
|
|
|
function textbox:set_align(mode)
|
2012-05-27 19:20:34 +02:00
|
|
|
local allowed = { left = "LEFT", center = "CENTER", right = "RIGHT" }
|
2011-02-27 23:41:45 +01:00
|
|
|
if allowed[mode] then
|
2015-07-22 03:03:53 +02:00
|
|
|
if self._layout:get_alignment() == allowed[mode] then
|
|
|
|
return
|
|
|
|
end
|
2012-11-18 20:44:03 +01:00
|
|
|
self._layout:set_alignment(allowed[mode])
|
2015-06-14 16:37:28 +02:00
|
|
|
self:emit_signal("widget::redraw_needed")
|
|
|
|
self:emit_signal("widget::layout_changed")
|
2011-02-27 23:41:45 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-04-14 07:04:48 +02:00
|
|
|
--- Set a textbox' font
|
|
|
|
-- @param font The font description as string
|
2012-11-18 20:44:03 +01:00
|
|
|
function textbox:set_font(font)
|
|
|
|
self._layout:set_font_description(beautiful.get_font(font))
|
2015-06-14 16:37:28 +02:00
|
|
|
self:emit_signal("widget::redraw_needed")
|
|
|
|
self:emit_signal("widget::layout_changed")
|
2011-04-14 07:04:48 +02:00
|
|
|
end
|
|
|
|
|
2010-10-06 12:42:56 +02:00
|
|
|
-- Returns a new textbox
|
2012-11-25 19:16:31 +01:00
|
|
|
local function new(text, ignore_markup)
|
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(textbox) do
|
2010-10-06 12:42:56 +02:00
|
|
|
if type(v) == "function" then
|
|
|
|
ret[k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-05-27 20:14:56 +02:00
|
|
|
local ctx = PangoCairo.font_map_get_default():create_context()
|
2015-05-14 22:57:22 +02:00
|
|
|
ctx:set_resolution(beautiful.xresources.get_dpi())
|
2012-05-27 20:14:56 +02:00
|
|
|
ret._layout = Pango.Layout.new(ctx)
|
|
|
|
|
2011-09-07 21:37:23 +02:00
|
|
|
ret:set_ellipsize("end")
|
|
|
|
ret:set_wrap("word_char")
|
|
|
|
ret:set_valign("center")
|
|
|
|
ret:set_align("left")
|
|
|
|
ret:set_font()
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2012-11-25 19:16:31 +01:00
|
|
|
if text then
|
|
|
|
if ignore_markup then
|
|
|
|
ret:set_text(text)
|
|
|
|
else
|
|
|
|
ret:set_markup(text)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-06 12:42:56 +02:00
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2012-06-12 15:55:10 +02:00
|
|
|
function textbox.mt:__call(...)
|
|
|
|
return new(...)
|
|
|
|
end
|
|
|
|
|
|
|
|
return setmetatable(textbox, textbox.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
|