static char TextDmGui_id[] = "$Header: /usr/tiburon/unix/gui/tmacs/RCS/TextDmGui.cc,v 1.3 1997/03/20 12:30:59 oreilly Exp $";


/*
$Log: TextDmGui.cc,v $
Revision 1.3  1997/03/20 12:30:59  oreilly
..

Revision 1.2  96/07/22  10:42:13  10:42:13  oreilly (Thomas C. O'Reilly)
First external release

*/
#include <time.h>
#include <stdlib.h>
#include <Xm/Form.h>
#include <Xm/PushB.h>
#include "Vk/VkInfoDialog.h"
#include "TextDmGui.h"
#include "DmErrno.h"

TextDmGui::TextDmGui(const char *name, Widget parent) :
  DmGuiObject(name, parent)
{
  _baseWidget = XtCreateWidget(name, xmRowColumnWidgetClass, parent, NULL, 0);

  installDestroyHandler();
  
  _nameLabel = XtVaCreateManagedWidget("name", xmLabelWidgetClass, 
				       _baseWidget, 
				       NULL);
  
  _currentValue = new LabeledText(_baseWidget, "current", XmHORIZONTAL,
				  "current");
  _currentValue->setEditable(False);

  Widget form = XtVaCreateWidget("form", xmFormWidgetClass, _baseWidget, NULL);

  _setValue = new LabeledText(form, "new", XmHORIZONTAL, "new");  

  _okButton = 
    XtVaCreateManagedWidget("ok", 
			    xmPushButtonWidgetClass, form,
			    XmNtopAttachment, XmATTACH_FORM,
			    XmNleftAttachment, XmATTACH_WIDGET,
			    XmNleftWidget, _setValue->baseWidget(),
			    NULL);

  XtAddCallback(_okButton, XmNactivateCallback,
		&TextDmGui::setCallback, (XtPointer )this);
  
  XtManageChild(form);

  _dmObject = NULL;
}



TextDmGui::~TextDmGui()
{
}


void TextDmGui::disableUserInput()
{
  _setValue->setEditable(False);
  XtSetSensitive(_okButton, False);
}

void TextDmGui::enableUserInput()
{
  _setValue->setEditable(True);
  XtSetSensitive(_okButton, True);
}


void TextDmGui::updateGui(DM_Item handle)
{
  MBool debug = FALSE;
  dprintf(stderr, "TextDmGui::updateGui()\n");
  /* Read value from Dm and display it */
  Int16 val;
  DM_Time t;
  Errno errno;
  if ((errno = _dmObject->read(&val, &t)) != SUCCESS)
  {  
    char buf[512];
    sprintDmError(errno, "TextDmGui::updateGui()", buf);
    fprintf(stderr, buf);
    return;
  }
  dprintf(stderr, "val=%d time=%d\n", val, t.tv_sec);
  
  char buf[100];
  sprintf(buf, "%d", val);
  _currentValue->set(buf);
  
}


int TextDmGui::createDmObject(const char *name, char *errorBuf)
{
  if (_dmObject != NULL)
  {
    deleteDmObject(_dmObject);
  }

  _dmObject = new DmInt16Object(name, 1);
  if (_dmObject->error())
  {
    _dmObject->errorMsg(errorBuf);
    return -1;
  }

  if (addDmObject(_dmObject, DmProvideFlag | DmConsumeFlag) == -1)
    return -1;

  XmString xmstr = XmStringCreateSimple((char *)name);
  XtVaSetValues(_nameLabel, XmNlabelString, xmstr, NULL);
  XmStringFree(xmstr);

  return 0;
}



void TextDmGui::setCallback(Widget w, XtPointer clientData, 
			    XtPointer callData)
{
  TextDmGui *obj = (TextDmGui *)clientData;
  obj->writeValue();
}


int TextDmGui::writeValue()
{
  char *ptr = _setValue->get();
  Int16 val = atoi(ptr);
  struct timeval tval;
  struct timezone tzone;
  if (gettimeofday(&tval, &tzone) == -1)
  {
    perror("TextDmGui::writeValue() - gettimeofday() failed");
    exit(1);
  }
  Errno errno;
  if ((errno = _dmObject->write(val, &tval)) != SUCCESS)
  {
    char buf[512];
    sprintDmError(errno, "TextDmGui::writeValue()", buf);
    theInfoDialog->postAndWait(buf);
    return -1;
  }
  return 0;
}
