1 ---------------------------------------------------------------------------
2 -- @author Uli Schlachter
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
15 local setmetatable
= setmetatable
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
)
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()
35 if self
._valign
== "center" then
36 offset
= (height
- logical
.height
) / 2
37 elseif self
._valign
== "bottom" then
38 offset
= height
- logical
.height
41 cr
:show_layout(self
._layout
)
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
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")
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")
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" }
76 self
._layout
:set_ellipsize(allowed
[mode
])
77 self
:emit_signal("widget::updated")
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" }
86 self
._layout
:set_wrap(allowed
[mode
])
87 self
:emit_signal("widget::updated")
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 }
97 self
:emit_signal("widget::updated")
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
119 local ret
= base
.make_widget()
121 for k
, v
in pairs(textbox
) do
122 if type(v
) == "function" then
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")
139 function textbox
.mt
:__call(...)
143 return setmetatable(textbox
, textbox
.mt
)
145 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80