/****************************************************************************
*
* $RCSfile: ChooserNodes.c $
* $Revision: 1.1 $
* $Date: 1997/09/05 13:58:45 $
* $Author: ssolie $
*
*****************************************************************************
*
* Copyright (c) 1997 Software Evolution.  All Rights Reserved.
*
*****************************************************************************
*
* ChooserNodes.c -- Chooser nodes source file
*
* This file contains the source code which makes using chooser nodes more
* convenient.
*/
#include <exec/memory.h>
#include <gadgets/chooser.h>
#include <utility/tagitem.h>

#include <proto/chooser.h>
#include <proto/exec.h>

#include "chooserNodes.h"


IMPORT struct ExecBase		*SysBase;
IMPORT struct ClassLibrary	*ChooserBase;


/****** SERL/createChooserNodes *********************************************
*
*   NAME
*	createChooserNodes -- Create chooser.gadget node list
*
*   SYNOPSIS
*	nodes = createChooserNodes(labels)
*
*	struct List *createChooserNodes(STRPTR *);
*
*   FUNCTION
*	This function creates a list of chooser nodes for a ClassAct
*	chooser.gadget object.  The chooser labels are supplied as an
*	array of string pointers terminated by a NULL pointer.
*
*	Requires the amiga.lib link library.
*
*   INPUTS
*	labels - NULL terminated array of string pointers.
*
*   RESULT
*	Pointer to a list of chooser nodes or NULL on error.
*
*   NOTES
*	Should have the option to use a memory pool.
*
*   SEE ALSO
*	SERL/deleteChooserNodes()
*
*****************************************************************************
*
* $RCSfile: ChooserNodes.c $
* $Revision: 1.1 $
* $Date: 1997/09/05 13:58:45 $
* $Author: ssolie $
*
* Copyright (c) 1997 Software Evolution.  All Rights Reserved.
*/
struct List *createChooserNodes(STRPTR labels[])
{
	struct List *list;
	struct Node *node;
	struct TagItem tags[2];
	ULONG i;

	list = AllocVec(sizeof(struct List), MEMF_ANY);
	if ( list != NULL )  {
		NewList(list);

		tags[0].ti_Tag = CNA_Text;
		tags[1].ti_Tag = TAG_END;

		i = 0;
		while ( labels[i] != NULL )  {
			tags[0].ti_Data = (ULONG)labels[i];
			node = AllocChooserNodeA(tags);
			if ( node == NULL )  {
				deleteChooserNodes(list);
				return(NULL);
			}

			AddTail(list, node);
			i++;
		}
	}

	return(list);
}


/****** SERL/deleteChooserNodes *********************************************
*
*   NAME
*	deleteChooserNodes -- Delete chooser.gadget node list
*
*   SYNOPSIS
*	deleteChooserNodes(list)
*
*	deleteChooserNodes(struct List*);
*
*   FUNCTION
*	This function deletes a list of chooser nodes created with the
*	createChooserNodes() function.
*
*   INPUTS
*	list - Private exec list of chooser nodes (may be NULL).
*
*   NOTES
*	Should have the option to use a memory pool.
*
*   SEE ALSO
*	SERL/createChooserNodes()
*
*****************************************************************************
*
* $RCSfile: ChooserNodes.c $
* $Revision: 1.1 $
* $Date: 1997/09/05 13:58:45 $
* $Author: ssolie $
*
* Copyright (c) 1997 Software Evolution.  All Rights Reserved.
*/
VOID deleteChooserNodes(struct List *list)
{
	struct Node *node;

	if ( list != NULL )  {
		while ( (node = RemHead(list)) != NULL )
			FreeChooserNode(node);

		FreeVec(list);
	}
}
