/*
\\              Support functions for MUIConstruct
*/

        #include <Global/Main_Global.h>
        

ULONG __stdargs DoSuperNew(struct IClass *cl,Object *obj,ULONG tag1,...)
{
        return(DoSuperMethod(cl,obj,OM_NEW,&tag1,NULL));
}  


void Display( char *Text)
{
MUI_Request(NULL, NULL, NULL, "Warning...", "_Continue", "%s",Text);
}


ULONG CountNodes( struct List *list )
{
        ULONG count;
        struct Node *node;
        for( count = 0, node = FirstNode( list ); node; count++, node = NextNode( node ) );
        return( count );
}    

void SortList( struct List *list, ULONG offset )
{
        // Sort nodes alphabetically (names start at node + nameoffset)

        ULONG count, cycle, test;
        struct Node *node, *minnode;
        UBYTE *ptr, *minptr;

        count = CountNodes( list );
        minnode = FirstNode( list );
        minptr = ((UBYTE *) minnode) + offset;
        for( cycle = count, node = FirstNode( list ); cycle; cycle-- )
        {
                for( test = 0; test < count; test++ )
                {
                        ptr = ((UBYTE *) node) + offset;
                                if( Stricmp( ptr, minptr ) == -1 )
                        {
                                minnode = node;
                                minptr = ((UBYTE *) node) + offset;
                        }
                }
                Remove( minnode );
                AddTail( list, minnode );
        }
}       


APTR FirstNode( APTR list )
{
        struct Node *node = ((struct List *)list)->lh_Head;
        if( !node->ln_Succ ) node = NULL;
        return( node );
}

APTR LastNode( APTR list )
{
        struct Node *node = ((struct List *)list)->lh_TailPred;
        if( !node->ln_Pred ) node = NULL;
        return( node );
}

APTR NextNode( APTR node )
{
        struct Node *n = node;
        return( n->ln_Succ->ln_Succ?n->ln_Succ:NULL );
}

APTR PrevNode( APTR node )
{
        struct Node *n = node;
        return( n->ln_Pred->ln_Pred?n->ln_Pred:NULL );
}      


SAVEDS ULONG Get( Object *Module, ULONG attr )
{
        /* Returns value of requested attr, or NULL */

        ULONG retval;
        if( !GetAttr( attr, Module, &retval ) )
                return( NULL );
        return( retval );
}   



UBYTE *copystr( UBYTE *source )
{
        // Allocate space and copy string

        ULONG len;
        UBYTE *dest;

        if( !(len = strlen( source )) )                                 return( NULL );
        if( !(dest = AllocPooled( Global->MemoryPool, len+1)) ) return( NULL );
        strcpy( dest, source );
        return( dest );
}

BOOL objok( struct Module *o )
{
        // Check for duplicate object names etc

        struct Module *testobj;       
        
       if( (o->Flags & CLASS_NEEDSNAME) && !o->Name )
        {
                Display( "This Object Requires A Name.");
                return( FALSE );
        }
        if( !o->Name ) return( TRUE );   // If The Object Doesn't Require A Name Just Return.

      /*  for( testobj = FirstNode( &Global->ObjectList ); testobj; (testobj = ParseObjectList( testobj )) )                
                {
                
                 if( (o != testobj) && !strcmp( o->Name, testobj->Name ) )
                       {
                        Display( "Object Name Given is Already in Use!" );
                        return( FALSE );
                        }
                }
       */
        return( TRUE );
}

struct Module * ParseObjectList( struct Module *o )
{
        
        struct Module *test;
	//Display("SOMEONE IS PARSING OBJECTS!!!");
        if( test = FirstNode( &o->Children ) )  
             return( test ); 
        if( test = NextNode( o ) )                                      return( test ); // Successor
        while( o = o->Parent )          // Get parent of Module
        {
                if( test = NextNode( o ) )                              return( test ); // Successor of parent
        }
        return( NULL );         // No more parents - no more entries in list
}


