static char AlarmActionSetup_id[] = "$Header: AlarmActionSetup.cc,v 1.4 97/05/30 15:42:25 oreilly Exp $";


/*
$Log:	AlarmActionSetup.cc,v $
Revision 1.4  97/05/30  15:42:25  15:42:25  oreilly (Thomas C. O'Reilly)
Added sticky alarms

Revision 1.3  97/03/20  12:28:28  12:28:28  oreilly (Thomas C. O'Reilly)
*** empty log message ***

Revision 1.2  96/10/28  09:11:30  09:11:30  oreilly (Thomas C. O'Reilly)
*** empty log message ***

Revision 1.1  96/07/22  10:41:18  10:41:18  oreilly (Thomas C. O'Reilly)
First external release

*/
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Xm/Label.h>
#include <Xm/ToggleB.h>
#include <Xm/PushB.h>
#include <Xm/Form.h>
#include <Xm/List.h>
#include <Xm/SelectioB.h>
#include <Xm/Separator.h>
#include <Xm/RowColumn.h>
#include <Vk/VkErrorDialog.h>
#include <Vk/VkInfoDialog.h>
#include "AlarmActionSetup.h"
#include "TiburonApp.h"

#define dprintf if (debug) fprintf

ActionSetupPanel::ActionSetupPanel(const char *name, Widget parent,
				 int *alarmActionBits) 
  : VkComponent(name)
{
  MBool debug = FALSE;
  
  _baseWidget = XtVaCreateWidget(name, xmFormWidgetClass, parent,
				 XmNfractionBase, 5,
				 NULL);


  installDestroyHandler();

  Widget label = 
    XtVaCreateManagedWidget("alarmActions", xmLabelWidgetClass, _baseWidget,
			    XmNtopAttachment, XmATTACH_FORM,
			    XmNleftAttachment, XmATTACH_FORM,
			    NULL);

  TiburonApp *app = (TiburonApp *)theApplication;

  Widget actionMgr = XtVaCreateWidget("", xmRowColumnWidgetClass, _baseWidget,
				      XmNtopAttachment, XmATTACH_WIDGET,
				      XmNtopWidget, label,
				      XmNleftAttachment, XmATTACH_FORM,
				      XmNrightAttachment, XmATTACH_FORM,
				      NULL);
  
  Widget mgr;
  
  for (int p = (int )highestPriority() - 1; p <= (int )lowestPriority() - 1;
       p++)
  {
    _actionBits[p] = alarmActionBits[p];
    
    mgr = XtVaCreateWidget("", xmFormWidgetClass, actionMgr, 
			   NULL);

    char name[100];

    sprintf(name, "priority%dAction", p + 1);
    Pixel foreground, background;


    switch (p + 1)
    { 
      case 1:
      app->priority1Colors(&foreground, &background);
      break;
      
      case 2:
      app->priority2Colors(&foreground, &background);
      break;
      
      case 3:
      app->priority3Colors(&foreground, &background);
      break;
    }
    
    Widget label = 
      XtVaCreateManagedWidget(name, xmLabelWidgetClass, mgr, 
			      XmNforeground, foreground,
			      XmNbackground, background,
			      XmNleftAttachment, XmATTACH_FORM,
			      XmNtopAttachment, XmATTACH_FORM,
			      XmNtopOffset, 10,
			      NULL); 

    Widget box = XtVaCreateWidget("", xmRowColumnWidgetClass, mgr, 
				  XmNorientation, XmHORIZONTAL,
				  XmNpacking, XmPACK_TIGHT,
				  XmNnumColumns, 1,
				  XmNleftAttachment, XmATTACH_WIDGET,
				  XmNleftWidget, label,
				  XmNtopAttachment, XmATTACH_FORM,
				  NULL);

    
    for (int a = 0; a < NAlarmActions; a++)
    {
      _actionToggle[p][a] = 
	XtVaCreateManagedWidget(alarmActionName(a), xmToggleButtonWidgetClass,
				box, NULL);

      if (alarmActionBits[p] & (01 << a))
	XtVaSetValues(_actionToggle[p][a], XmNset, True, NULL);
      else
	XtVaSetValues(_actionToggle[p][a], XmNset, False, NULL);

      XtAddCallback(_actionToggle[p][a], XmNvalueChangedCallback, 
		    &ActionSetupPanel::actionToggleCallback, (XtPointer )this);
      
    }
    XtManageChild(mgr);
    XtManageChild(box);
  }

  XtManageChild(actionMgr);
  

  _ok = XtVaCreateManagedWidget("ok", xmPushButtonWidgetClass, _baseWidget,
				XmNbottomAttachment, XmATTACH_FORM,
				XmNleftAttachment, XmATTACH_POSITION,
				XmNleftPosition, 1,
				XmNrightAttachment, XmATTACH_POSITION,
				XmNrightPosition, 2,
				XmNshowAsDefault, True,
				NULL);

  _cancel = 
    XtVaCreateManagedWidget("cancel", xmPushButtonWidgetClass, _baseWidget,
			    XmNbottomAttachment, XmATTACH_FORM,
			    XmNleftAttachment, XmATTACH_POSITION,
			    XmNleftPosition, 3,
			    XmNrightAttachment, XmATTACH_POSITION,
			    XmNrightPosition, 4,
			    XmNshowAsDefault, True,
			    NULL);


  Widget separator = 
    XtVaCreateManagedWidget("", xmSeparatorWidgetClass, _baseWidget,
			    XmNleftAttachment, XmATTACH_FORM,
			    XmNrightAttachment, XmATTACH_FORM,
			    XmNbottomAttachment, XmATTACH_WIDGET,
			    XmNbottomWidget, _ok,
			    NULL);

  XtVaSetValues(actionMgr, 
		XmNbottomAttachment, XmATTACH_WIDGET,
		XmNbottomWidget, separator,
		NULL);
}


void ActionSetupPanel::setAlarmActions()
{
  for (int p = 0; p < NPriorityLevels; p++)
  {
    _actionBits[p] = 0;
    for (int a = 0; a < NAlarmActions; a++)
    {
      Boolean set;
      XtVaGetValues(_actionToggle[p][a], XmNset, &set, NULL);
      if (set)
	_actionBits[p] |= (01 << a);
      else
	_actionBits[p] &= ~(01 << a);
    }
  }
}


void ActionSetupPanel::actionToggleCallback(Widget w, XtPointer clientData,
					   XtPointer callData)
{
  ActionSetupPanel *obj = (ActionSetupPanel *)clientData;
  obj->setAlarmActions();
}



ActionSetupWindow::ActionSetupWindow(const char *name, 
					       int *alarmActionBits) : 
   FriendlyWindow(name)
{

  panel = new ActionSetupPanel("actionSetup", mainWindowWidget(), 
			      alarmActionBits);
  
  panel->show();

  addView(panel->baseWidget());

  return;
}





void ActionSetupPanel::setOkCallback(XtCallbackProc callback, 
				     XtPointer clientData)  
{
  XtAddCallback(_ok, XmNactivateCallback, callback, clientData);
}
