
/******************************************************************
 *                                                                *
 *                  Computer Science Department                   *
 *                                                                *
 *                        Master's Project                        *
 *                                                                *
 *                Graphical Development Environment               *
 *                                                                *
 *                     for Postgres Database                      *
 *                                                                *
 *                    Designed Especially for                     *
 *                                                                *
 *                 Postgres Developers and Users                  *
 *                                                                *
 *                              At                                *
 *                                                                *
 *                     Concordia University                       *
 *                                                                *
 ******************************************************************
 *                                                                *
 * Designer and Programmer:  Khaled Jababo                        *
 * Email                  :  jababo@cs.concordia.ca               *
 *                                                                *
 * Supervised by          :  Dr. Bipin C. Desai.                  *
 *                           Computer Science Department.         *
 * Email                  :  bcdesai@cs.concordia.ca              *
 *                                                                *
 * Last Modified          :  June 18, 1994                        *
 *                                                                *
 *                                                                *
 *                                                                *
 * Disclaimer:  There are no guarantees as to the accuracy, cu-   *
 *             rrency, quality, or usefulness of these programs   *
 *             or any information that is provided by using it.   *
 *             Furthermore, there is no assumption of responsi-   *
 *             bility for any los or damage which may be caused   *
 *             by their use.                                      *
 *                                                                *
 *                                                                *
 * Forward comments or questions about this report or the GDEP    *
 * system to:                                                     *
 *              bcdesai@cs.concordia.ca                           *
 *              jababo@cs.concordia.ca                            *
 *                                                                *
 ******************************************************************/


/******************************************************************/
/***********************     instance.c    ************************/
/*    This file contains all the functions needed by  the applica */
/*  tion to respond to the user request to manipulate instance by */
/*  instance of a particular class in the database.  For example, */
/*  it responds to the user request of showing nxt instance, show */
/*  previous instance, add a new instance, update an instance and */
/*  so on.                                                        */
/******************************************************************/

#include "mainfile.h"

#ifndef INSTANCE
#define INSTANCE
#define XaTermWidth 32
#define XaTermHeight 32
static char XaTermBits[] = {
   0x00, 0x00, 0x00, 0x00, 0xf8, 0xff, 0xff, 0x1f, 0x04, 0x00, 0x00, 0x20,
   0xe4, 0xff, 0xff, 0x27, 0x14, 0x00, 0x00, 0x28, 0x14, 0x80, 0x01, 0x28,
   0x14, 0x80, 0x01, 0x28, 0x14, 0x80, 0x01, 0x28, 0x14, 0x80, 0x01, 0x28,
   0x14, 0x80, 0x01, 0x28, 0x14, 0x80, 0x01, 0x28, 0x14, 0x80, 0x00, 0x28,
   0x14, 0x80, 0x00, 0x28, 0x14, 0x80, 0x00, 0x28, 0x14, 0x80, 0x00, 0x28,
   0x14, 0x00, 0x00, 0x28, 0x14, 0x80, 0x00, 0x28, 0x14, 0x00, 0x00, 0x28,
   0xe4, 0xff, 0xff, 0x27, 0x04, 0x00, 0x00, 0x20, 0xf8, 0xff, 0xff, 0x1f,
   0xfe, 0xff, 0xff, 0x7f, 0x02, 0x00, 0x00, 0x40, 0x02, 0x00, 0x00, 0x40,
   0x02, 0x00, 0xc0, 0x5f, 0x02, 0x00, 0x00, 0x40, 0xfa, 0xff, 0xff, 0x5f,
   0xae, 0xaa, 0xaa, 0x6a, 0x56, 0x55, 0x55, 0x55, 0xab, 0xaa, 0xaa, 0xea,
   0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00};

#endif


/******************************************************************/
/*                         XaFsetCarriageReturn                   */
/*    This function is to get a text and replace the \n by \\n in */
/*  order for postgres to accept it as carriage return, because \ */
/*  is a special character for postgres.                          */
/*                                                                */
/******************************************************************/

char* XaFsetCarriageReturn(XaLbuffer)
  char* XaLbuffer;

  { /* begin XaFsetCarriageReturn function */
    int                 XaLi, XaLj ;
    MaBufferType        MaLtempBuffer ;

    XaLj = 0 ;
    strcpy(MaLtempBuffer, "") ;
    for (XaLi = 0; XaLi < strlen(XaLbuffer); XaLi++)
      {
        if (XaLbuffer[XaLi] == '\n')
          { 
            if (XaGDEPexportFileCompatible == MaFALSE)
              MaLtempBuffer[XaLj++] = '\\' ;

            MaLtempBuffer[XaLj++] = '\\' ;
            MaLtempBuffer[XaLj++] = 'n' ;
          }
        else
          {
            if (XaLbuffer[XaLi] == '\"')
              {
                if (XaGDEPexportFileCompatible == MaFALSE)
                  MaLtempBuffer[XaLj++] = '\\' ;

                MaLtempBuffer[XaLj++] = '\\' ;
              }

            MaLtempBuffer[XaLj++] = XaLbuffer[XaLi] ;
          }
      }
    MaLtempBuffer[XaLj] = '\0' ;
    return(MaLtempBuffer) ;

  } /* end  XaFsetCarriageReturn function */


/******************************************************************/
/*                      XaFnoInstanceDisplayShell                 */
/*    This function is to take care of the display when there is  */
/*  no more instances in the class to be displayed.               */
/*                                                                */
/******************************************************************/

