
/******************************************************************
 *                                                                *
 *                  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                            *
 *                                                                *
 ******************************************************************/


/******************************************************************/
/***********************     xatools.c    *************************/
/*    This file contains all the functions needed as tools to the */
/*  Graphical User Interface of the project. For example, clear   */
/*  a particular window, get contents of a particular window, fill*/
/*  a particular window with a string, and so on.                 */
/******************************************************************/

char *MaGattributeTypeList[ ] = { "int2", "int4", "int28", "float4",
                 "float8", "char", "char16", "text", "abstime", "oid"  } ;

#include "mainfile.h"

#include "helpdata.dat"  /* Needed for the Help Dialog Box */


/******************************************************************/
/*                                                                */
/*    This is to  initialize all help text logo picture for the   */
/*  project.                                                      */
/*                                                                */
/******************************************************************/

#ifndef HELPLOGO
#define HELPLOGO

#define XaHelpDialogBoxLogoWidth  40
#define XaHelpDialogBoxLogoHeight 32

static char XaHelpDialogBoxLogoBits[] = {
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xff, 0x01, 0x00, 0x00, 0x30,
   0x00, 0x06, 0x00, 0x00, 0x0c, 0x3c, 0x18, 0x00, 0x00, 0x02, 0x7e, 0x20,
   0x00, 0x00, 0x01, 0x7e, 0x40, 0x00, 0x80, 0x00, 0x7e, 0x80, 0x00, 0x80,
   0x00, 0x3c, 0x80, 0x00, 0x40, 0x00, 0x00, 0x00, 0x01, 0x40, 0x00, 0x18,
   0x00, 0x01, 0x20, 0x00, 0x3c, 0x00, 0x02, 0x20, 0x00, 0x7e, 0x00, 0x02,
   0x20, 0x00, 0x7e, 0x00, 0x02, 0x20, 0x00, 0x7e, 0x00, 0x02, 0x20, 0x00,
   0x7e, 0x00, 0x02, 0x20, 0x00, 0x7e, 0x00, 0x02, 0x20, 0x00, 0x7e, 0x00,
   0x02, 0x20, 0x00, 0x7e, 0x00, 0x02, 0x20, 0x00, 0x7e, 0x00, 0x02, 0x20,
   0x00, 0x7e, 0x00, 0x02, 0x20, 0x00, 0x7e, 0x00, 0x02, 0x40, 0x00, 0x7e,
   0x00, 0x01, 0x40, 0x00, 0x7e, 0x00, 0x01, 0x80, 0x00, 0x7e, 0x80, 0x00,
   0x80, 0x00, 0x7e, 0x80, 0x00, 0x00, 0x01, 0x7e, 0x40, 0x00, 0x00, 0x02,
   0x00, 0x20, 0x00, 0x00, 0x0c, 0x00, 0x18, 0x00, 0x00, 0x30, 0x00, 0x06,
   0x00, 0x00, 0xc0, 0xff, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00};


#endif


/******************************************************************/
/*                        XaFclearTextBufferArea                  */
/*    This function is to clear the buffer and the text area of a */
/*  specific scrolled text and its buffer.                        */
/*                                                                */
/******************************************************************/

void XaFclearTextBufferArea(XaLbuffer, XaLtext)
  char      *XaLbuffer ;
  Widget    XaLtext ;

  { /* begin XaFclearTextBufferArea function */

    strcpy(XaLbuffer,"\0");
    XmTextReplace(XaLtext,0,MaMaxBufSize,XaLbuffer);
    XmTextSetString(XaLtext, XaLbuffer);

  } /* end   XaFclearTextBufferArea function */


/******************************************************************/
/*                       XaFfillupTextBufferArea                  */
/*    This function is to fill the the text area and its buffer of*/
/*  a specific scrolled text with the conntents of XaLbuffer.     */
/*                                                                */
/******************************************************************/

void XaFfillupTextBufferArea(XaLbuffer, XaLtext)
  char      *XaLbuffer ;
  Widget    XaLtext ;

  { /* begin XaFfillupTextBufferArea function */
    int XaLlength = strlen(XaLbuffer) ;
/*
    int XaLi ;
    int XaLnumberLines = 0 ;
    for (XaLi = 0; XaLbuffer[XaLi] != '\0'; XaLi++)
      if ((XaLi < strlen(XaLbuffer) && (XaLi < MaMaxBufSize; XaLi++) &&
         (XaLbuffer[XaLi] == '\n'))
         XaLnumberLines++ ;
*/
    if (XaLlength >= MaMaxBufSize)
      XaLbuffer[XaLlength-2] = '\0' ;

    XmTextReplace(XaLtext,0,MaMaxBufSize,XaLbuffer);
    XmTextSetString(XaLtext, XaLbuffer);
/*
    XmTextScroll(XaLtext, XaLnumberLines - XaWorkAreaDisplayRows) ;
*/
  } /* end   XaFfillupTextBufferArea function */


/******************************************************************/
/*                         XaFgetTextBufferArea                   */
/*    This function is to get the contents of a text buffer.      */
/*                                                                */
/******************************************************************/

char *XaFgetTextBufferArea(XaLtextWidget)
  Widget    XaLtextWidget ;

  { /* begin XaFgetTextBufferArea function */
    char         *XaLtext ;

    if (XaLtext = XmTextGetString(XaLtextWidget)) 
      {
        if ( (strlen(XaLtext) +1) > MaMaxBufSize)
          printf("Warning Text size Exceeds Maximum Beffer Size\n") ;
        else
          return(XaLtext) ;
      }
    return("\0") ;

  } /* end   XaFgetTextBufferArea function */


/******************************************************************/
/*                             XaFsetDbName                       */
/*    This function is to set the Database Name to its new Value  */
/*  and take care of all the views and parameters that get affec- */
/*  ted by the change.                                            */
/*                                                                */
/******************************************************************/

void XaFsetDbName(XaLdbName)
  char    *XaLdbName ;

  { /* begin XaFsetDbName function */

    MaFcloseDb() ;
    strcpy(MaDbName, XaLdbName) ;
    XaFfillupTextBufferArea(MaDbName, XaWorkAreaHeaderInfoDbsName) ;
    MaFstartDb(MaDbName) ;
    strcpy(MaClassName, "\0") ;
    XaFfillupTextBufferArea(MaClassName, XaWorkAreaHeaderInfoClsName) ;

  } /* end   XaFsetDbName function */


/******************************************************************/
/*                           XaFsetClassName                      */
/*    This function is to set the RootClass Name to its new Value */
/*  and take care of all the views and parameters that get affec- */
/*  ted by the change.                                            */
/*                                                                */
/******************************************************************/

void XaFsetClassName(XaLclassName)
  char    *XaLclassName ;

  { /* begin XaFsetClassName function */

    strcpy(MaClassName, XaLclassName) ;
    XaFfillupTextBufferArea(MaClassName, XaWorkAreaHeaderInfoClsName) ;

  } /* end   XaFsetClassName function */


/******************************************************************/
/*                           XaFisEmptyString                     */
/*    This function is to check if the string is empty then retrn */
/*  true to the calling program and display a Notice popup to the */
/*  user informing him of the message passed, otherwise it return */
/*  false.                                                        */
/*                                                                */
/******************************************************************/

int XaFisEmptyString(XaLwidget,XaLstring,XaLmessage)
  Widget  XaLwidget ;
  char    *XaLstring  ;
  char    *XaLmessage ;

  { /* begin XaFisEmptyString function */

    if (XaLstring[0] == '\0')
      {
        Widget XaLdialog ;
        XmString XaLtext ;
        void XtDestroyWidget() ;

        XaLdialog = XmCreateMessageDialog(XtParent(XaLwidget), "XaLdialog", NULL, 0) ;
        XaLtext   = XmStringCreateSimple(XaLmessage) ;

        XtVaSetValues(XaLdialog,
                      XmNdialogType     , XmDIALOG_ERROR,
                      XmNmessageString  , XaLtext,
                      NULL) ;

        XmStringFree(XaLtext) ;
        XtUnmanageChild(XmMessageBoxGetChild(XaLdialog, XmDIALOG_CANCEL_BUTTON)) ;
        XtUnmanageChild(XmMessageBoxGetChild(XaLdialog, XmDIALOG_HELP_BUTTON)) ;
        XtAddCallback(XaLdialog, XmNokCallback, XtDestroyWidget, NULL) ;

        XtManageChild(XaLdialog) ;
        return(MaTRUE) ;
      }
    return(MaFALSE) ;

  } /* end   XaFisEmptyString function */


