[comp.windows.x.motif] How do you center a widget within a form?

montnaro@spyder.crd.ge.com (Skip Montanaro) (03/28/91)

All the *Attachment constraint resources in the XmForm widget relate to the
edges of the widget. Not knowing the size of the form and, say, a PushButton
widget (for instance, at creation time), how do you specify that the button
should be centered left-to-right within the form? It would be nice if you
could specify center{Attachment,Position,Offset,Widget} resources.

Thanks,
--
Skip (montanaro@crdgw1.ge.com)

asente@adobe.com (Paul Asente) (03/28/91)

In article <MONTNARO.91Mar27135851@spyder.crd.ge.com> montanaro@crdgw1.ge.com (Skip Montanaro) writes:
>
>All the *Attachment constraint resources in the XmForm widget relate to the
>edges of the widget. Not knowing the size of the form and, say, a PushButton
>widget (for instance, at creation time), how do you specify that the button
>should be centered left-to-right within the form? 

You need to use two steps.  First, create the pushbutton with left attachment
to 50%.  Then, do an XtGetValues to get the width of the pushbutton.  Then,
do an XtSetValues to make the left offset be -width/2.

	-paul asente
		asente@adobe.com	...decwrl!adobe!asente

    Ratz put a bucket of liquid in front of me.
   "I wanted a glass of docs, Ratz.  What the hell is this?" I barked.
   "Motif don't fit in a glass anymore," he barked back.
    I looked at the liquid.  It was totally opaque to me.

colin@nbc1.ge.com (Colin Rafferty) (03/30/91)

In article <MONTNARO.91Mar27135851@spyder.crd.ge.com> montnaro@spyder.crd.ge.com (Skip Montanaro) writes:
> All the *Attachment constraint resources in the XmForm widget relate to the
> edges of the widget. Not knowing the size of the form and, say, a PushButton
> widget (for instance, at creation time), how do you specify that the button
> should be centered left-to-right within the form? It would be nice if you
> could specify center{Attachment,Position,Offset,Widget} resources.

Well Skip, from what I've figured out, the only way to center an
object horizontally forces you to specify the width (rather than
having it computed).  The general idea is to have an invisible,
zero-width, and centered object (leftPosition = rightPosition = 50).
You can then attach the left and right sides of your widget to that
object.  I like to use a Separator with no line as the centered
object.  Here is some UIL code for it:

        object Center : XmSeparator widget {
            arguments {
                XmNseparatorType = XmNO_LINE;
                XmNwidth = 0;
                XmNleftAttachment = XmATTACH_POSITION;
                XmNleftPosition = 50;
                XmNrightAttachment = XmATTACH_POSITION;
                XmNrightPosition = 50;
            };
        };
        
        object Widget : XmGenericWidget widget {
            arguments {
                XmNleftAttachment = XmATTACH_OPPOSITE_WIDGET;
                XmNleftWidget = XmSeparator Center;
                XmNleftOffset = (WIDTH / 2);
                XmNrightAttachment = XmATTACH_OPPOSITE_WIDGET;
                XmNrightWidget = XmSeparator Center;
                XmNrightOffset = (WIDTH / 2);
            };
        };

Note that in this example, the left & right offsets are specified.
What you really need to do is calculate the offsets after the widgets
have been fetched, but before they're realized.

I agree with your desire for center* resources.  I think that this is
doing backflips for something that should be built-in.

Hope that this helps you out.

				-Colin

-- 
Colin Owen Rafferty              |      I believe in compulsory cannibalism.
colin@nbc1.ge.com                |      If people were forced to eat 
(I don't speak for NBC.          |      what they killed, there would
Watch Tom Brokaw for that.)      |      be no more wars.      -- Abbie Hoffman