/* WIDE AREA INFORMATION SERVER SOFTWARE:
   No guarantees or restrictions.  See the readme file for the full standard
   disclaimer.

   This is part of the X user-interface for the WAIS software.  Do with it
   as you please.

   Version 0.82
   Wed Apr 24 1991

   jonathan@Think.COM

*/

#include "xwais.h"

static void
Makelabel(parent)
Widget parent;
{
  Arg arglist[10];
  Cardinal num_args;

  num_args = 0;
  XtSetArg(arglist[num_args], XtNlabel, "X WAIStation"); num_args++;

  labelwindow = XtCreateManagedWidget("labelWindow",labelWidgetClass, 
				      parent, arglist, num_args);
}

ScrollList
MakeQuestionFrame(parent)
Widget parent;
{
  Widget frame, labelwid, button;
  Widget view;
  ScrollList result;
  Arg arglist[10];
  Cardinal num_args;

  frame = XtCreateManagedWidget("questions", formWidgetClass, parent, NULL, ZERO);

  num_args = 0;
  XtSetArg(arglist[num_args], XtNlabel, "Questions:"); num_args++;

  labelwid = XtCreateManagedWidget("questionlabel",labelWidgetClass, 
			frame, arglist, num_args);

  num_args = 0;
  XtSetArg (arglist[num_args], XtNforceBars, True); num_args++;
  XtSetArg (arglist[num_args], XtNallowVert, True); num_args++;

  view = XtCreateManagedWidget ("qview", viewportWidgetClass,
				frame, arglist, num_args);

  result = (ScrollList) s_malloc(sizeof(_ScrollList));

  result->offset = 0;

  result->ListWidget =
    MakeListWidget(view, "questionWindow", Question_items, NULL, NULL);

  button = MakeCommandButton(frame, "qadd", AddQuestion,
			     view, NULL, NULL);
  button = MakeCommandButton(frame, "qedit", EditQuestion,
			     view, NULL, NULL);
  button = MakeCommandButton(frame, "qdelete", DeleteQuestion,
			     view, NULL, NULL);
  return result;
}

ScrollList
MakeSourceFrame(parent)
Widget parent;
{
  Widget frame, labelwid, button, view;
  ScrollList result;
  Arg arglist[10];
  Cardinal num_args;

  frame = XtCreateManagedWidget("sources", formWidgetClass, parent, NULL, ZERO);

  num_args = 0;
  XtSetArg(arglist[num_args], XtNlabel, "Sources:"); num_args++;

  labelwid = XtCreateManagedWidget("sourcelabel",labelWidgetClass, 
				   frame, arglist, num_args);

  num_args = 0;
  XtSetArg (arglist[num_args], XtNforceBars, True); num_args++;
  XtSetArg (arglist[num_args], XtNallowVert, True); num_args++;

  view = XtCreateManagedWidget ("sview", viewportWidgetClass,
				frame, arglist, num_args);

  result = (ScrollList) s_malloc(sizeof(_ScrollList));

  result->offset = 0;

  result->ListWidget =
    MakeListWidget(view, "sourceWindow", Source_items, NULL, NULL);

  button = MakeCommandButton(frame, "sadd", AddSource,
			     view, NULL, NULL);
  button = MakeCommandButton(frame, "sedit", EditSource,
			     view, NULL, NULL);
  button = MakeCommandButton(frame, "sdelete", DeleteSource,
			     view, NULL, NULL);
  return result;
}

Widget
MakeMessage(parent)
Widget parent;
{
  Widget stringlabelwid;
  Arg arglist[10];
  Cardinal num_args;

  stringlabelwid = MakeLabel(parent, "mainstatuslabel", "Status:",
			     NULL, NULL);
  num_args = 0;
  XtSetArg(arglist[num_args], XtNeditType, XawtextEdit); num_args++;
  XtSetArg(arglist[num_args], XtNsensitive, False); num_args++;
  XtSetArg(arglist[num_args], XtNfromVert, stringlabelwid); num_args++;
  return XtCreateManagedWidget("messageWindow", asciiTextWidgetClass,
			       parent, arglist, num_args);
}