/******************************************************************/
/*                            XaFgetTopShell                      */
/*    This function is to climb widget tree until we get to the   */
/*  top widget of the tree.                                       */
/*                                                                */
/******************************************************************/

Widget XaFgetTopShell(XaLwidget)
  Widget    XaLwidget ;

  { /* begin XaFgetTopShell function */

    while (XaLwidget && !XtIsWMShell(XaLwidget))
       XaLwidget = XtParent(XaLwidget) ;
    return XaLwidget ;

  } /* end   XaFgetTopShell function */


/******************************************************************/
/*                        XaFupdateXYposition                     */
/*    This function is to read the current X & Y Position of the  */
/*  main window and help  dialog popup window and update the two  */
/*  points values.                                                */
/*                                                                */
/******************************************************************/

void XaFupdateXYposition()

  { /* begin XaFupdateXYposition function */
     Dimension XaPosX, XaPosY ;
     Widget XaLwidget ;

    /************************************************************/
    /* Here to request the x & y position of the screen to save */
    /* its values before destroying the window.                 */
    /************************************************************/

    XaLwidget = XaFgetTopShell(XaTopLevel) ;
    XtVaGetValues(XaLwidget,
                  XmNx,           &XaPosX,
                  XmNy,           &XaPosY,
                  NULL) ;
    XaMainWindowX = XaPosX - XaTopShellBorderWidth  ;
    XaMainWindowY = XaPosY - XaTopShellBorderHeight ;

    /************************************************************/
    /* Here to request the x & y position of the help popup wi- */
    /* ndow to save its values before destroying the window.    */
    /************************************************************/

    if (XaHelpDialogBoxWindow && (XaHelpWindowExistStatus == MaTRUE) )
      {
        Dimension h ;
        XtVaGetValues(XaHelpDialogBoxWindow, XmNx, &h, NULL) ;
        XaHelpDialogBoxX = h - XaTopShellBorderWidth ;
        XtVaGetValues(XaHelpDialogBoxWindow, XmNy, &h, NULL) ;
        XaHelpDialogBoxY = h - XaTopShellBorderHeight ;
      }

    /************************************************************/
    /* Here to request the x & y position of the instance shell */
    /* window to save its values before destroying the window.  */
    /************************************************************/

    if (XaInstanceShellBoxWindow && (XaInstanceShellExistStatus == MaTRUE) )
      {
        Dimension h ;
        XtVaGetValues(XaInstanceShellBoxWindow, XmNx, &h, NULL) ;
        XaInstanceShellBoxX = h - XaTopShellBorderWidth ;
        XtVaGetValues(XaInstanceShellBoxWindow, XmNy, &h, NULL) ;
        XaInstanceShellBoxY = h - XaTopShellBorderHeight ;
      }

  } /* end   XaFupdateXYposition function */


/******************************************************************/
/*                      XaFsetWorkingCursorFont                   */
/*    This function is to set the cursor font to working to indi- */
/*  cate to the user that the application is currently busy and   */
/*  he has to wait.                                               */
/*                                                                */
/******************************************************************/

void XaFsetWorkingCursorFont()

  { /* begin XaFsetWorkingCursorFont function */

    static Cursor XaLcursor ;
    Display *XaLdpy = XtDisplay(XaTopLevel) ;
    XSetWindowAttributes XaLattrs;
    Boolean XaLon = True ;

    if (!XaLcursor)
      XaLcursor = XCreateFontCursor(XaLdpy, XC_watch) ;
    XaLattrs.cursor = XaLon ? XaLcursor : None ;

    XChangeWindowAttributes(XaLdpy, XtWindow(XaTopLevel), CWCursor, &XaLattrs) ;

    XFlush(XaLdpy) ;

  } /* end    XaFsetWorkingCursorFont function */


/******************************************************************/
/*                      XaFsetRegularCursorFont                   */
/*    This function is to set the cursor to a  regular font  to   */
/*  indicate to the user that the application is ready for user   */
/*  requests.                                                     */
/*                                                                */
/******************************************************************/

void XaFsetRegularCursorFont()

  { /* begin XaFsetRegularCursorFont function */

    static Cursor XaLcursor ;
    Display *XaLdpy = XtDisplay(XaTopLevel) ;
    XSetWindowAttributes XaLattrs;
    Boolean XaLon = False ;

    if (!XaLcursor)
      XaLcursor = XCreateFontCursor(XaLdpy, XC_watch) ;
    XaLattrs.cursor = XaLon ? XaLcursor : None ;

    XChangeWindowAttributes(XaLdpy, XtWindow(XaTopLevel), CWCursor, &XaLattrs) ;

    XFlush(XaLdpy) ;

  } /* end    XaFsetRegularCursorFont function */


/******************************************************************/
/*                   XaFsetWidgetWorkingCursorFont                */
/*    This function is to set the cursor font to working to indi- */
/*  cate to the user that the application is currently busy and   */
/*  he has to wait in a specific Widget.                          */
/*                                                                */
/******************************************************************/

void XaFsetWidgetWorkingCursorFont(XaLwidget)
  Widget XaLwidget ;

  { /* begin XaFsetWidgetWorkingCursorFont function */
    Widget XaLshell = XaFgetTopShell(XaLwidget) ;
    static Cursor XaLcursor ;
    Display *XaLdpy = XtDisplay(XaLshell) ;
    XSetWindowAttributes XaLattrs;
    Boolean XaLon = True ;

    XaFsetWorkingCursorFont() ;

    if (!XaLcursor)
      XaLcursor = XCreateFontCursor(XaLdpy, XC_watch) ;
    XaLattrs.cursor = XaLon ? XaLcursor : None ;

    XChangeWindowAttributes(XaLdpy, XtWindow(XaLshell), CWCursor, &XaLattrs) ;

    XFlush(XaLdpy) ;

  } /* end    XaFsetWidgetWorkingCursorFont function */


/******************************************************************/
/*                   XaFsetWidgetRegularCursorFont                */
/*    This function is to set the cursor to a  regular font  to   */
/*  indicate to the user that the application is ready for user   */
/*  requests in a specific Widget.                                */
/*                                                                */
/******************************************************************/

void XaFsetWidgetRegularCursorFont(XaLwidget)
  Widget XaLwidget ;

  { /* begin XaFsetWidgetRegularCursorFont function */
    Widget XaLshell = XaFgetTopShell(XaLwidget) ;
    static Cursor XaLcursor ;
    Display *XaLdpy = XtDisplay(XaLshell) ;
    XSetWindowAttributes XaLattrs;
    Boolean XaLon = False ;

    XaFsetRegularCursorFont() ;
    if (!XaLcursor)
      XaLcursor = XCreateFontCursor(XaLdpy, XC_watch) ;
    XaLattrs.cursor = XaLon ? XaLcursor : None ;

    XChangeWindowAttributes(XaLdpy, XtWindow(XaLshell), CWCursor, &XaLattrs) ;

    XFlush(XaLdpy) ;

  } /* end    XaFsetWidgetRegularCursorFont function */


/******************************************************************/
/*                            XaFhelpCallback                     */
/*    This function  is to respond to a help callback  event.  It */
/*  takes the help number and display a help popup window message */
/*  accordingly.                                                  */
/*                                                                */
/******************************************************************/

