/*
 *    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 "Button.h"
#include "PopUpWindow.h"
#include "UIInputTextItem.h"

#include "TreeDisplayer.h"
#include "TreeNode.h"
#include "TTC.h"

#include "tree_editor_ui.h"

void tree_editor_bw_add_node_notify_handler (UIObject *object)
{
  PopUpWindow *node_info = (PopUpWindow *)
			   object->getObjectData (NODE_INFO_ADDRESS);
  node_info->setLabel ("Add Node");
  node_info->show (TRUE);

  return;
}

void delete_node_handler (UIObject *object)
{
  TreeDisplayer *tree = (TreeDisplayer *)
			object->getObjectData (TREE_DISPLAYER_ADDRESS);
  char          *object_id = tree->getSelectedNodeID ();

  Tt_status status;

  if (!object_id) {
    TreeNode *root = tree->getRoot ();
    object_id = root->getID (status);

    if (status > TT_WRN_LAST) {
      return;
    }
  }

  TreeNode node (object_id, status);

  if (status > TT_WRN_LAST) {
    return;
  }

  if (status < TT_WRN_LAST) {
    status = node.deleteNode (); 
  }

  return;
}

void apply_node_info (UIObject *object)
{
  PopUpWindow   *node_info = (PopUpWindow *)
			     object->getObjectData (NODE_INFO_ADDRESS);
  TreeDisplayer *tree = (TreeDisplayer *)
		        node_info->getObjectData (TREE_DISPLAYER_ADDRESS);
  char          *file = tree->getFile ();

  Tt_status status = TT_OK;

  if (status > TT_WRN_LAST) {
    return;
  }

  UIInputTextItem *image_file = (UIInputTextItem *)
			        node_info->getObjectData (IMAGE_FILE_ADDRESS);
  UIInputTextItem *text = (UIInputTextItem *)
			  node_info->getObjectData (TEXT_ADDRESS);

  char     *parent_id = tree->getSelectedNodeID ();
  char     *value = (char *)NULL;
  TreeNode  child_node (image_file->getValue (value),
		        text->getValue (value),
		        file, TREE_NODE_OTYPE, status);

  if (status > TT_WRN_LAST) {
    (void)child_node.destroy ();
    return;
  }

  if (parent_id) {
    TreeNode parent_node (parent_id, status);

    if (status < TT_WRN_LAST) {
      status = parent_node.addChild (child_node);

      if (status != TT_OK) {
        (void)child_node.destroy ();
      }
    }  
  }
  else {
    TreeNode *root = tree->getRoot ();
    status = root->addChild (child_node);

    if (status != TT_OK) {
      (void)child_node.destroy ();
    }
  }

  return;
}

void cancel_node_info (UIObject *object)
{
  PopUpWindow *node_info = (PopUpWindow *)
			   object->getObjectData (NODE_INFO_ADDRESS);
  node_info->setPinned (FALSE);
  node_info->show (FALSE);

  return;
}
