/
textbox.lua.in
141 lines (121 loc) · 4.25 KB
/
textbox.lua.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
---------------------------------------------------------------------------
-- @author Uli Schlachter
-- @author dodo
-- @copyright 2010, 2011 Uli Schlachter, dodo
-- @release @AWESOME_VERSION@
---------------------------------------------------------------------------
local base = require("wibox.widget.base")
local beautiful = require("beautiful")
local lgi = require("lgi")
local cairo = lgi.cairo
local Pango = lgi.Pango
local PangoCairo = lgi.PangoCairo
local type = type
local unpack = unpack
local setmetatable = setmetatable
local pairs = pairs
local error = error
module("wibox.widget.textbox")
-- Setup a pango layout for the given textbox and cairo context
local function setup_layout(box, width, height)
local layout = box._layout
layout.width = Pango.units_from_double(width)
layout.height = Pango.units_from_double(height)
end
--- Draw the given textbox on the given cairo context in the given geometry
function draw(box, wibox, cr, width, height)
cr:update_layout(box._layout)
setup_layout(box, width, height)
local ink, logical = box._layout:get_pixel_extents()
local offset = 0
if box._valign == "center" then
offset = (height - logical.height) / 2
elseif box._valign == "bottom" then
offset = height - logical.height
end
cr:move_to(0, offset)
cr:show_layout(box._layout)
end
--- Fit the given textbox
function fit(box, width, height)
setup_layout(box, width, height)
local ink, logical = box._layout:get_pixel_extents()
return logical.width, logical.height
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)
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
box._layout.text = parsed
box._layout.attributes = attr
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)
box._layout.text = text
box._layout.attributes = nil
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 = "NONE", start = "START", middle = "MIDDLE", ["end"] = "END" }
if allowed[mode] then
box._layout:set_ellipsize(allowed[mode])
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 = "WORD", char = "CHAR", word_char = "WORD_CHAR" }
if allowed[mode] then
box._layout:set_wrap(allowed[mode])
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
box._valign = mode
box:emit_signal("widget::updated")
end
end
--- 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 = "LEFT", center = "CENTER", right = "RIGHT" }
if allowed[mode] then
box._layout:set_alignment(allowed[mode])
box:emit_signal("widget::updated")
end
end
--- Set a textbox' font
-- @param font The font description as string
function set_font(box, font)
box._layout:set_font_description(beautiful.get_font(font))
end
-- 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
local ctx = PangoCairo.font_map_get_default():create_context()
ret._layout = Pango.Layout.new(ctx)
ret:set_ellipsize("end")
ret:set_wrap("word_char")
ret:set_valign("center")
ret:set_align("left")
ret:set_font()
return ret
end
setmetatable(_M, { __call = function (_, ...) return new(...) end })
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80