Thanks for that - works perfectly.
-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of abhi
Sent: Sunday, 14 June 2009 2:55 PM
To: [email protected]
Subject: Re: Custom widgets in Shoes
Daniel,
The code below fixes the problem. The ovals were overlapping since the
positions of the shape are relative to the container. I have done the
following to prevent the overlapping of the ovals and to fix a few
other problems.
1. wrap the oval within a flow of fixed height
2. specify positions of oval relative to the flow
3. fix the height of the widget, which by default contains a stack
container. This prevents wrapping of consecutive radio butttons.
Width = 12
Height = 14
OutlineColor = "#000000"
OutlineWidth = 2
DeselectedColor = "#CCCCCC"
class Shoes::Onedot < Shoes::Widget
attr_reader :selected_color
attr_reader :selected
def initialize(opts = {})
self.width = 20
self.height = 20
flow :height => 20, :width => 20 do
super()
selected = opts[:selected]
selected_color = opts[:selected_color]
stroke OutlineColor
strokewidth OutlineWidth
@oval = oval(4, 3, Width, Height)
draw_dot
@oval.click do
toggle
end
end
end
def change(&block)
@on_changed = block
end
def draw(container, boolean)
super(container, boolean)
end
def draw_dot
@oval.fill = (selected ? selected_color || royalblue : DeselectedColor)
end
def selected_color=(value)
@selected_color = value
draw_dot
end
def selected=(value)
@selected = value
changed
end
def toggle
# For some reason, selected= doesn't work properly...
@selected = !selected
changed
end
private
def changed
if @on_changed then
if @on_changed.arity == 0 then
@on_changed.call
else
@on_changed.call(selected)
end
end
draw_dot
end
end
Shoes.app do
stack do
sampledot = onedot
sampledot.selected = true
sampledot.selected_color = tomato
end
stack do
flow(:width => "100%") do
dot2 = onedot
dot3 = onedot
end
end
end
On Sun, Jun 14, 2009 at 1:51 AM, Daniel Heath<[email protected]>
wrote:
> Hi all,
>
> I'm trying to write a custom widget in shoes which is similar to a radio
> button, but is drawn using shapes to get a particular appearance.
>
> All works nicely except for placement. Shoes doesn't seem to realize what
> size the object is, so no space ends up being allocated for it (width &
> height of 0).
>
> The sample code below draws three of these, but they all get drawn in the
> same space. If I include a para or something, they get moved around - but
I
> would prefer shoes to recognize the width of the oval & position
everything
> accordingly.
>
> Thanks,
>
> Daniel
>
> Width = 12
> Height = 14
>
> OutlineColor = "#000000"
> OutlineWidth = 2
> DeselectedColor = "#CCCCCC"
>
> class Shoes::Onedot < Shoes::Widget
> attr_reader :selected_color
> attr_reader :selected
>
> def initialize(opts = {})
> super()
> selected = opts[:selected]
> selected_color = opts[:selected_color]
>
> stroke OutlineColor
> strokewidth OutlineWidth
> �...@oval = oval(top, left, Width, Height)
> draw_dot
> �[email protected] do
> toggle
> end
> end
>
> def change(&block)
> �...@on_changed = block
> end
>
> def draw(container, boolean)
> super(container, boolean)
> end
>
> def draw_dot
> �[email protected] = (selected ? selected_color || royalblue : DeselectedColor)
> end
>
> def selected_color=(value)
> �...@selected_color = value
> draw_dot
> end
>
> def selected=(value)
> �...@selected = value
> changed
> end
>
> def toggle
> # For some reason, selected= doesn't work properly...
> �...@selected = !selected
> changed
> end
>
> private
>
> def changed
> if @on_changed then
> if @on_changed.arity == 0 then
> �...@on_changed.call
> else
> �...@on_changed.call(selected)
> end
> end
> draw_dot
> end
>
> end
>
>
> Shoes.app do
>
> stack do
> sampledot = onedot
> sampledot.selected = true
> sampledot.selected_color = tomato
> end
>
> stack do
> flow(:width => "100%") do
> dot2 = onedot
> dot3 = onedot
> end
> end
>
> end
>
>
--
Queer little twists and quirks go into the making of an individual.
To suppress them all and follow clock and calendar and creed until the
individual is lost in the neutral gray of the host is to be less than
true to our inheritance.
Life, that gorgeous quality of life, is not accomplished by following
another man's rules.
It is true we have the same hungers and same thirsts, but they are for
different things and in different ways and in different seasons.
Lay down your own day, follow it to its noon, or you will sit in an
outer hall listening to the chimes but never reaching high enough to
strike your own.