/*  
 *    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. 
 */  
  
#ifndef _TREE_NODE_H
#define _TREE_NODE_H

#include "Image.h"
#include "CSDrawIcon.h"
#include "TreeNode_consts.h"
#include "TTGenericArrayTemplate.h"
#include "TTGenericHash.h"
#include "TTObjectSpec.h"

class CSDrawIcon;
class Image;
class TTMessage;

DECLARE_TT_GENERIC_ARRAY(TreeNode);

class TreeNode : public TTObjectSpec {
public:
  TreeNode (const char *image_file,
	    const char *text,
	    const char *file,
	    const char *type,
	    Tt_status  &status) : TTObjectSpec (file, type, status)
    {
      setTreeNodeDefaults ();

      if (status < TT_WRN_LAST) {
	status = write ();
      }

      if (status < TT_WRN_LAST) {
	char *temp_file_string = strcpy (new char [strlen (image_file) + 1],
					 image_file);
	char *temp_text_string = strcpy (new char [strlen (text) + 1],
					 text);
        status = manageIcon (temp_file_string, temp_text_string);
	delete temp_file_string;
	delete temp_text_string;
      }
    }

  TreeNode (const char *objid,
	    Tt_status  &status) : TTObjectSpec (objid, status)
    {
      setTreeNodeDefaults ();

      if (status < TT_WRN_LAST) {
        status = manageIcon ();
      }
    }

  ~TreeNode ();

  Tt_status addChild (TreeNode &child);
  Tt_status deleteNode ();

  TreeNodeArray *getChildren (Tt_status&) const;

  CSDrawIcon *getIcon (Tt_status&) const;

  Tt_status  setImageFile (const char *);
  char      *getImageFile (Tt_status&) const;

  Tt_status  setText (const char *);
  char      *getText (Tt_status&) const;

private:
  Tt_status manageIcon (char *image_file=NULL, char *text=NULL);
  Tt_status sendMessage (TTMessage &);

  void setTreeNodeDefaults ()
    {
      numTreeNodes++;

      if (!treeNodeIcons) {
	treeNodeIcons = new TTGenericHash;
      }
    }

  Tt_status setImageFileProperty (const char *file)
    {
      Tt_status status = setProperty (TREE_NODE_IMAGE_FILE_PROPERTY, file);

      if (status < TT_WRN_LAST) {
	status = write ();
      }

      return status;
    }

  Tt_status setTextProperty (const char *text)
    {
      Tt_status status = setProperty (TREE_NODE_TEXT_PROPERTY, text);
      
      if (status < TT_WRN_LAST) {
	status = write ();
      }

      return status;
    }

  static int            numTreeNodes;
  static TTGenericHash *treeNodeIcons;

  class TreeNodeIcon : public TTGeneric {
  public:
    TreeNodeIcon  ()
      {
	icon = (CSDrawIcon *)NULL;
	image = (Image *)NULL;
      }

    ~TreeNodeIcon ()
      {
	if (icon) {
	  icon->show (FALSE);
          delete icon;
        }

	if (image) delete image;
      }

    CSDrawIcon *icon;
    Image      *image;
  };

  void setTreeNodeIcon (const char *object_key, TreeNodeIcon *icon)
    {
      treeNodeIcons->addItem (object_key, (TTGeneric *)icon);
    }

  TreeNodeIcon *getTreeNodeIcon (const char *object_key) const
    {
      return (TreeNodeIcon *)treeNodeIcons->getItem (object_key);
    }

  TreeNodeIcon *removeTreeNodeIcon (const char *object_key) const
    {
      return (TreeNodeIcon *)treeNodeIcons->removeItem (object_key);
    }

#ifdef __GNUC__
public:
#endif
  static void nodeSelectionHandler (UIObject *);
};

#endif