void XaFhelpCallback (XaLwidget, XaLitemNo)
  Widget    XaLwidget ;
  int       XaLitemNo ;

  { /* begin XaFhelpCallback function */

#include "helpdata.dat"  /* Needed for the Help Dialog Box */

    Widget    XaLhelpDialog, XaLpane, XaLtextWindow, XaLform, XaLocalWidget, XaLlabel ;
    extern    void    XaFdestroyHelpShell() ;
    Pixmap    XaLpixmap ;
    Pixel     fg, bg ;
    Arg       XaLargs[9] ;
    int       i ;
    char      *p, XaLbuf[BUFSIZ] ;
    char      *XaLhelpDialogBoxLabel ;

    /********************************************************************/
    /* This is to check if there exist a help popup window already then */
    /* Destroy it before creating a new Help popup window. This will    */
    /* ensure that no more than one help window exist at one time.      */
    /********************************************************************/

    if (XaHelpDialogBoxWindow && (XaHelpWindowExistStatus == MaTRUE) )
      {
        XaFupdateXYposition() ;
      }

    XaLhelpDialogBoxLabel = XaGhelpTextListTitle[XaLitemNo] ;

    if (XaHelpDialogBoxX < 0 || XaHelpDialogBoxX > 700)
      XaHelpDialogBoxX = 0 ;
    if (XaHelpDialogBoxY < 0 || XaHelpDialogBoxY > 700)
      XaHelpDialogBoxY = 0 ;

    XaLhelpDialog = XtVaCreatePopupShell(XaLhelpDialogBoxLabel,
                                   xmDialogShellWidgetClass, XaFgetTopShell(XaLwidget),
                                   XmNdeleteResponse,        XmDESTROY,
                                   XmNx,                     XaHelpDialogBoxX,
                                   XmNy,                     XaHelpDialogBoxY,
                                   NULL) ;

    XaHelpDialogBoxWindow   = XaLhelpDialog ;
    XaHelpWindowExistStatus = MaTRUE ;

    XaLpane = XtVaCreateWidget("XaLpane", xmPanedWindowWidgetClass, XaLhelpDialog,
#ifdef notdef
                               XmNsashWidth,  1,
                               XmNsashHeight, 1,
#endif
                               NULL) ;

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

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

    XaLpixmap = XCreatePixmapFromBitmapData(XtDisplay(XaLform),
                                       RootWindowOfScreen(XtScreen(XaLform)),
                                       XaHelpDialogBoxLogoBits,
                                       XaHelpDialogBoxLogoWidth,
                                       XaHelpDialogBoxLogoHeight,
                                       fg, bg, DefaultDepthOfScreen(XtScreen(XaLform)));

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

    for (p = XaLbuf, i = 0; XaGhelpTextListName[XaLitemNo][i]; i++) {
      p+= strlen(strcpy(p, XaGhelpTextListName[XaLitemNo][i])) ;
      if (!isspace(p[-1]))
         *p++ = ' ' ;

    }

    *--p = 0 ;

    XtSetArg(XaLargs[0],XmNscrollVertical,        True                     ) ;
    XtSetArg(XaLargs[1],XmNscrollHorizontal,      False                    ) ;
    XtSetArg(XaLargs[2],XmNeditMode,              XmMULTI_LINE_EDIT        ) ;
    XtSetArg(XaLargs[3],XmNeditable,              False                    ) ;
    XtSetArg(XaLargs[4],XmNcursorPositionVisible, False                    ) ;
    XtSetArg(XaLargs[5],XmNwordWrap,              True                     ) ;
    XtSetArg(XaLargs[6],XmNvalue,                 XaLbuf                   ) ;
    XtSetArg(XaLargs[7],XmNrows,                  XaHelpDialogBoxTextRows  ) ;
    XtSetArg(XaLargs[8],XmNwidth,                 XaHelpDialogBoxTextWidth ) ;

    XaLtextWindow = XmCreateScrolledText(XaLform, "help_text", XaLargs, 9) ;

    XtVaSetValues(XtParent(XaLtextWindow),
                  XmNleftAttachment,    XmATTACH_WIDGET,
                  XmNleftWidget,        XaLlabel,
                  XmNtopAttachment,     XmATTACH_FORM,
                  XmNrightAttachment,   XmATTACH_FORM,
                  XmNbottomAttachment,  XmATTACH_FORM,
                  NULL) ;

    XtManageChild(XaLtextWindow) ;

    XtManageChild(XaLform) ;

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

    XaLocalWidget = XtVaCreateManagedWidget("ok",
                                   xmPushButtonGadgetClass, XaLform,
                                   XmNtopAttachment,     XmATTACH_FORM,
                                   XmNbottomAttachment,  XmATTACH_FORM,
                                   XmNleftAttachment,    XmATTACH_POSITION,
                                   XmNleftPosition,      2,
                                   XmNrightAttachment,   XmATTACH_POSITION,
                                   XmNrightPosition,     4,
                                   XmNshowAsDefault,     True,
                                   XmNdefaultButtonShadowThickness, 1,
                                   NULL) ;

    XtAddCallback(XaLocalWidget, XmNactivateCallback, 
                  XaFdestroyHelpShell, XaLhelpDialog) ;

    XtManageChild(XaLform) ;

    XtManageChild(XaLpane) ;
    XtPopup(XaLhelpDialog, XtGrabNone) ;
    XaHelpWindowExistStatus = MaTRUE ;

  } /* end   XaFhelpCallback function */


/******************************************************************/
/*                            XaFdestroyHelpShell                 */
/*    This function is to destroy a specific shell.               */
/*                                                                */
/******************************************************************/

void XaFdestroyHelpShell(XaLwidget, XaLshell)
  Widget XaLwidget, XaLshell ;

  { /* begin XaFdestroyHelpShell function */

    /************************************************************/
    /* This Block in case you need the dialog box to save its   */
    /* location on the screen.                                  */
    /************************************************************/
    
    if (XaHelpDialogBoxWindow && (XaHelpWindowExistStatus == MaTRUE) )
      XaFupdateXYposition() ;
    
    XaHelpWindowExistStatus = MaFALSE ;

    XtDestroyWidget(XaLshell) ;

  } /* end   XaFdestroyHelpShell function */


/******************************************************************/
/*                            XaFdestroyShell                     */
/*    This function is to destroy a specific shell.               */
/*                                                                */
/******************************************************************/

void XaFdestroyShell(XaLwidget, XaLshell)
  Widget XaLwidget, XaLshell ;

  { /* begin XaFdestroyShell function */

    /************************************************************/
    /* This Block in case you need the dialog box to save its   */
    /* location on the screen.                                  */
    /************************************************************/
    /*
    if (XaHelpDialogBoxWindow)
      XaFupdateXYposition() ;
    */

    XtDestroyWidget(XaLshell) ;

  } /* end   XaFdestroyShell function */


/******************************************************************/
/*               XaFhandleConfirmUserRequestYesDialog             */
/*    This function is to handle the response to the user to a yes*/
/*  of a confirm user request dialog.                             */
/*                                                                */
/******************************************************************/

Widget XaFhandleConfirmUserRequestYesDialog(XaLdialog, XaLpClientData)
  Widget XaLdialog ;
  MaClientDataStruct XaLpClientData ;
  
  { /* begin XaFhandleConfirmUserRequestYesDialog function */
    MaClientDataStruct extern MaFfreeClientData() ;
    Widget extern XaFpostDialog() ;

    if (XaLpClientData != NULL)
      switch (XaLpClientData -> MaLselectionMode)
        {
          case MaSelectionActionEditInstanceDelete :

                     XaFsetWidgetWorkingCursorFont(XaLpClientData->MaLshellWidgetId) ;

                     XaLpClientData -> MaLselectionMode = -1 ;
                     XaFdeleteInstanceDisplayShell(XaLdialog, XaLpClientData) ;

                     XaFsetWidgetRegularCursorFont(XaLpClientData -> MaLshellWidgetId) ;
                     break ;

          case MaSelectionActionDestroyRootClass :
                     XaLpClientData -> MaLselectionMode = -1 ;
                     {
                       char * XaLret ;
                       MaStringType XaLquery ;

                       XaFsetWorkingCursorFont() ;

                       sprintf(XaLquery,"destroy %s", XaLpClientData -> MaLclassName) ;
                       PQexec("begin") ;
                       XaLret = PQexec(XaLquery) ;
                       PQexec("end") ;

                       XaFsetRegularCursorFont() ;

                       if (strcmp(XaLret,"CEND") == 0)
                         {
                           sprintf(XaLpClientData -> MaLdialogMessage,
                                  "Class %s has been Destroyed Successfully.",
                                  XaLpClientData -> MaLclassName) ;
                           XaLpClientData -> MaLdialogType = XaDialogNotice ;
                           XaFsetClassName("") ;
                         }
                       else
                         {
                           sprintf(XaLpClientData -> MaLdialogMessage,
                                  "Class %s Could Not be Destroyed.",
                                  XaLpClientData -> MaLclassName) ;
                           XaLpClientData -> MaLdialogType = XmDIALOG_ERROR ;
                         }
                       (void) XaFpostDialog(XtParent(XtParent(XaLdialog)),
                                            XaLpClientData) ;
                     }

                     break ;

          default : 
                    break ;
        } /* end switch block */

  } /* end   XaFhandleConfirmUserRequestYesDialog function */


