Merge pull request #586 from psychon/widget_funcs

Widgets: set_width / set_height
This commit is contained in:
Daniel Hahler 2015-12-12 19:13:28 +01:00
commit 981987764e
1 changed files with 77 additions and 29 deletions

View File

@ -15,6 +15,66 @@ local table = table
local base = {} local base = {}
-- {{{ Functions on widgets
--- Functions available on all widgets
base.widget = {}
--- Set/get a widget's buttons.
-- @param _buttons The table of buttons that should bind to the widget.
function base.widget:buttons(_buttons)
if _buttons then
self.widget_buttons = _buttons
end
return self.widget_buttons
end
--- Set a widget's visible property
-- @tparam boolean b Wether the widget is visible at all
function base.widget:set_visible(b)
if b ~= self.visible then
self.visible = b
self:emit_signal("widget::layout_changed")
-- In case something ignored fit and drew the widget anyway
self:emit_signal("widget::redraw_needed")
end
end
--- Set a widget's opacity
-- @tparam number o The opacity to use (a number from 0 to 1). 0 is fully
-- transparent while 1 is fully opaque.
function base.widget:set_opacity(o)
if o ~= self.opacity then
self.opacity = o
self:emit_signal("widget::redraw")
end
end
--- Set the widget's width
-- @tparam number|nil s The width that the widget has. `nil` means to apply the
-- default mechanism of calling the `:fit` method. A number overrides the result
-- from `:fit`.
function base.widget:set_width(s)
if s ~= self._forced_width then
self._forced_width = s
self:emit_signal("widget::layout_changed")
end
end
--- Set the widget's height
-- @tparam number|nil s The height that the widget has. `nil` means to apply the
-- default mechanism of calling the `:fit` method. A number overrides the result
-- from `:fit`.
function base.widget:set_height(s)
if s ~= self._forced_height then
self._forced_height = s
self:emit_signal("widget::layout_changed")
end
end
-- }}}
-- {{{ Caches -- {{{ Caches
-- Indexes are widgets, allow them to be garbage-collected -- Indexes are widgets, allow them to be garbage-collected
@ -103,6 +163,10 @@ function base.fit_widget(parent, context, widget, width, height)
end end
end end
-- Apply forced size
w = widget._forced_width or w
h = widget._forced_height or h
-- Also sanitize the output. -- Also sanitize the output.
w = math.max(0, math.min(w, width)) w = math.max(0, math.min(w, width))
h = math.max(0, math.min(h, height)) h = math.max(0, math.min(h, height))
@ -135,16 +199,6 @@ function base.layout_widget(parent, context, widget, width, height)
end end
end end
--- Set/get a widget's buttons.
-- This function is available on widgets created by @{make_widget}.
function base:buttons(_buttons)
if _buttons then
self.widget_buttons = _buttons
end
return self.widget_buttons
end
-- Handle a button event on a widget. This is used internally and should not be -- Handle a button event on a widget. This is used internally and should not be
-- called directly. -- called directly.
function base.handle_button(event, widget, x, y, button, modifiers, geometry) function base.handle_button(event, widget, x, y, button, modifiers, geometry)
@ -359,7 +413,16 @@ function base.make_widget(proxy, widget_name)
-- No buttons yet -- No buttons yet
ret.widget_buttons = {} ret.widget_buttons = {}
ret.buttons = base.buttons
-- Widget is visible
ret.visible = true
-- Widget is fully opaque
ret.opacity = 1
-- Size is not restricted/forced
ret._forced_width = nil
ret._forced_height = nil
-- Make buttons work -- Make buttons work
ret:connect_signal("button::press", function(...) ret:connect_signal("button::press", function(...)
@ -390,24 +453,9 @@ function base.make_widget(proxy, widget_name)
clear_caches(ret) clear_caches(ret)
end) end)
-- Add visible property and setter. -- Add functions
ret.visible = true for k, v in pairs(base.widget) do
function ret:set_visible(b) ret[k] = v
if b ~= self.visible then
self.visible = b
self:emit_signal("widget::layout_changed")
-- In case something ignored fit and drew the widget anyway
self:emit_signal("widget::redraw_needed")
end
end
-- Add opacity property and setter.
ret.opacity = 1
function ret:set_opacity(b)
if b ~= self.opacity then
self.opacity = b
self:emit_signal("widget::redraw")
end
end end
-- Add __tostring method to metatable. -- Add __tostring method to metatable.