wibox: Make the functions a little more object-y

This replaces lots of function foo.bar(this, ...) with function foo:bar(...).
There should be no other changes in this commit.

The point is to make it easier for api documentation tools to figure out that
these are methods on objects.

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2012-11-18 20:44:03 +01:00
parent ee46c9e5ae
commit 90f7f55348
11 changed files with 193 additions and 199 deletions

View File

@ -30,39 +30,37 @@ wibox.widget = require("wibox.widget")
wibox.drawable = require("wibox.drawable") wibox.drawable = require("wibox.drawable")
--- Set the widget that the wibox displays --- Set the widget that the wibox displays
function wibox.set_widget(_wibox, widget) function wibox:set_widget(widget)
_wibox._drawable:set_widget(widget) self._drawable:set_widget(widget)
end end
--- Set the background of the wibox --- Set the background of the wibox
-- @param wibox The wibox to use
-- @param c The background to use. This must either be a cairo pattern object, -- @param c The background to use. This must either be a cairo pattern object,
-- nil or a string that gears.color() understands. -- nil or a string that gears.color() understands.
function wibox.set_bg(_wibox, c) function wibox:set_bg(c)
_wibox._drawable:set_bg(c) self._drawable:set_bg(c)
end end
--- Set the foreground of the wibox --- Set the foreground of the wibox
-- @param wibox The wibox to use
-- @param c The foreground to use. This must either be a cairo pattern object, -- @param c The foreground to use. This must either be a cairo pattern object,
-- nil or a string that gears.color() understands. -- nil or a string that gears.color() understands.
function wibox.set_fg(_wibox, c) function wibox:set_fg(c)
_wibox._drawable:set_fg(c) self._drawable:set_fg(c)
end end
--- Helper function to make wibox:buttons() work as expected --- Helper function to make wibox:buttons() work as expected
function wibox.buttons(box, ...) function wibox:buttons(...)
return box.drawin:buttons(...) return self.drawin:buttons(...)
end end
--- Helper function to make wibox:struts() work as expected --- Helper function to make wibox:struts() work as expected
function wibox.struts(box, ...) function wibox:struts(...)
return box.drawin:struts(...) return self.drawin:struts(...)
end end
--- Helper function to make wibox:geometry() work as expected --- Helper function to make wibox:geometry() work as expected
function wibox.geometry(box, ...) function wibox:geometry(...)
return box.drawin:geometry(...) return self.drawin:geometry(...)
end end
local function setup_signals(_wibox) local function setup_signals(_wibox)

View File

@ -74,28 +74,28 @@ local function widget_changed(layout, old_w, new_w)
end end
--- Set the layout's first widget. This is the widget that is at the left/top --- Set the layout's first widget. This is the widget that is at the left/top
function align.set_first(layout, widget) function align:set_first(widget)
widget_changed(layout, layout.first, widget) widget_changed(self, self.first, widget)
layout.first = widget self.first = widget
end end
--- Set the layout's second widget. This is the centered one. --- Set the layout's second widget. This is the centered one.
function align.set_second(layout, widget) function align:set_second(widget)
widget_changed(layout, layout.second, widget) widget_changed(self, self.second, widget)
layout.second = widget self.second = widget
end end
--- Set the layout's third widget. This is the widget that is at the right/bottom --- Set the layout's third widget. This is the widget that is at the right/bottom
function align.set_third(layout, widget) function align:set_third(widget)
widget_changed(layout, layout.third, widget) widget_changed(self, self.third, widget)
layout.third = widget self.third = widget
end end
function align.reset(layout) function align:reset()
for k, v in pairs({ "first", "second", "third" }) do for k, v in pairs({ "first", "second", "third" }) do
layout[v] = nil self[v] = nil
end end
layout:emit_signal("widget::updated") self:emit_signal("widget::updated")
end end
local function get_layout(dir) local function get_layout(dir)

View File

