#include <Xm/Frame.h>
#include <Xm/Form.h>
#include <Xm/RowColumn.h>
#include <Xm/PushB.h>
#include <Xm/ToggleB.h>
#include <Xm/Label.h>
#include <Xm/ArrowB.h>
#include <Xm/Scale.h>
#include <xpm.h>
#include <Vk/VkErrorDialog.h>
#include "powerDM.h"
#include "DmErrno.h"
#include "netNanny.xpm"
#include "TiburonApp.h"
#include "SuctionSampler.h"
#include "toolsledNames.h"

SetPositionDialog::SetPositionDialog(const char *name, const char *dmItemName) 
  : VkGenericDialog(name)
{
  _dmJarNumberName = strdup(dmItemName);
}
  

SetPositionDialog::~SetPositionDialog()
{
  free((void *)_dmJarNumberName);
}


Widget SetPositionDialog::createDialog(Widget parent)
{
  Widget base = VkGenericDialog::createDialog(parent);

  Widget mgr = XtVaCreateWidget("mgr", xmRowColumnWidgetClass, base,
				XmNorientation, XmVERTICAL,
				NULL);

  Widget label = 
    XtVaCreateManagedWidget("setPositionLabel", xmLabelWidgetClass, mgr,
			    NULL);

  _jarNumber = new DmNat16Text("jarNumber", mgr,
			       _dmJarNumberName, AperiodicGuaranteed);

  _jarNumber->show();

  XtManageChild(mgr);

  _showOK = True;
  _showCancel = _showApply = False;

  return base;
}


Widget SetPositionDialog::post(const char *msg,
			       XtCallbackProc okCB,
			       XtCallbackProc cancelCB,
			       XtCallbackProc applyCB,
			       XtPointer clientData,
			       const char *helpString,
			       VkSimpleWindow *parent)
{
  VkDialogManager::post(msg, okCB, cancelCB, applyCB, clientData, helpString,
			parent);
}



void SetPositionDialog::applyCallback(Widget parent,
				      XtPointer clientData,
				      XtPointer callData)
{
  Boolean debug = ((TiburonApp *)theApplication)->debug();
  
  SetPositionDialog *obj = (SetPositionDialog *)clientData;
  obj->_jarNumber->okayCallback();
}