void XaFnoInstanceDisplayShell(XaLwidget, XaLpClientData)
  Widget             XaLwidget    ;
  MaClientDataStruct XaLpClientData ;

  { /* begin XaFnoInstanceDisplayShell function */
    void extern XaFcancelInstanceDisplayShell() ;

    XaFcancelInstanceDisplayShell(XaLwidget, XaLpClientData) ;

    sprintf(XaLpClientData -> MaLdialogMessage, 
           "No More Instances For Class %s",
           XaLpClientData -> MaLclassName) ;

    XaLpClientData -> MaLdialogType = XaDialogNotice ;
    XaLpClientData -> MaLfreeNodeOk = MaTRUE ;
    XaFpostDialog(XaWorkAreaHeaderLogo ,XaLpClientData) ;

  } /* begin XaFnoInstanceDisplayShell function */


/******************************************************************/
/*                     XaFnextInstanceDisplayShell                */
/*    This function is to Handle the Callback next call from the  */
/*  instance display shell.                                       */
/*                                                                */
/******************************************************************/

void XaFnextInstanceDisplayShell(XaLwidget, XaLpClientData)
  Widget             XaLwidget    ;
  MaClientDataStruct XaLpClientData ;

  { /* begin XaFnextInstanceDisplayShell function */

    XtManageChild(XaLpClientData -> MaLshellPreviousButton) ;
    XtManageChild(XaLpClientData -> MaLshellNextButton    ) ;

    /***************************************************/
    /*  Retrieve the oid of the Instances of the Class */
    /* and set the previous, current and next Oids.    */
    /***************************************************/
    XaLpClientData -> MaLfreeNodeOk = MaFALSE ;

    if (!XaFsetClassObjectIdsInstances(XaLwidget, XaLpClientData))
      XaFnoInstanceDisplayShell(XaLwidget, XaLpClientData) ;

    if (XaLpClientData == NULL)
      return ;

    XaLpClientData -> MaLfreeNodeOk = MaTRUE ;

    if (XaLpClientData -> MaLpreviousOid < 0)
      XtUnmanageChild(XaLpClientData -> MaLshellPreviousButton) ;
    if (XaLpClientData -> MaLnextOid == MaMaxOidNumber)
      XtUnmanageChild(XaLpClientData -> MaLshellNextButton) ;

    MaFsetOidsInstanceAttributesValue(XaLpClientData) ;
      
    XaFdisplayFillAttributeFields(XaLpClientData) ;

  } /* end   XaFnextInstanceDisplayShell function */


/******************************************************************/
/*                    XaFpreviousInstanceDisplayShell             */
/*    This function is to Handle the Callback previous call from  */
/*  the instance display shell.                                   */
/*                                                                */
/******************************************************************/

void XaFpreviousInstanceDisplayShell(XaLwidget, XaLpClientData)
  Widget             XaLwidget    ;
  MaClientDataStruct XaLpClientData ;

  { /* begin XaFpreviousInstanceDisplayShell function */
    XtManageChild(XaLpClientData -> MaLshellPreviousButton) ;
    XtManageChild(XaLpClientData -> MaLshellNextButton    ) ;

    XaLpClientData -> MaLcurrentOid = (XaLpClientData -> MaLpreviousOid) - 1 ;

    /***************************************************/
    /*  Retrieve the oid of the Instances of the Class */
    /* and set the previous, current and next Oids.    */
    /***************************************************/
    if (!XaFsetClassObjectIdsInstances(XaLwidget, XaLpClientData))
      return ;

    if (XaLpClientData -> MaLpreviousOid < 0)
      XtUnmanageChild(XaLpClientData -> MaLshellPreviousButton) ;
    if (XaLpClientData -> MaLnextOid == MaMaxOidNumber)
      XtUnmanageChild(XaLpClientData -> MaLshellNextButton) ;

    MaFsetOidsInstanceAttributesValue(XaLpClientData) ;

    XaFdisplayFillAttributeFields(XaLpClientData) ;
      
  } /* end   XaFpreviousInstanceDisplayShell function */


/******************************************************************/
/*                     XaFclearInstanceDisplayShell               */
/*    This function is to Handle the Callback clear call from the */
/*  instance display shell.  It clears all the fields of the view */
/*  for the particular class.                                     */
/*                                                                */
/******************************************************************/

void XaFclearInstanceDisplayShell(XaLwidget, XaLpClientData)
  Widget             XaLwidget    ;
  MaClientDataStruct XaLpClientData ;

  { /* begin XaFclearInstanceDisplayShell function */
    MaStringListStruct       MaLpStringList ;

    MaLpStringList = XaLpClientData -> MaLstringList ;
    while (MaLpStringList != NULL)
      {
        strcpy(MaLpStringList -> MaLstringData, "\0") ;
        XaFclearTextBufferArea(MaLpStringList -> MaLstringData,
                               MaLpStringList -> MaLchildWidgetId) ;
        MaLpStringList = MaLpStringList -> MaLpNextString ;
      }

    MaLpStringList = XaLpClientData -> MaLtextStringList ;
    while (MaLpStringList != NULL)
      {
        strcpy(MaLpStringList -> MaLstringData, "\0") ;
        XaFclearTextBufferArea(MaLpStringList -> MaLstringData,
                               MaLpStringList -> MaLchildWidgetId) ;
        MaLpStringList = MaLpStringList -> MaLpNextString ;
      }

  } /* end   XaFclearInstanceDisplayShell function */


