/* *************************************************************************
 *  Copyright (C) 1990-1993 by ILOG.
 *  All Rights Reserved.
 *
 *  N O T I C E
 *
 *  THIS MATERIAL IS CONSIDERED A TRADE SECRET BY ILOG.
 *  UNAUTHORIZED ACCESS, USE, REPRODUCTION OR DISTRIBUTION IS PROHIBITED.
 * *************************************************************************/

/*
 *  File :        IlogViews.c
 *  Description : This file implements the IlogViews widgets.
 */

/*
 * Includes required
 */
#include <IlogViewsP.h>
#include <IlogViews.h>

#define WARN_RESIZE_POLICY "Incorrect resize policy."


#if defined(VMS)
#include <descrip.h>
#define NOSHARE noshare
#else /*VMS*/
#define NOSHARE 
#endif /*VMS*/

#ifdef _NO_PROTO
#define _ILPROTO(list) ()
#else
#define _ILPROTO(list) list
#endif

static XtResource NOSHARE
managerResources[] = {
    { XtNilvManager, XtCilvManager, XtRPointer, sizeof(XtPointer),
	  XtOffset(XmIlvManagerWidget, ilvmanager.ilvmanager),
	  XmRImmediate, NULL},
    { XtNilvManagerId, XtCilvManagerId, XtRString, sizeof(String),
	  XtOffset(XmIlvManagerWidget, ilvmanager.ilvmanagerid),
	  XmRImmediate, NULL},
     { XtNilvSelectInt, XtCilvSelectInt, XtRBoolean, sizeof(Boolean),
	  XtOffset(XmIlvManagerWidget, ilvmanager.ilvselectint),
	  XmRImmediate, NULL},
    { XtNilvFileName, XtCilvFileName, XtRString, sizeof(String),
	  XtOffset(XmIlvManagerWidget, ilvmanager.ilvfilename),
	  XmRImmediate, NULL},
    { XtNilvView, XtCilvView, XtRPointer, sizeof(XtPointer),
	  XtOffset(XmIlvManagerWidget, ilvmanager.ilvview),
	  XmRImmediate, NULL},
    { XtNilvHorizontalScrollBar, XtCilvHorizontalScrollBar, XmRWindow,
	  sizeof(Widget), XtOffset(XmIlvManagerWidget, ilvmanager.ilvhsb),
	  XmRImmediate, NULL },
    { XtNilvVerticalScrollBar, XtCilvVerticalScrollBar, XmRWindow,
	  sizeof(Widget), XtOffset(XmIlvManagerWidget, ilvmanager.ilvvsb),
	  XmRImmediate, NULL }
};

static XtResource NOSHARE
cellManagerResources[] = {
    { XtNilvCellManager, XtCilvCellManager, XtRPointer, sizeof(XtPointer),
	  XtOffset(XmIlvCellManagerWidget, ilvcellmanager.ilvcellmanager),
	  XmRImmediate, NULL},
    { XtNilvManagerId, XtCilvManagerId, XtRString, sizeof(String),
	  XtOffset(XmIlvCellManagerWidget, ilvcellmanager.ilvmanagerid),
	  XmRImmediate, NULL},
    { XtNilvView, XtCilvView, XtRPointer, sizeof(XtPointer),
	  XtOffset(XmIlvCellManagerWidget, ilvcellmanager.ilvview),
	  XmRImmediate, NULL},
    { XtNilvNbRow, XtCilvNbRow, XtRInt, sizeof(int),
	  XtOffset(XmIlvCellManagerWidget, ilvcellmanager.ilvnbrow),
	  XmRImmediate, (caddr_t)10 },
    { XtNilvNbCol, XtCilvNbCol, XtRInt, sizeof(int),
	  XtOffset(XmIlvCellManagerWidget, ilvcellmanager.ilvnbcol),
	  XmRImmediate, (caddr_t)10 }
};

/*
 * External functions
 */
