fixed: Enable the property system

And add the missing documentation
This commit is contained in:
Emmanuel Lepage Vallee 2016-05-26 19:41:58 -04:00
parent 18500838d9
commit b89fbcf940
2 changed files with 47 additions and 41 deletions

View File

@ -29,11 +29,9 @@
-- @name swap_widgets
-- @class function
--- Get all children of this layout.
--- Get all direct children of this layout.
-- @param layout The layout you are modifying.
-- @return a list of all widgets
-- @name get_children
-- @class function
-- @property children
--- Reset a ratio layout. This removes all widgets from the layout.
-- @param layout The layout you are modifying.

View File

@ -23,28 +23,28 @@ local fixed = {}
-- @param height The available height.
function fixed:layout(context, width, height)
local result = {}
local pos,spacing = 0, self._spacing
local pos,spacing = 0, self._private.spacing
for k, v in pairs(self.widgets) do
for k, v in pairs(self._private.widgets) do
local x, y, w, h, _
if self.dir == "y" then
if self._private.dir == "y" then
x, y = 0, pos
w, h = width, height - pos
if k ~= #self.widgets or not self._fill_space then
if k ~= #self._private.widgets or not self._private.fill_space then
_, h = base.fit_widget(self, context, v, w, h);
end
pos = pos + h + spacing
else
x, y = pos, 0
w, h = width - pos, height
if k ~= #self.widgets or not self._fill_space then
if k ~= #self._private.widgets or not self._private.fill_space then
w, _ = base.fit_widget(self, context, v, w, h);
end
pos = pos + w + spacing
end
if (self.dir == "y" and pos-spacing > height) or
(self.dir ~= "y" and pos-spacing > width) then
if (self._private.dir == "y" and pos-spacing > height) or
(self._private.dir ~= "y" and pos-spacing > width) then
break
end
table.insert(result, base.place_widget_at(v, x, y, w, h))
@ -60,7 +60,7 @@ function fixed:add(...)
assert(args.n > 0, "need at least one widget to add")
for i=1, args.n do
base.check_widget(args[i])
table.insert(self.widgets, args[i])
table.insert(self._private.widgets, args[i])
end
self:emit_signal("widget::layout_changed")
end
@ -70,9 +70,9 @@ end
-- @tparam number index The widget index to remove
-- @treturn boolean index If the operation is successful
function fixed:remove(index)
if not index or index < 1 or index > #self.widgets then return false end
if not index or index < 1 or index > #self._private.widgets then return false end
table.remove(self.widgets, index)
table.remove(self._private.widgets, index)
self:emit_signal("widget::layout_changed")
@ -106,14 +106,10 @@ function fixed:remove_widgets(...)
return #args > (recursive and 1 or 0) and ret
end
--- Get all children of this layout
-- @treturn table a list of all widgets
function fixed:get_children()
return self.widgets
return self._private.widgets
end
--- Replace the layout children
-- @tparam table children A table composed of valid widgets
function fixed:set_children(children)
self:reset()
if #children > 0 then
@ -138,12 +134,12 @@ function fixed:replace_widget(widget, widget2, recursive)
end
function fixed:swap(index1, index2)
if not index1 or not index2 or index1 > #self.widgets
or index2 > #self.widgets then
if not index1 or not index2 or index1 > #self._private.widgets
or index2 > #self._private.widgets then
return false
end
local widget1, widget2 = self.widgets[index1], self.widgets[index2]
local widget1, widget2 = self._private.widgets[index1], self._private.widgets[index2]
self:set(index1, widget2)
self:set(index2, widget1)
@ -177,11 +173,11 @@ function fixed:swap_widgets(widget1, widget2, recursive)
end
function fixed:set(index, widget2)
if (not widget2) or (not self.widgets[index]) then return false end
if (not widget2) or (not self._private.widgets[index]) then return false end
base.check_widget(widget2)
self.widgets[index] = widget2
self._private.widgets[index] = widget2
self:emit_signal("widget::layout_changed")
@ -193,10 +189,10 @@ end
-- @param widget The widget
-- @treturn boolean If the operation is successful
function fixed:insert(index, widget)
if not index or index < 1 or index > #self.widgets + 1 then return false end
if not index or index < 1 or index > #self._private.widgets + 1 then return false end
base.check_widget(widget)
table.insert(self.widgets, index, widget)
table.insert(self._private.widgets, index, widget)
self:emit_signal("widget::layout_changed")
return true
@ -210,10 +206,10 @@ function fixed:fit(context, orig_width, orig_height)
local width, height = orig_width, orig_height
local used_in_dir, used_max = 0, 0
for _, v in pairs(self.widgets) do
for _, v in pairs(self._private.widgets) do
local w, h = base.fit_widget(self, context, v, width, height)
local in_dir, max
if self.dir == "y" then
if self._private.dir == "y" then
max, in_dir = w, h
height = height - in_dir
else
@ -226,7 +222,7 @@ function fixed:fit(context, orig_width, orig_height)
used_in_dir = used_in_dir + in_dir
if width <= 0 or height <= 0 then
if self.dir == "y" then
if self._private.dir == "y" then
used_in_dir = orig_height
else
used_in_dir = orig_width
@ -235,36 +231,38 @@ function fixed:fit(context, orig_width, orig_height)
end
end
local spacing = self._spacing * (#self.widgets-1)
local spacing = self._private.spacing * (#self._private.widgets-1)
if self.dir == "y" then
if self._private.dir == "y" then
return used_max, used_in_dir + spacing
end
return used_in_dir + spacing, used_max
end
function fixed:reset()
self.widgets = {}
self._private.widgets = {}
self:emit_signal("widget::layout_changed")
end
--- 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
-- won't be handled specially and there can be space left unused.
-- @property fill_space
function fixed:fill_space(val)
if self._fill_space ~= val then
self._fill_space = not not val
if self._private.fill_space ~= val then
self._private.fill_space = not not val
self:emit_signal("widget::layout_changed")
end
end
local function get_layout(dir, widget1, ...)
local ret = base.make_widget()
local ret = base.make_widget(nil, nil, {enable_properties = true})
util.table.crush(ret, fixed)
util.table.crush(ret, fixed, true)
ret.dir = dir
ret.widgets = {}
ret._private.dir = dir
ret._private.widgets = {}
ret:set_spacing(0)
ret:fill_space(false)
@ -294,14 +292,24 @@ function fixed.vertical(...)
end
--- Add spacing between each layout widgets
-- @param spacing Spacing between widgets.
-- @property spacing
-- @tparam number spacing Spacing between widgets.
function fixed:set_spacing(spacing)
if self._spacing ~= spacing then
self._spacing = spacing
if self._private.spacing ~= spacing then
self._private.spacing = spacing
self:emit_signal("widget::layout_changed")
end
end
function fixed:get_spacing()
return self._private.spacing or 0
end
--@DOC_widget_COMMON@
--@DOC_object_COMMON@
return fixed
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80