Two issues using GridBagSizer

I am having a couple of issues with sizing objects using GridBagSizer.

The first is that the GridBagSizer itself isn’t expanding to fill the
area available to it. For reasons that will be more clear when I get to
the second issue, the GridBagSizer actually has only one cell. When I
add the panel sized with the GridBagSizer to the BoxSizer that holds it,
I use the EXPAND flag, but it doesn’t expand to fill the entire
horizontal area (see attached image, top half).

The second problem relates to the way I’m using the GridBagSizer. It
contains only a single cell that has two separate panels in it. These
panels are mutually exclusive (either one or the other is showing, or
possibly neither, but never both), so I want them to occupy the same
space, which works using the GridBagSizer. When I first executed the
code, the contents of @add_panel and @edit_panel would both be squished
into a very small area at the upper-left corner of the @option_panel.
At position 1 in the code, I added the following lines:

@option_sizer.calc_min
@option_sizer.recalc_sizes

This fixed the problem for the @add_panel only–@edit_panel would still
appear squished into the corner. I then moved the two lines to appear
at both position 2 and position 3 in the code, but the problem persisted
(see attached image, bottom half).

require ‘ui/add_panel’
require ‘ui/edit_panel’
include Wx

class DetailPanel < Panel
def initialize(parent)
super parent

@box = StaticBox.new self, :label => 'Entry Detail View'
... # creating other frames
@option_panel = Panel.new self
@add_panel = AddEntryPanel.new @option_panel
@edit_panel = EditEntryPanel.new @option_panel

@main_sizer = BoxSizer.new VERTICAL
set_sizer @main_sizer
@box_sizer = StaticBoxSizer.new @box, VERTICAL
... # defining other sizers
@option_sizer = GridBagSizer.new
@option_panel.set_sizer @option_sizer

@main_sizer.add @box_layout, 1, EXPAND|LEFT|RIGHT, 1

... # adding other frames to @box_sizer
@box_sizer.add @option_panel, 1, EXPAND|TOP, 6

... # other sizers within @box_sizer

@option_sizer.add @add_panel, GBPosition.new(0, 0), GBSpan.new,

EXPAND
@option_sizer.add @edit_panel, GBPosition.new(0, 0), GBSPan.new,
EXPAND
#position 1

... # assign event handlers
show
@add_panel.hide
@edit_panel.hide

end

def add_button_click(event)
@edit_panel.hide
@add_panel.show
#position 2
end

def edit_button_click(event)
@add_panel.hide
@edit_panel.show
#position 3
end
end

ui/add_panel.rb and ui/entry_panel.rb contain the necessary class
definitions for those AddPanel and EntryPanel, of course.

Any help (including complete overhauls if there’s a better way I should
be doing this) is welcome.

Thanks,
Doug G.

Hi

Doug G. wrote:

The first is that the GridBagSizer itself isn’t expanding to fill the
area available to it. For reasons that will be more clear when I get to
the second issue, the GridBagSizer actually has only one cell. When I
add the panel sized with the GridBagSizer to the BoxSizer that holds it,
I use the EXPAND flag, but it doesn’t expand to fill the entire
horizontal area (see attached image, top half).

I couldn’t fully follow what was going on in your code, but hope I can
help with a few suggestions:

  • Try using one of the GUI designers (eg DialogBlocks) to test build
    your layout. They make it easy to swap and nest Sizers and you can
    immediately see the effects. From then you can go and look at the XML to
    see how the Sizers are flagged and nested.

  • Use a com

The second problem relates to the way I’m using the GridBagSizer. It
contains only a single cell that has two separate panels in it.
Why are you using GridBagSizer then? If all you want is a place to swap
two panels or widgets in and out of, a simple BoxSizer would do fine.
Have a look at Sizer#detach and Sizer#replace methods, in conjunction
with using Window#hide or Window#destroy on the window you’re removing.

These
panels are mutually exclusive (either one or the other is showing, or
possibly neither, but never both), so I want them to occupy the same
space, which works using the GridBagSizer.
Ok, see suggestion above. Here’s a simple class which wraps multiple
controls, presenting only one at a time.

Sorry, hit send too early…

The example class you might look at is:
http://weft-qda.rubyforge.org/svn/trunk/weft-qda/lib/weft/wxgui/controls/multitype_control.rb

Note that when swapping items in and out of a Sizer, you should probably
call layout() afterwards to readjust the relative spacing.

The first is that the GridBagSizer itself isn’t expanding to fill the
area available to it. For reasons that will be more clear when I get to
the second issue, the GridBagSizer actually has only one cell. When I
add the panel sized with the GridBagSizer to the BoxSizer that holds it,
I use the EXPAND flag, but it doesn’t expand to fill the entire
horizontal area (see attached image, top half).

  • As for the expansion, you probably want to check that all the inner
    widgets are also set to expand within their parent. So in the case of
    your short textcontrols in the upper pane, they may need to be told to
    expand to full width.
@option_panel = Panel.new self

Note that if a Window is the sole child of a managed Frame it will
resize automatically to fill that Frame. You don’t need to put it within
a sizer.

hth
alex

Alex F. wrote:
[snip]

Why are you using GridBagSizer then? If all you want is a place to swap
two panels or widgets in and out of, a simple BoxSizer would do fine.
Have a look at Sizer#detach and Sizer#replace methods, in conjunction
with using Window#hide or Window#destroy on the window you’re removing.

[snip]

Alex,

Perfect! I used a BoxSizer and added detach calls to the button_click
event handlers. Hadn’t discovered the detach method previously. That
solved both problems. Thanks for your help!

Doug

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs