(c)  Copyright 1990 Commodore-Amiga, Inc.   All rights reserved.
The information contained herein is subject to change without notice,
and is provided "as is" without warranty of any kind, either expressed
or implied.  The entire risk as to the use of this information is
assumed by the user.

/* wbarg.c
 * How to get an icon from a Workbench argument
 * Written by David N. Junod, 27-Aug-90
 */

#include <exec/types.h>
#include <exec/memory.h>
#include <exec/libraries.h>
#include <libraries/dos.h>
#include <workbench/icon.h>
#include <workbench/startup.h>
#include <clib/dos_protos.h>
#include <clib/exec_protos.h>
#include <clib/wb_protos.h>
#include <pragmas/dos.h>
#include <pragmas/exec.h>
#include <pragmas/wb.h>
#include <string.h>

extern struct Library *IconBase, *DOSBase;

struct DiskObject *GetDiskObjectNew (STRPTR name);
struct DiskObject *GetDiskObject (STRPTR name);

#pragma libcall IconBase GetDiskObjectNew 84 801

/****** wbarg/IconFromWBArg ********************************************
*
*   NAME
*	IconFromWBArg - Obtains the icon from a Workbench argument.
*
*   SYNOPSIS
*	dob = IconFromWBArg (wbarg);
*
*	struct DiskObject *dob;
*	struct WBArg *wbarg;
*
*   FUNCTION
*	Given a valid Workbench argument, this command will return the
*	proper icon.  This call uses the new (V36) GetDiskObjectNew call
*	to ensure that an icon will be returned.
*
*	This functions requires dos.library and icon.library to be opened
*	by the application.
*
*   INPUTS
*	wbarg	- Pointer to a filled in WBArg structure.
*
*   RESULTS
*	dob	- Pointer to a DiskObject structure.  Application must call
*		  FreeDiskObject when done with the icon.
*
**********************************************************************/

struct DiskObject *IconFromWBArg (struct WBArg * arg)
{
    struct DiskObject *dob = NULL;
    UBYTE work_name[34];
    BPTR old, new;

    /* Copy the WBArg contents */
    strcpy (work_name, arg->wa_Name);

    /* Duplicate the lock */
    if (new = DupLock (arg->wa_Lock))
    {
	BOOL success = FALSE;

	/*
	 * If we don't have a name, then get one. Only Tools & Projects have a
	 * valid name.
	 */
	if (strlen (work_name) == 0)
	{
	    LONG msize = sizeof (struct FileInfoBlock);
	    struct FileInfoBlock *info;

	    /* This block needs to be longword aligned, so allocate it */
	    if (info = (struct FileInfoBlock *) AllocMem (msize, MEMF_CLEAR))
	    {
		/* Examine the lock, so that we can figure out the name */
		if (Examine (new, info))
		{
		    /* Copy the name of the lock */
		    strcpy (work_name, info->fib_FileName);

		    /* See if the lock is a directory */
		    if (info->fib_DirEntryType > 0)
		    {
			/* Unlock the existing lock */
			UnLock (new);

			/* Go to the parent drawer */
			if ((new = ParentDir (arg->wa_Lock)) == NULL)
			{
			    /* A disk icon was passed */
			    strcpy (work_name, "disk");
			    new = DupLock (arg->wa_Lock);

			}	/* End of if disk */
		    }		/* End of if directory */

		    /* We have a name */
		    success = TRUE;

		}		/* End of examine */

		/* Free the temporary block */
		FreeMem ((APTR) info, msize);
	    }
	}
	else
	{
	    /* Show that we have a name to work with */
	    success = TRUE;
	}

	/* See if we should try getting the icon */
	if (success)
	{
	    /* go to the directory where the icon resides */
	    old = CurrentDir (new);

	    /*
	     * GetDiskObjectNew is a new function, for AmigaOS version 2.0,
	     * therefore we need to check the version level.
	     */
	    if (IconBase->lib_Version >= 36)
		dob = GetDiskObjectNew (work_name);
	    else
		dob = GetDiskObject (work_name);

	    /* go back to where we used to be */
	    CurrentDir (old);
	}

	/* release the duplicated lock */
	UnLock (new);
    }

    /* return the icon */
    return (dob);
}