extern XtPointer __GetIlvDisplay _ILPROTO ((Widget));
extern XtPointer __NewIlvManager _ILPROTO ((XtPointer));
extern XtPointer __NewIlvCellManager _ILPROTO ((XtPointer, int, int));
extern XtPointer __NewIlvView _ILPROTO ((Widget));
extern XtPointer __NewIlvCellView _ILPROTO ((Widget, XtPointer));
extern void IlvManagerRedraw _ILPROTO ((XtPointer, XtPointer, Region));
extern void IlvCellManagerRedraw _ILPROTO ((XtPointer, XtPointer, Region));
extern void IlvManagerSetDoubleBuffering _ILPROTO ((XtPointer, XtPointer, Boolean));
extern void IlvManagerViewDestroy _ILPROTO ((XtPointer));
extern void IlvManagerViewInitialize _ILPROTO ((XtPointer, XtPointer));
extern void IlvCellManagerViewDestroy _ILPROTO ((XtPointer));
extern void IlvCellManagerViewInitialize _ILPROTO ((XtPointer, XtPointer));
extern void IlvManagerSetScrollHook _ILPROTO ((XtPointer, Widget, Widget));
extern XtPointer IlvManagerCreateScrollHook _ILPROTO ((XtPointer, XtPointer, Widget, Widget));
extern void IlvManagerLoadFile _ILPROTO ((XtPointer, XtPointer));
extern void IlvConnectToManager _ILPROTO ((XtPointer, XtPointer));
extern void IlvDisconnectFromManager _ILPROTO ((XtPointer, XtPointer));
extern void IlvConnectToCellManager _ILPROTO ((XtPointer, XtPointer));
extern void IlvDisconnectFromCellManager _ILPROTO ((XtPointer, XtPointer));

/*
 * Internal Macros
 */
#define _mgrContext xmIlvManagerClassRec.ilogviews_class._managersContext
#define _cellMgrContext xmIlvCellManagerClassRec.ilogviews_class._managersContext

/*
 * Forwarded private functions
 */
static void _MgrInitialize   _ILPROTO ((XmIlvManagerWidget request,
					XmIlvManagerWidget new));
static void _MgrDestroy   _ILPROTO ((XmIlvManagerWidget widget));
static void _MgrResize    _ILPROTO ((XmIlvManagerWidget widget));
static void _MgrRedisplay _ILPROTO ((XmIlvManagerWidget viewsmgr,
				     XEvent * event, Region region));
static Boolean _MgrSetValues _ILPROTO ((XmIlvManagerWidget current,
					XmIlvManagerWidget request,
					XmIlvManagerWidget new));
static void _MgrRealize _ILPROTO ((Widget, Mask*, XSetWindowAttributes*));

static void _CellMgrInitialize   _ILPROTO ((XmIlvCellManagerWidget request,
					    XmIlvCellManagerWidget new));
static void _CellMgrDestroy   _ILPROTO ((XmIlvCellManagerWidget widget));
static void _CellMgrResize    _ILPROTO ((XmIlvCellManagerWidget widget));
static void _CellMgrRedisplay _ILPROTO ((XmIlvCellManagerWidget viewsmgr,
					 XEvent * event, Region region));
static Boolean _CellMgrSetValues _ILPROTO ((XmIlvCellManagerWidget current,
					    XmIlvCellManagerWidget request,
					    XmIlvCellManagerWidget new));
static void _CellMgrRealize _ILPROTO ((Widget, Mask*, XSetWindowAttributes*));


static XtPointer _XmIlvManagerGetManager _ILPROTO ((XmIlvManagerWidget));
static XtPointer _XmIlvCellManagerGetManager _ILPROTO ((XmIlvCellManagerWidget));
static void _XmIlvManagerSetManager _ILPROTO((XmIlvManagerWidget,
					      XtPointer, String));


/****************************************************************
 *
 * Full class record constant
 *
 ****************************************************************/
externaldef(xmIlogViewsclassrec) XmIlogViewsClassRec NOSHARE 
xmIlvManagerClassRec = {
    {   /* core_class fields      */
	(WidgetClass) &xmDrawingAreaClassRec,	/* superclass        */
	"XmIlvManager",				/* class_name         */
	sizeof(XmIlvManagerRec),		/* widget_size       */
	NULL,					/* class_initialize   */
	NULL,					/* class_part_init    */
	FALSE,					/* class_inited       */
	(XtInitProc)_MgrInitialize,		/* initialize         */
	NULL,					/* initialize_hook    */
	_MgrRealize,			        /* realize            */
	(XtActionList)NULL,			/* actions	      */
	0,					/* num_actions	      */
	managerResources,			/* resources          */
	XtNumber(managerResources),		/* num_resources      */
	NULLQUARK,				/* xrm_class          */
	TRUE,					/* compress_motion    */
	FALSE,					/* compress_exposure  */
	TRUE,					/* compress_enterlv   */
	FALSE,					/* visible_interest   */
	(XtWidgetProc)_MgrDestroy,		/* destroy            */
#if defined(_RESIZE)
	(XtWidgetProc)_Resize,                 /* resize             */
#else  /* !_RESIZE */
	(XtWidgetProc)XtInheritResize,		/* resize             */
#endif /* !_RESIZE */
	(XtExposeProc)_MgrRedisplay,		/* expose            */
	(XtSetValuesFunc)_MgrSetValues,		/* set_values         */
	NULL,					/* set_values_hook    */
	(XtAlmostProc)_XtInherit,		/* set_values_almost  */
	NULL,					/* get_values_hook    */
	NULL,					/* accept_focus       */
	XtVersion,				/* version            */
	NULL,					/* callback_private   */
	NULL,					/* tm_table           */
	(XtGeometryHandler) NULL,		/* query_geometry     */
	NULL,             	                /* display_accelerator   */
	NULL,		                        /* extension             */
    },
    {  /* composite_class fields */
	(XtGeometryHandler)XtInheritGeometryManager,   /* geometry_manager  */
	(XtWidgetProc)XtInheritChangeManaged,	       /* change_managed    */
	(XtWidgetProc)_XtInherit,		       /* insert_child      */
	(XtWidgetProc)_XtInherit,		       /* delete_child      */
	NULL,                                          /* extension         */
    },
    {		/* constraint_class fields */
	NULL,	/* resource list        */   
	0,	/* num resources        */   
	0,	/* constraint size      */   
	NULL,	/* init proc            */   
	NULL,	/* destroy proc         */   
	NULL,	/* set values proc      */   
	NULL,	/* extension            */
    },
    {		/* manager_class fields */
	NULL,			/* translations           */
	NULL,			/* syn_resources      	  */
	0,			/* num_get_resources 	  */
	NULL,			/* syn_cont_resources     */
	0,			/* num_get_cont_resources */
	XmInheritParentProcess,	/* parent_process         */
	NULL,			/* extension           */    
    },
    {		/* drawingArea class - none */     
	0						/* mumble */
     }	,
     {          /* ilogViews class */
	 NULL /* _managersContext */
     }
};

externaldef(xmIlogViewsclassrec) WidgetClass
xmIlvManagerWidgetClass = (WidgetClass)&xmIlvManagerClassRec;

externaldef(xmIlogViewsclassrec) XmIlogViewsClassRec NOSHARE 
xmIlvCellManagerClassRec = {
    {   /* core_class fields      */
	(WidgetClass) &xmDrawingAreaClassRec,	/* superclass        */
	"XmIlvCellManager",			/* class_name         */
	sizeof(XmIlvManagerRec),		/* widget_size       */
	NULL,					/* class_initialize   */
	NULL,					/* class_part_init    */
	FALSE,					/* class_inited       */
	(XtInitProc) _CellMgrInitialize,	/* initialize         */
	NULL,					/* initialize_hook    */
	_CellMgrRealize,			/* realize            */
	(XtActionList)NULL,			/* actions	      */
	0,					/* num_actions	      */
	cellManagerResources,			/* resources          */
	XtNumber(cellManagerResources),		/* num_resources      */
	NULLQUARK,				/* xrm_class          */
	TRUE,					/* compress_motion    */
	FALSE,					/* compress_exposure  */
	TRUE,					/* compress_enterlv   */
	FALSE,					/* visible_interest   */
	(XtWidgetProc)_CellMgrDestroy,		/* destroy            */
#if defined (_RESIZE)
	(XtWidgetProc)_CellMgrResize,          /* resize             */
#else
	(XtWidgetProc)XtInheritResize,		/* resize             */
#endif
	(XtExposeProc)_CellMgrRedisplay,	/* expose            */
	(XtSetValuesFunc)_CellMgrSetValues,	/* set_values         */
	NULL,					/* set_values_hook    */
	(XtAlmostProc)_XtInherit,		/* set_values_almost  */
	NULL,					/* get_values_hook    */
	NULL,					/* accept_focus       */
	XtVersion,				/* version            */
	NULL,					/* callback_private   */
	NULL,					/* tm_table           */
	(XtGeometryHandler) NULL,		/* query_geometry     */
	NULL,             	                /* display_accelerator   */
	NULL,		                        /* extension             */
    },
    {  /* composite_class fields */
	(XtGeometryHandler)XtInheritGeometryManager,	/* geometry_manager  */
	(XtWidgetProc)XtInheritChangeManaged,		/* change_managed    */
	(XtWidgetProc)_XtInherit,			/* insert_child      */
	(XtWidgetProc)_XtInherit,			/* delete_child      */
	NULL,                                     	/* extension         */
    },
    {		/* constraint_class fields */
	NULL,	/* resource list        */   
	0,	/* num resources        */   
	0,	/* constraint size      */   
	NULL,	/* init proc            */   
	NULL,	/* destroy proc         */   
	NULL,	/* set values proc      */   
	NULL,	/* extension            */
    },
    {		/* manager_class fields */
	NULL,			/* translations           */
	NULL,			/* syn_resources      	  */
	0,			/* num_get_resources 	  */
	NULL,			/* syn_cont_resources     */
	0,			/* num_get_cont_resources */
	XmInheritParentProcess,	/* parent_process         */
	NULL,			/* extension           */    
    },
    {		/* drawingArea class - none */     
	0						/* mumble */
     }	,
     {          /* ilogViews class */
	 NULL /* _managersContext */
     }
};
externaldef(xmIlogViewsclassrec) WidgetClass
xmIlvCellManagerWidgetClass = (WidgetClass) &xmIlvCellManagerClassRec;


