
#include <clib/intuition_protos.h>
#include <clib/gadtools_protos.h>
#include <clib/exec_protos.h>

#include <stdlib.h>
#include <string.h>

#include "project.h"

extern char PubName[];

char *ProgramName;

char *ProjectPathName;
char *ProjectName;
char *WindowTitle;

char *StartDir;
char *PrgDir;

/****************************************************************************
***									  ***
***	Fonctions divers						  ***
***									  ***
****************************************************************************/

void ReplaceChar( char **dest , char *source )
{
    free( *dest );
    *dest = strdup( source );
}

/****************************************************************
***	Change le nom du projet en cours		      ***
****************************************************************/

void ChangeProjectName( char *pathname ) {
  char temp[200];

    ReplaceChar( &ProjectPathName , pathname );
    ReplaceChar( &ProjectName , BaseName( pathname ) );
    ProjectName[ strlen(ProjectName) - 4 ] = 0;
    strcpy( temp , "DICE Project Handler : " );
    strcat( temp , BaseName( pathname ) );
    ReplaceChar( &WindowTitle , temp );
    SetWindowTitles( ProjectWnd , WindowTitle , (UBYTE *)-1 );
}

/****************************************************************
***	Change le nom du repertoire de DICE		      ***
****************************************************************/

void ChangeDICEDir( char *dir ) {
  char *prgt;

    ReplaceChar( &DICEDir , dir );
    prgt = malloc( strlen( DICEDir ) + 10 );
    strcpy( prgt , DICEDir );
    strcat( prgt , "/bin/" );
    ReplaceChar( &PrgDir , prgt );
    free( prgt );
}

/****************************************************************
***  Affiche une requete si un projet existe déjà ( Save as ) ***
****************************************************************/

LONG Overwrite( char *name ) {
  FILE *test;
  char message[200];

    if ( test = fopen( name , "r" ) ) {
	fclose( test );
	strcpy( message , "A project named " );
	strcat( message , BaseName( name ) );
	strcat( message , " already exists\nDo you want to overwrite it ?" );
	return( Message( ProjectWnd , message , "Overwrite|Forget" ) );
    }
    return( TRUE );
}

/****************************************************************
***	Lance l'edition d'un module spécifié par son nom      ***
****************************************************************/

void EditModule( char *name ) {
  char *buf;

    SetDefaultPubScreen( PubName );
    buf = malloc( strlen( PrgDir ) + strlen( name ) + 20 );
    strcpy( buf , PrgDir );
    strcat( buf , "DME \"" );
    strcat( buf , name );
    strcat( buf , "\"" );
    Launch( buf , TRUE );
    free( buf );
}

/****************************************************************
***	Gestion des fichiers de commentaires		      ***
****************************************************************/

char *CommentName( char *module ) {
  char *result;

    result = malloc( strlen( module ) + 10 );
    strcpy( result , module );
    strcat( result , ".comment" );
    return( result );
}

BOOL IsCommented( char *module ) {
  char *modulec;
  struct FileInfoBlock fib;
  BOOL r=FALSE;

    modulec = CommentName( module );
    if ( GetInfo( &fib , modulec ) ) {
	r = TRUE;
    }
    free( modulec );
    return( r );
}

/****************************************************************
***	Affiche une liste ou choisir un élément 	      ***
****************************************************************/

struct ModuleNode *ChooseInList( char *title , struct List *liste ) {
  BOOL			done=FALSE;
  struct IntuiMessage	*m;
  ULONG 		sec=-1,mic;
  int			n;
  struct ModuleNode	*wn=NULL;

    if ( !OpenCListeWindow( title ) ) {
	gtag[0].ti_Data = (long)liste;
	GT_SetGadgetAttrsA( CListeGadgets[0] , CListeWnd , NULL , gtag );
	while ( !done ) {
	    Wait( 1 << CListeWnd->UserPort->mp_SigBit );
	    while( m = GT_GetIMsg( CListeWnd->UserPort )) {
		GT_ReplyIMsg( m );
		switch ( m->Class ) {
		  case IDCMP_CLOSEWINDOW:
		    done = TRUE;
		    break;
		  case	  IDCMP_GADGETUP:
		    n = m->Code;
		    wn = liste->lh_Head;
		    while( wn->node.ln_Succ && n-- ) {
			wn = wn->node.ln_Succ;
		    }
		    if ( sec != -1 && DoubleClick( sec , mic , m->Seconds , m->Micros ) )
			done = TRUE;
		    else
			wn = NULL;
		    sec = m->Seconds;
		    mic = m->Micros;
		    break;
		}
	    }
	}
	CloseCListeWindow( );
    }
    return( wn );
}