/******************************************************************/
/*                 XaFsetNameValuesInstanceDisplayShell           */
/*    This function is to get the values of all the fields of the */
/*  view for the particular class & assigns its attributes in the */
/*  structure according to the  values entered by the user in the */
/*  view.  It returns a string consisting of every field followed */
/*  by its value.                                                 */
/*                                                                */
/******************************************************************/

char* XaFsetNameValuesInstanceDisplayShell(XaLwidget, XaLpClientData)
  Widget             XaLwidget    ;
  MaClientDataStruct XaLpClientData ;

  { /* begin XaFsetNameValuesInstanceDisplayShell function */
    int                 MaLfirstPass = MaTRUE ;
    int                 MaLstringField ;
    MaBufferType        MaLquery ;
    MaStringListStruct  MaLpStringList ;

    strcpy(MaLquery, "(") ;

    MaLpStringList = XaLpClientData -> MaLstringList ;
    while (MaLpStringList != NULL)
      {
       if (strcmp(MaLpStringList -> MaLlabelName,"") != 0)
        {
        if ((XaLpClientData -> MaLselectionAction ==
                                 MaSelectionActionExportClassDefinition)||
            (XaLpClientData -> MaLselectionAction == 
                                 MaSelectionActionExportClassInstances))
          ;
        else
          strcpy(MaLpStringList -> MaLstringData,
                 XaFgetTextBufferArea(MaLpStringList -> MaLchildWidgetId)) ;

        if (MaLfirstPass == MaTRUE)
          MaLfirstPass = MaFALSE ;
        else
          strcat(MaLquery, " , ") ;

        if ((strcmp("int2", MaLpStringList -> MaLlabelType) == 0) ||
            (strcmp("int4",  MaLpStringList -> MaLlabelType) == 0) ||
            (strcmp("int28",    MaLpStringList -> MaLlabelType) == 0) ||
            (strcmp("float4",    MaLpStringList -> MaLlabelType) == 0) ||
            (strcmp("float8",    MaLpStringList -> MaLlabelType) == 0))
          MaLstringField = MaFALSE ;
        else
          {
            MaLstringField = MaTRUE ;

          /*************************************************************************/
          /* For Compatibility with monitor to input files this can be removed in  */
          /* order for the carriage return to appear in text fields.  But then, we */
          /* still have errors on quotes when they appear in text fields.          */
          /*************************************************************************/
          if (XaLpClientData -> MaLselectionAction ==
                                               MaSelectionActionExportClassInstances)
            {
              MaBufferType MaLtempRecord ;
              strcpy(MaLtempRecord,
                     XaFsetCarriageReturn(MaLpStringList -> MaLstringData)) ;
              strcpy(MaLpStringList -> MaLstringData, MaLtempRecord) ;
            }
          }

        if (XaLpClientData -> MaLselectionAction ==
                                 MaSelectionActionExportClassDefinition)
          MaLstringField = MaFALSE ;

        strcat(MaLquery,MaLpStringList -> MaLlabelName ) ;
        strcat(MaLquery," = ") ;
        if (MaLstringField)
          strcat(MaLquery,"\"") ;
        strcat(MaLquery,MaLpStringList -> MaLstringData) ;
        if (MaLstringField)
          strcat(MaLquery,"\"") ;
        }
       MaLpStringList = MaLpStringList -> MaLpNextString ;
      }

    MaLpStringList = XaLpClientData -> MaLtextStringList ;
    while (MaLpStringList != NULL)
      {
        strcpy(MaLpStringList -> MaLstringData,
               XaFsetCarriageReturn(
                     XaFgetTextBufferArea(MaLpStringList -> MaLchildWidgetId))) ;

        if (MaLfirstPass == MaTRUE)
          MaLfirstPass = MaFALSE ;
        else
          strcat(MaLquery, " , ") ;

        strcat(MaLquery,MaLpStringList -> MaLlabelName ) ;
        strcat(MaLquery," = \"") ;
        strcat(MaLquery,MaLpStringList -> MaLstringData) ;
        strcat(MaLquery,"\"") ;

        MaLpStringList = MaLpStringList -> MaLpNextString ;
      }

    strcat(MaLquery, ")") ;
    return(MaLquery) ;

  } /* end   XaFsetNameValuesInstanceDisplayShell function */


/******************************************************************/
/*                     XaFupdateInstanceDisplayShell              */
/*    This function is to Hndle the Callback update call from the */
/*  instance display shell.                                       */
/*                                                                */
/******************************************************************/

void XaFupdateInstanceDisplayShell(XaLwidget, XaLpClientData)
  Widget             XaLwidget      ;
  MaClientDataStruct XaLpClientData ;

  { /* begin XaFupdateInstanceDisplayShell function */
    char                *MaLret   ;
    MaStringType        MaLstring ;
    MaBufferType        MaLquery  ;
    extern MaClientDataStruct MaFgetPtrClientData() ;
    MaClientDataStruct  XaLtempClientData = MaFgetPtrClientData() ;

    sprintf(MaLquery, "replace %s ", XaLpClientData -> MaLclassName) ;
    strcat (MaLquery,
            XaFsetNameValuesInstanceDisplayShell(XaLwidget, XaLpClientData)) ;

    sprintf(MaLstring, " where %s.oid = \"%d\"",
            XaLpClientData -> MaLclassName,
            XaLpClientData -> MaLcurrentOid) ;

    strcat (MaLquery, MaLstring) ;

    PQexec("begin") ;
    MaLret = PQexec(MaLquery) ;
    PQexec("end") ;

    if (*MaLret == 'R')
      {
        sprintf(XaLtempClientData -> MaLdialogMessage, 
               "Instance Could not be Modified in Class %s",
               XaLpClientData -> MaLclassName) ;

        XaLtempClientData -> MaLdialogType = XaDialogNotice ;
        XaFpostDialog(XtParent(XaLwidget) ,XaLtempClientData) ;
      }
    else 
      {
        sprintf(XaLtempClientData -> MaLdialogMessage, 
               "Instance Has been Modified Successfully in Class %s",
               XaLpClientData -> MaLclassName) ;

        XaLtempClientData -> MaLdialogType = XaDialogNotice ;
        XaFpostDialog(XtParent(XaLwidget) ,XaLtempClientData) ;
      }


  } /* end   XaFupdateInstanceDisplayShell function */