/* --------------------------------------------------------------------------
 * Ensure that manager - managerId association is coherent
 * -------------------------------------------------------------------------- */
static void
__CheckManagerCoherency
#ifdef _NO_PROTO
		       (w)
XmIlvManagerWidget w;
#else
		       (XmIlvManagerWidget w)
#endif /* _NO_PROTO */
{
    XID id;
    XtPointer manager;
    int result;
    if (!_IlvManagerId(w))
	_IlvManagerId(w) = XtName(w);
    /* Check manager - id correspondance */
    id = (XID)XrmStringToQuark(_IlvManagerId(w));
    result = XFindContext(XtDisplay(w), id, _mgrContext, &manager);
    if (result) { /* No manager was found with this ID */
	_IlvDisplay(w) = XmGetIlvDisplay((Widget)w);
	if (!_IlvManager(w))
	    _IlvManager(w) = __NewIlvManager(w);
	XSaveContext(XtDisplay(w), id,
		     _mgrContext, _IlvManager(w));
    } else { /* Manager was found with this ID */
	if (_IlvManager(w) && (manager != _IlvManager(w))) {
	    _IlvDisplay(w) = XmGetIlvDisplay((Widget)w);
	    _IlvManager(w) = __NewIlvManager(w);
	    XSaveContext(XtDisplay(w), id,
			 _mgrContext, _IlvManager(w));
	} else {
	    _IlvDisplay(w) = XmGetIlvDisplay((Widget)w);
	    _IlvManager(w) = manager;
	}
    }
}

/* --------------------------------------------------------------------------
 * Ensure that the width and height are not 0.
 * -------------------------------------------------------------------------- */
static void _Initialize
#ifdef _NO_PROTO
		       (request, new)
XmIlvManagerWidget request;
XmIlvManagerWidget new;
#else
		       (XmIlvManagerWidget request,
			XmIlvManagerWidget new)
#endif /* _NO_PROTO */
{
    /*	Make sure height and width are not zero.
     */
    if (new->core.width == 0)
	new->core.width = new->drawing_area.margin_width << 1;
    if (new->core.width == 0) new->core.width = 10;
    if (new->core.height == 0)
	new->core.height = new->drawing_area.margin_height << 1;
    if (new->core.height == 0) new->core.height = 10;
    /*	Validate resize policy.
     */
    if ((request->drawing_area.resize_policy != XmRESIZE_SWINDOW) &&
	(request->drawing_area.resize_policy != XmRESIZE_NONE)    &&
	(request->drawing_area.resize_policy != XmRESIZE_ANY)     &&
	(request->drawing_area.resize_policy != XmRESIZE_GROW)) {
	_XmWarning((Widget)new, WARN_RESIZE_POLICY);		
	new->drawing_area.resize_policy = XmRESIZE_ANY;
    }
}

static void _MgrInitialize
#ifdef _NO_PROTO
			  (request, new)
XmIlvManagerWidget request;
XmIlvManagerWidget new;
#else
			  (XmIlvManagerWidget request,
			   XmIlvManagerWidget new)
#endif /* _NO_PROTO */
{
    _Initialize(request, new);
    /*
     * Initialize the hash table containing all the managers created
     * by the XmIlvManagerWidgetClass 
     */
    if (!_mgrContext) _mgrContext = XUniqueContext();
    _IlvScrollHook(new) = NULL;
    __CheckManagerCoherency(new);
}

static void _CellMgrInitialize
#ifdef _NO_PROTO
			  (request, new)
XmIlvCellManagerWidget request;
XmIlvCellManagerWidget new;
#else
			  (XmIlvCellManagerWidget request,
			   XmIlvCellManagerWidget new)
