[fxruby-users] composite widgets

Jannis Pohlmann info at sten-net.de
Thu May 12 05:32:42 EDT 2005


Jacob Hanson schrieb:

>Sunday, May 8, 2005, 3:09:19 PM, you wrote
>
>>Hello Jacob.
>>Yu can actually do this in FXRuby.
>>
>
>>Usually, you'd subclass a container widget like FXPacker, FXMatrix or
>>FXVerticalFrame. That's the most
>>preferable way to easily to manage and react child widgets. Even in C++
>>that's the best solution since
>>most of the child wigets are going to be automatically destroyed by 
>>their parent, then.
>>
>
>Thanks Jannis,
>
>I have begun implementing it based off of FXVerticalFrame. Here's a
>related question. This widget needs to expand/contract based on the
>number of items that have been added to it. I can do this after
>everything is created and call myframe.create; myframe.recalc to
>update the controls that need to be drawn or hidden and resize my
>frame accordingly. But! If I attempt to add or remove an item
>before the rest of the containing window is drawn for the first time,
>I get a segfault when trying to .create. Is there a way to test if
>I need to run .create or should I be doing something different?
>
>Thanks,
>
>Jacob
>
Hi Jacob,

When adding/removing items before the parent widget is created you won't 
have to call
create on them. The creation of the parent widget does that 
automatically. But when
adding/removing items after the parent's creation you will have to call 
.create on each new
item and call .recalc of the parent widget.

The two ways:

    class YourWidget < FXVerticalFrame
       def initialize(parent, opts)
          super
       end

       def create
          super
       end

       def addLabel(text)
          FXLabel.new(self, text) do |label|
             label.create
          end
          self.recalc
       end
    end

or
   
    class YourWidget < FXVerticalFrame
       def initialize(parent, opts)
          super
          texts = Array.new(["text1", "text2", "text3"])
          texts.each do |txt|
             FXLabel.new(self, txt)
          end
       end

       def create
          super
       end

       # Of course, you could use the "addLabel" method here, too.
    end


Hope this helps,
Jannis


More information about the fxruby-users mailing list