static char MicroH2OTemp_id[] = "$Header: /u/oreilly/rov/tmacs/RCS/MicroEnvironment.cc,v 1.7 1998/12/18 21:15:56 oreilly Exp $";


/*
$Log: MicroEnvironment.cc,v $
Revision 1.7  1998/12/18 21:15:56  oreilly
Remove event handlers in destructor

Revision 1.6  1997/12/12 17:54:26  oreilly
*** empty log message ***

Revision 1.5  1997/10/03 09:30:45  oreilly
*** empty log message ***

Revision 1.4  97/09/10  08:34:49  08:34:49  oreilly (Thomas C. O'Reilly)
Removed debug messages

Revision 1.3  97/05/30  16:20:34  16:20:34  oreilly (Thomas C. O'Reilly)
*** empty log message ***

Revision 1.2  97/03/20  12:30:05  12:30:05  oreilly (Thomas C. O'Reilly)
..

Revision 1.1  96/10/31  10:40:56  10:40:56  oreilly (Thomas C. O'Reilly)
Initial revision

*/
#include <Xm/RowColumn.h>
#include <Xm/Form.h>
#include <Xm/Label.h>
#include <Xm/PushB.h>
#include <Xm/Separator.h>
#include <Xm/ScrolledW.h>
#include "MicroEnvironment.h"
#include "RovMainWindow.h"
#include "MicroGui.h"
#include "AlarmText.h"
#include "AlarmedText.h"
#include "TiburonApp.h"
#include "gf_5vDm.h"

#define dprintf if (debug) fprintf

MicroEnvironmentTable::MicroEnvironmentTable(const char *name, Widget parent,
					     int nColumns,
					     int period)
  : VkComponent(name)
{
  _baseWidget = XtVaCreateWidget(name, xmFormWidgetClass, parent, NULL);

  installDestroyHandler();
  
  _nColumns = nColumns;
  _period = period;
  _scrollBarsNeeded = False;
  _scrolledWindow = 0;
}


MicroEnvironmentTable::~MicroEnvironmentTable()
{
  if (_scrolledWindow) {
    XtRemoveEventHandler(_scrolledWindow,
			 ExposureMask,
			 False,
			 (XtEventHandler )&MicroEnvironmentTable::exposeCallback,
			 (XtPointer )this);

    XtRemoveEventHandler(_scrolledWindow,
			 StructureNotifyMask,
			 False,
			 (XtEventHandler )
			 &MicroEnvironmentTable::resizeCallback,
			 (XtPointer )this);
  }
}


void MicroEnvironmentTable::addRows(VehicleComponent *parent)
{
  VehicleComponent **child;
  SListIter<VehicleComponent *> children(parent->childList);

  while (child = children.next())
  {
    if ((*child)->isMicro())
    {
      Micro *micro = (Micro *)(*child);
      MicroH2OTemp *h2OTemp;
      MicroGf5v *gf5v;
      MicroGf *gf;
      char errorBuf[errorMsgSize];
      AlarmedTableRow *row;
      
      switch (_type)
      {
	case H2OTempTable:
	h2OTemp = micro->h2OTemp(_workArea, _period);
	if (h2OTemp->error())
	{
	  h2OTemp->errorMsg(errorBuf);
	  setError(errorBuf);
	}

	row = h2OTemp;
	
	break;
	
	case GfTable:
	gf = micro->gf(_workArea, _period);
	if (gf->error())
	{
	  gf->errorMsg(errorBuf);
	  setError(errorBuf);
	}
	
	row = gf;

	break;

	case Gf5vTable:
	gf5v = micro->gf5v(_workArea, _period);
	if (gf5v->error())
	{
	  gf5v->errorMsg(errorBuf);
	  setError(errorBuf);
	}
	
	row = gf5v;

	break;
      }
      
      /* Some micro's may not actually have a GUI for the specified table -
	 this is indicated by a NULL VkComponent pointer */
      if (row)
      {
	XtVaCreateManagedWidget("separator", xmSeparatorWidgetClass, 
				_workArea, NULL);

	_rows.add(&row);
      }
    }

    addRows(*child);
  }
}