#endif /* _NO_PROTO */
{
    _Initialize((XmIlvManagerWidget)request, (XmIlvManagerWidget)new);
    /*
     * Initialize the hash table containing all the managers created
     * by the XmIlvManagerWidgetClass 
     */
    if (!_cellMgrContext) _cellMgrContext = XUniqueContext();
    if (_IlvNbRow(new) <= 0) _IlvNbRow(new) = 10;
    if (_IlvNbCol(new) <= 0) _IlvNbCol(new) = 10;
    _XmIlvCellManagerGetManager(new);
}

/* --------------------------------------------------------------------------
 *
 * -------------------------------------------------------------------------- */
static void _MgrDestroy
#if defined(_NO_PROTO)
			(widget)
XmIlvManagerWidget widget;
#else
			(XmIlvManagerWidget widget)
#endif
{
    /* Delete the scrollhook if any */
    if (_IlvScrollHook(widget))
	IlvManagerDeleteScrollHook(_IlvScrollHook(widget));
}
static void _CellMgrDestroy
#if defined(_NO_PROTO)
			(widget)
XmIlvCellManagerWidget widget;
#else
			(XmIlvCellManagerWidget widget)
#endif
{
}

/* --------------------------------------------------------------------------
 * General redisplay function called on exposure events.
 * -------------------------------------------------------------------------- */
static void
_MgrRedisplay
#if defined(_NO_PROTO)
          (viewsmgr, event, region)
XmIlvManagerWidget viewsmgr;
XEvent*           event;
Region            region;
#else
	  (XmIlvManagerWidget viewsmgr,
	   XEvent*           event,
	   Region            region)
#endif
{
    if (!(event->xexpose.count) && _IlvManager(viewsmgr))
	IlvManagerRedraw(_IlvManager(viewsmgr), _IlvView(viewsmgr), region);
}
static void
_CellMgrRedisplay
#if defined(_NO_PROTO)
                 (viewsmgr, event, region)
XmIlvCellManagerWidget viewsmgr;
XEvent*           event;
Region            region;
#else
	  (XmIlvCellManagerWidget viewsmgr,
	   XEvent*                event,
	   Region                 region)
#endif
{
    if (!(event->xexpose.count) && _IlvCellManager(viewsmgr))
	IlvCellManagerRedraw(_IlvCellManager(viewsmgr),
			     _IlvCellView(viewsmgr), region);
}

/* --------------------------------------------------------------------------
 * Invoke the application resize callbacks.
 * -------------------------------------------------------------------------- */
static void _Resize
#if defined(_NO_PROTO)
			(viewsmgr)
XmIlvManagerWidget viewsmgr;
#else
			(XmIlvManagerWidget viewsmgr)
#endif
{
}

/* --------------------------------------------------------------------------
 * Function called whenever XtSetValues is called
 * Lets control the resource's values
 * -------------------------------------------------------------------------- */
static Boolean
_MgrSetValues
#if defined(_NO_PROTO)
	  (current, request, new)
XmIlvManagerWidget current;
XmIlvManagerWidget request;
XmIlvManagerWidget new;
#else
	  (XmIlvManagerWidget current,
	   XmIlvManagerWidget request,
	   XmIlvManagerWidget new)
#endif
{
    XtPointer oldmgr = _IlvManager(current);
    XtPointer newmgr = _IlvManager(request);
    int filenameEnabled = 1;
    /*
     *    Check if manager changed:
     */
    if (newmgr != oldmgr) {
	_IlvDisplay(new) = _IlvDisplay(request);
	_IlvManager(new) = newmgr;
	if (_IlvManagerId(request)) {
	    _IlvManagerId(new) = strdup(_IlvManagerId(request));
	} else
	    _IlvManagerId(new) = XtName(new);
	__CheckManagerCoherency(new);
	if (newmgr) filenameEnabled = 0;
    }
    if (!_IlvManagerId(new)) _IlvManagerId(new) = XtName(new);
    if (_IlvManagerId(request) &&
	(!_IlvManagerId(current) ||
	 strcmp(_IlvManagerId(request), _IlvManagerId(current)))) {
	_IlvManagerId(new) = strdup(_IlvManagerId(request));
	_IlvManager(new) = 0;
	__CheckManagerCoherency(new);
	filenameEnabled = 0;
    }
    if (!_IlvManager(new)) _XmIlvManagerGetManager(new);
    if (_IlvManager(current) != _IlvManager(new)) {
	if (_IlvView(new)) {
	    if (_IlvManager(current))
		IlvDisconnectFromManager(_IlvManager(current), _IlvView(new));
	    if (_IlvManager(new)) {
		IlvConnectToManager(_IlvManager(new), _IlvView(current));
		if (XtIsRealized(new))
		    IlvManagerViewInitialize(_IlvManager(new), _IlvView(new));
	    }
	}
	
    }
    /* IlvFileName [CSG]*/
    if (filenameEnabled && _IlvFileName(new))
	IlvManagerLoadFile(_IlvManager(new), _IlvFileName(new));
    /* Scroll management */
    if (XtIsRealized(new)) {
	if ((_IlvHsb(request) != _IlvHsb(current)) ||
	    (_IlvVsb(request) != _IlvVsb(current))) {
	    if (!_IlvScrollHook(new))
		_IlvScrollHook(new) =
		    IlvManagerCreateScrollHook(_IlvManager(new), _IlvView(new),
					       _IlvHsb(request),
					       _IlvVsb(request));
	    else 
		IlvManagerSetScrollHook(_IlvScrollHook(new),
					_IlvHsb(request), _IlvVsb(request));
	    _IlvHsb(new) = _IlvHsb(request);
	    _IlvVsb(new) = _IlvVsb(request);
	}
    }
    return True;
}
static Boolean
_CellMgrSetValues
#if defined(_NO_PROTO)
	  (current, request, new)