SuctionSamplerIndex::SuctionSamplerIndex(const char *name,
					 Widget parent,
					 const char *appName,
					 DmBooleanObject *dmIndexSense,
					 DmBooleanObject *dmAutoIndex,
					 DmNat16Object *dmJarNumber,
					 int period)
  : DmGuiObject(name, parent)
{
  _baseWidget = 
    XtVaCreateWidget(name, xmRowColumnWidgetClass, parent,
		     XmNorientation, XmVERTICAL,
		     NULL);

  installDestroyHandler();

  Widget mgr;
  Widget toggleMgr1, toggleMgr2;

  mgr = XtVaCreateWidget("mgr", xmFormWidgetClass, _baseWidget, NULL);

  toggleMgr1 = 
    XtVaCreateWidget("toggleMgr1", xmRowColumnWidgetClass, mgr,
		     XmNorientation, XmVERTICAL,
		     XmNleftAttachment, XmATTACH_FORM,
		     XmNtopAttachment, XmATTACH_FORM,
		     XmNbottomAttachment, XmATTACH_FORM,
		     NULL);

  _revIndex = 
    XtVaCreateManagedWidget("ccwIndex", xmArrowButtonWidgetClass, toggleMgr1,
			    XmNarrowDirection, XmARROW_LEFT,
			    NULL);

  XtAddCallback(_revIndex, XmNarmCallback, 
		&SuctionSamplerIndex::callback,
		(XtPointer )this);

  XtAddCallback(_revIndex, XmNdisarmCallback, 
		&SuctionSamplerIndex::callback,
		(XtPointer )this);

  _revIndexCurrent = new DynamicLabel("revIndexCurrent", toggleMgr1);
  _revIndexCurrent->show();
  
  XtManageChild(toggleMgr1);
  
  toggleMgr2 = 
    XtVaCreateWidget("toggleMgr2", xmRowColumnWidgetClass, mgr,
		     XmNorientation, XmVERTICAL,
		     XmNleftAttachment, XmATTACH_WIDGET,
		     XmNleftWidget, toggleMgr1,
		     XmNtopAttachment, XmATTACH_FORM,
		     XmNbottomAttachment, XmATTACH_FORM,
		     NULL);

  _fwdIndex = 
    XtVaCreateManagedWidget("cwIndex", xmArrowButtonWidgetClass, toggleMgr2,
			    XmNarrowDirection, XmARROW_RIGHT,
			    NULL);
  
  XtAddCallback(_fwdIndex, XmNarmCallback, 
		&SuctionSamplerIndex::callback,
		(XtPointer )this);

  XtAddCallback(_fwdIndex, XmNdisarmCallback, 
		&SuctionSamplerIndex::callback,
		(XtPointer )this);

  _fwdIndexCurrent = new DynamicLabel("fwdIndexCurrent", toggleMgr2);
  _fwdIndexCurrent->show();

  XtManageChild(toggleMgr2);
  
  // Get colors to use in re-drawing buttons
  XtVaGetValues(_fwdIndex,
		XmNtopShadowColor, &_lightEdgeColor, 
		XmNbottomShadowColor, &_darkEdgeColor,
		NULL);

  Widget label = 
    XtVaCreateManagedWidget("indexLabel", xmLabelWidgetClass, mgr, 
			    XmNleftAttachment, XmATTACH_WIDGET,
			    XmNleftWidget, _fwdIndex,
			    XmNtopAttachment, XmATTACH_FORM,
			    NULL);

  _index = 
    XtVaCreateManagedWidget("index", xmLabelWidgetClass, mgr, 
			    XmNleftAttachment, XmATTACH_WIDGET,
			    XmNleftWidget, label,
			    XmNtopAttachment, XmATTACH_FORM,
			    NULL);
  
  
  _autoIndex = 
    XtVaCreateManagedWidget("autoIndex", xmToggleButtonWidgetClass, mgr,
			    XmNleftAttachment, XmATTACH_WIDGET,
			    XmNleftWidget, _index,
			    XmNtopAttachment, XmATTACH_FORM,
			    NULL);

  XtAddCallback(_autoIndex, XmNvalueChangedCallback, 
		&SuctionSamplerIndex::callback,
		(XtPointer )this);

  _definePosition = 
    XtVaCreateManagedWidget("definePosition", xmPushButtonWidgetClass, mgr,
			    XmNleftAttachment, XmATTACH_WIDGET,
			    XmNleftWidget, _autoIndex,
			    XmNtopAttachment, XmATTACH_FORM,
			    NULL);

  XtAddCallback(_definePosition, XmNarmCallback, 
		&SuctionSamplerIndex::callback,
		(XtPointer )this);

  XtManageChild(mgr);

  TiburonApp *app = (TiburonApp *)theApplication;
  app->okColors(&_atIndexForeground, &_atIndexBackground);
  app->priority2Colors(&_movingForeground, &_movingBackground);
  app->powerOnColors(&_onForeground, &_onBackground);

  // Use default foreground/background for "off" state 
  XtVaGetValues(_baseWidget, 
		XmNforeground, &_offForeground,
		XmNbackground, &_offBackground,
		NULL);
  
  app->faultColors(&_faultForeground, &_faultBackground);

  _dmIndexSense = dmIndexSense;
  _dmAutoIndex = dmAutoIndex;
  _dmJarNumber = dmJarNumber;

  char prefix[MaxDmNameLen];
  strcpy(prefix, POWER_DM_PREFIX);
  char *namePtr = prefix + strlen(prefix);

  sprintf(namePtr, "%s.%s", appName, SSIndexRevName);
  _dmReverse = new SwitchDm(prefix);

  sprintf(namePtr, "%s.%s", appName, SSIndexFwdName);
  _dmForward = new SwitchDm(prefix);
  
  _revStatusDetector = 
    new DmInputDetector("revStatus", parent, _dmReverse->status, this);

  _fwdStatusDetector = 
    new DmInputDetector("fwdStatus", parent, _dmForward->status, this);
  
  if (addDmObject(_dmForward->request, DmProvideFlag) == -1)
    return;

  if (addDmObject(_dmForward->current, DmConsumeFlag) == -1)
    return;

  if (addDmObject(_dmReverse->request, DmProvideFlag) == -1)
    return;

  if (addDmObject(_dmReverse->current, DmConsumeFlag) == -1)
    return;

  if (addDmObject(_dmIndexSense, DmConsumeFlag) == -1)
    return;

  if (addDmObject(_dmJarNumber, DmConsumeFlag) == -1)
    return;

  if (addDmObject(_dmAutoIndex, DmProvideFlag | DmConsumeFlag) == -1)
    return;

  if (setConsumePeriod(period) == -1)
    return;

  _autoMode = FALSE;
  
  _fwdSwitchStatus = _revSwitchStatus = SWITCH_OFF;
  _prevIndexSense = -999;
  _prevJarNumber = 999;

  _setPositionDialog = new SetPositionDialog("setPositionDialog",
					     _dmJarNumber->getName());
					     
  updateFromPeer(_revStatusDetector, NULL);
  updateFromPeer(_fwdStatusDetector, NULL);
  updateGui();
}


