diff --git a/widgets/checkbox.lua b/widgets/checkbox.lua index 2bfc844..eeb1515 100644 --- a/widgets/checkbox.lua +++ b/widgets/checkbox.lua @@ -1,32 +1,27 @@ -local color = require("gears.color") -local cairo = require( "lgi" ).cairo +--------------------------------------------------------------------------- +-- A boolean display widget. +-- +--@DOC_wibox_widget_defaults_checkobox_EXAMPLE@ +-- @author Emmanuel Lepage Valle +-- @copyright 2012 Emmanuel Lepage Vallee +-- @release @AWESOME_VERSION@ +-- @classmod wibox.widget.checkbox +--------------------------------------------------------------------------- -local beautiful = require( "beautiful" ) +local color = require( "gears.color" ) +local base = require( "wibox.widget.base" ) +local beautiful = require( "beautiful" ) local module = {} -local checkedI -local notcheckedI -local isinit = false - -local function init() - local size = beautiful.menu_height or 16 - notcheckedI = cairo.ImageSurface(cairo.Format.ARGB32, size,size) - checkedI = cairo.ImageSurface(cairo.Format.ARGB32, size,size) - local cr2 = cairo.Context(notcheckedI) - local cr = cairo.Context(checkedI) - cr:set_operator(cairo.Operator.CLEAR) - cr2:set_operator(cairo.Operator.CLEAR) - cr:paint() - cr2:paint() - cr:set_operator(cairo.Operator.SOURCE) - cr2:set_operator(cairo.Operator.SOURCE) +local function default_checked(self, context, cr, width, height) + local size = math.min(width, height) local sp = size*0.15 local rs = size - (2*sp) - cr:set_source(color(beautiful.fg_normal)) - cr2:set_source(color(beautiful.fg_normal)) - cr:set_line_width(2) - cr2:set_line_width(2) + + cr:set_line_width(1) +-- cr:translate(1,1) +-- cr:set_antialias(1) cr:move_to( sp , sp );cr:line_to( rs , sp ) cr:move_to( sp , sp );cr:line_to( sp , rs ) cr:move_to( sp , rs );cr:line_to( rs , rs ) @@ -34,64 +29,141 @@ local function init() cr:move_to( sp , sp );cr:line_to( rs , rs ) cr:move_to( sp , rs );cr:line_to( rs , sp ) cr:stroke() - - cr2:move_to( sp , sp );cr2:line_to (rs , sp , beautiful.fg_normal ) - cr2:move_to( sp , sp );cr2:line_to (sp , rs , beautiful.fg_normal ) - cr2:move_to( sp , rs );cr2:line_to (rs , rs , beautiful.fg_normal ) - cr2:move_to( rs , sp );cr2:line_to (rs , rs , beautiful.fg_normal ) - cr2:stroke() - - isinit = true end -local function holo() - local size = beautiful.menu_height - 2 or 16 - notcheckedI = cairo.ImageSurface(cairo.Format.ARGB32, size,size) - checkedI = cairo.ImageSurface(cairo.Format.ARGB32, size,size) - local cr2 = cairo.Context(notcheckedI) - local cr = cairo.Context(checkedI) - cr:translate(1,1) - cr2:translate(1,1) - size = size - 2 - cr:set_operator(cairo.Operator.CLEAR) - cr2:set_operator(cairo.Operator.CLEAR) - cr:paint() - cr2:paint() - cr:set_operator(cairo.Operator.SOURCE) - cr2:set_operator(cairo.Operator.SOURCE) - local col = color(beautiful.menu_outline_color or beautiful.menu_border_color or beautiful.border_color) - cr:set_source(col) - cr2:set_source(col) +local function default_unchecked(self, context, cr, width, height) + local size = math.min(width, height) + local sp = size*0.15 + local rs = size - (2*sp) + cr:set_line_width(1) - cr2:set_line_width(1) - size = size -2 +-- cr:translate(1,1) +-- cr:set_antialias(1) + cr:move_to( sp , sp );cr:line_to (rs , sp ) + cr:move_to( sp , sp );cr:line_to (sp , rs ) + cr:move_to( sp , rs );cr:line_to (rs , rs ) + cr:move_to( rs , sp );cr:line_to (rs , rs ) + cr:stroke() +end + +local function holo_checked(self, context, cr, width, height) + local size = math.min(width, height) - 2 + cr:arc(size/2+1,size/2+1,size/2,0,math.pi*2) cr:stroke() - cr2:arc(size/2+1,size/2+1,size/2,0,math.pi*2) - cr2:stroke() - size = size - 8 - cr:set_source(color(beautiful.fg_normal)) + + size = size*0.5 + cr:arc(size/2+5,size/2+5,size/2,0,math.pi*2) cr:fill() end +local function holo_unchecked(self, context, cr, width, height) + local size = math.min(width, height) - 2 + + cr:arc(size/2+1,size/2+1,size/2,0,math.pi*2) + cr:stroke() +end + local style = { holo = holo, default = init, } -function module.checked() - if not isinit then - style[beautiful.menu_checkbox_style or "default"]() +local draw_f = { + default = { + [true ] = default_checked, + [false] = default_unchecked, + }, + holo = { + [true ] = holo_checked, + [false] = holo_unchecked, + } +} + +local function draw(self, context, cr, width, height) + local col = self._private.color + local checked = self._private.checked + local style = self._private.style + + if col then + cr:save() + cr:set_source(col) + end + + draw_f[style][checked](self, context, cr, width, height) + + if col then + cr:restore() end - return checkedI end -function module.unchecked() - if not isinit then - style[beautiful.menu_checkbox_style or "default"]() - end - return notcheckedI +local function fit(box, context, w, h) + local size = math.min(w, h) + return size, size end -return module +--- If the checkbox is checked. +-- @property checked +-- @param boolean + +local function get_checked(self) + return self._private.checked +end + +local function set_checked(self, value) + self._private.checked = value or false + + self:emit_signal("widget::redraw_needed") +end + +--- The default checkbox style. +-- @beautiful beautiful.checkbox_style +-- @tparam[opt="default"] string checkbox_style + +--- The checkbox style. +-- @property style +-- @param string + +local function get_style(self) + return self._private.style +end + +local function set_style(self, value) + assert(type(value) == "string") + assert(style[value]) + + self._private.style = value + self:emit_signal("widget::redraw_needed") +end + +local function get_color(self) + return self._private.color +end + +local function set_color(self, value) + self._private.color = color(value) + + self:emit_signal("widget::redraw_needed") +end + +--- The checkbox color. +-- @property color + +local function new(checked, args) + checked, args = checked or false, args or {} + + local w = base.make_widget() + w._private.style = args.style or beautiful.checkbox_style or "default" + w._private.checked = checked + w._private.color = args.color and color(args.color) or nil + + rawset(w, "fit" , fit ) + rawset(w, "draw", draw) + + return w +end + +return setmetatable(module, { __call = function(_, ...) return new(...) end}) + +-- kate: space-indent on; indent-width 4; replace-tabs on; diff --git a/widgets/init.lua b/widgets/init.lua index 86dbe74..ca0be2e 100644 --- a/widgets/init.lua +++ b/widgets/init.lua @@ -1,7 +1,8 @@ -- Do some evil monkeypatching for upstreamable widgets local wibox = require("wibox") -wibox.layout.grid = require("radical.widgets.grid") +wibox.layout.grid = require( "radical.widgets.grid" ) +wibox.widget.checkbox = require( "radical.widgets.checkbox" ) return { checkbox = require( "radical.widgets.checkbox" ),