Widget
MakeSourcePopup(parent)
Widget parent;
{
  Widget shell, frame, labelwid, stringlabelwid, button;
  WidgetClass wclass;
  static String
    namestring,
    serverstring,
    servicestring,
    dbstring,
    cost, units,
    maintainer,
    description;
  int CurrentSource;
  Source source;

  CurrentSource = get_selected_source();

  if (CurrentSource != NO_ITEM_SELECTED) {
    source = findSource(CurrentSource);
    namestring = source->name;
    serverstring = source->server;
    servicestring = source->service;
    dbstring = source->database;
    cost = source->cost;
    units = source->units;
    maintainer = source->maintainer;
    description = source->description;
  } else {
    namestring = "default";
    serverstring = "localhost";
    servicestring = "8000";
    dbstring = "";
    cost = "";
    units = "";
    maintainer = "";
    description = "";
  }

  shell = XtCreatePopupShell("sourcepopup", applicationShellWidgetClass,
			     parent, NULL, ZERO);
  frame = XtCreateManagedWidget("sourcepopupform", formWidgetClass,
				shell, NULL, ZERO);

  labelwid = MakeLabel(frame, "sourceditlabel", "Source Edit", NULL, NULL);

  stringlabelwid = MakeLabel(frame, "sourcenamelabel", "       Name:", labelwid, NULL);
  snamewid = MakeStringBox(frame, "sourcename", namestring, labelwid, stringlabelwid);

  stringlabelwid = MakeLabel(frame, "serverlabel",     "     Server:", snamewid, NULL);
  serverwid = MakeStringBox(frame, "server", serverstring, snamewid, stringlabelwid);

  stringlabelwid = MakeLabel(frame, "servicelabel", "    Service:", serverwid, NULL);
  servicewid = MakeStringBox(frame, "service", serverstring, serverwid, stringlabelwid);

  stringlabelwid = MakeLabel(frame, "dblabel",      "   Database:", stringlabelwid, NULL);
  dbwid = MakeStringBox(frame, "database", dbstring, servicewid, stringlabelwid);

  stringlabelwid = MakeLabel(frame, "costlabel",    "       Cost:", stringlabelwid, NULL);
  costwid = MakeStringBox(frame, "cost", cost, dbwid, stringlabelwid);

  stringlabelwid = MakeLabel(frame, "unitlabel",    "      Units:", stringlabelwid, NULL);
  unitwid = MakeStringBox(frame, "costunit", units, costwid, stringlabelwid);

  stringlabelwid = MakeLabel(frame, "maintlabel",   " Maintainer:", stringlabelwid, NULL);
  maintainerwid = MakeStringBox(frame, "maintainer", maintainer, unitwid, stringlabelwid);

  stringlabelwid = MakeLabel(frame, "desclabel",    "Description:", stringlabelwid, NULL);
  descwid = MakeStringBox(frame, "description", description, stringlabelwid, NULL);

  button = MakeCommandButton(frame, "sdone", CloseSourceEdit, descwid, NULL, NULL);
  MakeCommandButton(frame, "cancel", CancelSourceEdit, descwid, button, NULL);

  return shell;
}

Widget
SetupWaisDisplay(parent)
Widget parent;
{
  Widget form, button;

  form = XtCreateManagedWidget ("form", formWidgetClass,
				parent, NULL, ZERO);

  Makelabel(form);

  questionwindow = MakeQuestionFrame(form);

  sourcewindow = MakeSourceFrame(form);

  button = MakeCommandButton(form, "help", XwaisHelp,
			     NULL, NULL, NULL);

  MakeCommandButton(form, "quit", DoQuit, NULL, button, NULL);

  messwidget = MakeMessage(form);

  sourcepopup = MakeSourcePopup(form);

  return form;
}
