2010-10-06 12:42:56 +02:00
|
|
|
---------------------------------------------------------------------------
|
2021-10-31 06:44:58 +01:00
|
|
|
-- A widget to display either plain or HTML text.
|
2016-05-25 22:37:04 +02:00
|
|
|
--
|
|
|
|
--@DOC_wibox_widget_defaults_textbox_EXAMPLE@
|
2021-03-27 20:41:28 +01:00
|
|
|
--
|
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
|
2019-06-06 08:15:53 +02:00
|
|
|
-- @widgetmod wibox.widget.textbox
|
2021-03-27 20:41:28 +01:00
|
|
|
-- @supermodule wibox.widget.base
|
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
|
|
|
|
2015-10-17 18:27:40 +02:00
|
|
|
--- Set the DPI of a Pango layout
|
|
|
|
local function setup_dpi(box, dpi)
|
2017-11-19 04:42:48 +01:00
|
|
|
assert(dpi, "No DPI provided")
|
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.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2015-10-17 18:27:40 +02:00
|
|
|
-- This returns the size that the textbox would use if infinite space were
|
|
|
|
-- available.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2019-06-06 22:32:53 +02:00
|
|
|
-- @method get_preferred_size
|
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)
|
2017-10-21 19:32:35 +02:00
|
|
|
local dpi
|
|
|
|
if s then
|
|
|
|
dpi = screen[s].dpi
|
|
|
|
else
|
|
|
|
gdebug.deprecate("textbox:get_preferred_size() requires a screen argument", {deprecated_in=5, raw=true})
|
|
|
|
dpi = beautiful.xresources.get_dpi()
|
|
|
|
end
|
2017-11-19 04:42:48 +01:00
|
|
|
|
2017-10-21 19:32:35 +02:00
|
|
|
return self:get_preferred_size_at_dpi(dpi)
|
2015-10-17 18:27:40 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Get the preferred height of a textbox at a given width.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2015-10-17 18:27:40 +02:00
|
|
|
-- This returns the height that the textbox would use when it is limited to the
|
|
|
|
-- given width.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2019-06-06 22:32:53 +02:00
|
|
|
-- @method get_height_for_width
|
2015-10-17 18:27:40 +02:00
|
|
|
-- @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)
|
2017-10-21 19:32:35 +02:00
|
|
|
local dpi
|
|
|
|
if s then
|
|
|
|
dpi = screen[s].dpi
|
|
|
|
else
|
|
|
|
gdebug.deprecate("textbox:get_preferred_size() requires a screen argument", {deprecated_in=5, raw=true})
|
|
|
|
dpi = beautiful.xresources.get_dpi()
|
|
|
|
end
|
|
|
|
return self:get_height_for_width_at_dpi(width, dpi)
|
2015-10-17 18:27:40 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Get the preferred size of a textbox.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2015-10-17 18:27:40 +02:00
|
|
|
-- This returns the size that the textbox would use if infinite space were
|
|
|
|
-- available.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2019-06-06 22:32:53 +02:00
|
|
|
-- @method get_preferred_size_at_dpi
|
2015-10-17 18:27:40 +02:00
|
|
|
-- @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.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2015-10-17 18:27:40 +02:00
|
|
|
-- This returns the height that the textbox would use when it is limited to the
|
|
|
|
-- given width.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2019-06-06 22:32:53 +02:00
|
|
|
-- @method get_height_for_width_at_dpi
|
2015-10-17 18:27:40 +02:00
|
|
|
-- @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
|
|
|
|
|
2019-11-29 06:33:44 +01:00
|
|
|
--- Set the text of the textbox.(with
|
2021-08-03 20:02:25 +02:00
|
|
|
-- [Pango markup](https://docs.gtk.org/Pango/pango_markup.html)).
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2015-08-13 16:12:29 +02: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
|
2015-02-26 16:08:21 +01:00
|
|
|
-- parts of it.
|
2019-06-06 22:32:53 +02:00
|
|
|
-- @method set_markup_silently
|
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")
|
2019-11-29 06:33:44 +01:00
|
|
|
self:emit_signal("property::markup", text)
|
2016-01-17 16:43:22 +01:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2021-10-31 06:44:58 +01:00
|
|
|
--- Set the HTML text of the textbox.
|
|
|
|
--
|
|
|
|
-- The main difference between `text` and `markup` is that `markup` is
|
|
|
|
-- able to render a small subset of HTML tags. See the
|
|
|
|
-- [Pango markup](https://docs.gtk.org/Pango/pango_markup.html)) documentation
|
|
|
|
-- to see what is and isn't valid in this property.
|
|
|
|
--
|
|
|
|
-- @DOC_wibox_widget_textbox_markup1_EXAMPLE@
|
|
|
|
--
|
|
|
|
-- The `wibox.widget.textbox` colors are usually set by wrapping into a
|
|
|
|
-- `wibox.container.background` widget, but can also be done using the
|
|
|
|
-- markup:
|
|
|
|
--
|
|
|
|
-- @DOC_wibox_widget_textbox_markup2_EXAMPLE@
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2016-05-26 23:39:17 +02:00
|
|
|
-- @property markup
|
2022-07-10 09:29:33 +02:00
|
|
|
-- @tparam[opt=self.text] string markup 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.
|
2019-11-29 06:33:44 +01:00
|
|
|
-- @propemits true false
|
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
|
2020-11-02 01:12:42 +01:00
|
|
|
gdebug.print_error(debug.traceback("Error parsing markup: "..message.."\nFailed with string: '"..text.."'"))
|
2016-01-17 16:43:22 +01:00
|
|
|
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
|
|
|
|
|
2021-10-31 06:44:58 +01:00
|
|
|
--- Set a textbox plain text.
|
|
|
|
--
|
|
|
|
-- This property renders the text as-is, it does not interpret it:
|
|
|
|
--
|
|
|
|
-- @DOC_wibox_widget_textbox_text1_EXAMPLE@
|
|
|
|
--
|
|
|
|
-- One exception are the control characters, which are interpreted:
|
|
|
|
--
|
|
|
|
-- @DOC_wibox_widget_textbox_text2_EXAMPLE@
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2016-05-26 23:39:17 +02:00
|
|
|
-- @property text
|
2022-07-10 09:29:33 +02:00
|
|
|
-- @tparam[opt=""] string text The text to display. Pango markup is ignored and shown
|
2019-11-29 06:33:44 +01:00
|
|
|
-- as-is.
|
|
|
|
-- @propemits true false
|
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")
|
2019-11-29 06:33:44 +01:00
|
|
|
self:emit_signal("property::text", text)
|
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
|
|
|
|
|
2021-07-31 14:48:33 +02:00
|
|
|
--- Set the text ellipsize mode.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2021-07-31 14:48:33 +02:00
|
|
|
-- See Pango for additional details:
|
|
|
|
-- [Layout.set_ellipsize](https://docs.gtk.org/Pango/method.Layout.set_ellipsize.html)
|
|
|
|
--
|
|
|
|
--@DOC_wibox_widget_textbox_ellipsize_EXAMPLE@
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2016-05-26 23:39:17 +02:00
|
|
|
-- @property ellipsize
|
2022-08-22 08:02:27 +02:00
|
|
|
-- @tparam[opt="end"] string ellipsize
|
|
|
|
-- @propertyvalue "start"
|
|
|
|
-- @propertyvalue "middle"
|
|
|
|
-- @propertyvalue "end"
|
|
|
|
-- @propertyvalue "none"
|
2019-11-29 06:33:44 +01:00
|
|
|
-- @propemits true false
|
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")
|
2019-11-29 06:33:44 +01:00
|
|
|
self:emit_signal("property::ellipsize", mode)
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-29 06:33:44 +01:00
|
|
|
--- Set a textbox wrap mode.
|
|
|
|
--
|
2021-10-31 06:44:58 +01:00
|
|
|
-- @DOC_wibox_widget_textbox_wrap1_EXAMPLE@
|
|
|
|
--
|
2016-05-26 23:39:17 +02:00
|
|
|
-- @property wrap
|
2021-12-21 06:54:15 +01:00
|
|
|
-- @tparam[opt="word_char"] string wrap Where to wrap? After "word", "char" or "word_char".
|
2022-08-22 08:02:27 +02:00
|
|
|
-- @propertyvalue "word"
|
|
|
|
-- @propertyvalue "char"
|
|
|
|
-- @propertyvalue "word_char"
|
2019-11-29 06:33:44 +01:00
|
|
|
-- @propemits true false
|
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")
|
2019-11-29 06:33:44 +01:00
|
|
|
self:emit_signal("property::wrap", mode)
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-14 19:09:42 +01:00
|
|
|
--- The vertical text alignment.
|
|
|
|
--
|
|
|
|
-- This aligns the text within the widget's bounds. In some situations this may
|
|
|
|
-- differ from aligning the widget with `wibox.container.place`.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2021-10-31 06:44:58 +01:00
|
|
|
--@DOC_wibox_widget_textbox_valign1_EXAMPLE@
|
|
|
|
--
|
2016-05-26 23:39:17 +02:00
|
|
|
-- @property valign
|
2022-08-22 08:02:27 +02:00
|
|
|
-- @tparam[opt="center"] string valign
|
|
|
|
-- @propertyvalue "top"
|
|
|
|
-- @propertyvalue "center"
|
|
|
|
-- @propertyvalue "bottom"
|
2019-11-29 06:33:44 +01:00
|
|
|
-- @propemits true false
|
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")
|
2019-11-29 06:33:44 +01:00
|
|
|
self:emit_signal("property::valign", mode)
|
2010-10-06 12:42:56 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-14 19:09:42 +01:00
|
|
|
--- The horizontal text alignment.
|
|
|
|
--
|
|
|
|
-- This aligns the text within the widget's bounds. In some situations this may
|
|
|
|
-- differ from aligning the widget with `wibox.container.place`.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2021-10-31 06:44:58 +01:00
|
|
|
--@DOC_wibox_widget_textbox_align1_EXAMPLE@
|
|
|
|
--
|
2022-09-04 09:33:09 +02:00
|
|
|
-- @property halign
|
|
|
|
-- @tparam[opt="left"] string halign
|
2022-08-22 08:02:27 +02:00
|
|
|
-- @propertyvalue "left"
|
|
|
|
-- @propertyvalue "center"
|
|
|
|
-- @propertyvalue "right"
|
2019-11-29 06:33:44 +01:00
|
|
|
-- @propemits true false
|
2016-05-26 23:39:17 +02:00
|
|
|
|
2022-09-04 09:33:09 +02:00
|
|
|
--- The horizontal text alignment.
|
|
|
|
--
|
|
|
|
-- Renamed to `halign` for consistency with other APIs.
|
|
|
|
--
|
|
|
|
-- @deprecatedproperty align
|
|
|
|
-- @tparam[opt="left"] string align
|
|
|
|
-- @propemits true false
|
|
|
|
|
|
|
|
function textbox:set_halign(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")
|
2019-11-29 06:33:44 +01:00
|
|
|
self:emit_signal("property::align", mode)
|
2022-09-04 09:33:09 +02:00
|
|
|
self:emit_signal("property::halign", mode)
|
2011-02-27 23:41:45 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-09-04 09:33:09 +02:00
|
|
|
function textbox:set_align(mode)
|
|
|
|
gdebug.deprecate(
|
|
|
|
"Use `textbox.halign` instead of `textbox.align`",
|
|
|
|
{deprecated_in=5, raw=true}
|
|
|
|
)
|
|
|
|
self:set_halign(mode)
|
|
|
|
end
|
|
|
|
|
2019-11-29 06:33:44 +01:00
|
|
|
--- Set a textbox font.
|
|
|
|
--
|
2021-10-26 23:27:19 +02:00
|
|
|
-- There is multiple valid font string representation. The most precise is
|
|
|
|
-- [XFT](https://wiki.archlinux.org/title/X_Logical_Font_Description). It
|
|
|
|
-- is also possible to use the family name, followed by the face and size
|
|
|
|
-- such as `Monospace Bold 10`. This script lists the fonts present
|
|
|
|
-- on your system:
|
|
|
|
--
|
|
|
|
-- #!/usr/bin/env lua
|
|
|
|
--
|
|
|
|
-- local lgi = require("lgi")
|
|
|
|
-- local pangocairo = lgi.PangoCairo
|
|
|
|
--
|
|
|
|
-- local font_map = pangocairo.font_map_get_default()
|
|
|
|
--
|
|
|
|
-- for k, v in pairs(font_map:list_families()) do
|
|
|
|
-- print(v:get_name(), "monospace?: "..tostring(v:is_monospace()))
|
|
|
|
-- for k2, v2 in ipairs(v:list_faces()) do
|
|
|
|
-- print(" ".. v2:get_face_name())
|
|
|
|
-- end
|
|
|
|
-- end
|
|
|
|
--
|
|
|
|
-- Save this script somewhere on your system, `chmod +x` it and run it. It
|
|
|
|
-- will list something like:
|
|
|
|
--
|
|
|
|
-- Sans monospace?: false
|
|
|
|
-- Regular
|
|
|
|
-- Bold
|
|
|
|
-- Italic
|
|
|
|
-- Bold Italic
|
|
|
|
--
|
|
|
|
-- In this case, the font could be `Sans 10` or `Sans Bold Italic 10`.
|
|
|
|
--
|
2021-10-31 06:44:58 +01:00
|
|
|
-- Here are examples of several font families:
|
|
|
|
--
|
|
|
|
--@DOC_wibox_widget_textbox_font1_EXAMPLE@
|
|
|
|
--
|
|
|
|
-- The font size is a number at the end of the font description string:
|
|
|
|
--
|
|
|
|
--@DOC_wibox_widget_textbox_font2_EXAMPLE@
|
|
|
|
--
|
2016-05-26 23:39:17 +02:00
|
|
|
-- @property font
|
2022-08-22 08:02:27 +02:00
|
|
|
-- @tparam[opt=beautiful.font] font font
|
2019-11-29 06:33:44 +01:00
|
|
|
-- @propemits true false
|
2021-10-31 06:44:58 +01:00
|
|
|
-- @usebeautiful beautiful.font The default font.
|
2016-05-26 23:39:17 +02:00
|
|
|
|
2012-11-18 20:44:03 +01:00
|
|
|
function textbox:set_font(font)
|
2021-10-26 22:31:33 +02:00
|
|
|
if font == self._private.font then return end
|
|
|
|
|
|
|
|
self._private.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")
|
2019-11-29 06:33:44 +01:00
|
|
|
self:emit_signal("property::font", font)
|
2011-04-14 07:04:48 +02:00
|
|
|
end
|
|
|
|
|
2021-10-31 06:44:58 +01:00
|
|
|
function textbox:get_font()
|
|
|
|
return self._private.font
|
|
|
|
end
|
|
|
|
|
2022-09-04 09:00:14 +02:00
|
|
|
--- Set the distance between the lines.
|
|
|
|
--
|
|
|
|
--@DOC_wibox_widget_textbox_line_spacing_EXAMPLE@
|
|
|
|
--
|
|
|
|
-- Please note that the Pango version (one of AwesomeWM dependency) must be at
|
|
|
|
-- least 1.44 for this to work.
|
|
|
|
--
|
|
|
|
-- @property line_spacing_factor
|
|
|
|
-- @tparam[opt=nil] number|nil line_spacing_factor
|
|
|
|
-- @propertyunit Distance between lines as a ratio of the line height. `1.0` means
|
|
|
|
-- no spacing. Less than `1.0` will squash the lines and more than `1.0` will move
|
|
|
|
-- them further apart.
|
|
|
|
-- @propertytype nil Automatic (most probably `1.0`).
|
|
|
|
-- @negativeallowed false
|
|
|
|
-- @propemits true false
|
|
|
|
|
|
|
|
function textbox:set_line_spacing_factor(spacing)
|
|
|
|
if not pcall(function() return self._private.layout.set_line_spacing ~= nil end) then
|
|
|
|
gdebug.print_error(debug.traceback(
|
|
|
|
"Error your version of Pango is too old to support line_spacing"
|
|
|
|
))
|
|
|
|
end
|
|
|
|
|
|
|
|
spacing = spacing or 0
|
|
|
|
self._private.layout:set_line_spacing(spacing)
|
|
|
|
self:emit_signal("widget::redraw_needed")
|
|
|
|
self:emit_signal("widget::layout_changed")
|
|
|
|
self:emit_signal("property::line_spacing", spacing)
|
|
|
|
end
|
|
|
|
|
|
|
|
function textbox:get_line_spacing_factor()
|
|
|
|
return self._private.layout:get_line_spacing()
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Justify the text when there is more space.
|
|
|
|
--
|
|
|
|
--@DOC_wibox_widget_textbox_justify_EXAMPLE@
|
|
|
|
--
|
|
|
|
-- @property justify
|
|
|
|
-- @tparam[opt=false] boolean justify
|
|
|
|
-- @propemits true false
|
|
|
|
|
|
|
|
function textbox:set_justify(justify)
|
|
|
|
self._private.layout:set_justify(justify)
|
|
|
|
self:emit_signal("widget::redraw_needed")
|
|
|
|
self:emit_signal("widget::layout_changed")
|
|
|
|
self:emit_signal("property::justify", justify)
|
|
|
|
end
|
|
|
|
|
|
|
|
function textbox:get_justify()
|
|
|
|
return self._private.layout:get_justify()
|
|
|
|
end
|
|
|
|
|
|
|
|
--- How to indent text with multiple lines.
|
|
|
|
--
|
|
|
|
-- Note that this does nothing if `align == "center"`.
|
|
|
|
--
|
|
|
|
--@DOC_wibox_widget_textbox_indent_EXAMPLE@
|
|
|
|
--
|
|
|
|
-- @property indent
|
|
|
|
-- @tparam[opt=0.0] number indent
|
|
|
|
-- @propertyunit points
|
|
|
|
-- @negativeallowed true
|
|
|
|
-- @propemits true false
|
|
|
|
|
|
|
|
function textbox:set_indent(indent)
|
|
|
|
self._private.layout:set_indent(Pango.units_from_double(indent))
|
|
|
|
self:emit_signal("widget::redraw_needed")
|
|
|
|
self:emit_signal("widget::layout_changed")
|
|
|
|
self:emit_signal("property::indent", indent)
|
|
|
|
end
|
|
|
|
|
|
|
|
function textbox:get_indent()
|
|
|
|
return self._private.layout:get_indent()
|
|
|
|
end
|
|
|
|
|
2016-05-23 08:54:01 +02:00
|
|
|
--- Create a new textbox.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2016-05-23 08:54:01 +02:00
|
|
|
-- @tparam[opt=""] string text The textbox content
|
|
|
|
-- @tparam[opt=false] boolean ignore_markup Ignore the pango/HTML markup
|
|
|
|
-- @treturn table A new textbox widget
|
2019-06-07 20:59:34 +02:00
|
|
|
-- @constructorfct 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)
|
2021-10-29 04:24:49 +02:00
|
|
|
ret._private.layout:set_font_description(beautiful.get_font(beautiful.font))
|
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")
|
2022-09-04 09:33:09 +02:00
|
|
|
ret:set_halign("left")
|
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
|
|
|
|
|
2020-09-03 04:07:53 +02:00
|
|
|
--- Get geometry of text label, as if textbox would be created for it on the screen.
|
|
|
|
--
|
|
|
|
-- @tparam string text The text content, pango markup supported.
|
|
|
|
-- @tparam[opt=nil] integer|screen s The screen on which the textbox would be displayed.
|
|
|
|
-- @tparam[opt=beautiful.font] string font The font description as string.
|
|
|
|
-- @treturn table Geometry (width, height) hashtable.
|
2020-12-18 15:40:40 +01:00
|
|
|
-- @staticfct wibox.widget.textbox.get_markup_geometry
|
2020-09-03 04:07:53 +02:00
|
|
|
function textbox.get_markup_geometry(text, s, font)
|
|
|
|
font = font or beautiful.font
|
|
|
|
local pctx = PangoCairo.font_map_get_default():create_context()
|
|
|
|
local playout = Pango.Layout.new(pctx)
|
|
|
|
playout:set_font_description(beautiful.get_font(font))
|
|
|
|
local dpi_scale = beautiful.xresources.get_dpi(s)
|
|
|
|
pctx:set_resolution(dpi_scale)
|
|
|
|
playout:context_changed()
|
|
|
|
local attr, parsed = Pango.parse_markup(text, -1, 0)
|
|
|
|
playout.attributes, playout.text = attr, parsed
|
|
|
|
local _, logical = playout:get_pixel_extents()
|
|
|
|
return logical
|
|
|
|
end
|
|
|
|
|
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
|