XmIlvCellManagerWidget current;
XmIlvCellManagerWidget request;
XmIlvCellManagerWidget new;
#else
	  (XmIlvCellManagerWidget current,
	   XmIlvCellManagerWidget request,
	   XmIlvCellManagerWidget new)
#endif
{
    int filenameEnabled = 1;
    /* IlvManagerId [CSG]*/
    if (_IlvCellManagerId(request)) 
	_IlvCellManagerId(new) = strdup(_IlvCellManagerId(request));
    if (!_IlvCellManagerId(new))
	_IlvCellManagerId(new) = XtName(new);
    if (!_IlvCellManagerId(current) ||
	strcmp(_IlvCellManagerId(current), _IlvCellManagerId(new))) {
	XtPointer mgr = _IlvCellManager(current);
	if (mgr && _IlvCellView(current)) {
	    filenameEnabled = 0;
	    IlvDisconnectFromCellManager(mgr, _IlvCellView(current));
	}
	_IlvCellManager(new) = 0;
	_XmIlvCellManagerGetManager(new);
	if (_IlvCellView(current)) {
	    IlvConnectToCellManager(_IlvCellManager(new),
				    _IlvCellView(current));
	    if (XtIsRealized(new))
		IlvCellManagerViewInitialize(_IlvCellManager(new),
					     _IlvCellView(new));
	}
    }
    return True;
}

/* --------------------------------------------------------------------------
 * -------------------------------------------------------------------------- */
static void _MgrRealize
#if defined(_NO_PROTO)
			(w, p_valueMask, attributes)
Widget                w;
Mask*                 p_valueMask;
XSetWindowAttributes* attributes;
#else
			(Widget                w,
			 Mask*                 p_valueMask,
			 XSetWindowAttributes* attributes)
#endif
{
    XmIlvManagerWidget mgrwidget = (XmIlvManagerWidget)w;
    XtRealizeProc     realize;
    XtPointer         manager   = NULL;
    String            managerid = 0;
    
    /*
     * Call the super method
     */
    realize = xmDrawingAreaWidgetClass->core_class.realize;
    if (realize) (*realize)(w, p_valueMask, attributes);
    /*
     * Set the manager, if the view is not yet associated to a manager
     */
    _XmIlvManagerGetManager(mgrwidget);
    /*
     * The widget must be realized before adding a view
     */
    if (!_IlvView(mgrwidget) && _IlvManager(mgrwidget)) {
	if (!_IlvDisplay(mgrwidget))
	    _IlvDisplay(mgrwidget) = XmGetIlvDisplay((Widget)mgrwidget);
	/*	
	 * Creating a new IlvView
	 */
	_IlvView(mgrwidget) = __NewIlvView((Widget)mgrwidget);
	/*
	 * View Initialization (interactor)
	 */
	IlvManagerViewInitialize(_IlvManager(mgrwidget), _IlvView(mgrwidget));
	/* Scroll managment */
	if ((_IlvHsb(mgrwidget) || _IlvVsb(mgrwidget))) {
	    if (!_IlvScrollHook(mgrwidget))
		_IlvScrollHook(mgrwidget) =
		    IlvManagerCreateScrollHook(_IlvManager(mgrwidget),
					       _IlvView(mgrwidget),
					       _IlvHsb(mgrwidget),
					       _IlvVsb(mgrwidget));
	    else 
		IlvManagerSetScrollHook(_IlvScrollHook(mgrwidget),
					_IlvHsb(mgrwidget),
					_IlvVsb(mgrwidget));
	}
    }
    
}
static void _CellMgrRealize
#if defined(_NO_PROTO)
			(w, p_valueMask, attributes)
