/*
 *    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 "Slider.h"
#include "Gauge.h"
#include "Notifier.h"
#include "TTPattern.h"
#include "TTSession.h"

extern void slider_notify (UIObject *);
extern void receive_tt_message (UIObject *, int);

void main (int argc, char **argv)
{
  BaseWindow window;
  window.initUI (argc, argv);
  window.setWidth (286);
  window.setHeight (69);
  window.setLabel ("TT C++ API 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);

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

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

  int       mark = tt_mark ();
  Tt_status status;
  TTSession session (TT_X_SESSION, NULL, status);

  if (status == TT_OK) {
    TTPattern pattern (TT_OBSERVE,
		       "ExampleMessage", 
		       TT_SESSION,
		       status);

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

    if (status == TT_OK) {
      slider.setObjectData ("TT_SESSION", (Generic *)&session);

      gauge.setObjectData ("TT_SESSION", (Generic *)&session);
      gauge.addInputFunction (receive_tt_message, session.getFD (status));

      Notifier notifier;
      notifier.start ();
    }
  }

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

  tt_release (mark);
  exit (0);
}
