/*
 *    SCCS ID: %Z%%M% %I% %E% SST
 *
 *    Copyright (c) 1992 Mark Soloway.  All rights reserved.
 *    See SST_LEGAL_NOTICE file for terms of the license.
 */

#include "BaseWindow.h"
#include "ComponentDisplay.h"
#include "AlphanumericInput.h"
#include "Slider.h"
#include "Gauge.h"
#include "Notifier.h"
#include "TTC.h"
#include "TTGaugePattern.h"

void receive_tt_message (UIObject *, int);

extern void scope_handler (UIObject *);
extern void send_target_handler (UIObject *, UIObject *);

void main (int argc, char **argv)
{
  BaseWindow window;
  window.initUI (argc, argv);
  window.setWidth (295);
  window.setHeight (108);
  window.setLabel ("File Scope Example");
  window.show (TRUE);
  window.setDisplayFooter (TRUE);
  window.setResizable (TRUE);

  ComponentDisplay controls (TRUE);
  controls.setX (0);
  controls.setY (0);
  controls.setExtendToEdge (WIDTH);
  controls.setExtendToEdge (HEIGHT);
  controls.setDisplayBorders (FALSE);
  window.addDisplay (controls);

  AlphanumericInput scope;
  scope.setX (23);
  scope.setY (15);
  scope.setInputDisplayLength (25);
  scope.setMaxInputLength (80);
  scope.setLabel ("Scope:");
  scope.setLabelPosition (LEFT);
  scope.setReadOnly (FALSE);
  scope.setNotifyHandler (scope_handler);
  controls.addComponent (scope);

  Slider send;
  send.setX (40);
  send.setY (40);
  send.setSliderWidth (100);
  send.setTicks (0);
  send.setLabel ("Send:");
  send.setLabelPosition (LEFT);
  send.setOrientation (HORIZONTAL);
  send.setDisplayEndBoxes (FALSE);
  send.setDisplayRange (TRUE);
  send.setDisplayValue (TRUE);
  send.setMinValue (0);
  send.setMaxValue (100);
  send.setValue (0);
  controls.addComponent (send);

  Gauge receive;
  receive.setX (10);
  receive.setY (70);
  receive.setGaugeWidth (100);
  receive.setTicks (0);
  receive.setLabel ("Receive:");
  receive.setLabelPosition (LEFT);
  receive.setOrientation (HORIZONTAL);
  receive.setDisplayRange (TRUE);
  receive.setMinValue (0);
  receive.setMaxValue (100);
  receive.setValue (0);
  controls.addComponent (receive);

  int       mark = tt_mark ();
  Tt_status status;
  TTSession session (status);

  if (status == TT_OK) {
    TTGaugePattern pattern (receive, status);

    if (status == TT_OK) {
      (void)pattern.setCategory (TT_OBSERVE);
      (void)pattern.addOp ("ExampleMessage");
      status = pattern.addScope (TT_FILE);
    }

    if (status == TT_OK)
      status = session.registerPattern (pattern);

    if (status == TT_OK) {
      scope.setObjectData ("TT_SESSION", (Generic *)&session);
      send.addNotifyTarget (scope, send_target_handler);

      scope.addInputFunction (receive_tt_message, session.getFD (status));

      Notifier notifier;
      notifier.start ();
    }
  }

  if (status != TT_OK) {
    printf ("Error initializing ToolTalk: TT error %d\n", status); 
  } 

  tt_release (mark);
  exit (0);
} 

void receive_tt_message (UIObject *object, int /* fd */)
{
  TTSession *session = (TTSession *)object->getObjectData ("TT_SESSION");
  Tt_status  status;

  while (session->receiveAsynchronousMessage (status)) ;
}
