/* $Id: Form.c,v 1.2 1997/04/28 15:52:07 archer Exp $
 *	FORM.C - General purpose geometry managing object
 *
 * Various hints & generic sticky form
 */
#include <Widget/FormP.h>


/**
 ** X11_FORM_ABSOLUTE_X
 **	Record the willingness of that item to be located somewhere specific
 **/
void X11_FormAbsoluteX(X11_widget wdg, int x)
{
	wdg->fixed_x = True;
	wdg->hint_x  = x;
}


/**
 ** X11_FORM_MARGINS
 **	Record margins between items in flexy-forms
 **/
void X11_FormMargins(X11_widget wdg, unsigned int w, unsigned int h)
{
	wdg->margin_w = w;
	wdg->margin_h = h;
}


/**
 ** X11_FORM_LABEL
 **	Record the object as labelling another
 **/
void X11_FormLabel(X11_widget wdg)
{
	wdg->labelling = True;
}


/**
 ** X11_FORM_ALIGN_RIGHT
 **	All those objects must be right-aligned
 **/
void X11_FormAlignRight(X11_widget array[], int nb)
{
X11_widget	*a;
int		x, rx;
int		i;
/*
 * Loop
 */
	a = array;
	rx = 0;
	for (i = nb; i; i--) {
		(*a[0]->on_geometry_check)(a[0], (XEvent *)0);
		x = a[0]->hint_x + a[0]->w;
		if (x > rx)
			rx = x;
		a++;
	}
/*
 * Position
 */
	a = array;
	for (i = nb; i; i--) {
		x = rx - a[0]->w;
		a[0]->hint_x = x;
		if (a[0]->x != x) {
			a[0]->x = x;
			if (a[0]->id)
				XMoveWindow(x11__screen, a[0]->id, x, a[0]->y);
		}
		a++;
	}
}


/**
 ** X11_FORM_ALIGN_LABELS
 **	First, all those labels are right-aligned. Then, the objects they
 **	label become left-aligned on *that* border.
 **/
void X11_FormAlignLabels(X11_widget *array, int nb)
{
X11_widget	wdg;
int		x;
int		i;
/*
 * Align labels
 */
	X11_FormAlignRight(array, nb);
/*
 * Align labelled objects
 */
	for (; nb-- > 0; array++)
		if (array[0]->labelling) {
			x = array[0]->x + array[0]->w;
			i = array[0]->margin_w;
			if ((wdg = array[0]->sibling) != 0) {
/*
 * Sanity check
 */
				if (nb && wdg == array[1])
					continue;
/*
 * Apply margins
 */
				if (wdg->margin_w > i)
					i = wdg->margin_w;
				wdg->fixed_x = True;
				wdg->hint_x  = x + i;
				if (wdg->x != wdg->hint_x) {
					wdg->x = wdg->hint_x;
					if (wdg->id)
						XMoveWindow(x11__screen, wdg->id, x+i, wdg->y);
				}
			}
		}
}


/**
 ** X11_FORM_FIXED_WIDTH
 **	Fix the geometry for that field
 **/
void X11_FormFixedWidth(X11_widget wdg)
{
	(*wdg->on_geometry_check)(wdg, (XEvent *)0);
	wdg->fixed_w = True;
	if (wdg->w_min < wdg->w)
		wdg->w_min = wdg->w;
}