Widget                w;
Mask*                 p_valueMask;
XSetWindowAttributes* attributes;
#else
			(Widget                w,
			 Mask*                 p_valueMask,
			 XSetWindowAttributes* attributes)
#endif
{
    XmIlvCellManagerWidget mgrwidget = (XmIlvCellManagerWidget)w;
    XtRealizeProc          realize;
    XtPointer              manager   = NULL;
    String                 managerid = 0;
    
    /*
     * Call the super method
     */
    realize = xmDrawingAreaWidgetClass->core_class.realize;
    if (realize) (*realize)(w, p_valueMask, attributes);
    /*
     * Set the manager, if the view is not yet associated to a manager
     */
    _XmIlvCellManagerGetManager(mgrwidget);
    /*
     * The widget must be realized before adding a view
     */
    if (XtIsRealized(mgrwidget) && !(_IlvCellView(mgrwidget))) {
	/*	
	 * Creating a new IlvView
	 */
	_IlvCellView(mgrwidget) = __NewIlvCellView((Widget)mgrwidget,
						   _IlvCellManager(mgrwidget));
	/*
	 * View Initialization (interactor)
	 */
	IlvCellManagerViewInitialize(_IlvCellManager(mgrwidget),
				     _IlvCellView(mgrwidget));
    }
    
}

/* --------------------------------------------------------------------------
 * Get the manager pointer from a widget
 * -------------------------------------------------------------------------- */
static XtPointer
_XmIlvManagerGetManager
#if defined(_NO_PROTO)
			(mgrwidget)
XmIlvManagerWidget mgrwidget;
#else
			(XmIlvManagerWidget mgrwidget)
#endif
{
    int        result    = 0;
    XtPointer  manager   = NULL;
    int        id        = 0;
    String     managerid = 0;

    manager = _IlvManager(mgrwidget);
    if (manager) return manager;
    /*
     * Default manager id is widget name
     */
    if (!(managerid = _IlvManagerId(mgrwidget)) || !strlen(managerid)) {
	managerid = XtName(mgrwidget);
	_IlvManagerId(mgrwidget) = managerid;
    }
    /*
     * Store the display
     */
    _IlvDisplay(mgrwidget) = XmGetIlvDisplay((Widget)mgrwidget);
    /*
     * Check for existing manager id
     */
    id = XrmStringToQuark(_IlvManagerId(mgrwidget));
    result = XFindContext(XtDisplay(mgrwidget), id, _mgrContext,
			  &manager);
    if (result) {
	manager = __NewIlvManager(mgrwidget);
	XSaveContext(XtDisplay(mgrwidget), id, _mgrContext, manager);
    }
    _IlvManager(mgrwidget) = manager;
    return manager;
}

/* --------------------------------------------------------------------------
 * Get the cell manager pointer from a widget
 * -------------------------------------------------------------------------- */
static XtPointer
_XmIlvCellManagerGetManager
#if defined(_NO_PROTO)
			(mgrwidget)
XmIlvCellManagerWidget mgrwidget;
#else  /* !_NO_PROTO */
			(XmIlvCellManagerWidget mgrwidget)
#endif /* !_NO_PROTO */
{
    int       result    = 0;
    XtPointer manager   = NULL;
    int       id        = 0;
    String    managerid = 0;

    manager = _IlvCellManager(mgrwidget);
    if (manager) return manager;
    /*
     * Default manager id is widget name
     */
    if (!(managerid = _IlvCellManagerId(mgrwidget)) || !strlen(managerid)) {
	managerid = XtName(mgrwidget);
	_IlvCellManagerId(mgrwidget) = managerid;
    }
    /*
     * Store the display
     */
    _IlvCellDisplay(mgrwidget) =
	(XtPointer)XmGetIlvDisplay((Widget)mgrwidget);
    /*
     * Check for existing manager id
     */
    id = XrmStringToQuark(_IlvCellManagerId(mgrwidget));
    result = XFindContext(XtDisplay(mgrwidget), id, _cellMgrContext,
			  &manager);
    manager = __NewIlvCellManager(mgrwidget,
				  _IlvNbCol(mgrwidget), _IlvNbRow(mgrwidget));
    id = XrmStringToQuark(managerid);
    XSaveContext(XtDisplay(mgrwidget), id, _cellMgrContext, manager);
    _IlvCellManager(mgrwidget) = manager;
    return manager;
}