/******************************************************************/
/*               XaFhandleConfirmUserRequestNoDialog              */
/*    This function is to handle the response to the user to a yes*/
/*  of a confirm user request dialog.                             */
/*                                                                */
/******************************************************************/

Widget XaFhandleConfirmUserRequestNoDialog(XaLdialog, XaLpClientData)
  Widget XaLdialog ;
  MaClientDataStruct XaLpClientData ;
  
  { /* begin XaFhandleConfirmUserRequestNoDialog function */
    MaClientDataStruct extern MaFfreeClientData() ;

    if (XaLpClientData != NULL)
      switch (XaLpClientData -> MaLselectionMode)
        {
          case MaSelectionActionEditInstanceDelete :
                     XaLpClientData -> MaLselectionMode = -1 ;
                     break ;

          case MaSelectionActionDestroyRootClass :
                     XaLpClientData -> MaLselectionMode = -1 ;
                     XaLpClientData = MaFfreeClientData(XaLpClientData) ;
                     break ;

          default : 
                    break ;
        } /* end switch block */

  } /* end   XaFhandleConfirmUserRequestNoDialog function */


/******************************************************************/
/*                    XaFconfirmUserRequestDialog                 */
/*    This function is to Popup Window to get confirmation from   */
/*  the user to do specific task. If the user answer yes, then it */
/*  will go ahead and performs the task, othertwise it cancels    */
/*  the task.                                                     */
/*                                                                */
/******************************************************************/

Widget XaFconfirmUserRequestDialog(XaLparent, XaLpClientData)
  Widget XaLparent ;
  MaClientDataStruct XaLpClientData ;
  
  { /* begin XaFconfirmUserRequestDialog function */
    static Widget  XaLdialog ;
    XmString XaLtext         ;
    XmString XaLokString     ;
    XmString XaLcancelString ;

    if (!XaLdialog)
      XaLdialog     = XmCreateWarningDialog(XtParent(XaLparent),
                                            "warning", NULL, 0) ;

    XtUnmanageChild(XmMessageBoxGetChild(XaLdialog, XmDIALOG_HELP_BUTTON)) ;

    XtAddCallback(XaLdialog, XmNokCallback,     
                  XaFhandleConfirmUserRequestYesDialog, XaLpClientData) ;
    XtAddCallback(XaLdialog, XmNcancelCallback,
                  XaFhandleConfirmUserRequestNoDialog, XaLpClientData);

    XaLtext         = XmStringCreateLtoR(XaLpClientData -> MaLdialogMessage,
                                         XmFONTLIST_DEFAULT_TAG) ;

    XaLokString     = XmStringCreateLocalized("Yes") ;

    XaLcancelString = XmStringCreateLocalized("No") ;

    XtVaSetValues(XaLdialog,
                   XmNdefaultButtonType , XmDIALOG_CANCEL_BUTTON,
                   XmNmessageString     , XaLtext,
                   XmNokLabelString     , XaLokString,
                   XmNcancelLabelString , XaLcancelString,
                   NULL) ;

    XmStringFree(XaLtext) ;
    XmStringFree(XaLokString) ;
    XmStringFree(XaLcancelString) ;

    XtManageChild(XaLdialog) ;
    XtPopup(XtParent(XaLdialog), XtGrabNone) ;

  } /* end   XaFconfirmUserRequestDialog function */


/******************************************************************/
/*                            XaFpostDialog                       */
/*    This function is to create a selection from the user that   */
/*  he can select from a list.  The list will be displayed in a   */
/*  scrolled list  selection box where  the user can select his   */
/*  choice by clicking on any item in the list or enetering his   */
/*  his choice in an input field. Upon Option Value, a Variable   */
/*  is supposed to be set to the selected value.                  */
/*                                                                */
/******************************************************************/

Widget XaFpostDialog(XaLparent, XaLpClientData)
  Widget XaLparent ;
  MaClientDataStruct XaLpClientData ;
  
  { /* begin XaFpostDialog function */
    Widget XaLdialog ;
    XmString XaLtext ;
    void XtDestroyWidget() ;
    MaClientDataStruct extern MaFfreeClientData() ;

    XaLdialog = XmCreateMessageDialog(XaLparent, "XaLdialog", NULL, 0) ;
    XaLtext   = XmStringCreateSimple(XaLpClientData -> MaLdialogMessage) ;

    XtVaSetValues(XaLdialog,
                   XmNdialogType     , XaLpClientData -> MaLdialogType,
                   XmNmessageString  , XaLtext,
                   NULL) ;

    XmStringFree(XaLtext) ;
    XtUnmanageChild(XmMessageBoxGetChild(XaLdialog, XmDIALOG_CANCEL_BUTTON)) ;
    XtUnmanageChild(XmMessageBoxGetChild(XaLdialog, XmDIALOG_HELP_BUTTON)) ;
    XtAddCallback(XaLdialog, XmNokCallback, XtDestroyWidget, NULL) ;

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

    XtManageChild(XaLdialog) ;
    return XaLdialog ;

  } /* end   XaFpostDialog function */


/******************************************************************/
/*                      XaFsetSelectionListOption                 */
/*    This function is to respond to the user selection that is   */
/*  selected in a selection dialog box, this function takes care  */
/*  only of selection that match one member of the list.          */
/*                                                                */
/******************************************************************/