SuctionSamplerIndex::~SuctionSamplerIndex()
{
  delete _fwdStatusDetector;
  delete _revStatusDetector;
  
  delete _dmForward;
  delete _dmReverse;

  delete _fwdIndexCurrent;
  delete _revIndexCurrent;
}


void SuctionSamplerIndex::updateGui(DM_Item handle)
{
  DM_Time t;
  Errno errno;
  char errorBuf[errorMsgSize];
  
  if ((errno = _dmAutoIndex->read(&_autoMode, &t)) != SUCCESS)
  {
    sprintDmError(errno, "SuctionSamplerIndex::updateGui()", errorBuf);
    return;
  }

  Boolean set;
  if (_autoMode)
    set = True;
  else
    set = False;
  
  XtVaSetValues(_autoIndex, XmNset, set, NULL);

  MBool indexSense;
  Nat16 jarNumber;

  if ((errno = _dmIndexSense->read(&indexSense, &t)) != SUCCESS)
  {
    sprintDmError(errno, "SuctionSamplerIndex::updateGui()", errorBuf);
    return;
  }

  if ((errno = _dmJarNumber->read(&jarNumber, &t)) != SUCCESS)
  {
    sprintDmError(errno, "SuctionSamplerIndex::updateGui()", errorBuf);
    return;
  }

  if (indexSense != _prevIndexSense || jarNumber != _prevJarNumber)
  {
    char buf[50];

    XmString xmstr;
  
    Pixel foreground, background;
    sprintf(buf, "%d", jarNumber);

    if (indexSense)
    {
      foreground = _atIndexForeground;
      background = _atIndexBackground;
    }
    else
    {
      foreground = _movingForeground;
      background = _movingBackground;
      strcat(buf, "...");
    }

    xmstr = XmStringCreateSimple(buf);

    XtVaSetValues(_index, XmNlabelString, xmstr, 
		  XmNforeground, foreground, XmNbackground, background,
		  NULL);
  
    XmStringFree(xmstr);

    _prevIndexSense = indexSense;
    _prevJarNumber = jarNumber;
  }

  if (_fwdSwitchStatus == SWITCH_ON)
    updateCurrent(Forward);

  if (_revSwitchStatus == SWITCH_ON)
    updateCurrent(Reverse);
}


