On Mon, Nov 17, 2008 at 02:39:32PM +0000, Einar Magnús Boson wrote:
> My problem now is that inside my widget's `initialize` stack.width is 0,
> owner is nil and parent.width is 0.
> only later do the widths take on values. I can't find where to put the
> logic to get the width of my container to draw. I tried this:
>
> http://pastie.org/316819
>
> but overriding draw screws up a lot of stuff. Just guessing is not getting
> me much further. Now I need help.
Yeah, overriding `draw` is never the answer. Even the `cascading`
widget I wrote some months ago is wrong, I feel.
I'm sorry about the lack of documentation on widgets, they aren't
essential to Hackety Hack, so they aren't important to me until
after Hackety Hack is done.
{o}
^
Try this for your slider: <http://gist.github.com/25836>
class Slider < Widget
def initialize opts = {}
background white
para "SLIDER HERE"
end
end
You see, a widget is just a `flow` that can be subclassed. A `flow`
is considered the default container (slot) in Shoes. The main Shoes
app window itself starts its little existence as a flow.
If you need it to be a stack, throw a stack inside of it.
Assuming the above `initialize` method, when you write
`slider :width => 0.9`, you will basically wind up with:
flow :width => 0.9 do
background white
para "SLIDER HERE"
end
However, the `flow` will report itself as a Slider object now.
Any options passed to the widget are passed to the flow first and
then to `initialize`.
{o}
^
What good is this? Well, now you can add methods to the Slider
object to manipulate the slot or to respond to slot events.
Take another look at the speedometer sample. The widget is just a
canvas to paint everything on to. And methods like `ticks`
and `position=` make it more than just a canvas, it becomes a
meaningful widget with friendly properties.
Anyway, the point is to avoid having to write custom drawing methods
and parsing of options. You can build the widget from all the art
objects within Shoes and the painting and resizing is handled without
intervention, you know?
_why