static char DangerButton_id[] = "$Header: /usr/tiburon/unix/gui/tmacs/RCS/DangerButton.cc,v 1.2 1997/03/20 12:28:58 oreilly Exp $";


/*
$Log: DangerButton.cc,v $
Revision 1.2  1997/03/20 12:28:58  oreilly
*** empty log message ***

Revision 1.1  96/07/22  10:41:50  10:41:50  oreilly (Thomas C. O'Reilly)
First external release

*/
#include <stdio.h>
#include <Xm/RowColumn.h>
#include <Xm/Form.h>
#include <Xm/ToggleB.h>
#include <Xm/ToggleBG.h>
#include <Xm/PushB.h>
#include <Xm/Separator.h>
#include <Xm/Label.h>
#include <Vk/VkApp.h> 
#include "DangerButton.h"
#define dprintf if (debug) fprintf


XtResource DangerButton::_resources[] = 
{
  {  
    "inactiveBackground",
    XtCBackground, 
    XtRPixel,
    sizeof(Pixel),
    XtOffset(DangerButton *, _inactiveBackground),
    XmRString,
    "LightGray"
  },

  {  
    "inactiveForeground",
    XtCForeground, 
    XtRPixel,
    sizeof(Pixel),
    XtOffset(DangerButton *, _inactiveForeground),
    XmRString,
    "Black"
  },

  {  
    "activeBackground",
    XtCBackground, 
    XtRPixel,
    sizeof(Pixel),
    XtOffset(DangerButton *, _activeBackground),
    XmRString,
    "Green"
  },

  {  
    "activeForeground",
    XtCForeground, 
    XtRPixel,
    sizeof(Pixel),
    XtOffset(DangerButton *, _activeForeground),
    XmRString,
    "Black"
  },

  {  
    "armedBackground",
    XtCBackground, 
    XtRPixel,
    sizeof(Pixel),
    XtOffset(DangerButton *, _armedBackground),
    XmRString,
    "Red"
  },

  {  
    "armedForeground",
    XtCForeground, 
    XtRPixel,
    sizeof(Pixel),
    XtOffset(DangerButton *, _armedForeground),
    XmRString,
    "White"
  }
};


String DangerButton::_defaultResources[] = 
{
  "*deactivate.labelString: Deactivate",
  "*activate.labelString: Activate",
  "*arm.labelString: Arm",
  NULL
};


DangerButton::DangerButton(const char *name, Widget parent) :
  VkComponent(name)
{
  setDefaultResources(parent, _defaultResources);
  
  _baseWidget = XtCreateWidget(_name, xmFormWidgetClass, parent, NULL, 0);
  installDestroyHandler();

  getResources(_resources, XtNumber(_resources));

  Widget separator = 
    XtVaCreateManagedWidget("separator", 
			    xmSeparatorWidgetClass, _baseWidget,
			    XmNtopAttachment, XmATTACH_FORM,
			    XmNrightAttachment, XmATTACH_FORM,
			    XmNleftAttachment, XmATTACH_FORM,
			    NULL);

  separator = 
    XtVaCreateManagedWidget("separator", 
			    xmSeparatorWidgetClass, _baseWidget,
			    XmNbottomAttachment, XmATTACH_FORM,
			    XmNrightAttachment, XmATTACH_FORM,
			    XmNleftAttachment, XmATTACH_FORM,
			    NULL);

  separator = 
    XtVaCreateManagedWidget("separator", 
			    xmSeparatorWidgetClass, _baseWidget,
			    XmNorientation, XmVERTICAL,
			    XmNtopAttachment, XmATTACH_FORM,
			    XmNbottomAttachment, XmATTACH_FORM,
			    XmNleftAttachment, XmATTACH_FORM,
			    NULL);

  separator = 
    XtVaCreateManagedWidget("separator", 
			    xmSeparatorWidgetClass, _baseWidget,
			    XmNorientation, XmVERTICAL,
			    XmNtopAttachment, XmATTACH_FORM,
			    XmNbottomAttachment, XmATTACH_FORM,
			    XmNrightAttachment, XmATTACH_FORM,
			    NULL);


  Widget title = XtVaCreateManagedWidget("title", xmLabelWidgetClass, 
					 _baseWidget, 
					 XmNtopAttachment, XmATTACH_FORM,
					 NULL);

  Widget mgr = _baseWidget;
  
  Widget toggleBox = XtVaCreateWidget("toggleBox", xmRowColumnWidgetClass, mgr,
				      XmNisHomogeneous, True,
				      XmNentryClass, xmToggleButtonGadgetClass,
				      XmNradioBehavior, True,
				      XmNtopAttachment, XmATTACH_WIDGET,
				      XmNtopWidget, title,
				      NULL);
  

  _deactivate = XtVaCreateManagedWidget("deactivate", 
					xmToggleButtonWidgetClass,
					toggleBox, NULL);

  XtAddCallback(_deactivate, XmNvalueChangedCallback, 
		&DangerButton::toggleCallback, this);

  _activate = XtVaCreateManagedWidget("activate", xmToggleButtonWidgetClass,
				 toggleBox, NULL);

  XtAddCallback(_activate, XmNvalueChangedCallback, 
		&DangerButton::toggleCallback, this);

  _arm = XtVaCreateManagedWidget("arm", xmToggleButtonWidgetClass,
				 toggleBox, NULL);

  XtAddCallback(_arm, XmNvalueChangedCallback, 
		&DangerButton::toggleCallback, this);


  _actionButton = XtVaCreateManagedWidget("actionButton",
					  xmPushButtonWidgetClass,
					  mgr,
					  XmNtopAttachment, XmATTACH_WIDGET,
					  XmNtopWidget, toggleBox,
					  XmNleftAttachment, XmATTACH_FORM,
					  XmNrightAttachment, XmATTACH_FORM,
					  NULL);

  XtAddCallback(_actionButton, XmNactivateCallback,
		&DangerButton::actionButtonCallback, (XtPointer )this);
  
  XtManageChild(toggleBox);
  XtManageChild(mgr);
  XtManageChild(_baseWidget);

  _flashPeriod = 500;

  // Set initial state
  _flashTimer = 0;  
  deactivate();
  XtVaSetValues(_deactivate, XmNset, True, NULL);
  XtVaSetValues(_activate, XmNset, False, NULL);
  XtVaSetValues(_arm, XmNset, False, NULL);  

}


