/* hacked down to nothing from MuHelp and MuError */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <Mu.h>
#include <Xm/MessageB.h>
#include <Xm/Form.h>
#include <Xm/PushB.h>
#include <Xm/DialogS.h>
#include <Xm/Text.h>

Widget top[2] = { NULL, NULL };
Widget helpbox[2] = { NULL, NULL };
Widget errWidgets[3] = { NULL, NULL };

void MyHelp( i, string )
    char *string;
     int i;
 {
    Arg args[10];
    int n;
    Widget dummy;

    if (helpbox[i] == NULL)
      {
	n = 0;
	XtSetArg(args[n], XmNdefaultButtonType, XmDIALOG_OK_BUTTON); n++;
	/* XtSetArg(args[n], XmNdialogType, XmDIALOG_INFORMATION); n++;  */
	XtSetArg(args[n], XmNdialogStyle, XmDIALOG_APPLICATION_MODAL); n++;
	XtSetArg(args[n], XmNborderWidth, 2); n++;
	helpbox[i]=XmCreateInformationDialog( top[i],"helpbox",args,n);
	XtRealizeWidget(helpbox[i]);
	MuSetStandardCursor(helpbox[i]);

	dummy = XmMessageBoxGetChild(helpbox[i], XmDIALOG_CANCEL_BUTTON);
	XtDestroyWidget(dummy);
	dummy = XmMessageBoxGetChild(helpbox[i], XmDIALOG_HELP_BUTTON);
	XtDestroyWidget(dummy);
      }

    XtSetArg(args[0], XmNmessageString,
	     XmStringLtoRCreate(string,XmSTRING_DEFAULT_CHARSET)); 
    XtSetValues(helpbox[i], args, 1);
    XtManageChild(helpbox[i]);
}

static void CreateMyWidget( i )
int i;
{
    Arg args[5];
    int n;
    Widget dummy;

    n = 0;
    XtSetArg(args[n], XmNdefaultButtonType, XmDIALOG_OK_BUTTON); n++;
    /* XtSetArg(args[n], XmNdialogType, XmDIALOG_ERROR); n++; */
    XtSetArg(args[n], XmNdialogStyle, XmDIALOG_APPLICATION_MODAL); n++;
    XtSetArg(args[n], XmNborderWidth, 2); n++;
    errWidgets[i] = XmCreateErrorDialog(top[i],
                                               "errWidget",
                                               args,n);
    dummy = XmMessageBoxGetChild(errWidgets[i],
                                 XmDIALOG_CANCEL_BUTTON);
    XtUnmanageChild(dummy);
    dummy = XmMessageBoxGetChild(errWidgets[i],
                                 XmDIALOG_HELP_BUTTON);
    XtUnmanageChild(dummy);
    XtRealizeWidget(errWidgets[i]);
    MuSetStandardCursor(errWidgets[i]);
}


void MyError( i, string )
char *string;
int i;
{

    Arg args[1];

    if (errWidgets[i] == NULL)  CreateMyWidget( i );

    XtSetArg(args[0], XmNmessageString,
             XmStringLtoRCreate(string,XmSTRING_DEFAULT_CHARSET));
    XtSetValues(errWidgets[i], args, 1);

    if (!XtIsManaged(errWidgets[i]))
        XtManageChild(errWidgets[i]);
}


void
ScrollHelpFile(i, filename)
     int i;
     char *filename;
{
  struct stat statbuf;
  char *helpbuf;
  int fd;
  static Widget helpdialog=NULL;
  static Widget helptext, helpclose;
  Arg args[10];
  int n;

  if (stat(filename, &statbuf))
    {
      MuError("MuHelpFile: Unable to stat help file.");
      return;
    }

  if ((helpbuf = (char *) malloc((1 + (int)statbuf.st_size) * sizeof(char)))
      == (char *) NULL)
    {
      MuError("MuHelpFile: Out of memory.\nUnable to malloc enough space for help file.");
      return;
    }

  if ((fd = open(filename, O_RDONLY, 0)) < 0)
    {
      MuError("MuHelpFile: Unable to open help file for reading.\nPerhaps the permissions on the file are wrong.");
      close(fd);
      free(helpbuf);
      return;
    }

  if ((read(fd, helpbuf, (int)statbuf.st_size)) != statbuf.st_size)
    {
      MuError("MuHelpFile: An error occurred while reading the file.\nThe number of bytes read does not match the length of the file.");
      close(fd);
      free(helpbuf);
      return;
    }

  helpbuf[statbuf.st_size] = '\0';

  if (helpdialog == NULL)
    {
      n=0;
/*    XtSetArg(args[n], XmNdialogStyle, XmDIALOG_APPLICATION_MODAL); n++; */
      XtSetArg(args[n], XmNborderWidth, 2); n++;
      XtSetArg(args[n], XmNverticalSpacing, 5); n++;
      XtSetArg(args[n], XmNhorizontalSpacing, 5); n++;
      XtSetArg(args[n], XmNwidth, 420); n++;
      XtSetArg(args[n], XmNheight, 220); n++;
      helpdialog = XmCreateFormDialog(top[i], "helpDialog", args, n);

      n=0;
      XtSetArg(args[n], XmNshowAsDefault, True); n++;
      XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
      XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
      helpclose = XmCreatePushButton(helpdialog, "helpClose", args, n);
      XtManageChild(helpclose);

      n=0;
      XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
      XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
      XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
      XtSetArg(args[n], XmNbottomAttachment, XmATTACH_WIDGET); n++;
      XtSetArg(args[n], XmNbottomWidget, helpclose); n++;
      XtSetArg(args[n], XmNeditable, False); n++;
      XtSetArg(args[n], XmNeditMode, XmMULTI_LINE_EDIT); n++;
      XtSetArg(args[n], XmNwordWrap, True); n++;
      XtSetArg(args[n], XmNscrollHorizontal, False); n++;
      helptext = XmCreateScrolledText(helpdialog, "helpText", args, n);
      XtManageChild(helptext);
    }

  XmTextSetString(helptext, helpbuf);
  close(fd);
  free(helpbuf);

  XtManageChild(helpdialog);
  return;
}
