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

#include <X11/Intrinsic.h>

#include <ilviews/allmgr.h>
#include <ilviews/view.h>
#include <ilviews/graphmgr.h>
#include <ilviews/cellmgr.h>
#include <ilviews/cellint.h>

#include <scrollhook.h>
#include <IlogViews.h>
#include <IlogViewsP.h>

#if (IlvVersion == 0x0102)
static IlvDisplay* mainIlvDisplay = 0;
#endif /* IlogViews 1.2 */

// --------------------------------------------------------------------------
// Create a new IlvDisplay
extern "C" XtPointer
__GetIlvDisplay(Widget widget)
{
#if (IlvVersion == 0x0102)
    // Works for one only display...
    if (!mainIlvDisplay)
	mainIlvDisplay = new IlvDisplay(XtDisplay(widget), "IlogViews");
    return (XtPointer)mainIlvDisplay;
#else  /* IlogViews > 1.2 */
    IlvDisplay* display = IlvGetDisplay(XtDisplay(widget));
    if (!display) display = new IlvDisplay(XtDisplay(widget), "IlogViews");
    return (XtPointer)display;
#endif /* IlogViews 1.2 */
}

// --------------------------------------------------------------------------
// Create a new IlvManager
extern "C" XtPointer
__NewIlvManager(XmIlvManagerWidget widget)
{
    IlvManager* manager = new IlvManager((IlvDisplay*)_IlvDisplay(widget), 3);
    manager->setName((char*)_IlvManagerId(widget));
    return manager;
}

// --------------------------------------------------------------------------
// Create a new IlvCellManager
extern "C" XtPointer
__NewIlvCellManager(XmIlvCellManagerWidget widget, int col, int row)
{
    IlvCellManager* cellmgr =
	new IlvCellManager((IlvDisplay*)_IlvCellDisplay(widget), col, row);
    cellmgr->setName((char*)_IlvCellManagerId(widget));
    return cellmgr;
}

// --------------------------------------------------------------------------
// Connect a IlvView
extern "C" void
IlvConnectToManager(IlvManager* manager, IlvView* view)
{
    // Connect a view to the manager
    manager->addView(view);
    manager->draw(view);
}
extern "C" void
IlvConnectToCellManager(IlvCellManager* manager, IlvView* view)
{
    // Connect a view to the manager
    manager->addView(view);
    manager->draw(view);
}

// --------------------------------------------------------------------------
// Disconnect a IlvView
extern "C" void
IlvDisconnectFromManager(XtPointer manager, IlvView* view)
{
    // Connect a view to the manager
    ((IlvManager*)manager)->removeView(view);
}
extern "C" void
IlvDisconnectFromCellManager(XtPointer manager, IlvView* view)
{
    // Connect a view to the manager
    ((IlvCellManager*)manager)->removeView(view);
}

// --------------------------------------------------------------------------
// Create a new IlvView
extern "C" XtPointer
__NewIlvView(XmIlvManagerWidget widget)
{
    IlvView* ilvview = new IlvView((IlvDisplay*)(_IlvDisplay(widget)), widget);
    IlvConnectToManager((IlvManager*)(_IlvManager(widget)), ilvview);
    return (XtPointer)ilvview;
}
extern "C" XtPointer
__NewIlvCellView(Widget widget, IlvCellManager* manager)
{
    IlvDisplay* ilvdisplay = manager->getDisplay();
    IlvView* ilvview = new IlvView(ilvdisplay, widget);
    IlvConnectToCellManager(manager, ilvview);
    return (XtPointer)ilvview;
}

// --------------------------------------------------------------------------
// Redraw manager's views
extern "C" void
IlvManagerRedraw(IlvManager* manager, IlvView* view, Region region)
{
    if (region) {
	// Get the clip region
	XRectangle rect_return;
	XClipBox(region,&rect_return);
	// Create an IlvRegion
	IlvRect rect(rect_return.x, rect_return.y, 
		     rect_return.width, rect_return.height);
	IlvRegion clip(rect);
	// Redraw the view
	manager->draw(view, IlvTrue, &clip);
    } else
	// Redraw the view
	manager->draw(view, IlvTrue);
}
extern "C" void
IlvCellManagerRedraw(IlvCellManager* manager,IlvView* view, Region region)
{
    if (region) {
	// Get the clip region
	XRectangle rect_return;
	XClipBox(region,&rect_return);
	// Create an IlvRegion
	IlvRect rect(rect_return.x, rect_return.y, 
		     rect_return.width, rect_return.height);
	IlvRegion clip(rect);
	manager->draw(view, IlvTrue, &rect);
    } else
	manager->draw(view, IlvTrue);
}

// --------------------------------------------------------------------------
// Set the double buffering mode on a view
extern "C" void
IlvManagerSetDoubleBuffering(IlvManager* manager, IlvView* view, IlvBoolean dbl_flag)
{
    manager->setDoubleBuffering(view, dbl_flag);
}

// --------------------------------------------------------------------------
// Destroy a view
extern "C" void
IlvManagerViewDestroy(IlvView* view)
{
    delete view;
}

// --------------------------------------------------------------------------
extern "C" void
IlvManagerViewInitialize(IlvManager* manager, IlvView* view)
{
    // Standard Manager default initialization
    manager->setInteractor(new IlvSelectInteractor(manager, view));
}
extern "C" void
IlvCellManagerViewInitialize(IlvCellManager* manager, IlvView* view)
{
    manager->setInteractor(new IlvCellManagerInteractorMakeCell(manager,
								view));
}

// --------------------------------------------------------------------------
extern "C" XtPointer
IlvManagerCreateScrollHook(IlvManager* manager, 
			   IlvView* view,
			   Widget hsb, 
			   Widget vsb)
{
    // Create a new ScrollHook
    IlvScrollViewHook* scrollhook = new IlvScrollViewHook(manager, view, vsb, hsb);
    manager->installViewHook(scrollhook);
    return (XtPointer)scrollhook;
}

// --------------------------------------------------------------------------
extern "C" void
IlvManagerDeleteScrollHook (IlvScrollViewHook* scrollhook)
{
    // Delete scroll view hook
    if (scrollhook) delete scrollhook;
}

// --------------------------------------------------------------------------
extern "C" void
IlvManagerSetScrollHook (IlvScrollViewHook* scrollhook,
			 Widget hsb, Widget vsb)
{
    if (scrollhook) {
	scrollhook->setVsb(vsb);
	scrollhook->setHsb(hsb);
	scrollhook->initScrollBars();
    }
}

// --------------------------------------------------------------------------
extern "C" void
IlvManagerLoadFile(IlvManager* manager, char* filename)
{
    manager->deleteAll(IlvTrue, IlvTrue);
    manager->read(filename);
}
