2010-10-06 12:42:56 +02:00
|
|
|
---------------------------------------------------------------------------
|
2016-05-25 22:37:04 +02:00
|
|
|
--
|
|
|
|
--@DOC_wibox_widget_defaults_textbox_EXAMPLE@
|
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
|
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")
|
2017-03-08 21:18:33 +01:00
|
|
|
local gtable = require("gears.table")
|
2012-05-27 19:20:34 +02:00
|
|
|
local Pango = lgi.Pango
|
|
|
|
local PangoCairo = lgi.PangoCairo
|
2010-10-06 12:42:56 +02:00
|
|
|
local setmetatable = setmetatable
|
|
|
|
|
2012-06-12 15:55:10 +02:00
|
|
|
local textbox = { mt = {} }
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2016-05-23 09:13:15 +02:00
|
|
|
--- The textbox font.
|
|
|
|
-- @beautiful beautiful.font
|
|
|
|
|
2015-10-17 18:27:40 +02:00
|
|
|
--- Set the DPI of a Pango layout
|
|
|
|
local function setup_dpi(box, dpi)
|
2016-05-26 23:39:17 +02:00
|
|
|
if box._private.dpi ~= dpi then
|
|
|
|
box._private.dpi = dpi
|
|
|
|
box._private.ctx:set_resolution(dpi)
|
|
|
|
box._private.layout:context_changed()
|
2015-09-17 14:35:09 +02:00
|
|
|
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)
|
2016-05-26 23:39:17 +02:00
|
|
|
box._private.layout.width = Pango.units_from_double(width)
|
|
|
|
box._private.layout.height = Pango.units_from_double(height)
|
2015-10-17 18:27:40 +02:00
|
|
|
setup_dpi(box, dpi)
|
|
|
|
end
|
|
|
|
|
2016-05-23 09:02:11 +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)
|
2016-05-26 23:39:17 +02:00
|
|
|
cr:update_layout(self._private.layout)
|
|
|
|
local _, logical = self._private.layout:get_pixel_extents()
|
2010-10-06 12:42:56 +02:00
|
|
|
local offset = 0
|
2016-05-26 23:39:17 +02:00
|
|
|
if self._private.valign == "center" then
|
2010-10-06 12:42:56 +02:00
|
|
|
offset = (height - logical.height) / 2
|
2016-05-26 23:39:17 +02:00
|
|
|
elseif self._private.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)
|
2016-05-26 23:39:17 +02:00
|
|
|
cr:show_layout(self._private.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-05-26 23:39:17 +02:00
|
|
|
local _, logical = self._private.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
|
|
|
|
|
2016-05-23 09:02:11 +02:00
|
|
|
-- Fit the given textbox
|
2015-10-17 18:27:40 +02:00
|
|
|
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)
|
2016-05-26 23:39:17 +02:00
|
|
|
self._private.layout.width = -1 -- no width set
|
|
|
|
self._private.layout.height = -max_lines -- show this many lines per paragraph
|
2015-10-17 18:27:40 +02:00
|
|
|
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)
|
2016-05-26 23:39:17 +02:00
|
|
|
self._private.layout.width = Pango.units_from_double(width)
|
|
|
|
self._private.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.
|
2017-03-12 00:57:32 +01:00
|
|
|
-- `<b>bold</b>`). You can use `gears.string.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)
|
2016-05-26 23:39:17 +02:00
|
|
|
if self._private.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
|
|
|
|
2016-05-26 23:39:17 +02:00
|
|
|
self._private.markup = text
|
|
|
|
self._private.layout.text = parsed
|
|
|
|
self._private.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)).
|
2016-05-26 23:39:17 +02:00
|
|
|
-- @property markup
|
2016-01-17 16:43:22 +01:00
|
|
|
-- @tparam string text The text to set. This can contain pango markup (e.g.
|
2017-03-12 00:57:32 +01:00
|
|
|
-- `<b>bold</b>`). You can use `gears.string.escape` to escape
|
2016-01-17 16:43:22 +01:00
|
|
|
-- parts of it.
|
2016-05-26 23:39:17 +02:00
|
|
|
-- @see text
|
|
|
|
|
2016-01-17 16:43:22 +01:00
|
|
|
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
|
|
|
|
|
2016-05-26 23:39:17 +02:00
|
|
|
function textbox:get_markup()
|
|
|
|
return self._private.markup
|
|
|
|
end
|
|
|
|
|
2010-10-06 12:42:56 +02:00
|
|
|
--- Set a textbox' text.
|
2016-05-26 23:39:17 +02:00
|
|
|
-- @property text
|
2010-10-06 12:42:56 +02:00
|
|
|
-- @param text The text to display. Pango markup is ignored and shown as-is.
|
2016-05-26 23:39:17 +02:00
|
|
|
-- @see markup
|
|
|
|
|
2012-11-18 20:44:03 +01:00
|
|
|
function textbox:set_text(text)
|
2016-05-26 23:39:17 +02:00
|
|
|
if self._private.layout.text == text and self._private.layout.attributes == nil then
|
2015-07-22 03:03:53 +02:00
|
|
|
return
|
|
|
|
end
|
2016-05-26 23:39:17 +02:00
|
|
|
self._private.markup = nil
|
|
|
|
self._private.layout.text = text
|
|
|
|
self._private.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
|
|
|
|
|
2016-05-26 23:39:17 +02:00
|
|
|
function textbox:get_text()
|
|
|
|
return self._private.layout.text
|
|
|
|
end
|
|
|
|
|
2010-10-06 12:42:56 +02:00
|
|
|
--- Set a textbox' ellipsize mode.
|
2016-05-26 23:39:17 +02:00
|
|
|
-- @property ellipsize
|
2010-10-06 12:42:56 +02:00
|
|
|
-- @param mode Where should long lines be shortened? "start", "middle" or "end"
|
2016-05-26 23:39:17 +02:00
|
|
|
|
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
|
2016-05-26 23:39:17 +02:00
|
|
|
if self._private.layout:get_ellipsize() == allowed[mode] then
|
2015-07-22 03:03:53 +02:00
|
|
|
return
|
|
|
|
end
|
2016-05-26 23:39:17 +02:00
|
|
|
self._private.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.
|
2016-05-26 23:39:17 +02:00
|
|
|
-- @property wrap
|
2010-10-06 12:42:56 +02:00
|
|
|
-- @param mode Where to wrap? After "word", "char" or "word_char"
|
2016-05-26 23:39:17 +02:00
|
|
|
|
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
|
2016-05-26 23:39:17 +02:00
|
|
|
if self._private.layout:get_wrap() == allowed[mode] then
|
2015-07-22 03:03:53 +02:00
|
|
|
return
|
|
|
|
end
|
2016-05-26 23:39:17 +02:00
|
|
|
self._private.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
|
|
|
|
|
2016-05-26 23:39:17 +02:00
|
|
|
--- The textbox' vertical alignment
|
|
|
|
-- @property valign
|
2010-10-06 12:42:56 +02:00
|
|
|
-- @param mode Where should the textbox be drawn? "top", "center" or "bottom"
|
2016-05-26 23:39:17 +02:00
|
|
|
|
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
|
2016-05-26 23:39:17 +02:00
|
|
|
if self._private.valign == mode then
|
2015-07-22 03:03:53 +02:00
|
|
|
return
|
|
|
|
end
|
2016-05-26 23:39:17 +02:00
|
|
|
self._private.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
|
|
|
|
|
2016-05-26 23:39:17 +02:00
|
|
|
--- Set a textbox' horizontal alignment.
|
|
|
|
-- @property align
|
2011-02-27 23:41:45 +01:00
|
|
|
-- @param mode Where should the textbox be drawn? "left", "center" or "right"
|
2016-05-26 23:39:17 +02:00
|
|
|
|
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
|
2016-05-26 23:39:17 +02:00
|
|
|
if self._private.layout:get_alignment() == allowed[mode] then
|
2015-07-22 03:03:53 +02:00
|
|
|
return
|
|
|
|
end
|
2016-05-26 23:39:17 +02:00
|
|
|
self._private.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
|
2016-05-26 23:39:17 +02:00
|
|
|
-- @property font
|
2011-04-14 07:04:48 +02:00
|
|
|
-- @param font The font description as string
|
2016-05-26 23:39:17 +02:00
|
|
|
|
2012-11-18 20:44:03 +01:00
|
|
|
function textbox:set_font(font)
|
2016-05-26 23:39:17 +02:00
|
|
|
self._private.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)
|
2016-05-26 23:39:17 +02:00
|
|
|
local ret = base.make_widget(nil, nil, {enable_properties = true})
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2017-03-08 21:18:33 +01:00
|
|
|
gtable.crush(ret, textbox, true)
|
2010-10-06 12:42:56 +02:00
|
|
|
|
2016-05-26 23:39:17 +02:00
|
|
|
ret._private.dpi = -1
|
|
|
|
ret._private.ctx = PangoCairo.font_map_get_default():create_context()
|
|
|
|
ret._private.layout = Pango.Layout.new(ret._private.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
|
|
|
|
|
2016-05-26 23:39:17 +02:00
|
|
|
--@DOC_widget_COMMON@
|
|
|
|
|
|
|
|
--@DOC_object_COMMON@
|
|
|
|
|
2012-06-12 15:55:10 +02:00
|
|
|
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
|