/*
 * $Source: /mit/sipbsrc/src/xscreensaver/RCS/PromptBox.c,v $
 * $Author: srz $
 *
 * This file is part of xscreensaver.  It contains a set of routines
 * for building form widgets fileed with lines of centered
 * text/bitmaps.
 *
 * Author: Jonathan Kamens, MIT Project Athena and
 *                          MIT Student Information Processing Board
 *
 * Copyright (c) 1989 by Jonathan Kamens.  This code may be
 * distributed freely as long as this notice is kept intact in its
 * entirety and every effort is made to send all corrections and
 * improvements to the code back to the author.  Also, don't try to
 * make any money off of it or pretend that you wrote it.
 */

#ifndef lint
     static char rcsid_PromptBox_c[] = "$Header: /mit/sipbsrc/src/xscreensaver/RCS/PromptBox.c,v 1.2 1999/12/16 07:49:15 srz Exp $";
#endif

#include "xsaver.h"
#include <X11/Xaw/Form.h>
#include <X11/Xaw/Label.h>
#include "globals.h"
#include "PromptBox.h"


extern Dimension widget_width();
extern void XtMoveWidget();





static Widget addBox(w, line, last)
Widget w, *last;
PromptLine *line;
{
     Arg arglist[10];
     int i = 0;

     XtSetArg(arglist[i], XtNborderWidth, 0); i++;
     XtSetArg(arglist[i], XtNresizable, True); i++;
     if (line->center) {
	  XtSetArg(arglist[i], XtNjustify, XtJustifyCenter); i++;
     }
     if (! line->use_default) {
	  if (line->bitmap) {
	       XtSetArg(arglist[i], XtNbitmap, line->bitmap); i++;
	       XtSetArg(arglist[i], XtNlabel, None); i++;
	  }
	  else {
	       XtSetArg(arglist[i], XtNbitmap, None); i++;
	       XtSetArg(arglist[i], XtNlabel, line->str); i++;
	  }
     }
     XtSetArg(arglist[i], XtNfromVert, *last); i++;
     if (line->spread) {
	  XtSetArg(arglist[i], XtNvertDistance, line->spread); i++;
     }
     *last = XtCreateWidget(line->name, labelWidgetClass, w, arglist, i);
     line->width = widget_width(*last);
     return(*last);
}





/*
 * parent is the parent widget of the prompt box widget; it must
 *   already be created.
 * name is the name of the prompt box to be created
 * strings is an array of the contents of the prompt box widget
 * num is the number of things in strings
 * return_widgets, if non-null, must contain num+1 locations -- it
 *   will be filled in with the label widget contents of the form
 *   widget followed by a null
 */
Widget PromptBox(parent, name, strings, num, return_widgets)
Widget parent;
char *name;
PromptLine *strings;
Widget *return_widgets;
int num;
{
     Widget prompt, lines[MAXPROMPT], last = (Widget) NULL;
     PromptLine *line_ptr;
     Dimension width = 0;
     Arg arglist[1];
     
     prompt = XtCreateWidget(name, formWidgetClass, parent, NULL, 0);
     for (line_ptr = strings; line_ptr - strings < num; line_ptr++) {
	  lines[line_ptr - strings] = addBox(prompt, line_ptr, &last);
	  if (width < line_ptr->width) width = line_ptr->width;
     }
     XtSetArg(arglist[0], XtNwidth, width);
     for (line_ptr = strings; line_ptr - strings < num; line_ptr++)
	  if (line_ptr->center)
	       XtSetValues(lines[line_ptr - strings], arglist, 1);

     if (return_widgets) {
	  for (line_ptr = strings; line_ptr - strings < num; line_ptr++)
	       return_widgets[line_ptr - strings] = lines[line_ptr - strings];
	  return_widgets[line_ptr - strings] = (Widget) NULL;
     }
     XtManageChildren(lines, num);
     XtRealizeWidget(prompt);
     return(prompt);
}




Widget CenteredPromptBox(parent, name, strings, num, return_widgets)
Widget parent;
char *name;
PromptLine *strings;
Widget *return_widgets;
int num;
{
     Widget prompt; 
     Dimension shell_width, shell_height, shell_x, shell_y, shell_border;
     XtWidgetGeometry preferred;

     prompt = PromptBox(parent, name, strings, num, return_widgets);

     /* Center the widget on the screen */
     XtQueryGeometry(prompt, NULL, &preferred);
     shell_width = preferred.width;
     shell_height = preferred.height;
     shell_border = preferred.border_width;
     shell_x = (display_width - shell_width - 2 * shell_border) / 2;
     shell_y = (display_height - shell_height - 2 * shell_border) / 2;
     XtMoveWidget(prompt, shell_x, shell_y);

     return(prompt);
}