@ -54,11 +54,11 @@ function fixed.draw_fixed(dir, widgets, fill_space, wibox, cr, width, height)
end end
--- Add a widget to the given fixed layout --- Add a widget to the given fixed layout
function fixed.add(layout, widget) function fixed:add(widget)
widget_base.check_widget(widget) widget_base.check_widget(widget)
table.insert(layout.widgets, widget) table.insert(self.widgets, widget)
widget:connect_signal("widget::updated", layout._emit_updated) widget:connect_signal("widget::updated", self._emit_updated)
layout._emit_updated() self._emit_updated()
end end
--- Fit the fixed layout into the given space --- Fit the fixed layout into the given space
@ -102,20 +102,20 @@ function fixed.fit_fixed(dir, widgets, orig_width, orig_height)
end end
--- Reset a fixed layout. This removes all widgets from the layout. --- Reset a fixed layout. This removes all widgets from the layout.
function fixed.reset(layout) function fixed:reset()
for k, v in pairs(layout.widgets) do for k, v in pairs(self.widgets) do
v:disconnect_signal("widget::updated", layout._emit_updated) v:disconnect_signal("widget::updated", self._emit_updated)
end end
layout.widgets = {} self.widgets = {}
layout:emit_signal("widget::updated") self:emit_signal("widget::updated")
end end
--- Set the layout's fill_space property. If this property is true, the last --- Set the layout's fill_space property. If this property is true, the last
-- widget will get all the space that is left. If this is false, the last widget -- widget will get all the space that is left. If this is false, the last widget
-- won't be handled specially and there can be space left unused. -- won't be handled specially and there can be space left unused.
function fixed.fill_space(layout, val) function fixed:fill_space(val)
layout._fill_space = val self._fill_space = val
layout:emit_signal("widget::updated") self:emit_signal("widget::updated")
end end
local function get_layout(dir) local function get_layout(dir)

View File

@ -14,56 +14,56 @@ local widget_base = require("wibox.widget.base")
local margin = { mt = {} } local margin = { mt = {} }
--- Draw a margin layout --- Draw a margin layout
function margin.draw(layout, wibox, cr, width, height) function margin:draw(wibox, cr, width, height)
local x = layout.left local x = self.left
local y = layout.top local y = self.top
local w = layout.right local w = self.right
local h = layout.bottom local h = self.bottom
if not layout.widget or width <= x + w or height <= y + h then if not self.widget or width <= x + w or height <= y + h then
return return
end end
base.draw_widget(wibox, cr, layout.widget, x, y, width - x - w, height - y - h) base.draw_widget(wibox, cr, self.widget, x, y, width - x - w, height - y - h)
end end
--- Fit a margin layout into the given space --- Fit a margin layout into the given space
function margin.fit(layout, width, height) function margin:fit(width, height)
local extra_w = layout.left + layout.right local extra_w = self.left + self.right
local extra_h = layout.top + layout.bottom local extra_h = self.top + self.bottom
local w, h = 0, 0 local w, h = 0, 0
if layout.widget then if self.widget then
w, h = layout.widget:fit(width - extra_w, height - extra_h) w, h = self.widget:fit(width - extra_w, height - extra_h)
end end
return w + extra_w, h + extra_h return w + extra_w, h + extra_h
end end
--- Set the widget that this layout adds a margin on. --- Set the widget that this layout adds a margin on.
function margin.set_widget(layout, widget) function margin:set_widget(widget)
if layout.widget then if self.widget then
layout.widget:disconnect_signal("widget::updated", layout._emit_updated) self.widget:disconnect_signal("widget::updated", self._emit_updated)
end end
if widget then if widget then
widget_base.check_widget(widget) widget_base.check_widget(widget)
widget:connect_signal("widget::updated", layout._emit_updated) widget:connect_signal("widget::updated", self._emit_updated)
end end
layout.widget = widget self.widget = widget
layout._emit_updated() self._emit_updated()
end end
--- Set all the margins to val. --- Set all the margins to val.
function margin.set_margins(layout, val) function margin:set_margins(val)
layout.left = val self.left = val
layout.right = val self.right = val
layout.top = val self.top = val
layout.bottom = val self.bottom = val
layout:emit_signal("widget::updated") self:emit_signal("widget::updated")
end end
--- Reset this layout. The widget will be unreferenced and the margins set to 0. --- Reset this layout. The widget will be unreferenced and the margins set to 0.
function margin.reset(layout) function margin:reset()
layout:set_widget(nil) self:set_widget(nil)
layout:set_margins(0) self:set_margins(0)
end end
--- Set the left margin that this layout adds to its widget. --- Set the left margin that this layout adds to its widget.

