# Having trouble show/hide bitmap in a sizer

**URL:** <https://www.ruby-forum.com/t/having-trouble-show-hide-bitmap-in-a-sizer/212682>\
**Category:** wxRuby\
**Created:** [October 10, 2011, 8:43pm UTC](https://www.ruby-forum.com/t/having-trouble-show-hide-bitmap-in-a-sizer/212682 "2011-10-10T20:43:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [October 10, 2011, 8:43pm UTC](https://www.ruby-forum.com/t/having-trouble-show-hide-bitmap-in-a-sizer/212682/1 "2011-10-10T20:43:53Z")

</div>

I’m trying to display one of two separate bitmap  
images along with some text. The images are a  
check and and a cross symbol and are displayed as

FIRMWARE C  
or  
FIRMWARE X  
or  
FIRMWARE

where C would be my checkmark image and X is the  
cross image. This is the code I am using to  
initially setup the Static bitmap and sizer. This  
is then added to a vertical box sizer containing a  
few other controls. Lastly for a test, I simply  
show the appropriate image as in  
@check\_sb\_firmware.show and @x\_sb\_firmware forthe  
other image. The problem that I am seeing is the  
first time I show the image, the image appears at  
the top of my panel, the second time I show the  
image it is in the correct position, just after my  
text. Am I handling this wrong or perhaps there is  
a better way? It seems like it should be a simple  
task.

Thanks  
Mark

def make\_firmware\_check(parent)  
firmware\_check\_sizer =  
Wx::BoxSizer.new(Wx::HORIZONTAL)  
check\_bm\_firmware =  
Bitmap.new(“images/check\_icon.png”,  
Wx::BITMAP\_TYPE\_ANY)  
@check\_sb\_firmware =  
Wx::StaticBitmap.new(parent, -1, check\_bm\_firmware)  
fw\_text = Wx::StaticText.new(parent,  
ID\_SERIAL\_STATIC\_TEXT, “FIRMWARE”,  
Wx::DEFAULT\_POSITION, Wx::DEFAULT\_SIZE, 0, “”)  
x\_bm\_firmware =  
Bitmap.new(“images/x\_icon.png”, Wx::BITMAP\_TYPE\_ANY)  
@x\_sb\_firmware = Wx::StaticBitmap.new(parent,  
-1, x\_bm\_firmware)  
firmware\_check\_sizer.add(fw\_text, 1,  
Wx::RIGHT|Wx::TOP|Wx::ALIGN\_LEFT, 5)  
firmware\_check\_sizer.add(@check\_sb\_firmware,  
0, Wx::ALIGN\_CENTER, 3)  
firmware\_check\_sizer.add(@x\_sb\_firmware, 0,  
Wx::ALIGN\_CENTER, 3)  
@check\_sb\_firmware.hide  
@x\_sb\_firmware.hide  
return firmware\_check\_sizer  
end

---

<div class="post-metadata">

**Author:** ![Mark\_Barton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/278dde/32.png) [@Mark\_Barton](https://www.ruby-forum.com/u/Mark_Barton)\
**Post date:** [October 11, 2011, 12:54pm UTC](https://www.ruby-forum.com/t/having-trouble-show-hide-bitmap-in-a-sizer/212682/2 "2011-10-11T12:54:53Z")

</div>

On 10/11/11 2:55 AM, Quintus wrote:

> > Wx::DEFAULT\_SIZE, 0, “”) x\_bm\_firmware =  
> > probably read this: [http://wxruby.rubyforge.org/doc/wxruby\_intro.html](http://wxruby.rubyforge.org/doc/wxruby_intro.html)  
> > because there’s usually no reason to assign IDs manually to widgets.  
> > should use your own control if you want to display larger images  
> > where I could have needed that has been removed already).  
> > 70s7DlsbaDnxoUPHi9R6izl2tLAB4P7VNi/HKMhy2m07PNDH7d5nzM+XfwUM3Gls  
> > EbcCRNoNGM0+fh/aqYXo78nJ3rhorknhH7HvKfbZUZkuOpIl804yIQE1C4WO9XIj  
> > WWPd9eTEfqR5YnaTNF7zlG2f9BZj/c26+/ndxqOt422vPBZEYLy4BXQgtKVQ83Y=  
> > =2wH4  
> > -----END PGP SIGNATURE-----
> 
> * * *
> 
> wxruby-users mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [http://rubyforge.org/mailman/listinfo/wxruby-users](http://rubyforge.org/mailman/listinfo/wxruby-users)

Thanks Marvin for the helpful information. I think  
you have hit the nail on the head or at least very  
close. In my test I was changing the layout,  
sleeping , changing the layout and so forth  
without allowing WxRuby to complete its loop even  
though I was calling layout each time. The bitmap  
works as expected if I allow the loop to complete  
before making the next change.

Again thanks for you help.

Mark

---

<div class="post-metadata">

**Author:** ![Mark\_Barton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/278dde/32.png) [@Mark\_Barton](https://www.ruby-forum.com/u/Mark_Barton)\
**Post date:** [October 11, 2011, 9:15am UTC](https://www.ruby-forum.com/t/having-trouble-show-hide-bitmap-in-a-sizer/212682/3 "2011-10-11T09:15:28Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Am 10.10.2011 20:36, schrieb Mark Barton:

> Wx::RIGHT|Wx::TOP|Wx::ALIGN\_LEFT, 5)  
> firmware\_check\_sizer.add(@check\_sb\_firmware, 0, Wx::ALIGN\_CENTER,  
> 3) firmware\_check\_sizer.add(@x\_sb\_firmware, 0, Wx::ALIGN\_CENTER,  
> 3) @check\_sb\_firmware.hide @x\_sb\_firmware.hide return  
> firmware\_check\_sizer end

Do yourself a favour and include the Wx module in your scope so you  
don’t have to always write that annoying Wx::\*. Second, you should  
probably read this: [http://wxruby.rubyforge.org/doc/wxruby\_intro.html](http://wxruby.rubyforge.org/doc/wxruby_intro.html)  
There’s no need to specify all that nasty default parameters for your  
wxRuby code, so e.g.

> fw\_text = Wx::StaticText.new(parent, ID\_SERIAL\_STATIC\_TEXT,  
> “FIRMWARE”, Wx::DEFAULT\_POSITION, Wx::DEFAULT\_SIZE, 0, “”)

becomes

> fw\_text = StaticText.new(parent, :id =\> ID\_SERIAL\_STATIC\_TEXT,  
> :label =\> “FIRMWARE”)

And you may leave out the “:id =\> ID\_SERIAL\_STATIC\_TEXT” as well,  
because there’s usually no reason to assign IDs manually to widgets.  
Even for event handling you can usually just pass the control instance  
into the event handler.

Next, the Wx::StaticBitmap class isn’t intended to draw normal images  
(as weird as it sounds). Quote from the docs:

> A static bitmap control displays a bitmap. It is meant for display  
> of the small icons in the dialog boxes and is not meant to be a  
> general purpose image display control. In particular, under Windows  
> 9x the size of bitmap is limited to 64×64 pixels and thus you  
> should use your own control if you want to display larger images  
> portably.

Finally I’m not sure how sizers work with hidden controls. Your code  
appears logically correct to me (although there are some stylistic  
problems as I already pointed out), maybe you’re running into an issue  
I’ve experienced some time ago as well: Changing a control’s contents  
doesn’t cause wxRuby to recalculate the sizer layout (in particular,  
the issue I was running into was that I had a right-aligned StaticText  
widget and the text could grow bigger and was then running out of the  
window). I _think_ it is possible to make wxRuby relayout the GUI by  
calling Sizer#recalc\_sizes, but I didn’t try this (the original code  
where I could have needed that has been removed already).

Vale,  
Marvin  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.11 (GNU/Linux)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org/](http://enigmail.mozdev.org/)

iQEcBAEBAgAGBQJOk+hNAAoJELh1XLHFkqhaJ8AH/3g1ZNLcAxF+jzOU1I2uwfFr  
JwHQ50tL3dL8E9yTfheUpKH426DovnBXwLD47FrZNhtHaxDZY2WkiCRDvHqDi+b+  
E06WlbTBej3jPTSFEfH0Rm3XRmsua6aozZPHC9XaoNz2bXwxwwdsJiao7OtoJ5bP  
70s7DlsbaDnxoUPHi9R6izl2tLAB4P7VNi/HKMhy2m07PNDH7d5nzM+XfwUM3Gls  
EbcCRNoNGM0+fh/aqYXo78nJ3rhorknhH7HvKfbZUZkuOpIl804yIQE1C4WO9XIj  
WWPd9eTEfqR5YnaTNF7zlG2f9BZj/c26+/ndxqOt422vPBZEYLy4BXQgtKVQ83Y=  
=2wH4  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![Mark\_Barton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/278dde/32.png) [@Mark\_Barton](https://www.ruby-forum.com/u/Mark_Barton)\
**Post date:** [October 12, 2011, 3:31am UTC](https://www.ruby-forum.com/t/having-trouble-show-hide-bitmap-in-a-sizer/212682/4 "2011-10-12T03:31:43Z")

</div>

On 10/10/11 19:36, Mark Barton wrote:

> This is the code I am using to initially setup the Static bitmap and  
> sizer. This is then added to a vertical box sizer containing a few  
> other controls. Lastly for a test, I simply show the appropriate image  
> as in @check\_sb\_firmware.show and @x\_sb\_firmware forthe other image.  
> The problem that I am seeing is the first time I show the image, the  
> image appears at the top of my panel, the second time I show the image  
> it is in the correct position, just after my text. Am I handling this  
> wrong or perhaps there is a better way?

Just to check, you do call parent.sizer = firmware\_check\_sizer in the  
calling function to assign the BoxSizer to the panel?

After hiding or deleting windows that are controlled by a sizer, you may  
need to call sizer.layout to re-assign the spacing correctly.

alex

---

<div class="post-metadata">

**Author:** ![Mark\_Barton](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/278dde/32.png) [@Mark\_Barton](https://www.ruby-forum.com/u/Mark_Barton)\
**Post date:** [October 12, 2011, 3:03pm UTC](https://www.ruby-forum.com/t/having-trouble-show-hide-bitmap-in-a-sizer/212682/5 "2011-10-12T15:03:00Z")

</div>

On 10/11/11 3:49 AM, Alex F. wrote:

> > my panel, the second time I show the image it  
> > sizer.layout to re-assign the spacing correctly.
> 
> alex
> 
> * * *
> 
> wxruby-users mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [http://rubyforge.org/mailman/listinfo/wxruby-users](http://rubyforge.org/mailman/listinfo/wxruby-users)  
> Hi Alex,

Hummmm, well, what I have is two horizontal  
BoxSizers with the text and static bitmaps added  
to a vertical BoxSizer along with a couple other  
controls. This vertical sizer is added to the left  
side of a two column FlexGridSizer. The  
FlexGridSizer is attached to the panel with:  
main\_panel.set\_sizer(myFlexGridSizer) followed by  
a main\_panel.layout.

Currently I have only the bitmap initially hidden  
and this seem to work if I allow the app loop to  
complete after changing the layout. The puzzling  
part is it does not work if I also hide the text  
as well as the bitmap in my function. In this  
case, both the bitmaps and text appear at the top  
of the panel and are partially visible.

Thanks  
Mark
