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")
|
2016-01-17 16:43:22 +01:00
|
|
|
local gdebug = require("gears.debug")
|
2010-10-06 12:42:56 +02:00
|
|
|
local beautiful = require("beautiful")
|
2012-05-27 19:20:34 +02:00
|
|
|
local lgi = require("lgi")
|
|
|
|
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-06-12 15:55:10 +02:00
|
|
|
local textbox = { mt = {} }
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2015-10-17 18:27:40 +02:00
|
|
|
--- Set the DPI of a Pango layout
|
|
|
|
local function setup_dpi(box, dpi)
|
2015-09-17 14:35:09 +02:00
|
|
|
if box.dpi ~= dpi then
|
|
|
|
box.dpi = dpi
|
|
|
|
box._ctx:set_resolution(dpi)
|
|
|
|
box._layout:context_changed()
|
|
|
|
end
|
2011-09-07 21:37:23 +02:00
|
|
|
end
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2015-10-17 18:27:40 +02:00
|
|
|
--- Setup a pango layout for the given textbox and dpi
|
|
|
|
local function setup_layout(box, width, height, dpi)
|
|
|
|
box._layout.width = Pango.units_from_double(width)
|
|
|
|
box._layout.height = Pango.units_from_double(height)
|
|
|
|
setup_dpi(box, dpi)
|
|
|
|
end
|
|
|
|
|
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)
|
2015-09-17 14:35:09 +02:00
|
|
|
setup_layout(self, width, height, context.dpi)
|
2012-11-18 20:44:03 +01:00
|
|
|
cr:update_layout(self._layout)
|
2016-02-07 14:13:43 +01:00
|
|
|
local _, 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
|
|
|
|
|
2015-10-17 18:27:40 +02:00
|
|
|
local function do_fit_return(self)
|
2016-02-07 14:13:43 +01:00
|
|
|
local _, 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-10-17 18:27:40 +02:00
|
|
|
--- Fit the given textbox
|
|
|
|
function textbox:fit(context, width, height)
|
|
|
|
setup_layout(self, width, height, context.dpi)
|
|
|
|
return do_fit_return(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Get the preferred size of a textbox.
|
|
|
|
-- This returns the size that the textbox would use if infinite space were
|
|
|
|
-- available.
|
2016-02-26 18:42:57 +01:00
|
|
|
-- @tparam integer|screen s The screen on which the textbox will be displayed.
|
2015-10-17 18:27:40 +02:00
|
|
|
-- @treturn number The preferred width.
|
|
|
|
-- @treturn number The preferred height.
|
|
|
|
function textbox:get_preferred_size(s)
|
|
|
|
return self:get_preferred_size_at_dpi(beautiful.xresources.get_dpi(s))
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Get the preferred height of a textbox at a given width.
|
|
|
|
-- This returns the height that the textbox would use when it is limited to the
|
|
|
|
-- given width.
|
|
|
|
-- @tparam number width The available width.
|
2016-02-26 18:42:57 +01:00
|
|
|
-- @tparam integer|screen s The screen on which the textbox will be displayed.
|
2015-10-17 18:27:40 +02:00
|
|
|
-- @treturn number The needed height.
|
|
|
|
function textbox:get_height_for_width(width, s)
|
|
|
|
return self:get_height_for_width_at_dpi(width, beautiful.xresources.get_dpi(s))
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Get the preferred size of a textbox.
|
|
|
|
-- This returns the size that the textbox would use if infinite space were
|
|
|
|
-- available.
|
|
|
|
-- @tparam number dpi The DPI value to render at.
|
|
|
|
-- @treturn number The preferred width.
|
|
|
|
-- @treturn number The preferred height.
|
|
|
|
function textbox:get_preferred_size_at_dpi(dpi)
|
|
|
|
local max_lines = 2^20
|
|
|
|
setup_dpi(self, dpi)
|
|
|
|
self._layout.width = -1 -- no width set
|
|
|
|
self._layout.height = -max_lines -- show this many lines per paragraph
|
|
|
|
return do_fit_return(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Get the preferred height of a textbox at a given width.
|
|
|
|
-- This returns the height that the textbox would use when it is limited to the
|
|
|
|
-- given width.
|
|
|
|
-- @tparam number width The available width.
|
|
|
|
-- @tparam number dpi The DPI value to render at.
|
|
|
|
-- @treturn number The needed height.
|
|
|
|
function textbox:get_height_for_width_at_dpi(width, dpi)
|
|
|
|
local max_lines = 2^20
|
|
|
|
setup_dpi(self, dpi)
|
|
|
|
self._layout.width = Pango.units_from_double(width)
|
|
|
|
self._layout.height = -max_lines -- show this many lines per paragraph
|
2016-02-07 14:13:43 +01:00
|
|
|
local _, h = do_fit_return(self)
|
2015-10-17 18:27:40 +02:00
|
|
|
return h
|
|
|
|
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.
|
2016-01-17 16:43:22 +01:00
|
|
|
-- @treturn[1] boolean true
|
|
|
|
-- @treturn[2] boolean false
|
|
|
|
-- @treturn[2] string Error message explaining why the markup was invalid.
|
|
|
|
function textbox:set_markup_silently(text)
|
2015-07-22 03:03:53 +02:00
|
|
|
if self._markup == text then
|
2016-01-17 16:43:22 +01:00
|
|
|
return true
|
2015-07-22 03:03:53 +02:00
|
|
|
end
|
|
|
|
|
2012-05-27 19:20:34 +02:00
|
|
|
local attr, parsed = Pango.parse_markup(text, -1, 0)
|
2015-08-01 18:25:10 +02:00
|
|
|
-- In case of error, attr is false and parsed is a GLib.Error instance.
|
2016-01-17 16:43:22 +01:00
|
|
|
if not attr then
|
|
|
|
return false, parsed.message or tostring(parsed)
|
|
|
|
end
|
2012-05-27 19:20:34 +02:00
|
|
|
|
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")
|
2016-01-17 16:43:22 +01:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
--- 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
|
|
|
|
-- parts of it.
|
|
|
|
function textbox:set_markup(text)
|
|
|
|
local success, message = self:set_markup_silently(text)
|
|
|
|
if not success then
|
|
|
|
gdebug.print_error(message)
|
|
|
|
end
|
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
|
|
|
|
|
2016-05-23 08:54:01 +02:00
|
|
|
--- Create a new textbox.
|
|
|
|
-- @tparam[opt=""] string text The textbox content
|
|
|
|
-- @tparam[opt=false] boolean ignore_markup Ignore the pango/HTML markup
|
|
|
|
-- @treturn table A new textbox widget
|
|
|
|
-- @function wibox.widget.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
|
|
|
|
|
2015-09-17 14:35:09 +02:00
|
|
|
ret._dpi = -1
|
|
|
|
ret._ctx = PangoCairo.font_map_get_default():create_context()
|
|
|
|
ret._layout = Pango.Layout.new(ret._ctx)
|
2012-05-27 20:14:56 +02:00
|
|
|
|
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")
|
2015-12-11 00:54:00 +01:00
|
|
|
ret:set_font(beautiful and beautiful.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
|
|
|
|
|
2016-02-07 14:13:43 +01:00
|
|
|
function textbox.mt.__call(_, ...)
|
2012-06-12 15:55:10 +02:00
|
|
|
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
|