View File

@ -16,10 +16,10 @@ local widget_base = require("wibox.widget.base")
local mirror = { mt = {} } local mirror = { mt = {} }
--- Draw this layout --- Draw this layout
function mirror.draw(layout, wibox, cr, width, height) function mirror:draw(wibox, cr, width, height)
if not layout.widget then return { width = 0, height = 0 } end if not self.widget then return { width = 0, height = 0 } end
if not layout.horizontal and not layout.vertical then if not self.horizontal and not self.vertical then
layout.widget:draw(wibox, cr, width, height) self.widget:draw(wibox, cr, width, height)
return -- nothing changed return -- nothing changed
end end
@ -27,75 +27,71 @@ function mirror.draw(layout, wibox, cr, width, height)
local t = { x = 0, y = 0 } -- translation local t = { x = 0, y = 0 } -- translation
local s = { x = 1, y = 1 } -- scale local s = { x = 1, y = 1 } -- scale
if layout.horizontal then if self.horizontal then
t.y = height t.y = height
s.y = -1 s.y = -1
end end
if layout.vertical then if self.vertical then
t.x = width t.x = width
s.x = -1 s.x = -1
end end
cr:translate(t.x, t.y) cr:translate(t.x, t.y)
cr:scale(s.x, s.y) cr:scale(s.x, s.y)
layout.widget:draw(wibox, cr, width, height) self.widget:draw(wibox, cr, width, height)
-- Undo the scale and translation from above. -- Undo the scale and translation from above.
cr:restore() cr:restore()
end end
--- Fit this layout into the given area --- Fit this layout into the given area
function mirror.fit(layout, ...) function mirror:fit(...)
if not layout.widget then if not self.widget then
return 0, 0 return 0, 0
end end
return layout.widget:fit(...) return self.widget:fit(...)
end end
--- Set the widget that this layout mirrors. --- Set the widget that this layout mirrors.
-- @param layout The layout
-- @param widget The widget to mirror -- @param widget The widget to mirror
function mirror.set_widget(layout, widget) function mirror:set_widget(widget)
if layout.widget then if self.widget then
layout.widget:disconnect_signal("widget::updated", layout._emit_updated) self.widget:disconnect_signal("widget::updated", self._emit_updated)
end end
if widget then if widget then
widget_base.check_widget(widget) widget_base.check_widget(widget)
widget:connect_signal("widget::updated", layout._emit_updated) widget:connect_signal("widget::updated", self._emit_updated)
end end
layout.widget = widget self.widget = widget
layout._emit_updated() self._emit_updated()
end end
--- Reset this layout. The widget will be removed and the axes reset. --- Reset this layout. The widget will be removed and the axes reset.
-- @param layout The layout function mirror:reset()
function mirror.reset(layout) self.horizontal = false
layout.horizontal = false self.vertical = false
layout.vertical = false self:set_widget(nil)
layout:set_widget(nil)
end end
--- Set the reflection of this mirror layout. --- Set the reflection of this mirror layout.
-- @param layout The layout
-- @param reflection a table which contains new values for horizontal and/or vertical (booleans) -- @param reflection a table which contains new values for horizontal and/or vertical (booleans)
function mirror.set_reflection(layout, reflection) function mirror:set_reflection(reflection)
if type(reflection) ~= 'table' then if type(reflection) ~= 'table' then
error("Invalid type of reflection for mirror layout: " .. error("Invalid type of reflection for mirror layout: " ..
type(reflection) .. " (should be a table)") type(reflection) .. " (should be a table)")
end end
for _, ref in ipairs({"horizontal", "vertical"}) do for _, ref in ipairs({"horizontal", "vertical"}) do
if reflection[ref] ~= nil then if reflection[ref] ~= nil then
layout[ref] = reflection[ref] self[ref] = reflection[ref]
end end
end end
layout._emit_updated() self._emit_updated()
end end
--- Get the reflection of this mirror layout. --- Get the reflection of this mirror layout.
-- @param layout The layout
-- @return a table of booleans with the keys "horizontal", "vertical". -- @return a table of booleans with the keys "horizontal", "vertical".
function mirror.get_reflection(layout) function mirror:get_reflection()
return { horizontal = layout.horizontal, vertical = layout.vertical } return { horizontal = self.horizontal, vertical = self.vertical }
end end
--- Returns a new mirror layout. A mirror layout mirrors a given widget. Use --- Returns a new mirror layout. A mirror layout mirrors a given widget. Use