void SuctionSamplerIndex::updateCurrent(SuctionSamplerIndex::Direction 
					direction)
{
  SwitchDm *dm;
  DynamicLabel *label;
  
  switch (direction)
  {
    case Forward:
    label = _fwdIndexCurrent;
    dm = _dmForward;
    break;
    
    case Reverse:
    label = _revIndexCurrent;
    dm = _dmReverse;
    break;
    
    default:
    return;
  }

  
  Int16 current;
  
  DM_Time t;

  dm->current->read(&current, &t);
  char buf[100];
  sprintf(buf, "%4.1fA", current / 1000.);
  label->setText(buf);
  
  return;
}


void SuctionSamplerIndex::updateFromPeer(DmGuiObject *peer, void *clientData)
{
  SwitchDm *switchDm;
  Widget button;
  switchStatus *status;
  DynamicLabel *label;
  
  if (peer == _revStatusDetector)
  {
    switchDm = _dmReverse;
    button = _revIndex;
    status = &_revSwitchStatus;
    label = _revIndexCurrent;
  }
  else if (peer == _fwdStatusDetector)
  {
    switchDm = _dmForward;
    button = _fwdIndex;
    status = &_fwdSwitchStatus;
    label = _fwdIndexCurrent;
  }
  else
    return;
  
  
  char errorBuf[errorMsgSize];
  
  Boolean debug = False;
  dprintf(stderr, "SuctionSamplerIndex::updateFromPeer()\n");

  DM_Time t;
  Errno err;
  
  // Read status
  Int32 value;
  
  if ((err = switchDm->status->read(&value, &t)) != SUCCESS)
  {
    sprintDmError(err, "SuctionSamplerIndex::updateFromPeer()", errorBuf);
    setError(errorBuf);
    return;
  }
  *status = value;
  
  // Set button colors appropriate to switch status
  Pixel foreground, background;
  Pixel topColor, bottomColor, selectColor, borderColor;
  
  char buf[100];

  switch (*status)
  {  
    case SWITCH_NO_POWER:
    foreground = _noPwrForeground;
    background = _noPwrBackground;
    label->setText("NO PWR");
    label->show();
    break;
    
    case SWITCH_OFF:
    foreground = _offForeground;
    background = _offBackground;
    label->setText(" ");
    break;
    
    case SWITCH_ON:
    foreground = _onForeground;
    background = _onBackground;
    label->show();
    break;
    
    case SWITCH_POWER_ERROR:
    foreground = _faultForeground;
    background = _faultBackground;
    label->setText("FAULT");
    label->show();
    break;

    default:
    return;
  }

  // Just set background color (i.e. do not change arrow color)
  XtVaSetValues(button, XmNbackground, background, NULL);
}


void SuctionSamplerIndex::toggleAuto(MBool set)
{
  DM_Time t;
  gettimeofday(&t, NULL);
  Errno errno;
  char errorBuf[errorMsgSize];
  
  if ((errno = _dmAutoIndex->write(set, &t)) != SUCCESS)
  {
    sprintDmError(errno, "SuctionSamplerIndex::toggleAuto()", errorBuf);
    theErrorDialog->postAndWait(errorBuf);
  }
}


void SuctionSamplerIndex::startStop(MBool start, Widget w)
{
  if (!start && _autoMode)
    // If auto mode, do not do anything when button is released
    return;

  
  DmEnumObject *dmRequest;
  
  if (w == _fwdIndex)
    dmRequest = _dmForward->request;
  else
    dmRequest = _dmReverse->request;
  
  DM_Time t;
  gettimeofday(&t, NULL);

  Errno errno;
  char errorBuf[errorMsgSize];
  switchStatus status;
  
  if (start)
    status = SWITCH_ON;
  else
    status = SWITCH_OFF;

  if ((errno = dmRequest->write(status, &t)) != SUCCESS)
  {
    sprintDmError(errno, "SuctionSamplerIndex::startStop()", errorBuf);
    theErrorDialog->postAndWait(errorBuf);
  }
}