/******************************************************************/
/*                     XaFdeleteInstanceDisplayShell              */
/*    This function is to Hndle the Callback delete call from the */
/*  instance display shell.                                       */
/*                                                                */
/******************************************************************/

void XaFdeleteInstanceDisplayShell(XaLwidget, XaLpClientData)
  Widget             XaLwidget    ;
  MaClientDataStruct XaLpClientData ;

  { /* begin XaFdeleteInstanceDisplayShell function */
    char         *XaLret ;
    MaStringType XaLquery ;
    extern MaClientDataStruct MaFgetPtrClientData() ;
    MaClientDataStruct  XaLtempClientData = MaFgetPtrClientData() ;

    sprintf(XaLquery, "delete %s where %s.oid = \"%d\"",
                     XaLpClientData -> MaLclassName,
                     XaLpClientData -> MaLclassName,
                     XaLpClientData -> MaLcurrentOid) ;
    PQexec("begin") ;
    XaLret = PQexec(XaLquery) ;
    PQexec("end") ;

    if (*XaLret == 'R')
      {
        sprintf(XaLtempClientData -> MaLdialogMessage, 
               "Instance Could not be deleted in Class %s",
               XaLpClientData -> MaLclassName) ;

        XaLtempClientData -> MaLdialogType = XaDialogNotice ;
        XaFpostDialog(XtParent(XaLwidget) ,XaLtempClientData) ;
      }
    else 
      {

        XaFnextInstanceDisplayShell(XaLwidget, XaLpClientData) ;
      }

  } /* end   XaFdeleteInstanceDisplayShell function */


/******************************************************************/
/*                  XaFdeleteConfirmInstanceDisplayShell          */
/*    This function is to Hndle the Callback delete call from the */
/*  instance display shell. It asks the user to confirm the dele- */
/*  of the instance before it delete the current instance.        */
/*                                                                */
/******************************************************************/

void XaFdeleteConfirmInstanceDisplayShell(XaLwidget, XaLpClientData)
  Widget             XaLwidget    ;
  MaClientDataStruct XaLpClientData ;

  { /* begin XaFdeleteConfirmInstanceDisplayShell function */
    extern Widget XaFconfirmUserRequestDialog() ;

    sprintf(XaLpClientData -> MaLdialogMessage,
            "Delete Current Instance in Class %s ?",
            XaLpClientData -> MaLclassName) ;

    XaLpClientData -> MaLselectionMode = MaSelectionActionEditInstanceDelete ;

    (void) XaFconfirmUserRequestDialog(XaLwidget, XaLpClientData) ;

  } /* end   XaFdeleteConfirmInstanceDisplayShell function */


/******************************************************************/
/*                      XaFaddInstanceDisplayShell                */
/*    This function is to Handle the Callback add call from the   */
/*  instance display shell.                                       */
/*                                                                */
/******************************************************************/

void XaFaddInstanceDisplayShell(XaLwidget, XaLpClientData)
  Widget             XaLwidget    ;
  MaClientDataStruct XaLpClientData ;

  { /* begin XaFaddInstanceDisplayShell function */
    char                *MaLret  ;
    MaBufferType        MaLquery ;
    extern MaClientDataStruct MaFgetPtrClientData() ;
    MaClientDataStruct  XaLtempClientData = MaFgetPtrClientData() ;
    
    sprintf(MaLquery, "append %s ", XaLpClientData -> MaLclassName) ;
    strcat (MaLquery,
            XaFsetNameValuesInstanceDisplayShell(XaLwidget, XaLpClientData)) ;

    PQexec("begin") ;
    MaLret = PQexec(MaLquery) ;
    PQexec("end") ;

    if (*MaLret == 'R')
      {
        sprintf(XaLtempClientData -> MaLdialogMessage, 
               "Instance Could not be Added to Class %s",
               XaLpClientData -> MaLclassName) ;

        XaLtempClientData -> MaLdialogType = XaDialogNotice ;
        (void) XaFpostDialog(XtParent(XaLwidget) ,XaLtempClientData) ;
      }
    else 
      {
        sprintf(XaLtempClientData -> MaLdialogMessage, 
               "Instance Has been Added Successfully to Class %s",
               XaLpClientData -> MaLclassName) ;

        XaLtempClientData -> MaLdialogType = XaDialogNotice ;
        (void) XaFpostDialog(XtParent(XaLwidget) ,XaLtempClientData) ;
      }

  } /* end   XaFaddInstanceDisplayShell function */