void XaFsetSelectionListOption(XaLwidget, XaLpClientData, XaLcbs)
  Widget             XaLwidget      ;
  MaClientDataStruct XaLpClientData ;
  XmSelectionBoxCallbackStruct *XaLcbs ;

  { /* begin XaFsetSelectionListOption function */
    char XaLmessage[256], *XaLprompt, *XaLvalue, *XaLret ;
    MaStringType XaLquery ;
    int XaLdialogType ;
    extern MaStringListStruct MaFgetStringListExePqCommand() ;

    XmStringGetLtoR(XaLcbs->value, XmSTRING_DEFAULT_CHARSET, &XaLvalue) ;

    if (XaLpClientData -> MaLselectionMode == MaSelectionMustNotMatch)
      {
        sprintf(XaLpClientData -> MaLdialogMessage,
                "Selection %s Already Exist. Selection Must not be in the list",
                XaLvalue) ;
        XaLpClientData -> MaLdialogType = XmDIALOG_ERROR ;
        (void) XaFpostDialog(XtParent(XtParent(XaLwidget)), XaLpClientData) ;
      }
    else
      {
        switch (XaLcbs->reason) {
          case XmCR_OK       : break ;
          case XmCR_NO_MATCH : break ;
          default            :  sprintf(XaLpClientData -> MaLdialogMessage,
                                       "Unknown Selection : %s.",
                                       XaLvalue) ;
                               XaLpClientData -> MaLdialogType = XmDIALOG_ERROR ;
                (void) XaFpostDialog(XtParent(XtParent(XaLwidget)), XaLpClientData) ;
          } /* end switch case */

        if (XaLcbs->reason == XmCR_OK)
          {
            switch (XaLpClientData -> MaLselectionAction) {
              case MaSelectionActionSetDataBase   :
                         XaFsetDbName(XaLvalue) ;
                         XaLpClientData = MaFfreeClientData(XaLpClientData) ;
                         break ;
              case MaSelectionActionSetClass      :
                         XaFsetClassName(XaLvalue) ;
                         XaLpClientData = MaFfreeClientData(XaLpClientData) ;
                         break ;

              case MaSelectionActionSetLogCommand :
                         strcpy(XaCommandBuffer, XaLvalue) ;
                         XaFfillupTextBufferArea(XaCommandBuffer,
                                                 XaWorkAreaCommandText) ;
                         XaLpClientData = MaFfreeClientData(XaLpClientData) ;
                         break ;

              case MaSelectionActionAddAttributeType :

                         XaFsetWorkingCursorFont() ;

                         strcpy(XaLpClientData -> MaLattributeType, XaLvalue) ;
                         if (XaLpClientData -> MaLCreateNewClassMode)
                           sprintf(XaLquery,"create %s (%s = %s) ",
                                          XaLpClientData -> MaLclassName,
                                          XaLpClientData -> MaLattributeName,
                                          XaLpClientData -> MaLattributeType) ;
                         else
                           sprintf(XaLquery,"addattr (%s = %s) to %s",
                                          XaLpClientData -> MaLattributeName,
                                          XaLpClientData -> MaLattributeType,
                                          XaLpClientData -> MaLclassName) ;

                         PQexec ("begin");
                         XaLret = PQexec (XaLquery);
                         if (*XaLret == 'R')
                           {
                             sprintf(XaLpClientData -> MaLdialogMessage,
                                     "Attribute %s Could not be added to Class %s",
                                      XaLpClientData -> MaLattributeName,
                                      XaLpClientData -> MaLclassName) ;
                             XaLpClientData -> MaLdialogType = XmDIALOG_ERROR ;
                             PQexec("end");

                             XaFsetRegularCursorFont() ;

                             (void) XaFpostDialog(XtParent(XtParent(XaLwidget)),
                                                  XaLpClientData) ;
                             XtFree(XaLvalue) ;
                             return ;
                           }

                         PQexec("end");

                         /*************************************************************/
                         /* Fix the Tree so children will Inherit the Added Attribute */
                         /*************************************************************/
                         {
                           MaStringListStruct XaLpClassTreeList ;
                           MaStringListStruct XaLtempClassTreeList ;
                           MaClientDataStruct XaLtempClientData ;

                           extern MaStringListStruct MaFfreeStringList  () ;
                           extern MaStringListStruct XaFexportClassTree () ;
                           extern MaClientDataStruct MaFgetPtrClientData() ;

                           XaLtempClientData = MaFgetPtrClientData() ;

                           strcpy(XaLtempClientData -> MaLclassName, 
                                  XaLpClientData    -> MaLclassName) ;

                           sprintf(XaLtempClientData -> MaLexportFilename,"%s%s%s",
                                   MaProjectPath, 
                                   XaLtempClientData -> MaLclassName,
                                   MaClassTreeFileTempExt) ;


                           /***********************************************************/
                           /*  Open Class Tree file to write Classes definition and   */
                           /*  instances to it                                        */
                           /***********************************************************/
                           if ((XaLtempClientData -> MaLstreamExportFile =
                                fopen(XaLtempClientData -> MaLexportFilename, "w"))
                               == NULL)
                             {
                               sprintf(XaLtempClientData -> MaLdialogMessage, 
                                      "Unable to Open Class Data %s",
                                      XaLtempClientData -> MaLexportFilename) ;

                               XaLtempClientData -> MaLdialogType = XmDIALOG_ERROR ;

                               XaFsetRegularCursorFont() ;

                               XaFpostDialog(XtParent(XaLwidget) ,XaLtempClientData) ;
                             }
                           else
                             {
                               XaLpClassTreeList =  XaFexportClassTree
                                                        (XaLwidget, XaLtempClientData) ;

                               PQexec("begin") ;
                               MaFdestoyClassTree(XaLpClassTreeList) ;
                               PQexec("end") ;

                               XaLpClassTreeList = MaFfreeStringList(XaLpClassTreeList);

                               fclose(XaLtempClientData -> MaLstreamExportFile) ;

                               (void) (XaFfixReturnCharacterInPostquelFile
                                                       (XaLwidget, XaLtempClientData)) ;

                               /*******************************************************/
                               /*  Open Class Tree file to read Classes definition &  */
                               /*  instances from it                                  */
                               /*******************************************************/
                               if ((XaLtempClientData -> MaLstreamExportFile =
                                     fopen(XaLtempClientData -> MaLexportFilename, "r"))
                                    == NULL)
                                 {
                                   sprintf(XaLtempClientData -> MaLdialogMessage, 
                                          "Unable to Open Class To Read Data From %s",
                                          XaLtempClientData -> MaLexportFilename) ;

                                   XaLtempClientData -> MaLdialogType = XmDIALOG_ERROR ;

                                   XaFsetRegularCursorFont() ;

                                   XaFpostDialog(XtParent(XaLwidget),
                                                 XaLtempClientData) ;
                                 }
                               else
                                 {
                                   XaFimportPostQuelFile(XaLwidget,XaLtempClientData) ;
                                   sprintf(XaLtempClientData -> MaLdialogMessage, 
                                          "Attribute %s Has been added Successfully to Class %s.",
                                          XaLpClientData -> MaLattributeName,
                                          XaLpClientData -> MaLclassName) ;

                                   sprintf(XaLquery, "rm %s",
                                           XaLtempClientData -> MaLexportFilename);
                                   system(XaLquery) ;

                                   XaLtempClientData -> MaLdialogType = XaDialogNotice ;

                                   XaFsetRegularCursorFont() ;

                                   XaFpostDialog(XtParent(XaLwidget),
                                                 XaLtempClientData) ;
                                 }
                             }
                         }

                         /*************************************************************/
                         /*  End Fix the Tree so children will Inherit the Added      */
                         /*  Attribute Block.                                         */
                         /*************************************************************/

                         XaFsetRegularCursorFont() ;

                         break ;


              case MaSelectionActionChangeParentClass :

                         XaFsetWorkingCursorFont() ;
                         
                         {
                           MaStringListStruct XaLpClassTreeList ;

                           extern MaStringListStruct MaFfreeStringList  () ;
                           extern MaStringListStruct XaFexportClassTree () ;

                           strcpy(XaLpClientData -> MaLnewParentName, XaLvalue) ;

                           /******************************************************/
                           /*  Check if there is Conflict between the new Parent */
                           /*  class and the class tree.                         */
                           /******************************************************/
                           if (MaFcheckConflictParentChildType
                                            (XaLwidget, XaLpClientData) == MaFALSE)
                             break ;

                           /***********************************************************/
                           /*  Export Class Tree with inheritance of New Parent Class */
                           /***********************************************************/
                           XaLpClientData -> MaLexportInheritMode = MaTRUE ;
                           XaLpClassTreeList = XaFexportClassTree
                                                            (XaLwidget,XaLpClientData) ;
                           
                           fclose(XaLpClientData -> MaLstreamExportFile) ;
                           (void) XaFfixReturnCharacterInPostquelFile
                                                           (XaLwidget, XaLpClientData) ;

                           PQexec("begin") ;
                           MaFdestoyClassTree(XaLpClassTreeList) ;
                           PQexec("end") ;

                           XaLpClassTreeList = MaFfreeStringList(XaLpClassTreeList) ;

                           /********************************************************/
                           /*  Open Change Parents Class Tree file to read Classes */
                           /*  definition and instances from it.                   */
                           /********************************************************/
                               if ((XaLpClientData -> MaLstreamExportFile =
                                   fopen(XaLpClientData -> MaLexportFilename, "r"))
                                   == NULL)
                                 {
                                   sprintf(XaLpClientData -> MaLdialogMessage, 
                                          "Unable to Open Class To Read Data From %s",
                                          XaLpClientData -> MaLexportFilename) ;

                                   XaLpClientData -> MaLdialogType = XmDIALOG_ERROR ;

                                   XaFsetRegularCursorFont() ;

                                   XaFpostDialog(XtParent(XaLwidget) ,XaLpClientData) ;
                                 }
                               else
                                 {
                                   XaFimportPostQuelFile(XaLwidget,XaLpClientData) ;
                                   sprintf(XaLpClientData -> MaLdialogMessage, 
                                    "Parent for Class %s Has been changed to Class %s.",
                                           XaLpClientData -> MaLclassName,
                                           XaLvalue) ;
/*
                                   sprintf(XaLquery, "rm %s",
                                           XaLpClientData -> MaLexportFilename);
                                   system(XaLquery) ;
*/
                                   XaLpClientData -> MaLdialogType = XaDialogNotice ;

                                   XaFsetRegularCursorFont() ;

                                   XaFpostDialog(XtParent(XaLwidget), XaLpClientData) ;
                                 }

                         }
                         
                         break ;

              case MaSelectionActionMosaicSelectSubject :

                         {
                           extern MaStringListStruct MaFgetStringListExePqCommand() ;
                           extern MaClientDataStruct MaFgetPtrClientData() ;
                           extern void XaFhandleSelectionListOption() ;

                           XaFsetWorkingCursorFont() ;
                         
                           sprintf(XaLquery,
                           "retrieve (e.title) from e in Mosaic where e.subject=\"%s\"",
                           XaLvalue) ;

                           XaLpClientData -> MaLstringList =  
                                               MaFgetStringListExePqCommand(XaLquery) ;

                           strcpy(XaLpClientData -> MaLlistLabel,"Mosaic Title List:") ;

                           XaLpClientData -> MaLselectionAction = 
                                                 MaSelectionActionMosaicSelectDocument ;
                           XaLpClientData -> MaLselectionMode   = MaSelectionMustMatch ;

printf("Issue Query: %s\n", XaLquery) ;

                           XaFsetRegularCursorFont () ;

                           XaFhandleSelectionListOption(XaLwidget, XaLpClientData) ;
                         }

                         break ;

              case MaSelectionActionMosaicSelectDocument :

                         {
                           XaFsetWorkingCursorFont() ;
                         
                           sprintf(XaLquery,
                           "retrieve (e.URL) from e in Mosaic where e.title=\"%s\"",
                           XaLvalue) ;

                           XaLpClientData -> MaLstringList =  
                                               MaFgetStringListExePqCommand(XaLquery) ;
/*
printf("Mosaic Selection is %s\n", XaLvalue) ;
printf("Mosaic URL is %s\n", XaLpClientData -> MaLstringList -> MaLstringData) ;
*/
                           sprintf(XaLquery, "%s %s &", MaMosaicApplicationPath, 
                                   XaLpClientData -> MaLstringList -> MaLstringData) ;
                           system(XaLquery) ;

                           strcpy(XaLpClientData -> MaLdialogMessage, 
                                  "Mosaic Selected Document Has Been Launched.") ;
                           XaLpClientData -> MaLdialogType = XaDialogNotice ;

                           XaFsetRegularCursorFont () ;

                           XaFpostDialog(XtParent(XaLwidget), XaLpClientData) ;
                         }

                         break ;

              default : printf("%d XaLpClientData in XaFsetSelectionListOption",
                               XaLpClientData -> MaLselectionAction) ;
                        printf("function is not available at this time\n") ;
                        XaLpClientData = MaFfreeClientData(XaLpClientData) ;
            } /* end switch case */
          } /* end if statement */
      } /* end else statement */

    XtFree(XaLvalue) ;

  } /* end   XaFsetSelectionListOption function */