void SuctionSamplerIndex::callback(Widget w,
				   XtPointer clientData,
				   XtPointer callData)
{
  SuctionSamplerIndex *obj = (SuctionSamplerIndex *)clientData;
  
  if (w == obj->_autoIndex)
  {
    XmToggleButtonCallbackStruct *cbs = 
      (XmToggleButtonCallbackStruct *)callData;

    MBool set;
    
    if (cbs->set)
      set = TRUE;
    else
      set = FALSE;
    
    obj->toggleAuto(set);
  }
  else if (w == obj->_fwdIndex || w == obj->_revIndex)
  {
    XmArrowButtonCallbackStruct *cbs = 
      (XmArrowButtonCallbackStruct *)callData;

    MBool start;
    
    if (cbs->reason == XmCR_ARM)
      start = TRUE;
    else
      start = FALSE;
    
    obj->startStop(start, w);
  }
  else if (w == obj->_definePosition) {
    obj->_setPositionDialog->setTitle("setPositionTitle");
    obj->_setPositionDialog->post(NULL,
				  &SetPositionDialog::applyCallback,
				  NULL,
				  NULL,
				  (XtPointer )obj->_setPositionDialog);
  }
}


SuctionSamplerPump::SuctionSamplerPump(const char *name,
				       Widget parent,
				       DmInt16Object *dmSpeed,
				       DmEnumObject *dmDirection)
  : DmGuiObject(name, parent)
{
  // Note: orientation is specified in resource file
  _baseWidget =
    XtVaCreateWidget(name, xmRowColumnWidgetClass, parent, NULL);

  installDestroyHandler();
  
  Widget mgr, mgr2;

  mgr = _baseWidget;

  Widget label;
  
  mgr2 = 
    XtVaCreateWidget("mgr2", xmRowColumnWidgetClass, mgr, 
		     XmNorientation, XmVERTICAL, NULL);
  
  XtVaCreateManagedWidget("pumpLabel", xmLabelWidgetClass, mgr2, NULL);
			  
  XtManageChild(mgr2);
  
  mgr2 = 
    XtVaCreateWidget("mgr2", xmRowColumnWidgetClass, mgr, 
		     XmNorientation, XmVERTICAL, NULL);

  _speed = XtVaCreateManagedWidget("speed", xmScaleWidgetClass, mgr2,
				   XmNorientation, XmHORIZONTAL,
				   XmNminimum, 0,
				   XmNmaximum, 100,
				   XmNvalue, 0,
				   XmNshowValue, True,
				   NULL);

  XtAddCallback(_speed, XmNdragCallback, 
		&SuctionSamplerPump::speedCallback,
		(XtPointer )this);

  XtAddCallback(_speed, XmNvalueChangedCallback, 
		&SuctionSamplerPump::speedCallback,
		(XtPointer )this);
  
  XtVaCreateManagedWidget("speedLabel", xmLabelWidgetClass, mgr2, NULL);
  
  XtManageChild(mgr2);
  
  Widget toggleMgr = 
    XtVaCreateWidget("toggleMgr", xmRowColumnWidgetClass, mgr,
		     XmNorientation, XmHORIZONTAL,
		     XmNradioBehavior, True,
		     XmNradioAlwaysOne, True,
		     NULL);

  _stop = XtVaCreateManagedWidget("stop", xmToggleButtonWidgetClass, toggleMgr, NULL); 
  _fwd = XtVaCreateManagedWidget("fwd", xmToggleButtonWidgetClass, toggleMgr, NULL); 
  _rev = XtVaCreateManagedWidget("rev", xmToggleButtonWidgetClass, toggleMgr, NULL); 

  XtAddCallback(_stop, XmNarmCallback, &SuctionSamplerPump::directionCallback,
		(XtPointer )this);

  XtAddCallback(_fwd, XmNarmCallback, &SuctionSamplerPump::directionCallback,
		(XtPointer )this);

  XtAddCallback(_rev, XmNarmCallback, &SuctionSamplerPump::directionCallback,
		(XtPointer )this);
  
  XtManageChild(toggleMgr);
  
  XtManageChild(mgr);

  _dmSpeed = dmSpeed;
  _dmDirection = dmDirection;
  
  if (addDmObject(_dmSpeed, DmConsumeFlag | DmProvideFlag) == -1)
    return;

  if (addDmObject(_dmDirection, DmConsumeFlag | DmProvideFlag) == -1)
    return;
  
  if (setConsumePeriod(Aperiodic) == -1)
    return;

  _draggingSpeed = False;
  
  updateGui();
}