void MicroEnvironmentTable::alarmUpdate(VkCallbackObject *obj,
					void *clientData,
					void *callData)
{
  Boolean debug = False;
  
  dprintf(stderr, "MicroEnvironmentTable::alarmUpdate() - column=%d\n",
	  (int )callData);

  setColumnPriority((int )callData);
}



void MicroEnvironmentTable::setColumnPriorities()
{
  for (int column = 1; column < _nColumns; column++)
    setColumnPriority(column);
}


void MicroEnvironmentTable::setColumnPriority(int column)
{
  Boolean debug = False;
  dprintf(stderr, "MicroEnvironmentTable::setColumnPriority(%d)\n", column);
  
  AlarmedTableRow *row;
  Priority maxPriority = NoPriority;
    
  for (int i = 0; i < _rows.size(); i++)
  {
    _rows.get(i, &row);
      
    maxPriority = 
      higherPriority(row->priority(column), maxPriority);
  }

  _header->setColumnPriority(column, maxPriority);
}


void MicroEnvironmentTable::createTable(int tableWidth)
{
  
  Widget separator =
    XtVaCreateManagedWidget("separator", xmSeparatorWidgetClass, _baseWidget,
			    XmNleftAttachment, XmATTACH_FORM,
			    XmNrightAttachment, XmATTACH_FORM,
			    XmNtopAttachment, XmATTACH_WIDGET,
			    XmNtopWidget, _header->baseWidget(),
			    NULL);

  _scrolledWindow = 
    XtVaCreateWidget("scrolledWindow", xmScrolledWindowWidgetClass,
		     _baseWidget,
		     XmNscrollingPolicy, XmAUTOMATIC,
		     XmNscrollBarDisplayPolicy, XmAS_NEEDED,
		     XmNtopAttachment, XmATTACH_WIDGET,
		     XmNtopWidget, separator,
		     XmNleftAttachment, XmATTACH_FORM,
		     XmNrightAttachment, XmATTACH_FORM,
		     XmNbottomAttachment, XmATTACH_FORM,		
		     NULL);

  
  XtVaGetValues(_scrolledWindow, XmNverticalScrollBar, &_scrollbar, NULL);
  
  XtAddEventHandler(_scrolledWindow,
		    ExposureMask,
		    False,
		    (XtEventHandler )&MicroEnvironmentTable::exposeCallback,
		    (XtPointer )this);

  XtAddEventHandler(_scrolledWindow,
		    StructureNotifyMask,
		    False,
		    (XtEventHandler )
		    &MicroEnvironmentTable::resizeCallback,
		    (XtPointer )this);

  _workArea = XtVaCreateWidget("workArea", xmRowColumnWidgetClass, 
			       _scrolledWindow,
			       XmNwidth, tableWidth,
			       XmNresizeWidth, False,
			       NULL);

  TiburonApp *app = (TiburonApp *)theApplication;
  
  addRows(app->vehicleConfig->vehicleBase);

//  setColumnPriorities();

  XtManageChild(_workArea);
  XtManageChild(_scrolledWindow);
}


