XRC and vertically centered wxStaticText

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
romeo9423
Experienced Solver
Experienced Solver
Posts: 72
Joined: Mon Nov 08, 2004 3:36 pm

XRC and vertically centered wxStaticText

Post by romeo9423 »

Hello,

I have discovered wxwindows and I am playing with it to port an existing MFC app. I have modified and improved the tools used to convert Microsoft RC file to wxwindows xrc format.
But now I have discovered that there is no way of vertically centered a wxStaticText. I read that we need to fake it with sizers (I am really disappointed. What about wxVERT_ALIGN_CENTRE or something like that).
So since I am using xrc how am I supposed to modify the following xrc to vertically center my text :

<object class="wxDialog" name= "DLG_LECTEUR_DIALOG">
<pos>0,0</pos> <size>1023,766</size>
<title></title>
<style>wxNO_BORDER</style>
<font>
<size>8</size>
<face>MS Sans Serif</face>
</font>
<object class="wxStaticText" name= "IDC_PANEL_CENTRAL">
<style>wxST_NO_AUTORESIZE|wxALIGN_CENTRE</style>
<pos>7,190</pos> <size>1005,384</size>
<label></label>
</object>
</resource>

And by the way why wxStaticText is in autoresize mode BY DEFAULT. That's so stupid.
I am doing lots of critics because I think wxwindows could be better.
SnakeChomp
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 235
Joined: Sun Oct 10, 2004 2:53 am

Re: XRC and vertically centered wxStaticText

Post by SnakeChomp »

romeo9423 wrote:And by the way why wxStaticText is in autoresize mode BY DEFAULT. That's so stupid.
I'm sure you could have come up with a better argument than that.

From docs:
"By default, the control will adjust its size to exactly fit to the size of the text when SetLabel is called."

Why would you not want this to happen? The most common use of a label is to identify the purpose of one or more controls. For that purpose, autosizing by default is incredibly convenient and makes the most sense, as this is what labels are mostly used for. There may be times where you would like a label to occupy a specific area, and then change its text without changing that area, which is also a perfectly acceptible use for a label, but that is not what labels are most commonly used for, and by no means should be default functionality.
But now I have discovered that there is no way of vertically centered a wxStaticText.
Just spent a few minutes getting my windows 2003 sdk up and running, and windows doesn't even natively support vertical alignment for labels. MFC was doing manual vertical centering, so if you want vertical centering for wxWidgets it'll have to be manually implemented as well.

You can create sizers using XRC though, just insert them as you would any object (I assume, haven't actually bothered with sizers at all, let alone with xrc). You'll either have to use a sizer to vertically center your label, or you'll have to deal with the fact that the text won't be vertically centered.
james.chengguangwang
Knows some wx things
Knows some wx things
Posts: 38
Joined: Wed Aug 02, 2006 4:59 am

Maybe this is a solution about it

Post by james.chengguangwang »

Hi All,

I have used wxGlade tool for designing the UI interface, the attributes is shown via the following xrc file content:

<?xml version="1.0" encoding="UTF-8"?>
<!-- generated by wxGlade 0.4.1 on Mon Aug 07 09:18:19 2006 -->

<resource version="2.3.0.1">
<object class="wxDialog" name="dialog_1" subclass="MyDialog">
<style>wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER|wxMINIMIZE_BOX|wxTHICK_FRAME</style>
<size>539, 387</size>
<title>Dialog With WxGlade</title>
<object class="wxBoxSizer">
<orient>wxVERTICAL</orient>
<object class="sizeritem">
<flag>wxTOP|wxBOTTOM|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxADJUST_MINSIZE</flag>
<border>3</border>
<object class="wxStaticText" name="label_1">
<style>wxALIGN_CENTRE</style>
<tooltip>wxStaticText Control</tooltip>
<label>A Demo Dialog Created By WxGlade Tool</label>
<font>
<style>normal</style>
<family>modern</family>
<weight>normal</weight>
<underlined>0</underlined>
<size>10</size>
</font>
</object>
</object>
<object class="sizeritem">
<flag>wxTOP|wxBOTTOM|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL</flag>
<border>3</border>
<object class="wxTextCtrl" name="text_ctrl_1">
<style>wxTE_MULTILINE</style>
<tooltip>wxTextCtrl Control</tooltip>
<size>532, 300</size>
</object>
</object>
<object class="sizeritem">
<option>1</option>
<flag>wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL</flag>
<object class="wxFlexGridSizer">
<hgap>1</hgap>
<rows>1</rows>
<cols>9</cols>
<vgap>1</vgap>
<object class="spacer">
<size>5, 5</size>
<flag>wxADJUST_MINSIZE</flag>
</object>
<object class="spacer">
<size>5, 5</size>
<flag>wxADJUST_MINSIZE</flag>
</object>
<object class="sizeritem">
<flag>wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxADJUST_MINSIZE</flag>
<border>6</border>
<object class="wxStaticText" name="label_2">
<size>50, 15</size>
<tooltip>wxStaticText Control</tooltip>
<label>Message :</label>
</object>
</object>
<object class="sizeritem">
<flag>wxADJUST_MINSIZE</flag>
<object class="wxTextCtrl" name="text_ctrl_2">
<tooltip>wxTextCtrl Control</tooltip>
<size>300, 23</size>
</object>
</object>
<object class="spacer">
<size>5, 5</size>
<flag>wxADJUST_MINSIZE</flag>
</object>
<object class="sizeritem">
<flag>wxADJUST_MINSIZE</flag>
<object class="wxButton" name="button_1">
<tooltip>wxButton Control</tooltip>
<label>Button One</label>
</object>
</object>
<object class="spacer">
<size>5, 5</size>
<flag>wxADJUST_MINSIZE</flag>
</object>
<object class="sizeritem">
<flag>wxADJUST_MINSIZE</flag>
<object class="wxButton" name="button_2">
<tooltip>wxButton Control</tooltip>
<label>Button Two</label>
</object>
</object>
<object class="spacer">
<size>20, 20</size>
<flag>wxADJUST_MINSIZE</flag>
</object>
</object>
</object>
</object>
</object>
</resource>

the xrc file is a basic dialog which derived from wxDialog, and the static text label "A Demo Dialog Created By WxGlade Tool" is centered, maybe you can get some userful information from it.
Post Reply