/******************************************************************/
/*                   XaFnoMatchSelectionListOption                */
/*    This function is to respond to the user clicking on the     */
/*  ok button in the selection box dialog and the selection does  */
/*  not match with any member of the list.                        */
/*                                                                */
/******************************************************************/

void XaFnoMatchSelectionListOption(XaLwidget, XaLpClientData, XaLcbs)
  Widget             XaLwidget      ;
  MaClientDataStruct XaLpClientData ;
  XmSelectionBoxCallbackStruct *XaLcbs ;

  { /* begin XaFnoMatchSelectionListOption function */
    char *XaLvalue, *XaLret ;
    MaStringType XaLquery ;
    MaClientDataStruct extern MaFfreeClientData() ;
    void XaFhandleSelectionListOption() ;
    extern MaStringListStruct MaFfreeStringList () ;
    extern MaStringListStruct MaFpointerStringToListAdd() ;
    extern MaStringListStruct MaFgetStringListExePqCommand() ;

    XmStringGetLtoR(XaLcbs->value, XmSTRING_DEFAULT_CHARSET, &XaLvalue) ;

    if (XaLpClientData -> MaLselectionMode == MaSelectionMustMatch)
      {
        sprintf(XaLpClientData -> MaLdialogMessage,
                "Not a valid Selection %s.",
                XaLvalue) ;
        XaLpClientData -> MaLdialogType = XmDIALOG_ERROR ;
        (void) XaFpostDialog(XtParent(XtParent(XaLwidget)), XaLpClientData) ;
      }
    else
      {
        switch (XaLcbs->reason) {
          case XmCR_OK       : break ;
          case XmCR_NO_MATCH : break ;
          default            :  sprintf(XaLpClientData -> MaLdialogMessage,
                                       "Unknown Selection : %s.",
                                       XaLvalue) ;
                               XaLpClientData -> MaLdialogType = XmDIALOG_ERROR ;
                (void) XaFpostDialog(XtParent(XtParent(XaLwidget)), XaLpClientData) ;
          } /* end switch case */

        if (XaLcbs->reason == XmCR_NO_MATCH)
          {
            switch (XaLpClientData -> MaLselectionAction) {
              case MaSelectionActionSetDataBase   :
                         XaFsetDbName(XaLvalue) ;
                         XaLpClientData = MaFfreeClientData(XaLpClientData) ;
                         break ;
              case MaSelectionActionSetClass      :
                         XaFsetClassName(XaLvalue) ;
                         XaLpClientData = MaFfreeClientData(XaLpClientData) ;
                         break ;

              case MaSelectionActionCreateDataBase :

                         XaFsetWorkingCursorFont() ;

                         printf("!!! Warning Only System Administrator is") ;
                         printf(" allowed to Create a New Database !!!\n") ;
                         PQexec("begin") ;
                         sprintf(XaLquery,"createdb %s",XaLvalue) ;
                         PQexec(XaLquery) ;
                         PQexec("end") ;
                         sprintf(XaLpClientData -> MaLdialogMessage,
                                 "New Database %s Has been created Successfully.",
                                 XaLvalue) ;
                         XaLpClientData -> MaLdialogType = XaDialogNotice ;

                         XaFsetRegularCursorFont() ;

                         (void) XaFpostDialog(XtParent(XtParent(XaLwidget)),
                                              XaLpClientData) ;
                         break ;

              case MaSelectionActionSetLogCommand :
                         strcpy(XaCommandBuffer, XaLvalue) ;
                         XaFfillupTextBufferArea(XaCommandBuffer,
                                                 XaWorkAreaCommandText) ;
                         XaLpClientData = MaFfreeClientData(XaLpClientData) ;
                         break ;

              case MaSelectionActionCreateNewClass :
                         {
                           extern MaClientDataStruct MaFgetPtrClientData() ;
                           MaClientDataStruct XaLtempClientData ;

                           XaFsetWorkingCursorFont() ;

                           XaLtempClientData = MaFgetPtrClientData() ;
                           XaLpClientData -> MaLstringList =
                                    MaFfreeStringList(XaLpClientData -> MaLstringList) ;
                           XaLtempClientData -> MaLstringList = 
                             MaFpointerStringToListAdd
                                           ("No Attribute in Current Class Yet",
                                            XaLpClientData -> MaLstringList) ;

                           strcpy(XaLtempClientData -> MaLclassName, XaLvalue ) ;
                           sprintf(XaLtempClientData -> MaLlistLabel, 
                                   "Enter Attribute Name For Class %s",
                                   XaLtempClientData -> MaLclassName) ;

                           XaLtempClientData -> MaLselectionAction =
                                                 MaSelectionActionAddAttributeName ;
                           XaLtempClientData -> MaLCreateNewClassMode = MaTRUE ;
                           XaLtempClientData -> MaLselectionMode   =
                                                 MaSelectionMustNotMatch ;

                           XaFsetRegularCursorFont() ;

                           XaFhandleSelectionListOption(XtParent(XaLwidget),
                                                        XaLtempClientData) ;
                           XaLpClientData = MaFfreeClientData(XaLpClientData) ;
                         }
                         break ;

              case MaSelectionActionAddNewChild :

                         XaFsetWorkingCursorFont() ;

                         sprintf(XaLquery,"create %s ( ) inherits (%s)",
                                          XaLvalue ,XaLpClientData -> MaLclassName) ;

                         PQexec ("begin");
                         XaLret = PQexec (XaLquery);
                         if (*XaLret == 'R')
                           {
                             sprintf(XaLpClientData -> MaLdialogMessage,
                                     "Child Class %s Could not be added to Class %s",
                                      XaLvalue, XaLpClientData -> MaLclassName) ;
                             XaLpClientData -> MaLdialogType = XmDIALOG_ERROR ;
                           }
                         else
                           {
                             sprintf(XaLpClientData -> MaLdialogMessage,
                                     "Child %s of Class %s Has added Successfully.",
                                      XaLvalue, XaLpClientData -> MaLclassName) ;
                             XaLpClientData -> MaLdialogType = XaDialogNotice ;
                           }
                         PQexec("end");

                         XaFsetRegularCursorFont() ;

                         (void) XaFpostDialog(XtParent(XtParent(XaLwidget)),
                                              XaLpClientData) ;
                         break ;

              case MaSelectionActionAddAttributeName :
                       {
                         int XaLindex ;

                         XaFsetWorkingCursorFont() ;

                         XaLpClientData -> MaLstringList =
                                    MaFfreeStringList(XaLpClientData -> MaLstringList) ;
                         strcpy(XaLpClientData -> MaLattributeName, XaLvalue) ;
/*
                         sprintf(XaLquery,"retrieve (pg_type.typname) \
                                           where pg_type.typname !~ \"^_.*\" \
                                           and pg_type.typname !~ \"pg_.*\"") ;
                         XaLpClientData -> MaLstringList =
                                                MaFgetStringListExePqCommand(XaLquery) ;
*/
                         for (XaLindex = 0; XaLindex < XtNumber(MaGattributeTypeList);
                              XaLindex++)
                           {
                             XaLpClientData -> MaLstringList = 
                                                  MaFpointerStringToListAdd(
                                                      MaGattributeTypeList[XaLindex],
                                                      XaLpClientData -> MaLstringList) ;
                           }

                         sprintf(XaLpClientData -> MaLlistLabel, 
                                 "Enter Type for Attribute %s For Class %s",
                                 XaLpClientData -> MaLattributeName,
                                 XaLpClientData -> MaLclassName) ;

                         XaLpClientData -> MaLselectionAction =
                                                     MaSelectionActionAddAttributeType ;
                         XaLpClientData -> MaLselectionMode   = MaSelectionMustMatch ;

                         XaFsetRegularCursorFont() ;

                         XaFhandleSelectionListOption
                                                 (XtParent(XaLwidget), XaLpClientData) ;
                       }
                         break ;

              default : printf("%d XaLpClientData in XaFsetSelectionListOption",
                               XaLpClientData -> MaLselectionAction) ;
                        printf("function is not available at this time\n") ;
                        XaLpClientData = MaFfreeClientData(XaLpClientData) ;
            } /* end switch case */
          } /* end if statement */
      } /* end else statement */

    XaFsetRegularCursorFont() ;

    XtFree(XaLvalue) ;

  } /* end   XaFnoMatchSelectionListOption function */