View File

@ -25,10 +25,10 @@ local function transform(layout, width, height)
end end
--- Draw this layout --- Draw this layout
function rotate.draw(layout, wibox, cr, width, height) function rotate:draw(wibox, cr, width, height)
if not layout.widget then return { width = 0, height = 0 } end if not self.widget then return { width = 0, height = 0 } end
local dir = layout:get_direction() local dir = self:get_direction()
if dir == "west" then if dir == "west" then
cr:rotate(pi / 2) cr:rotate(pi / 2)
@ -43,39 +43,39 @@ function rotate.draw(layout, wibox, cr, width, height)
-- Since we rotated, we might have to swap width and height. -- Since we rotated, we might have to swap width and height.
-- transform() does that for us. -- transform() does that for us.
base.draw_widget(wibox, cr, layout.widget, 0, 0, transform(layout, width, height)) base.draw_widget(wibox, cr, self.widget, 0, 0, transform(self, width, height))
end end
--- Fit this layout into the given area --- Fit this layout into the given area
function rotate.fit(layout, width, height) function rotate:fit(self, width, height)
if not layout.widget then if not self.widget then
return 0, 0 return 0, 0
end end
return transform(layout, layout.widget:fit(transform(layout, width, height))) return transform(self, self.widget:fit(transform(self, width, height)))
end end
--- Set the widget that this layout rotates. --- Set the widget that this layout rotates.
function rotate.set_widget(layout, widget) function rotate:set_widget(widget)
if layout.widget then if self.widget then
layout.widget:disconnect_signal("widget::updated", layout._emit_updated) self.widget:disconnect_signal("widget::updated", self._emit_updated)
end end
if widget then if widget then
widget_base.check_widget(widget) widget_base.check_widget(widget)
widget:connect_signal("widget::updated", layout._emit_updated) widget:connect_signal("widget::updated", self._emit_updated)
end end
layout.widget = widget self.widget = widget
layout._emit_updated() self._emit_updated()
end end
--- Reset this layout. The widget will be removed and the rotation reset. --- Reset this layout. The widget will be removed and the rotation reset.
function rotate.reset(layout) function rotate:reset()
layout.direction = nil self.direction = nil
layout:set_widget(nil) self:set_widget(nil)
end end
--- Set the direction of this rotating layout. Valid values are "north", "east", --- Set the direction of this rotating layout. Valid values are "north", "east",
-- "south" and "west". On an invalid value, this function will throw an error. -- "south" and "west". On an invalid value, this function will throw an error.
function rotate.set_direction(layout, dir) function rotate:set_direction(dir)
local allowed = { local allowed = {
north = true, north = true,
east = true, east = true,
@ -87,13 +87,13 @@ function rotate.set_direction(layout, dir)
error("Invalid direction for rotate layout: " .. tostring(dir)) error("Invalid direction for rotate layout: " .. tostring(dir))
end end
layout.direction = dir self.direction = dir
layout._emit_updated() self._emit_updated()
end end
--- Get the direction of this rotating layout --- Get the direction of this rotating layout
function rotate.get_direction(layout) function rotate:get_direction()
return layout.direction or "north" return self.direction or "north"
end end
--- Returns a new rotate layout. A rotate layout rotates a given widget. Use --- Returns a new rotate layout. A rotate layout rotates a given widget. Use

View File

@ -17,81 +17,81 @@ local type = type
local background = { mt = {} } local background = { mt = {} }
--- Draw this widget --- Draw this widget
function background.draw(box, wibox, cr, width, height) function background:draw(wibox, cr, width, height)
if not box.widget then if not self.widget then
return return
end end
cr:save() cr:save()
if box.background then if self.background then
cr:set_source(box.background) cr:set_source(self.background)
cr:paint() cr:paint()
end end
if box.bgimage then if self.bgimage then
local pattern = cairo.Pattern.create_for_surface(box.bgimage) local pattern = cairo.Pattern.create_for_surface(self.bgimage)
cr:set_source(pattern) cr:set_source(pattern)
cr:paint() cr:paint()
end end
cr:restore() cr:restore()
if box.foreground then if self.foreground then
cr:save() cr:save()
cr:set_source(box.foreground) cr:set_source(self.foreground)
end end
layout_base.draw_widget(wibox, cr, box.widget, 0, 0, width, height) layout_base.draw_widget(wibox, cr, self.widget, 0, 0, width, height)
if box.foreground then if self.foreground then
cr:restore() cr:restore()
end end
end end
--- Fit this widget into the given area --- Fit this widget into the given area
function background.fit(box, width, height) function background:fit(width, height)
if not box.widget then if not self.widget then
return 0, 0 return 0, 0
end end
return box.widget:fit(width, height) return self.widget:fit(width, height)
end end
--- Set the widget that is drawn on top of the background --- Set the widget that is drawn on top of the background
function background.set_widget(box, widget) function background:set_widget(widget)
if box.widget then if self.widget then
box.widget:disconnect_signal("widget::updated", box._emit_updated) self.widget:disconnect_signal("widget::updated", self._emit_updated)
end end
if widget then if widget then
base.check_widget(widget) base.check_widget(widget)
widget:connect_signal("widget::updated", box._emit_updated) widget:connect_signal("widget::updated", self._emit_updated)
end end
box.widget = widget self.widget = widget
box._emit_updated() self._emit_updated()
end end
--- Set the background to use --- Set the background to use
function background.set_bg(box, bg) function background:set_bg(bg)
if bg then if bg then
box.background = color(bg) self.background = color(bg)
else else
box.background = nil self.background = nil
end end
box._emit_updated() self._emit_updated()
end end
--- Set the foreground to use --- Set the foreground to use
function background.set_fg(box, fg) function background:set_fg(fg)
if fg then if fg then
box.foreground = color(fg) self.foreground = color(fg)
else else
box.foreground = nil self.foreground = nil
end end
box._emit_updated() self._emit_updated()
end end
--- Set the background image to use --- Set the background image to use
function background.set_bgimage(box, image) function background:set_bgimage(image)
box.bgimage = surface.load(image) self.bgimage = surface.load(image)
box._emit_updated() self._emit_updated()
end end
local function new() local function new()

View File

@ -14,12 +14,12 @@ local table = table
local base = {} local base = {}
--- Set/get a widget's buttons --- Set/get a widget's buttons
function base.buttons(widget, _buttons) function base:buttons(_buttons)
if _buttons then if _buttons then
widget.widget_buttons = _buttons self.widget_buttons = _buttons
end end
return widget.widget_buttons return self.widget_buttons
end end
--- Handle a button event on a widget. This is used internally. --- Handle a button event on a widget. This is used internally.

View File

@ -16,35 +16,35 @@ local print = print
local imagebox = { mt = {} } local imagebox = { mt = {} }
--- Draw an imagebox with the given cairo context in the given geometry. --- Draw an imagebox with the given cairo context in the given geometry.
function imagebox.draw(box, wibox, cr, width, height) function imagebox:draw(wibox, cr, width, height)
if not box.image then return end if not self.image then return end
cr:save() cr:save()
if not box.resize_forbidden then if not self.resize_forbidden then
-- Let's scale the image so that it fits into (width, height) -- Let's scale the image so that it fits into (width, height)
local w = box.image:get_width() local w = self.image:get_width()
local h = box.image:get_height() local h = self.image:get_height()
local aspect = width / w local aspect = width / w
local aspect_h = height / h local aspect_h = height / h
if aspect > aspect_h then aspect = aspect_h end if aspect > aspect_h then aspect = aspect_h end
cr:scale(aspect, aspect) cr:scale(aspect, aspect)
end end
cr:set_source_surface(box.image, 0, 0) cr:set_source_surface(self.image, 0, 0)
cr:paint() cr:paint()
cr:restore() cr:restore()
end end
--- Fit the imagebox into the given geometry --- Fit the imagebox into the given geometry
function imagebox.fit(box, width, height) function imagebox:fit(width, height)
if not box.image then if not self.image then
return 0, 0 return 0, 0
end end
local w = box.image:get_width() local w = self.image:get_width()
local h = box.image:get_height() local h = self.image:get_height()
if w > width then if w > width then
h = h * width / w h = h * width / w
@ -55,7 +55,7 @@ function imagebox.fit(box, width, height)
h = height h = height
end end
if not box.resize_forbidden then if not self.resize_forbidden then
local aspect = width / w local aspect = width / w
local aspect_h = height / h local aspect_h = height / h
@ -71,7 +71,7 @@ end
--- Set an imagebox' image --- Set an imagebox' image
-- @param image Either a string or a cairo image surface. A string is -- @param image Either a string or a cairo image surface. A string is
-- interpreted as the path to a png image file. -- interpreted as the path to a png image file.
function imagebox.set_image(box, image) function imagebox:set_image(image)
local image = image local image = image
if type(image) == "string" then if type(image) == "string" then
@ -93,18 +93,18 @@ function imagebox.set_image(box, image)
end end
end end
box.image = image self.image = image
box:emit_signal("widget::updated") self:emit_signal("widget::updated")
return true return true
end end
--- Should the image be resized to fit into the available space? --- Should the image be resized to fit into the available space?
-- @param allowed If false, the image will be clipped, else it will be resized -- @param allowed If false, the image will be clipped, else it will be resized
-- to fit into the available space. -- to fit into the available space.
function imagebox.set_resize(box, allowed) function imagebox:set_resize(allowed)
box.resize_forbidden = not allowed self.resize_forbidden = not allowed
box:emit_signal("widget::updated") self:emit_signal("widget::updated")
end end
-- Returns a new imagebox -- Returns a new imagebox

View File

@ -18,7 +18,7 @@ local created_systray = false
local horizontal = true local horizontal = true
local base_size = nil local base_size = nil
function systray.draw(box, wibox, cr, width, height) function systray:draw(wibox, cr, width, height)
local x, y, width, height = lbase.rect_to_device_geometry(cr, 0, 0, width, height) local x, y, width, height = lbase.rect_to_device_geometry(cr, 0, 0, width, height)
local num_entries = capi.awesome.systray() local num_entries = capi.awesome.systray()
local bg = beautiful.bg_systray or beautiful.bg_normal local bg = beautiful.bg_systray or beautiful.bg_normal
@ -37,7 +37,7 @@ function systray.draw(box, wibox, cr, width, height)
capi.awesome.systray(wibox.drawin, x, y, base, horizontal, bg) capi.awesome.systray(wibox.drawin, x, y, base, horizontal, bg)
end end
function systray.fit(box, width, height) function systray:fit(width, height)
local num_entries = capi.awesome.systray() local num_entries = capi.awesome.systray()
local base = base_size local base = base_size
if base == nil then if base == nil then

View File

@ -27,91 +27,91 @@ local function setup_layout(box, width, height)
end end
--- Draw the given textbox on the given cairo context in the given geometry --- Draw the given textbox on the given cairo context in the given geometry
function textbox.draw(box, wibox, cr, width, height) function textbox:draw(wibox, cr, width, height)
cr:update_layout(box._layout) cr:update_layout(self._layout)
setup_layout(box, width, height) setup_layout(self, width, height)
local ink, logical = box._layout:get_pixel_extents() local ink, logical = self._layout:get_pixel_extents()
local offset = 0 local offset = 0
if box._valign == "center" then if self._valign == "center" then
offset = (height - logical.height) / 2 offset = (height - logical.height) / 2
elseif box._valign == "bottom" then elseif self._valign == "bottom" then
offset = height - logical.height offset = height - logical.height
end end
cr:move_to(0, offset) cr:move_to(0, offset)
cr:show_layout(box._layout) cr:show_layout(self._layout)
end end
--- Fit the given textbox --- Fit the given textbox
function textbox.fit(box, width, height) function textbox:fit(width, height)
setup_layout(box, width, height) setup_layout(self, width, height)
local ink, logical = box._layout:get_pixel_extents() local ink, logical = self._layout:get_pixel_extents()
return logical.width, logical.height return logical.width, logical.height
end end
--- Set a textbox' text. --- Set a textbox' text.
-- @param text The text to set. This can contain pango markup (e.g. <b>bold</b>) -- @param text The text to set. This can contain pango markup (e.g. <b>bold</b>)
function textbox.set_markup(box, text) function textbox:set_markup(text)
local attr, parsed = Pango.parse_markup(text, -1, 0) local attr, parsed = Pango.parse_markup(text, -1, 0)
-- In case of error, attr is false and parsed is an error message -- In case of error, attr is false and parsed is an error message
if not attr then error(parsed) end if not attr then error(parsed) end
box._layout.text = parsed self._layout.text = parsed
box._layout.attributes = attr self._layout.attributes = attr
box:emit_signal("widget::updated") self:emit_signal("widget::updated")
end end
--- Set a textbox' text. --- Set a textbox' text.
-- @param text The text to display. Pango markup is ignored and shown as-is. -- @param text The text to display. Pango markup is ignored and shown as-is.
function textbox.set_text(box, text) function textbox:set_text(text)
box._layout.text = text self._layout.text = text
box._layout.attributes = nil self._layout.attributes = nil
box:emit_signal("widget::updated") self:emit_signal("widget::updated")
end end
--- Set a textbox' ellipsize mode. --- Set a textbox' ellipsize mode.
-- @param mode Where should long lines be shortened? "start", "middle" or "end" -- @param mode Where should long lines be shortened? "start", "middle" or "end"
function textbox.set_ellipsize(box, mode) function textbox:set_ellipsize(mode)
local allowed = { none = "NONE", start = "START", middle = "MIDDLE", ["end"] = "END" } local allowed = { none = "NONE", start = "START", middle = "MIDDLE", ["end"] = "END" }
if allowed[mode] then if allowed[mode] then
box._layout:set_ellipsize(allowed[mode]) self._layout:set_ellipsize(allowed[mode])
box:emit_signal("widget::updated") self:emit_signal("widget::updated")
end end
end end
--- Set a textbox' wrap mode. --- Set a textbox' wrap mode.
-- @param mode Where to wrap? After "word", "char" or "word_char" -- @param mode Where to wrap? After "word", "char" or "word_char"
function textbox.set_wrap(box, mode) function textbox:set_wrap(mode)
local allowed = { word = "WORD", char = "CHAR", word_char = "WORD_CHAR" } local allowed = { word = "WORD", char = "CHAR", word_char = "WORD_CHAR" }
if allowed[mode] then if allowed[mode] then
box._layout:set_wrap(allowed[mode]) self._layout:set_wrap(allowed[mode])
box:emit_signal("widget::updated") self:emit_signal("widget::updated")
end end
end end
--- Set a textbox' vertical alignment --- Set a textbox' vertical alignment
-- @param mode Where should the textbox be drawn? "top", "center" or "bottom" -- @param mode Where should the textbox be drawn? "top", "center" or "bottom"
function textbox.set_valign(box, mode) function textbox:set_valign(mode)
local allowed = { top = true, center = true, bottom = true } local allowed = { top = true, center = true, bottom = true }
if allowed[mode] then if allowed[mode] then
box._valign = mode self._valign = mode
box:emit_signal("widget::updated") self:emit_signal("widget::updated")
end end
end end
--- Set a textbox' horizontal alignment --- Set a textbox' horizontal alignment
-- @param mode Where should the textbox be drawn? "left", "center" or "right" -- @param mode Where should the textbox be drawn? "left", "center" or "right"
function textbox.set_align(box, mode) function textbox:set_align(mode)
local allowed = { left = "LEFT", center = "CENTER", right = "RIGHT" } local allowed = { left = "LEFT", center = "CENTER", right = "RIGHT" }
if allowed[mode] then if allowed[mode] then
box._layout:set_alignment(allowed[mode]) self._layout:set_alignment(allowed[mode])
box:emit_signal("widget::updated") self:emit_signal("widget::updated")
end end
end end
--- Set a textbox' font --- Set a textbox' font
-- @param font The font description as string -- @param font The font description as string
function textbox.set_font(box, font) function textbox:set_font(font)
box._layout:set_font_description(beautiful.get_font(font)) self._layout:set_font_description(beautiful.get_font(font))
end end
-- Returns a new textbox -- Returns a new textbox