static char AlarmText_id[] = "$Header: /usr/tiburon/unix/gui/tmacs/RCS/AlarmText.cc,v 1.7 1997/10/03 09:25:29 oreilly Exp $";


/*
$Log: AlarmText.cc,v $
Revision 1.7  1997/10/03 09:25:29  oreilly
*** empty log message ***

Revision 1.6  97/08/12  14:16:28  14:16:28  oreilly (Thomas C. O'Reilly)
Popup menu to show alarm definition

Revision 1.5  97/05/30  15:46:37  15:46:37  oreilly (Thomas C. O'Reilly)
*** empty log message ***

Revision 1.4  97/05/06  16:23:14  16:23:14  oreilly (Thomas C. O'Reilly)
Foreground and background are class attributes. Supply default format.

Revision 1.3  97/04/30  19:28:56  19:28:56  oreilly (Thomas C. O'Reilly)
Allow for NULL alarm passed to constructor

Revision 1.2  97/03/20  12:28:38  12:28:38  oreilly (Thomas C. O'Reilly)
*** empty log message ***

Revision 1.1  96/07/22  10:41:41  10:41:41  oreilly (Thomas C. O'Reilly)
First external release

*/
#include <Xm/RowColumn.h>
#include <Xm/Label.h>
#include "AlarmText.h"
#include "TiburonApp.h"
#include "AlarmedObject.h"

const char *const AlarmText::alarmUpdateCallback = "alarmUpdateCallback";

AlarmText::AlarmText(const char *name, Widget parent, Alarm *alarm, 
		     char *format)
  : DmGuiObject(name, parent)
{
  _baseWidget = XtCreateWidget(name, xmRowColumnWidgetClass, parent, 
			       NULL, 0);
  
  installDestroyHandler();
  
  XtAddEventHandler(_baseWidget,
		    ButtonPressMask,
		    False, 
		    (XtEventHandler )&DmGuiObject::eventHandler, 
		    (XtPointer )this);

  _valueText = XtVaCreateManagedWidget("value", xmLabelWidgetClass,
				       _baseWidget, NULL);

  XtAddEventHandler(_valueText,
		    ButtonPressMask,
		    False, 
		    (XtEventHandler )&DmGuiObject::eventHandler, 
		    (XtPointer )this);
  
  if (alarm)
  {
    if (addDmObject(alarm->dmObject(), DmConsumeFlag) == -1)
      return;

    if (setConsumePeriod(Aperiodic) == -1) 
      return;
  }
  else
  {
    /* NULL alarm specified */
    XmString xmstr = XmStringCreateSimple((char *)Alarm::UnkValueMnem);
    XtVaSetValues(_valueText, XmNlabelString, xmstr, NULL);
    XmStringFree(xmstr);
  }
  
  _alarm = alarm;
  _format = strdup(format);

  _priority = NoPriority;
  _nAlarmChanges = 0;
  _value = 999;
  
  // Force update
  updateGui();
}



void AlarmText::updateGui(DM_Item handle)
{
  if (!_alarm)
    return;
  
  _alarm->update();

  int value = _alarm->value();
  
  Priority priority = _alarm->priority(value);

  if (priority != _priority || !_nAlarmChanges || value != _value)
  {
    char buf[100];

    sprintf(buf, 
	    _format, Alarm::valueMnemonic(_alarm->type(), _alarm->value()));

    XmString xmstr = XmStringCreateSimple(buf);
  
    TiburonApp *app = (TiburonApp *)theApplication;

    app->priorityColors(priority, &_foreground, &_background);

    XtVaSetValues(_valueText, 
		  XmNlabelString, xmstr,
		  XmNforeground, _foreground, XmNbackground, _background, 
		  NULL);

    XmStringFree(xmstr);

    _priority = priority;
    _value = value;
    _nAlarmChanges++;

    callCallbacks(alarmUpdateCallback, (void *)_priority);
  }
}


Priority AlarmText::priority()
{
  return _priority;
}


void AlarmText::postOptions(XEvent *event)
{
  if (_alarm)
    AlarmedObject::addMenuItem(_popupMenu, _alarm);

  _popupMenu->show(event);
}


