wibox: Make the functions a little more object-y
[awesome.git] / lib / wibox / widget / textbox.lua.in
blob2d664e2b53fea29c6b87f93d1f74459ee68bf624
1 ---------------------------------------------------------------------------
2 -- @author Uli Schlachter
3 -- @author dodo
4 -- @copyright 2010, 2011 Uli Schlachter, dodo
5 -- @release @AWESOME_VERSION@
6 ---------------------------------------------------------------------------
8 local base = require("wibox.widget.base")
9 local beautiful = require("beautiful")
10 local lgi = require("lgi")
11 local cairo = lgi.cairo
12 local Pango = lgi.Pango
13 local PangoCairo = lgi.PangoCairo
14 local type = type
15 local setmetatable = setmetatable
16 local pairs = pairs
17 local error = error
19 -- wibox.widget.textbox
20 local textbox = { mt = {} }
22 -- Setup a pango layout for the given textbox and cairo context
23 local function setup_layout(box, width, height)
24 local layout = box._layout
25 layout.width = Pango.units_from_double(width)
26 layout.height = Pango.units_from_double(height)
27 end
29 --- Draw the given textbox on the given cairo context in the given geometry
30 function textbox:draw(wibox, cr, width, height)
31 cr:update_layout(self._layout)
32 setup_layout(self, width, height)
33 local ink, logical = self._layout:get_pixel_extents()
34 local offset = 0
35 if self._valign == "center" then
36 offset = (height - logical.height) / 2
37 elseif self._valign == "bottom" then
38 offset = height - logical.height
39 end
40 cr:move_to(0, offset)
41 cr:show_layout(self._layout)
42 end
44 --- Fit the given textbox
45 function textbox:fit(width, height)
46 setup_layout(self, width, height)
47 local ink, logical = self._layout:get_pixel_extents()
48 return logical.width, logical.height
49 end
51 --- Set a textbox' text.
52 -- @param text The text to set. This can contain pango markup (e.g. <b>bold</b>)
53 function textbox:set_markup(text)
54 local attr, parsed = Pango.parse_markup(text, -1, 0)
55 -- In case of error, attr is false and parsed is an error message
56 if not attr then error(parsed) end
58 self._layout.text = parsed
59 self._layout.attributes = attr
60 self:emit_signal("widget::updated")
61 end
63 --- Set a textbox' text.
64 -- @param text The text to display. Pango markup is ignored and shown as-is.
65 function textbox:set_text(text)
66 self._layout.text = text
67 self._layout.attributes = nil
68 self:emit_signal("widget::updated")
69 end
71 --- Set a textbox' ellipsize mode.
72 -- @param mode Where should long lines be shortened? "start", "middle" or "end"
73 function textbox:set_ellipsize(mode)
74 local allowed = { none = "NONE", start = "START", middle = "MIDDLE", ["end"] = "END" }
75 if allowed[mode] then
76 self._layout:set_ellipsize(allowed[mode])
77 self:emit_signal("widget::updated")
78 end
79 end
81 --- Set a textbox' wrap mode.
82 -- @param mode Where to wrap? After "word", "char" or "word_char"
83 function textbox:set_wrap(mode)
84 local allowed = { word = "WORD", char = "CHAR", word_char = "WORD_CHAR" }
85 if allowed[mode] then
86 self._layout:set_wrap(allowed[mode])
87 self:emit_signal("widget::updated")
88 end
89 end
91 --- Set a textbox' vertical alignment
92 -- @param mode Where should the textbox be drawn? "top", "center" or "bottom"
93 function textbox:set_valign(mode)
94 local allowed = { top = true, center = true, bottom = true }
95 if allowed[mode] then
96 self._valign = mode
97 self:emit_signal("widget::updated")
98 end
99 end
101 --- Set a textbox' horizontal alignment
102 -- @param mode Where should the textbox be drawn? "left", "center" or "right"
103 function textbox:set_align(mode)
104 local allowed = { left = "LEFT", center = "CENTER", right = "RIGHT" }
105 if allowed[mode] then
106 self._layout:set_alignment(allowed[mode])
107 self:emit_signal("widget::updated")
111 --- Set a textbox' font
112 -- @param font The font description as string
113 function textbox:set_font(font)
114 self._layout:set_font_description(beautiful.get_font(font))
117 -- Returns a new textbox
118 local function new()
119 local ret = base.make_widget()
121 for k, v in pairs(textbox) do
122 if type(v) == "function" then
123 ret[k] = v
127 local ctx = PangoCairo.font_map_get_default():create_context()
128 ret._layout = Pango.Layout.new(ctx)
130 ret:set_ellipsize("end")
131 ret:set_wrap("word_char")
132 ret:set_valign("center")
133 ret:set_align("left")
134 ret:set_font()
136 return ret
139 function textbox.mt:__call(...)
140 return new(...)
143 return setmetatable(textbox, textbox.mt)
145 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80