void MicroEnvironmentTable::resizeExposeHandler()
{
  Boolean debug = False;
  
  Dimension workAreaHeight, scrolledWindowHeight;
  
  XtVaGetValues(_scrolledWindow, XmNheight, &scrolledWindowHeight, NULL);
  XtVaGetValues(_workArea, XmNheight, &workAreaHeight, NULL);

  dprintf(stderr, "scrolledWindowHeight: %d, workAreaHeight: %d\n",
	  scrolledWindowHeight, workAreaHeight);


  Boolean scrollBarsNeeded;
  
  if (workAreaHeight > scrolledWindowHeight)
    scrollBarsNeeded = True;
  else
    scrollBarsNeeded = False;

  if (scrollBarsNeeded != _scrollBarsNeeded)
  {
    _scrollBarsNeeded = scrollBarsNeeded;
    
    AlarmedTableRow *row;
    for (int i = 0; i < _rows.size(); i++)
    {
      _rows.get(i, &row);

      if (_scrollBarsNeeded)
	row->addCallback(AlarmedTableRow::alarmUpdateCallback, this,
			 (VkCallbackMethod )
			 &MicroEnvironmentTable::alarmUpdate);
      else
	row->removeCallback(AlarmedTableRow::alarmUpdateCallback, this,
			    (VkCallbackMethod )
			    &MicroEnvironmentTable::alarmUpdate);
	
    }

    if (_scrollBarsNeeded)
      setColumnPriorities();
    else
      _header->clearColumnPriorities();
  }
}


void MicroEnvironmentTable::exposeCallback(Widget w, 
					   XtPointer clientData,
					   XEvent *event)
{
  Boolean debug = False;
  dprintf(stderr, "exposeCallback()\n");
  
  MicroEnvironmentTable *obj = (MicroEnvironmentTable *)clientData;
  obj->resizeExposeHandler();
  
}

void MicroEnvironmentTable::resizeCallback(Widget w, 
					   XtPointer clientData,
					   XEvent *event)
{
  Boolean debug = False;
  MicroEnvironmentTable *obj = (MicroEnvironmentTable *)clientData;
  
  dprintf(stderr, 
	  "MicroEnvironmentTable \"%s\" - scrolled window resized\n",
	  obj->name());

  if (XtIsRealized(obj->baseWidget()))
      obj->resizeExposeHandler();
}


MicroH2OTempTable::MicroH2OTempTable(const char *name, Widget parent,
				     int period)
  : MicroEnvironmentTable(name, parent, MicroH2OTempColumns, period)
{
  Boolean debug = False;
  
  _type = H2OTempTable;
  
  int width, height;
  TiburonApp *app = (TiburonApp *)theApplication;
  app->rovMainWindow()->viewPortSize(&width, &height);
  
  int H2OTempTableWidth = width - 50;

  _header = new MicroH2OTempHeader("header", _baseWidget);

  XtVaSetValues(_header->baseWidget(),
		XmNleftAttachment, XmATTACH_FORM,
		XmNtopAttachment, XmATTACH_FORM,
		XmNwidth, H2OTempTableWidth,
		NULL);
		
  _header->show();

  createTable(H2OTempTableWidth);
}




MicroGfTable::MicroGfTable(const char *name, Widget parent, int period)
  : MicroEnvironmentTable(name, parent, MicroGfColumns, period)
{
  _type = GfTable;
  
  int width, height;
  TiburonApp *app = (TiburonApp *)theApplication;
  app->rovMainWindow()->viewPortSize(&width, &height);
  
  int GfTableWidth = width - 50;

  _header = new MicroGfHeader("header", _baseWidget);

  XtVaSetValues(_header->baseWidget(),
		XmNleftAttachment, XmATTACH_FORM,
		XmNtopAttachment, XmATTACH_FORM,
		XmNwidth, GfTableWidth,
		NULL);

  _header->show();

  createTable(GfTableWidth);
}


MicroGf5vTable::MicroGf5vTable(const char *name, Widget parent, int period)
  : MicroEnvironmentTable(name, parent, MicroGf5vColumns, period)
{
  _type = Gf5vTable;
  
  int width, height;
  TiburonApp *app = (TiburonApp *)theApplication;
  app->rovMainWindow()->viewPortSize(&width, &height);
  
  int GfTableWidth = width - 50;

  _header = new MicroGf5vHeader("header", _baseWidget);

  XtVaSetValues(_header->baseWidget(),
		XmNleftAttachment, XmATTACH_FORM,
		XmNtopAttachment, XmATTACH_FORM,
		XmNwidth, GfTableWidth,
		NULL);

  _header->show();
  
  createTable(GfTableWidth);
}












