/* 
 *    SCCS ID: %Z%%M% %I% %E% SST
 *
 *    Copyright (c) 1992-1993 Mark Soloway.  All rights reserved.
 *    See SST_LEGAL_NOTICE file for terms of the license.
 */ 
 
#include "TreeDisplayer.h"
#include "TTMessage.h"

#include "BaseWindow.h"
#include "Image.h"
#include "ComponentDisplay.h"
#include "Button.h"
#include "PopUpWindow.h"
#include "AlphanumericInput.h"
#include "CanvasShellDisplay.h"
#include "Scroller.h"
#include "Notifier.h"
#include "ObjectTree.h"

#include "tree_editor_ui.h"

extern void tree_editor_bw_add_node_notify_handler (UIObject *);
extern void delete_node_handler (UIObject *);
extern void apply_node_info (UIObject *);
extern void cancel_node_info (UIObject *);
extern void done_handler (UIObject *);

static done;

void main (int argc, char **argv)
{
  BaseWindow bw;
  bw.initUI (argc, argv);
  bw.setWidth (657);
  bw.setHeight (427);
  bw.show (TRUE);
  bw.setLabel ("Tree Editor");
  bw.setDisplayFooter (TRUE);
  bw.setResizable (TRUE);
  bw.setDoneHandler (done_handler);

  static unsigned short icon_bits [] = {
#include "tree_editor.icon"
  };
  Image icon (64, 64, icon_bits);
  bw.setIcon (icon);
  bw.setIconMask (icon);

  if (argc < 2) {
    printf ("Usage: tree_editor <filename>\n");
    exit (0);
  }

  ComponentDisplay cd (TRUE);
  cd.setX (0);
  cd.setY (0);
  cd.setExtendToEdge (WIDTH);
  cd.setHeight (37);
  cd.setDisplayBorders (FALSE);
  bw.addDisplay (cd);

  Button add_node;
  add_node.setX (10);
  add_node.setY (10);
  add_node.setLabel ("Add Node...");
  add_node.setNotifyHandler (tree_editor_bw_add_node_notify_handler);
  cd.addComponent (add_node);

  Button delete_node;
  delete_node.setX (110);
  delete_node.setY (10);
  delete_node.setLabel ("Delete Node");
  delete_node.setNotifyHandler (delete_node_handler);
  cd.addComponent (delete_node);

  CanvasShellDisplay cs;
  cs.setX (0);
  cs.setY (37);
  cs.setPaintWidth (1000);
  cs.setPaintHeight (1000);
  cs.setAutoExpand (TRUE);
  bw.addDisplay (cs);

  Scroller s1 (HORIZONTAL);
  cs.addComponent (s1);

  Scroller s2 (VERTICAL);
  cs.addComponent (s2);

  PopUpWindow node_info;
  node_info.setWidth (323);
  node_info.setHeight (124);
  node_info.show (FALSE);
  node_info.setDisplayFooter (TRUE);
  node_info.setResizable (TRUE);
  node_info.setPinned (FALSE);
  bw.addChildWindow (node_info);

  ComponentDisplay controls1 (TRUE);
  controls1.setX (0);
  controls1.setY (0);
  controls1.setExtendToEdge (WIDTH);
  controls1.setExtendToEdge (HEIGHT);
  controls1.setDisplayBorders (FALSE);
  node_info.addDisplay (controls1);

  AlphanumericInput image_file;
  image_file.setX (20);
  image_file.setY (20);
  image_file.setInputDisplayLength (25);
  image_file.setMaxInputLength (256);
  image_file.setLabel ("Image File:");
  image_file.setLabelPosition (LEFT);
  image_file.setReadOnly (FALSE);
  controls1.addComponent (image_file);

  AlphanumericInput text;
  text.setX (62);
  text.setY (50);
  text.setInputDisplayLength (25);
  text.setMaxInputLength (25);
  text.setLabel ("Text:");
  text.setLabelPosition (LEFT);
  text.setReadOnly (FALSE);
  controls1.addComponent (text);

  Button apply;
  apply.setX (90);
  apply.setY (90);
  apply.setLabel ("Apply");
  apply.setNotifyHandler (apply_node_info);
  controls1.addComponent (apply);

  Button cancel;
  cancel.setX (160);
  cancel.setY (90);
  cancel.setLabel ("Cancel");
  cancel.setNotifyHandler (cancel_node_info);
  controls1.addComponent (cancel);

  ObjectTree tree;
  tree.setWidth (657);
  tree.setHeight (427);
  cs.addComponent (tree);

  int           mark;
  Tt_status     status;
  TreeDisplayer tree_displayer (argv [1], cs, tree, status);

  add_node.setObjectData (NODE_INFO_ADDRESS, node_info);
  apply.setObjectData (NODE_INFO_ADDRESS, node_info);
  cancel.setObjectData (NODE_INFO_ADDRESS, node_info);

  delete_node.setObjectData (TREE_DISPLAYER_ADDRESS,
			     (Generic *)&tree_displayer);

  node_info.setObjectData (IMAGE_FILE_ADDRESS, image_file);
  node_info.setObjectData (TEXT_ADDRESS, text);

  node_info.setObjectData (TREE_DISPLAYER_ADDRESS,
			   (Generic *)&tree_displayer);

  Notifier notifier;
  notifier.dispatchEvent ();

  done = FALSE;
  tree_displayer.setTimeOut (100);
  for (; (status < TT_WRN_LAST) && (!done);) {
    // Loop 10 times to make sure we are handling user interections
    // reasonably...
    for (int i=0; i < 10; i++)
      notifier.dispatchEvent ();

    mark = tt_mark ();
    TTMessage *message = tree_displayer.waitForMessage (status);
    if (message) delete message;
    tt_release (mark);
  }

  if (status > TT_WRN_LAST) {
    printf ("Error processing ToolTalk message: %d\n", status);
  }

  exit (0);
}

void done_handler (UIObject *object)
{
  if (((BaseWindow *)object)->getDestroyStatus () == DESTROY_CLEANUP) {
    done = TRUE;
  }
}