/******************************************************************/
/*                    XaFcancelSelectionListOption                */
/*    This function is to respond to the user clicking on the     */
/*  cancel button in the selection box dialog.                    */
/*                                                                */
/******************************************************************/

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

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

    XaLpClientData = MaFfreeClientData(XaLpClientData) ;

    XtDestroyWidget(XaLwidget) ;

  } /* end   XaFcancelSelectionListOption function */


/******************************************************************/
/*                      XaFhandleSelectionListOption              */
/*    This function is to create a selection from the user that   */
/*  he can select from a list.  The list will be displayed in a   */
/*  scrolled list  selection box where  the user can select his   */
/*  choice by clicking on any item in the list or enetering his   */
/*  his choice in an input field. Upon Option Value, a Variable   */
/*  is supposed to be set to the selected value.                  */
/*                                                                */
/******************************************************************/

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

  { /* begin XaFhandleSelectionListOption function */
   MaClientDataStruct extern MaFfreeClientData() ;
   if (XaLpClientData -> MaLstringList == NULL)
     {
       MaStringType XaLmessage ;
       sprintf(XaLmessage,"List %s Is Empty",XaLpClientData -> MaLlistLabel) ;
       if (XaFisEmptyString(XaLwidget,"",XaLmessage)) ;

       XaLpClientData = MaFfreeClientData (XaLpClientData) ;
       return ;
     }
   else
    {
    int         XaLi      ;
    int         XaLsize = MaFgetSizeStringList(XaLpClientData -> MaLstringList) ;
    Widget      XaLdialog ;
    XmString    XaLt, *XaLstr ;
    MaStringListStruct XaLtempPointer = XaLpClientData -> MaLstringList ;
    void               extern XaFsetSelectionListOption() ;
    MaStringListStruct extern MaFfreeStringList() ;
    
    XaLstr = (XmString *) XtMalloc(XaLsize * sizeof(XmString)) ;
    
    /************************************************************/
    /*    Initializing the label & the contents of the selction */
    /*  dialog box.                                             */
    /************************************************************/

    XaLt = XmStringCreateSimple(XaLpClientData -> MaLlistLabel) ;

    for (XaLi=0; XaLi<XaLsize; XaLi++)
      {
        if (XaLtempPointer == NULL)
          printf("Warning Null XaLtempPointer in XaFhandleSelectionListOption\n") ;
        else
          {
            XaLstr[XaLi] = XmStringCreateSimple(XaLtempPointer->MaLstringData) ;
            XaLtempPointer = XaLtempPointer->MaLpNextString ;
          }
      }

    XaLdialog = XmCreateSelectionDialog(XtParent(XaLwidget), "selection", NULL, 0) ;

     XtVaSetValues(XaLdialog,
                   XmNlistLabelString, XaLt,
                   XmNlistItems      , XaLstr,
                   XmNlistItemCount  , XaLsize,
                   XmNmustMatch      , True,
                   NULL) ;

     XtUnmanageChild(XmSelectionBoxGetChild(XaLdialog, XmDIALOG_HELP_BUTTON)) ;
     XtUnmanageChild(XmSelectionBoxGetChild(XaLdialog, XmDIALOG_APPLY_BUTTON)) ;

     XtAddCallback(XaLdialog, XmNokCallback     , XaFsetSelectionListOption,
                              XaLpClientData,NULL) ;
     XtAddCallback(XaLdialog, XmNnoMatchCallback, XaFnoMatchSelectionListOption,
                              XaLpClientData,NULL) ;
     XtAddCallback(XaLdialog, XmNcancelCallback, XaFcancelSelectionListOption,
                              XaLpClientData,NULL) ;

     XmStringFree(XaLt) ;

     while (--XaLi >= 0)
       XmStringFree(XaLstr[XaLi]) ;
     XtFree(XaLstr) ;

     XtManageChild(XaLdialog) ;
     } /* end else block */

  } /* end   XaFhandleSelectionListOption function */


/*****************************************************************/
/*                    XaFsetClassAttributeNameType               */
/*    Function to set the Client Data Pointers of attribute name */
/*  and type of a class.   It returns False  if there is not any */
/*  attribute for the specified Class Name.                      */
/*                                                               */
/*****************************************************************/