void DangerButton::deactivate()
{
  MBool debug = FALSE;
  XtVaSetValues(_deactivate, XmNset, True, NULL);  
  XtVaSetValues(_activate, XmNset, False, NULL);  
  XtVaSetValues(_arm, XmNset, False, NULL);  

  XtSetSensitive(_arm, False);
  XtSetSensitive(_actionButton, False);
  XtVaSetValues(_actionButton, XmNbackground, _inactiveBackground, 
		XmNforeground, _inactiveForeground, NULL);
  
  if (_flashTimer)
  {
    dprintf(stderr, "DangerButton::deactivate() - remove timer %d\n",
	    _flashTimer);
    XtRemoveTimeOut(_flashTimer);
    _flashTimer = 0;
  }
}


void DangerButton::activate()
{
  MBool debug = FALSE;
  XtVaSetValues(_deactivate, XmNset, False, NULL);
  XtVaSetValues(_activate, XmNset, True, NULL);
  XtVaSetValues(_arm, XmNset, False, NULL);

  XtSetSensitive(_arm, True);
  XtSetSensitive(_actionButton, False);
  XtVaSetValues(_actionButton, XmNbackground, _activeBackground, 
		XmNforeground, _activeForeground, NULL);

  if (_flashTimer)
  {
    dprintf(stderr, "DangerButton::activate() - remove timer %d\n", 
	    _flashTimer);
    XtRemoveTimeOut(_flashTimer);
    _flashTimer = 0;
  }
}


void DangerButton::arm()
{
  MBool debug = FALSE;
  XtVaSetValues(_deactivate, XmNset, False, NULL);
  XtVaSetValues(_activate, XmNset, False, NULL);
  XtVaSetValues(_arm, XmNset, True, NULL);
  XtSetSensitive(_actionButton, True);
  XtVaSetValues(_actionButton, XmNbackground, _armedBackground, 
		XmNforeground, _armedForeground, NULL);

  _phase = 0;

  _flashTimer = XtAppAddTimeOut(theApplication->appContext(), _flashPeriod,
				&DangerButton::timerCallback, 
				(XtPointer )this);

  dprintf(stderr, "DangerButton::arm() - installed timer %d\n", _flashTimer);
}


void DangerButton::toggle(Widget w)
{
  if (w == _deactivate)
    deactivate();
  else if (w == _activate)
    activate();
  else if (w == _arm)
    arm();
}


void DangerButton::actionButtonPressed()
{
  deactivate();
}


void DangerButton::actionButtonCallback(Widget w, XtPointer clientData,
					XtPointer callData)
{
  DangerButton *obj = (DangerButton *)clientData;
  obj->actionButtonPressed();
}


void DangerButton::toggleCallback(Widget w, XtPointer clientData,
				  XtPointer callData)
{
  XmToggleButtonCallbackStruct *state = 
    (XmToggleButtonCallbackStruct *)callData;
  
  if (!state->set)
    return;
  
  DangerButton *obj = (DangerButton *)clientData;
  obj->toggle(w);
}


void DangerButton::timerCallback(XtPointer clientData, XtIntervalId *id)
{
  MBool debug = FALSE;
  
  DangerButton *obj = (DangerButton *)clientData; 
  obj->_flashTimer = XtAppAddTimeOut(theApplication->appContext(), 
				     obj->_flashPeriod, 
				     &DangerButton::timerCallback, 
				     (XtPointer )obj);

  if (obj->_phase)
  {
    XtVaSetValues(obj->_actionButton, 
		  XmNbackground, obj->_activeBackground, 
		  XmNforeground, obj->_activeForeground, NULL);

    obj->_phase = 0;
  }
  else
  {
    XtVaSetValues(obj->_actionButton, 
		  XmNbackground, obj->_armedBackground, 
		  XmNforeground, obj->_armedForeground, NULL);

    obj->_phase = 1;
  }
  
}