SuctionSamplerPump::~SuctionSamplerPump()
{
}


void SuctionSamplerPump::updateGui(DM_Item handle)
{
  DM_Time t;
  Errno errno;
  char errorBuf[errorMsgSize];
  Int16 speed;
  
  if (!_draggingSpeed)
  {
    if ((errno = _dmSpeed->read(&speed, &t)) != SUCCESS)
    {
      sprintDmError(errno, "SuctionSamplerPump::updateGui(), reading speed", errorBuf);
      setError(errorBuf);
      return;
    }

    XtVaSetValues(_speed, XmNvalue, speed, NULL);
  }

  Int32 direction;
  
  if ((errno = _dmDirection->read(&direction, &t)) != SUCCESS)
  {
    sprintDmError(errno, "SuctionSamplerPump::updateGui(), reading direction",
		  errorBuf);

    setError(errorBuf);
    return;
  }

  switch (direction)
  {
    case PumpStop:
    XtVaSetValues(_stop, XmNset, True, NULL);
    XtVaSetValues(_fwd, XmNset, False, NULL);
    XtVaSetValues(_rev, XmNset, False, NULL);
    XtSetSensitive(_fwd, True);
    XtSetSensitive(_rev, True);
    break;
    
    case PumpForward:
    XtVaSetValues(_fwd, XmNset, True, NULL);
    XtVaSetValues(_stop, XmNset, False, NULL);
    XtVaSetValues(_rev, XmNset, False, NULL);
    XtSetSensitive(_fwd, True);
    XtSetSensitive(_rev, False);
    break;

    case PumpReverse:
    XtVaSetValues(_rev, XmNset, True, NULL);
    XtVaSetValues(_fwd, XmNset, False, NULL);
    XtVaSetValues(_stop, XmNset, False, NULL);
    XtSetSensitive(_fwd, False);
    XtSetSensitive(_rev, True);
    break;      
    
    default:
    sprintf(errorBuf, 
	    "SuctionSamplerPump::udpateGui(), unknown direction: %d", direction); 

    setError(errorBuf);
    return;
  }
}


void SuctionSamplerPump::setSensitive(Boolean sensitive)
{
  XtSetSensitive(_baseWidget, sensitive);
}


void SuctionSamplerPump::setSpeed(int value)
{
  DM_Time t;
  gettimeofday(&t, NULL);
  
  Int16 speed = (Int16 )value;
  Errno errno;
  char errorBuf[errorMsgSize];
  
  if ((errno = _dmSpeed->write(speed, &t)) != SUCCESS)
  {
    sprintDmError(errno, "SuctionSamplerPump::setSpeed()", errorBuf);
    fprintf(stderr, "%s\n", errorBuf);
  }
}


void SuctionSamplerPump::setDirection(PumpDirection direction)
{
  DM_Time t;
  gettimeofday(&t, NULL);
  Errno errno;
  char errorBuf[errorMsgSize];
  Int32 value = direction;
  
  if ((errno = _dmDirection->write(value, &t)) != SUCCESS)
  {
    sprintDmError(errno, "SuctionSamplerPump::setDirection()", errorBuf);
    fprintf(stderr, "%s\n", errorBuf);
  }
}