int XaFsetClassAttributeNameType(XaLwidget, XaLpClientData)
  Widget  XaLwidget ;
  MaClientDataStruct XaLpClientData ;

  { /* begin XaFsetClassAttributeNameType function */

    MaStringType       XaLqueryClassType  ;
    MaStringListStruct XaLqueryStringList ;
    MaClientDataStruct extern MaFfreeClientData () ;
    MaStringListStruct extern MaFgetStringListExePqCommand() ;
    MaStringListStruct extern MaFpointerNodeToListAdd() ;

    sprintf(XaLqueryClassType,"retrieve (attribute=a.attname, types=t.typname)\
            from a in pg_attribute,\
            t in pg_type, r in pg_class \
            where a.attrelid = r.oid  and a.atttypid = t.oid  and  (a.attnum > 0 \
            \/* or a.attnum = -3 *\/) and  r.relname = \"%s\"",
            XaLpClientData -> MaLclassName );

    XaLqueryStringList = MaFgetStringListExePqCommand(XaLqueryClassType) ;
    if (XaLqueryStringList == NULL)
      {
        sprintf(XaLqueryClassType,"Root Class %s Does Not Have Any Attribute",
                XaLpClientData -> MaLclassName) ;

        XaLpClientData = MaFfreeClientData(XaLpClientData) ;

        if (XaFisEmptyString(XaLwidget,"",XaLqueryClassType)) ;
        return(MaFALSE) ;
      }

    while (XaLqueryStringList != NULL)
      {
        MaStringListStruct XaLtempListPointer = XaLqueryStringList ;
        XaLqueryStringList = XaLqueryStringList -> MaLpNextString ;
        XaLtempListPointer -> MaLpNextString = NULL ;
        strcpy(XaLtempListPointer -> MaLlabelName, 
               MaFgetWordNumberInString(XaLtempListPointer -> MaLstringData, 1)) ;
        strcpy(XaLtempListPointer -> MaLlabelType, 
               MaFgetWordNumberInString(XaLtempListPointer -> MaLstringData, 2)) ;
        strcpy(XaLtempListPointer -> MaLstringData, "") ;

        if (strcmp(MaPqTextType, XaLtempListPointer -> MaLlabelType) == 0)
          { 
            XaLpClientData -> MaLtextStringList = 
                    MaFpointerNodeToListAdd(XaLtempListPointer,
                                            XaLpClientData -> MaLtextStringList) ;
          }
        else
          {  
            XaLpClientData -> MaLstringList = 
                    MaFpointerNodeToListAdd(XaLtempListPointer,
                                            XaLpClientData -> MaLstringList) ;
          }
      }

    return(MaTRUE) ;

  } /* end   XaFsetClassAttributeNameType function */


/*****************************************************************/
/*                   XaFsetClassObjectIdsInstances               */
/*    Function to set the Client Data Pointers of Object Ids in- */
/*  stances values of the specified Class Name. It returns False */
/*  if the specified class does not have any instance.           */
/*                                                               */
/*****************************************************************/

int XaFsetClassObjectIdsInstances(XaLwidget, XaLpClientData)
  Widget  XaLwidget ;
  MaClientDataStruct XaLpClientData ;

  { /* begin XaFsetClassObjectIdsInstances function */

    MaStringType       XaLqueryClassOids     ;
    MaStringType       XaLqueryTempClassOids ;
    MaStringListStruct XaLqueryOidsList      ;
    MaStringListStruct XaLqueryTempOidsList  ;
    MaStringListStruct extern MaFfreeStringList () ;
    MaStringListStruct extern MaFgetStringListExePqCommand() ;

    sprintf(XaLqueryClassOids,"retrieve (e.oid) \
            from e in %s ",XaLpClientData -> MaLclassName);

    XaLpClientData -> MaLobjectIdsList =
                 MaFfreeStringList(XaLpClientData -> MaLobjectIdsList) ;

    XaLqueryOidsList = MaFgetStringListExePqCommand(XaLqueryClassOids) ;
    if (XaLqueryOidsList == NULL)
      {
        sprintf(XaLqueryClassOids,"Root Class %s Does Not Have Any Instance",
                XaLpClientData -> MaLclassName) ;

        if (XaLpClientData -> MaLfreeNodeOk == MaTRUE)
          {
            XaLpClientData = MaFfreeClientData(XaLpClientData) ;
            if (XaFisEmptyString(XaLwidget,"",XaLqueryClassOids)) ;
          }
        return(MaFALSE) ;
      }


    XaLpClientData -> MaLobjectIdsList = XaLqueryOidsList ;

    /*****************************************************/
    /*  This is to sort the oids in an Increasing Order. */
    /*****************************************************/
    while (XaLqueryOidsList != NULL)
      {
        XaLqueryTempOidsList = XaLqueryOidsList -> MaLpNextString ;
        while (XaLqueryTempOidsList != NULL)
          {
            if (stoi(XaLqueryTempOidsList -> MaLstringData) <
                stoi(XaLqueryOidsList     -> MaLstringData))
              {
                strcpy(XaLqueryTempClassOids, XaLqueryTempOidsList -> MaLstringData) ;
                strcpy(XaLqueryTempOidsList -> MaLstringData,
                       XaLqueryOidsList     -> MaLstringData) ;
                strcpy(XaLqueryOidsList     -> MaLstringData, XaLqueryTempClassOids) ;
              }
            XaLqueryTempOidsList = XaLqueryTempOidsList -> MaLpNextString ;
          }
        XaLqueryOidsList = XaLqueryOidsList -> MaLpNextString ;
      }

    XaLqueryOidsList = XaLpClientData -> MaLobjectIdsList ;

    /********************************************************************/
    /*  This is to initialize the previous, current and next oid values */
    /********************************************************************/
    if (XaLpClientData -> MaLcurrentOid == 0)
      {
        XaLpClientData -> MaLpreviousOid = -1 ;
        XaLpClientData -> MaLnextOid     = MaMaxOidNumber ;
        XaLpClientData -> MaLcurrentOid = 
                   stoi(XaLpClientData -> MaLobjectIdsList -> MaLstringData) ;
        if (XaLpClientData -> MaLobjectIdsList -> MaLpNextString != NULL)
          XaLpClientData -> MaLnextOid = 
          stoi(XaLpClientData -> MaLobjectIdsList -> MaLpNextString -> MaLstringData) ;
      }
    else
      {
        int XaLfound = MaFALSE ;
          XaLpClientData -> MaLpreviousOid = -1 ;
          XaLpClientData -> MaLnextOid     = MaMaxOidNumber ;
        while (XaLqueryOidsList != NULL && XaLfound == MaFALSE)
          {
            if (XaLpClientData -> MaLcurrentOid <
                                    stoi(XaLqueryOidsList -> MaLstringData))
              {
                XaLpClientData -> MaLcurrentOid = 
                                    stoi(XaLqueryOidsList -> MaLstringData) ;

                if (XaLqueryOidsList -> MaLpNextString != NULL)
                  XaLpClientData -> MaLnextOid =
                  stoi(XaLpClientData->MaLobjectIdsList->MaLpNextString->MaLstringData);
                XaLfound = MaTRUE ;
              }
            else
              {
                XaLpClientData -> MaLpreviousOid =
                                   stoi(XaLqueryOidsList -> MaLstringData) ;

                XaLqueryOidsList = XaLqueryOidsList -> MaLpNextString ;
              }
          }
        if (XaLfound == MaFALSE)
          {
            XaLpClientData -> MaLpreviousOid = -1 ;
            XaLpClientData -> MaLnextOid     = MaMaxOidNumber ;
            XaLpClientData -> MaLcurrentOid = 
                       stoi(XaLpClientData -> MaLobjectIdsList -> MaLstringData) ;
            if (XaLpClientData -> MaLobjectIdsList -> MaLpNextString != NULL)
              XaLpClientData -> MaLnextOid = 
            stoi(XaLpClientData -> MaLobjectIdsList -> MaLpNextString -> MaLstringData);

          }
      }

    return(MaTRUE) ;

  } /* end   XaFsetClassObjectIdsInstances function */


/******************************************************************/
/*                     XaFdisplayFillAttributeFields              */
/*    This function is to fill the values of the fields in the    */
/*  instance view case.                                           */
/*                                                                */
/******************************************************************/

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

  { /* begin XaFdisplayFillAttributeFields function */
    MaStringListStruct XaLpFieldList ;

    XaLpFieldList = XaLpClientData -> MaLstringList ;

    while (XaLpFieldList != NULL)
      {
        XaFfillupTextBufferArea(XaLpFieldList ->MaLstringData,
                                XaLpFieldList -> MaLchildWidgetId) ;
        XaLpFieldList = XaLpFieldList -> MaLpNextString ;
      }
    XaLpFieldList = XaLpClientData -> MaLtextStringList ;

    while (XaLpFieldList != NULL)
      {
        XaFfillupTextBufferArea(XaLpFieldList ->MaLstringData,
                                XaLpFieldList -> MaLchildWidgetId) ;
        XaLpFieldList = XaLpFieldList -> MaLpNextString ;
      }

  } /* end   XaFdisplayFillAttributeFields function */


/*****************************************************************/
/*                                                               */
/*                        END OF xatools.c FILE                  */
/*                                                               */
/*****************************************************************/