/* --------------------------------------------------------------------------
 * Set the manager pointer to the view
 * -------------------------------------------------------------------------- */
static void
_XmIlvManagerSetManager
#if defined(_NO_PROTO)
				(mgrwidget, manager, managerid)
XmIlvManagerWidget mgrwidget;
XtPointer manager;
String managerid;
#else  /* !_NO_PROTO */
				(XmIlvManagerWidget mgrwidget,
				 XtPointer         manager,
				 String            managerid)
#endif /* !_NO_PROTO */
{
    int id = 0;
    if (managerid) id = XrmStringToQuark(managerid);
    XSaveContext(XtDisplay(mgrwidget), id, _mgrContext, manager);
    _IlvManager(mgrwidget)   = manager;
    _IlvManagerId(mgrwidget) = managerid;
}
static void
_XmIlvCellManagerSetManager
#if defined(_NO_PROTO)
				(mgrwidget, manager, managerid)
XmIlvCellManagerWidget mgrwidget;
XtPointer manager;
String    managerid;
#else  /* !_NO_PROTO */
				(XmIlvCellManagerWidget mgrwidget,
				 XtPointer              manager,
				 String                 managerid)
#endif /* !_NO_PROTO */
{
    int id = 0;
    if (managerid) id = XrmStringToQuark(managerid);
    XSaveContext(XtDisplay(mgrwidget), id, _cellMgrContext, manager);
    _IlvCellManager(mgrwidget)   = manager;
    _IlvCellManagerId(mgrwidget) = managerid;
}

/*  --------------------------------------------------------------------------
 * Convenience's functions 
 * -------------------------------------------------------------------------- */

/* --------------------------------------------------------------------------
 * This function creates and returns an IlogViews widget.
 * -------------------------------------------------------------------------- */
Widget XmCreateIlvManager
#if defined(_NO_PROTO)
				(p, name, args, n)
Widget          p;     /*  parent widget */
String          name;  /*  widget name */
ArgList         args;  /*  arg list    */
Cardinal        n;     /*  arg count   */
#else
				(Widget   p,
				 String   name,
				 ArgList  args,
				 Cardinal n)
#endif
{
    return XtCreateWidget(name, xmIlvManagerWidgetClass, p, args, n);
}
Widget XmCreateIlvCellManager
#if defined(_NO_PROTO)
				(p, name, args, n)
Widget          p;     /*  parent widget */
String          name;  /*  widget name */
ArgList         args;  /*  arg list    */
Cardinal        n;     /*  arg count   */
#else
				(Widget   p,
				 String   name,
				 ArgList  args,
				 Cardinal n)
#endif
{
    return XtCreateWidget(name, xmIlvCellManagerWidgetClass, p, args, n);
}


/* --------------------------------------------------------------------------
 * Get the IlogViews Display
 * -------------------------------------------------------------------------- */
XtPointer
XmGetIlvDisplay
#if defined(_NO_PROTO)
				(mgrwidget)
Widget mgrwidget;
#else
				(Widget mgrwidget)
#endif
{
    return __GetIlvDisplay(mgrwidget);
}

/* --------------------------------------------------------------------------
 * Get the IlogViews Manager
 * -------------------------------------------------------------------------- */
XtPointer
XmGetIlvManager
#if defined(_NO_PROTO)
				(mgrwidget)
Widget mgrwidget;
#else
				(Widget mgrwidget)
#endif
{
    return _IlvManager((XmIlvManagerWidget)mgrwidget);
}
XtPointer
XmGetIlvCellManager
#if defined(_NO_PROTO)
				(mgrwidget)
Widget mgrwidget;
#else
				(Widget mgrwidget)
#endif
{
    return _IlvCellManager((XmIlvCellManagerWidget)mgrwidget);
}

/* --------------------------------------------------------------------------
 * Get the IlogViews View
 * -------------------------------------------------------------------------- */
XtPointer
XmGetIlvManagerView
#if defined(_NO_PROTO)
		     (mgrwidget)
Widget mgrwidget;
#else
		     (Widget mgrwidget)
#endif
{
    return _IlvView((XmIlvManagerWidget)mgrwidget);
}
XtPointer
XmGetIlvCellManagerView
#if defined(_NO_PROTO)
		     (mgrwidget)
Widget mgrwidget;
#else
		     (Widget mgrwidget)
#endif
{
    return _IlvCellView((XmIlvCellManagerWidget)mgrwidget);
}