void SuctionSamplerPump::speedCallback(Widget w,
				       XtPointer clientData,
				       XtPointer callData)
{
  SuctionSamplerPump *obj = (SuctionSamplerPump *)clientData;
  
  XmScaleCallbackStruct *cbs = (XmScaleCallbackStruct *)callData;

  if (cbs->reason == XmCR_DRAG)
    obj->_draggingSpeed = True;
  else
    obj->_draggingSpeed = False;

  obj->setSpeed(cbs->value);
}


void SuctionSamplerPump::directionCallback(Widget w,
					   XtPointer clientData,
					   XtPointer callData)
{

  SuctionSamplerPump *obj = (SuctionSamplerPump *)clientData;

  PumpDirection direction;
  if (w == obj->_stop)
    direction = PumpStop;
  else if (w == obj->_fwd)
    direction = PumpForward;
  else if (w == obj->_rev)
    direction = PumpReverse;
  else
  {
    fprintf(stderr, "SuctionSamplerPump::directionCallback() - unknown direction!\n");
    return;
  }
    
  obj->setDirection(direction);
}


SuctionSampler::SuctionSampler(const char *name, Widget parent, 
			       const char *appName,
			       DmBooleanObject *dmAutoIndex,
			       DmBooleanObject *dmIndexSense,
			       DmNat16Object *dmJarNumber,
			       DmInt16Object *dmSpeed,
			       DmEnumObject *dmDirection)
{
  _baseWidget =
    XtVaCreateWidget(name, xmRowColumnWidgetClass, parent,
		     XmNorientation, XmVERTICAL,
		     NULL);
  
  installDestroyHandler();
  
  Widget label = 
    XtVaCreateManagedWidget("suctionSamplerLabel", xmLabelWidgetClass, 
			    _baseWidget, NULL);

  index = new SuctionSamplerIndex("index", _baseWidget, appName, 
				  dmIndexSense, dmAutoIndex, dmJarNumber, 
				  500);
  index->show();

  Widget mgr = 
    XtVaCreateWidget("mgr", xmRowColumnWidgetClass, _baseWidget, 
		     XmNorientation, XmHORIZONTAL, NULL);
  
  pump = new SuctionSamplerPump("pump", mgr, dmSpeed, dmDirection);
  pump->show();

  Widget mgr2 = 
    XtVaCreateWidget("mgr", xmRowColumnWidgetClass, mgr,
		     XmNorientation, XmVERTICAL,
		     NULL);
  
  XtVaCreateManagedWidget("nannyLabel", xmLabelWidgetClass, mgr2, NULL);
  
#ifdef ZILCH
  // Setup nanny pixmap
  XpmAttributes xpmAttributes;

  xpmAttributes.closeness = 40000;
  xpmAttributes.valuemask = XpmSize | XpmCloseness;
  
  Pixmap mask;
  
  if (XpmCreatePixmapFromData(theApplication->display(), 
			      DefaultRootWindow(theApplication->display()),
			      netNanny_xpm,
			      &_nannyPixmap,
			      &mask,
			      &xpmAttributes) != XpmSuccess)
  {
    _nannyPixmap = NULL;
  }

  XtVaCreateManagedWidget("pixmap", xmLabelWidgetClass, mgr2,
			  XmNlabelType, XmPIXMAP,
			  XmNlabelPixmap, _nannyPixmap,
			  NULL);

#else
  _nannyPixmap = NULL;  
#endif

  XtManageChild(mgr2);
  XtManageChild(mgr);
}

  
SuctionSampler::~SuctionSampler()
{
  delete index;
  delete pump;

  if (_nannyPixmap)
    XFreePixmap(theApplication->display(), _nannyPixmap);

}