/******************************************************************/
/*                    XaFcancelInstanceDisplayShell               */
/*    This function is to Hndle the Callback cancel call from the */
/*  instance display shell.                                       */
/*                                                                */
/******************************************************************/

void XaFcancelInstanceDisplayShell(XaLwidget, XaLpClientData)
  Widget             XaLwidget    ;
  MaClientDataStruct XaLpClientData ;

  { /* begin XaFcancelInstanceDisplayShell function */
    MaClientDataStruct extern MaFfreeClientData() ;

    if (XaInstanceShellBoxWindow && (XaInstanceShellExistStatus == MaTRUE) )
      {
        XaFupdateXYposition() ;
      }

    XaInstanceShellExistStatus = MaFALSE ;

    XtDestroyWidget(XaLpClientData -> MaLshellWidgetId) ;

    if (XaLpClientData -> MaLfreeNodeOk)
      XaLpClientData = MaFfreeClientData(XaLpClientData) ;

  } /* end   XaFcancelInstanceDisplayShell function */


/******************************************************************/
/*                       XaFinstanceDisplayShell                  */
/*    This function is to Construct the shell to display, edit or */
/*  add instances for the class specified in the Class Name field */
/*  of the main apllication window.                               */
/*                                                                */
/******************************************************************/

void XaFinstanceDisplayShell(XaLwidget, XaLpClientData)
  Widget             XaLwidget    ;
  MaClientDataStruct XaLpClientData ;

  { /* begin XaFinstanceDisplayShell function */
    Widget XaLinstanceShell,
           XaLpaneInter, XaLmainInter,
           XaLpane, XaLform, XaLlabel, XaLheaderInfo, XaLheaderText,
           XaLheaderInfoMain, XaLheaderInfoRowCol, XaLheaderInfoRowColForm,
           XaLocalWidgetNext, XaLocalWidgetPrevious,
           XaLocalWidgetUpdate, XaLocalWidgetAdd,
           XaLocalWidgetCancel ;

    Pixmap    XaLpixmap ;
    Pixel     fg, bg ;
    Arg       XaLargs[9] ;
    int       i ;
    char      *p, XaLbuf[BUFSIZ] ;


    /********************************************************************/
    /*  This is to check if there exist an instance shell already then  */
    /* save its X and Y position to create the new shell in the same X  */
    /* and Y position.                                                  */
    /********************************************************************/
    if (XaInstanceShellBoxWindow && (XaInstanceShellExistStatus == MaTRUE))
      XaFupdateXYposition() ;

    if (XaInstanceShellBoxX < 0 || XaInstanceShellBoxX > 700)
      XaInstanceShellBoxX = 0 ;
    if (XaInstanceShellBoxY < 0 || XaInstanceShellBoxY > 700)
      XaInstanceShellBoxY = 0 ;

    /********************************************************************/
    /*    This is to Initialize the Instance Values of the First Oid if */
    /*  there exist at least one oid.                                   */ 
    /********************************************************************/
    MaFsetOidsInstanceAttributesValue(XaLpClientData) ;


    XaLpClientData -> MaLshellWidgetId = XtVaCreatePopupShell(
                                    XaLpClientData -> MaLlistLabel,
                                    xmDialogShellWidgetClass, XaFgetTopShell(XaLwidget),
                                    XmNdeleteResponse,        XmDESTROY,
                                    XmNx,                     XaInstanceShellBoxX,
                                    XmNy,                     XaInstanceShellBoxY,
                                    NULL) ;

    XaInstanceShellBoxWindow   = XaLpClientData -> MaLshellWidgetId ;
    XaInstanceShellExistStatus = MaTRUE ;

    XaLpane = XtVaCreateWidget("XaLpane",
                               xmPanedWindowWidgetClass,
                               XaLpClientData -> MaLshellWidgetId,
                               XmNsashWidth,  1,
                               XmNsashHeight, 1,
                               NULL) ;

    /************************************************************************/
    /*  Create the Header Logo and Class Title Name for the Instance Shell  */
    /************************************************************************/

    XaLform = XtVaCreateWidget("XaLform1", xmFormWidgetClass, XaLpane, NULL) ;

      /**************************************************/
      /*  Create the Logo Window for the instance Shell */
      /**************************************************/

      XtVaGetValues(XaLform,
                  XmNforeground, &fg,
                  XmNbackground, &bg,
                  NULL) ;

      XaLpixmap = XCreatePixmapFromBitmapData(XtDisplay(XaLform),
                                      RootWindowOfScreen(XtScreen(XaLform)),
                                      XaTermBits,
                                      XaTermWidth ,
                                      XaTermHeight,
                                      fg, bg, DefaultDepthOfScreen(XtScreen(XaLform))) ;

      XaLlabel  = XtVaCreateManagedWidget("XaLlabel",xmLabelGadgetClass, XaLform,
                                     XmNlabelType,         XmPIXMAP,
                                     XmNlabelPixmap,       XaLpixmap,
                                     XmNleftAttachment,    XmATTACH_FORM,
                                     XmNtopAttachment,     XmATTACH_FORM,
                                     XmNbottomAttachment,  XmATTACH_FORM,
                                     NULL) ;

      /******************************************************/
      /*  end Create the Logo Window for the instance Shell */
      /******************************************************/

      /****************************************************/
      /*  Create Header Class Name for the instance Shell */
      /****************************************************/

      XaLheaderInfo = XtVaCreateWidget("XaWorkAreaHeaderInfo",
                                       xmFormWidgetClass,         XaLform,
                                       XmNtopAttachment,          XmATTACH_FORM,
                                       XmNleftAttachment,         XmATTACH_WIDGET,
                                       XmNleftWidget,             XaLlabel,
                                       XmNrightAttachment,        XmATTACH_FORM,
                                       XmNbottomAttachment,       XmATTACH_FORM,
                                       XmNscrollBarDisplayPolicy, XmAS_NEEDED,
                                       XmNscrollingPolicy,        XmAUTOMATIC,
                                       NULL) ;


      XaLheaderInfoMain = XtVaCreateWidget("XaLmain",
                                           xmMainWindowWidgetClass,
                                           XaLheaderInfo,
                                           NULL) ;

      XaLheaderInfoRowCol = XtVaCreateWidget("XaWorkAreaHeaderInfoRowCol",
                                             xmRowColumnWidgetClass,
                                             XaLheaderInfoMain,
                                             NULL) ;

      XaLheaderInfoRowColForm =  XtVaCreateWidget("XaWorkAreaHeaderInfo",
                                        xmFormWidgetClass, 
                                        XaLheaderInfoRowCol,
                                        XmNfractionBase, 10,
                                        NULL) ;

      XtVaCreateManagedWidget("Class Name : ",
                              xmLabelGadgetClass,       XaLheaderInfoRowColForm,
                              XmNtopAttachment,         XmATTACH_FORM,
                              XmNbottomAttachment,      XmATTACH_FORM,
                              XmNleftAttachment,        XmATTACH_FORM,
                              XmNrightAttachment,       XmATTACH_POSITION,
                              XmNrightPosition,         4,
                              XmNalignment,             XmALIGNMENT_END,
                              NULL) ;

      XaLheaderText = XtVaCreateManagedWidget("XaLtext",
                              xmTextFieldWidgetClass,   XaLheaderInfoRowColForm,
                              XmNtraversalOn,           False,
                              XmNcursorPositionVisible, False,
                              XmNrightAttachment,       XmATTACH_POSITION,
                              XmNrightPosition,         10,
                              XmNleftAttachment,        XmATTACH_POSITION,
                              XmNleftPosition,          5,
                              NULL) ;

      XmTextReplace(XaLheaderText,0, 
      strlen(XaLpClientData->MaLclassName), XaLpClientData->MaLclassName) ;

      XtManageChild(XaLheaderInfoRowColForm) ;
      XtManageChild(XaLheaderInfoRowCol) ;
      XtManageChild(XaLheaderInfoMain) ;
      XtManageChild(XaLheaderInfo) ;

      /********************************************************/
      /*  end Create Header Class Name for the instance Shell */
      /********************************************************/

    XtManageChild(XaLform) ;

    /************************************************************************/
    /*  end create Header Logo and Class Title Name for the Instance Shell  */
    /************************************************************************/


    /********************************************************************/
    /*  Create the Fields for every attribute in the Class. A text type */
    /*  attribute will have a scrolled text window, and all other field */
    /*  type will have a field text window.                             */
    /********************************************************************/


    /*************************************************/
    /*  Create the Fields that are not of text type. */
    /*************************************************/
    if (XaLpClientData -> MaLstringList != NULL)
     {
      Widget  XaLrowcolWidget ;
      int XaLi ;
      int XaLsize = MaFgetSizeStringList(XaLpClientData -> MaLstringList) ;
      MaStringListStruct MaLpStringList = XaLpClientData -> MaLstringList ;

      XaLrowcolWidget  = XtVaCreateWidget("XaLrowcolWidget",
                               xmRowColumnWidgetClass, XaLpane,
                               XmNpacking,             XmPACK_COLUMN,
                               XmNnumColumns,          XaLsize,
                               XmNorientation,         XmHORIZONTAL,
                               XmNisAligned,           True,
                               XmNentryAlignment,      XmALIGNMENT_END,
                               /* options are XmALIGNMENT_BEGINNING (default), */
                               /*             XmALIGNMENT_CENTER,              */
                               /*             XmALIGNMENT_END                  */
                               NULL) ;

      while (MaLpStringList != NULL)
        {
          XtVaCreateManagedWidget(MaLpStringList -> MaLlabelName, 
                                xmLabelGadgetClass,
                                XaLrowcolWidget,
                                NULL) ;

          MaLpStringList -> MaLchildWidgetId = XtVaCreateManagedWidget(
                  "",
                  xmTextWidgetClass,        XaLrowcolWidget,
                  XmNtraversalOn,           XaLpClientData -> MaLeditableMode,
                  XmNcursorPositionVisible, XaLpClientData -> MaLeditableMode,
                  XmNcolumns,               30,
                  NULL) ;

          XaFfillupTextBufferArea(MaLpStringList ->MaLstringData,
                                MaLpStringList -> MaLchildWidgetId) ;
          MaLpStringList = MaLpStringList -> MaLpNextString ;
        }
      XtManageChild(XaLrowcolWidget) ;
     }

    /*********************************************/
    /*  Create the Fields that are of text type. */
    /*********************************************/
    if (XaLpClientData -> MaLtextStringList != NULL)
     {
      Widget XaLdisplayWidget ;
      Arg    XaLarguments[10] ;
      MaStringListStruct MaLpTextStringList = XaLpClientData -> MaLtextStringList ;

          XtSetArg(XaLarguments[0], XmNrows,      XaInstanceAreaDisplayRows);
          XtSetArg(XaLarguments[1], XmNcolumns,   80);
          XtSetArg(XaLarguments[2], XmNeditMode,  XmMULTI_LINE_EDIT);
          XtSetArg(XaLarguments[3], XmNwordWrap,  True);
          XtSetArg(XaLarguments[4], XmNblinkRate, 0);
          XtSetArg(XaLarguments[5], XmNeditable,
                                        XaLpClientData -> MaLeditableMode);
          XtSetArg(XaLarguments[6], XmNautoShowCursorPosition,
                                        XaLpClientData -> MaLeditableMode);
          XtSetArg(XaLarguments[7], XmNcursorPositionVisible,
                                        XaLpClientData -> MaLeditableMode);
          XtSetArg(XaLarguments[8], XmNtraversalOn,
                                        XaLpClientData -> MaLeditableMode);

          XaLmainInter = XtVaCreateWidget("XaLdisplayWidget",
                                          xmMainWindowWidgetClass,
                                          XaLpane,
                                          NULL);

          XaLpaneInter = XtVaCreateWidget("XaLdisplayWidget",
                                          xmPanedWindowWidgetClass,
                                          XaLmainInter,
                                          NULL);

      while (MaLpTextStringList != NULL)
        {
          Widget XaLbutton ;
          extern void XaFunmanageChild() ;

          XaLdisplayWidget =  XtVaCreateWidget("XaLdisplayWidget",
                                               xmPanedWindowWidgetClass,
                                               XaLpaneInter,
                                               NULL);

          XaLbutton =  XtVaCreateManagedWidget(MaLpTextStringList -> MaLlabelName,
                                              xmPushButtonGadgetClass, XaLdisplayWidget,
                                              NULL) ;

         XtAddCallback(XaLbutton, XmNactivateCallback,
                       XaFunmanageChild,XaLdisplayWidget) ;

          MaLpTextStringList -> MaLchildWidgetId = 
                                XmCreateScrolledText(XaLdisplayWidget,
                                                     "XaWorkAreaDisplayText",
                                                      XaLarguments, 8);

          XaFfillupTextBufferArea(MaLpTextStringList -> MaLstringData,
                                  MaLpTextStringList -> MaLchildWidgetId) ;

          XtManageChild(MaLpTextStringList -> MaLchildWidgetId) ;
          XtManageChild(XaLdisplayWidget) ;

          MaLpTextStringList = MaLpTextStringList -> MaLpNextString ;
       }
       XtManageChild(XaLpaneInter) ;
       XtManageChild(XaLmainInter) ;
      }

    /********************************************************************/
    /*  End Create the Fields for every attribute in the Class.         */
    /********************************************************************/


    /******************************************************************/
    /*  Create the Actions Bar previous, next, update, add and cancel */
    /*  options for the Instance Shell                                */
    /******************************************************************/

    XaLform = XtVaCreateWidget("XaLform2", xmFormWidgetClass, XaLpane,
                            XmNfractionBase,      21,
                            XmNpaneMinimum,       XaButtonSize,
                            XmNpaneMaximum,       XaButtonSize,
                            NULL) ;

    if ((XaLpClientData -> MaLselectionAction == MaSelectionActionEditInstance)
      ||(XaLpClientData -> MaLselectionAction == MaSelectionActionViewInstance))
      {
        XaLocalWidgetPrevious = XtVaCreateManagedWidget("Previous",
                                   xmPushButtonGadgetClass, XaLform,
                                   XmNtopAttachment,     XmATTACH_FORM,
                                   XmNbottomAttachment,  XmATTACH_FORM,
                                   XmNleftAttachment,    XmATTACH_POSITION,
                                   XmNleftPosition,      1,
                                   XmNrightAttachment,   XmATTACH_POSITION,
                                   XmNrightPosition,     4,
                                   XmNshowAsDefault,     True,
                                   XmNdefaultButtonShadowThickness, 1,
                                   NULL) ;

        XtAddCallback(XaLocalWidgetPrevious, XmNactivateCallback, 
                      XaFpreviousInstanceDisplayShell, XaLpClientData) ;

        XaLpClientData -> MaLshellPreviousButton = XaLocalWidgetPrevious ;

        if (XaLpClientData -> MaLpreviousOid < 0)
          XtUnmanageChild(XaLpClientData -> MaLshellPreviousButton) ;
      }

    if ((XaLpClientData -> MaLselectionAction == MaSelectionActionEditInstance)
      ||(XaLpClientData -> MaLselectionAction == MaSelectionActionViewInstance))
      {
        XaLocalWidgetNext = XtVaCreateManagedWidget("Next",
                                   xmPushButtonGadgetClass, XaLform,
                                   XmNtopAttachment,     XmATTACH_FORM,
                                   XmNbottomAttachment,  XmATTACH_FORM,
                                   XmNleftAttachment,    XmATTACH_POSITION,
                                   XmNleftPosition,      5,
                                   XmNrightAttachment,   XmATTACH_POSITION,
                                   XmNrightPosition,     8,
                                   XmNshowAsDefault,     True,
                                   XmNdefaultButtonShadowThickness, 1,
                                   NULL) ;

        XtAddCallback(XaLocalWidgetNext, XmNactivateCallback, 
                      XaFnextInstanceDisplayShell, XaLpClientData) ;

        XaLpClientData -> MaLshellNextButton = XaLocalWidgetNext ;

        if (XaLpClientData -> MaLnextOid == MaMaxOidNumber)
          XtUnmanageChild(XaLpClientData -> MaLshellNextButton) ;
      }

    if (XaLpClientData -> MaLselectionAction == MaSelectionActionEditInstance)
      {
        XaLocalWidgetUpdate = XtVaCreateManagedWidget("Update",
                                   xmPushButtonGadgetClass, XaLform,
                                   XmNtopAttachment,     XmATTACH_FORM,
                                   XmNbottomAttachment,  XmATTACH_FORM,
                                   XmNleftAttachment,    XmATTACH_POSITION,
                                   XmNleftPosition,      9,
                                   XmNrightAttachment,   XmATTACH_POSITION,
                                   XmNrightPosition,     12,
                                   XmNshowAsDefault,     True,
                                   XmNdefaultButtonShadowThickness, 1,
                                   NULL) ;

        XtAddCallback(XaLocalWidgetUpdate, XmNactivateCallback, 
                      XaFupdateInstanceDisplayShell, XaLpClientData) ;
      }

    if (XaLpClientData -> MaLselectionAction == MaSelectionActionEditInstance)
      {
        XaLocalWidgetUpdate = XtVaCreateManagedWidget("Delete",
                                   xmPushButtonGadgetClass, XaLform,
                                   XmNtopAttachment,     XmATTACH_FORM,
                                   XmNbottomAttachment,  XmATTACH_FORM,
                                   XmNleftAttachment,    XmATTACH_POSITION,
                                   XmNleftPosition,      13,
                                   XmNrightAttachment,   XmATTACH_POSITION,
                                   XmNrightPosition,     16,
                                   XmNshowAsDefault,     True,
                                   XmNdefaultButtonShadowThickness, 1,
                                   NULL) ;

        XtAddCallback(XaLocalWidgetUpdate, XmNactivateCallback, 
                      XaFdeleteConfirmInstanceDisplayShell, XaLpClientData) ;
      }

    if (XaLpClientData -> MaLselectionAction == MaSelectionActionAddInstance)
      {
        XaLocalWidgetAdd = XtVaCreateManagedWidget("Clear",
                                   xmPushButtonGadgetClass, XaLform,
                                   XmNtopAttachment,     XmATTACH_FORM,
                                   XmNbottomAttachment,  XmATTACH_FORM,
                                   XmNleftAttachment,    XmATTACH_POSITION,
                                   XmNleftPosition,      1,
                                   XmNrightAttachment,   XmATTACH_POSITION,
                                   XmNrightPosition,     4,
                                   XmNshowAsDefault,     True,
                                   XmNdefaultButtonShadowThickness, 1,
                                   NULL) ;

        XtAddCallback(XaLocalWidgetAdd, XmNactivateCallback, 
                      XaFclearInstanceDisplayShell, XaLpClientData) ;
      }

    if (XaLpClientData -> MaLselectionAction == MaSelectionActionAddInstance)
      {
        XaLocalWidgetAdd = XtVaCreateManagedWidget("Add",
                                   xmPushButtonGadgetClass, XaLform,
                                   XmNtopAttachment,     XmATTACH_FORM,
                                   XmNbottomAttachment,  XmATTACH_FORM,
                                   XmNleftAttachment,    XmATTACH_POSITION,
                                   XmNleftPosition,      5,
                                   XmNrightAttachment,   XmATTACH_POSITION,
                                   XmNrightPosition,     8,
                                   XmNshowAsDefault,     True,
                                   XmNdefaultButtonShadowThickness, 1,
                                   NULL) ;

        XtAddCallback(XaLocalWidgetAdd, XmNactivateCallback, 
                      XaFaddInstanceDisplayShell, XaLpClientData) ;
      }

    XaLocalWidgetCancel = XtVaCreateManagedWidget("Cancel",
                                   xmPushButtonGadgetClass, XaLform,
                                   XmNtopAttachment,     XmATTACH_FORM,
                                   XmNbottomAttachment,  XmATTACH_FORM,
                                   XmNleftAttachment,    XmATTACH_POSITION,
                                   XmNleftPosition,      17,
                                   XmNrightAttachment,   XmATTACH_POSITION,
                                   XmNrightPosition,     20,
                                   XmNshowAsDefault,     True,
                                   XmNdefaultButtonShadowThickness, 1,
                                   NULL) ;

    XtAddCallback(XaLocalWidgetCancel, XmNactivateCallback, 
                  XaFcancelInstanceDisplayShell, XaLpClientData) ;

    XtManageChild(XaLform) ;

    /******************************************************************/
    /*  End Create the Actions Bar previous, next, update, add and    */
    /*  cancel options for the Instance Shell                         */
    /******************************************************************/


    XtManageChild(XaLpane) ;
    XtPopup(XaLpClientData -> MaLshellWidgetId, XtGrabNone) ;
    XaInstanceShellExistStatus = MaTRUE ;

  } /* end   XaFinstanceDisplayShell function */


/******************************************************************/
/*                             XaFunmanageChild                   */
/*    This function is to Unmanage a Widget.                      */
/*                                                                */
/******************************************************************/

void XaFunmanageChild(XaLwidget, XaLchild)
  Widget    XaLwidget ;
  Widget    XaLchild  ;

  { /* begin XaFunmanageChild function */

    if (XaLchild != NULL)
      XtUnmanageChild(XaLchild) ;

  } /* end   XaFunmanageChild function */


/*****************************************************************/
/*                                                               */
/*                       END OF instance.c FILE                  */
/*                                                               */
/*****************************************************************/
