[comp.windows.x.motif] How do I center a widget inside an XmForm?

news@bbn.com (News system owner ID) (06/06/91)

I've been looking over the dazzling array of attachment options
available in XmForm, but I can't figure out how to position a
child widget (a Label, for instance) in either of the following ways:

   - attach the CENTER of the widget to the CENTER of the form, in
        either the x or y dimensions (or both)

   - attach it to an adjoining widget (say, a Button) so that the 
        CENTER of the Button touches the CENTER of the Label

The XmATTACH_WIDGET, XmATTACH_FORM, and XmATTACH_OPPOSITE_WIDGET all
seem to force me to attach corners and edges, rather than centers.

The XmATTACH_POSITION option would let me center my Label (by
using the same value for both leftPosition and rightPosition), but 
then I get a label that grows and shrinks as the form grows or shrinks.
I want a label that stays the same size but whose location changes
so that it remains in the center.

/Ron Newman		rnewman@bbn.com
Ron Newman		rnewman@bbn.com

wohler@sapwdf.UUCP (Bill Wohler) (06/06/91)

[same question within a month.  must be a FAQ...]


news@bbn.com (News system owner ID) writes:
>   - attach the CENTER of the widget to the CENTER of the form, in
>        either the x or y dimensions (or both)

ron,

  here is where a previous discussion has led.  i have yet to
  implement it, but perhaps you can run with the ideas.  let us know
  what you find.

						--bw

Bill Wohler writes:
 >
 >   that's a little better, but you still have to know the width of your
 >   text in the button and the width of the parent.  before your widget
 >   is managed and realized, these values are a little hard to come by.
 >   perhaps you know of some tricks of realizing a widget, getting some
 >   values and then setting some values (in this case, the location of a
 >   button) with a minimal light show (windows flashing on and off).

Just because a widget is managed, it does not mean that it is mapped.
See XmNmappedWhenManaged.  You can realize and manage the widget,
change values and then map it after you get it the way you want.  The
"getting" of values can be via XtGetValues in most cases.  However,
sometimes this is buggy in Motif, returning nonsensical values.  There-
fore I wrote macros that extract x, y, width and height from core be-
cause they are always right.  I then placed them in a getWidgetGeometry
routine so I could do it the "right" way if the problem with garbage
return values is ever universally fixed.  As far as the size of the
parent goes, this is a similar problem.  However, I would think that
the size of the parent is less dynamic.  If that is not the case,
and the size of the parent is varying, and you want to keep the child
centered, but a constant size, do it on the fly as follows:

   Create and Manage parent.
   Create Managed child with mapped when managed False.
   Check out the width of the parent and the width of the resultant child.
   Do your centering calculation.
   Use XtSetValues to move the widget to the desired location or
      attachment offset.
   Map the widget.

Larry Rogers
larry@boris.webo.dg.com

-- 
						--bw
-----
Bill Wohler <wohler@sap-ag.de> <sapwdf!wohler>
Heidelberg Red Barons Ultimate Frisbee Team

E_CMA@vaxa.nerc-murchison.ac.uk (06/07/91)

> I've been looking over the dazzling array of attachment options
> available in XmForm, but I can't figure out how to position a
> child widget (a Label, for instance)in either of the following ways:
>
>   - attach the CENTER of the widget to the CENTER of the form, in
>        either the x or y dimensions (or both)
>
>   - attach it to an adjoining widget (say, a Button) so that the
>        CENTER of the Button touches the CENTER of the Label

 The answer is, you don't. I have found the most effective way to center 
 a label / button/ etc. in a single row is to embed the label in a
 RowColumn with :
 XmNorientation = XmHORIZONTAL, XmNborderWidth = 0 ;
 XmNleftAttachment = XmATTACH_FORM, XmNrightAttachment= XmATTACH_FORM.
 and XmNentryAlignment = XmALIGNMENT_CENTER.

 That way the RowColumn stretches with the Form and the Label doesn't.

 Do this with a Label Gadget and you get a borderless centered title.
 For general layout, embedding things in Row Colums can result in superior 
 neatness. It also forces all items to be visible so you don't have to worry
 about resizing your Form if you add another widget.

 Cheers
     Carolyn

*************************************************************************
* Snailmail:		    *  E_Mail	                                *
*			    *  CBS%UK.AC.NERC-MURCHISON.VAXA::E_CMA     *	
* Dr C.M.Allen		    *  JANET: e_cma@uk.ac.nmh.va                *
* British Geological Survey *                                           *
* Murchison House           * 	                                        *
* West Mains Road           **                                          *
*                           *                                           *
* Edinburgh                 * Tel: 031-667-1000 x277 from U.K.		*
* Scotland                  * 						*
* EH9,3LA                   * 						*
*************************************************************************