/************************************************************************/
/*									*/
/*	File win_alea.c							*/
/*									*/
/************************************************************************/
/* Open the window for the random modules				*/
/************************************************************************/

#include <exec/types.h>
#include <intuition/intuition.h>
#include <intuition/gadgetclass.h>
#include <libraries/gadtools.h>
#include <graphics/displayinfo.h>
#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/gadtools_protos.h>
#include <dos/dos.h>
#include "/includes/struct.h"
#include "/includes/tom_gadget.h"

extern	struct my_data	my_info;
extern	struct	appel_proc	data_proc;
#define	MIN_DUREE	10
#define	MAX_DUREE	999
extern	int	nb_module;
extern	char	*p_nom[];
extern	int	selection,last_sel;
extern	struct my_data	my_info;
extern	LONG	dir_save;
extern	BPTR	old_lock;

extern	char	*p_name;

struct	Node ModulesNodes_alea[MAX_NOM];
USHORT	flg_sel[MAX_NOM];
char	*p_nom_alea[MAX_NOM];
#define	MAX_LONG	20

struct	Gadget	*init_gadg();
void		 press_gadg();
void		 end_gadg();

struct List ModulesListAlea = {
	( struct Node * )&ModulesListAlea.lh_Tail,
	( struct Node * )0l,
	( struct Node * )&ModulesListAlea.lh_Head,0,0 };

static	struct	own_init	my_init={&init_gadg,&press_gadg,&end_gadg};
char	*texte_alea="     Alea\n\n    V1.0\n\nSelect ALL if you want to have a\nseleection of all the modules, or\nunselect ALL and choose some modules\nin the list at the left.\n";

struct	tom_gadget	gadg_alea[]={
		{"_Duree",	SLIDER   , 68, 78, 187,11,MIN_DUREE,MIN_DUREE,MAX_DUREE,0,(char *)&my_info.tempo_end},
		{"_All",	CHECKBOX ,320, 37,  10,10,TRUE,0,0,0,0},
#ifndef	WB_13
		{"Selection",	OWN_GADGET ,85,16, 193,55,10,0,0,0,(char *)&my_init},
#endif
#ifdef	WB_13
		{"Selection",	LISTVIEW   ,82,16, 193,55,10,0,0,0,(char *)&ModulesListAlea},
#endif
		{0,		END_LISTE,  0,  0,   0, 0, 0,0,0,0,0}};


/*****************************************************************************/

create_list()
{
	int i;
	char *p_chaines;

	p_chaines=(char *)malloc(nb_module*MAX_LONG);
	for(i=0;i<nb_module;i++){
		p_nom_alea[i]=p_chaines+i*MAX_LONG;

		strcpy(p_nom_alea[i]+1,p_nom[i]);
		if(flg_sel[i]==FALSE){
			*p_nom_alea[i]=' ';
		}else{
			*p_nom_alea[i]='>';
		}
		ModulesNodes_alea[i].ln_Name=p_nom_alea[i];
		ModulesNodes_alea[i].ln_Type=0;
		AddTail(&ModulesListAlea,&ModulesNodes_alea[i]);
	}
}

/*****************************************************************************/

struct	Gadget	*init_gadg(p_gadg,g ,p_ng)
struct	tom_gadget	*p_gadg;
struct	Gadget	*g;
struct NewGadget     *p_ng;
{
	create_list();

    g = CreateGadget( LISTVIEW_KIND, g, p_ng,
		GTLV_Labels, &ModulesListAlea, TAG_DONE );
	return(g);
}


/*****************************************************************************/

void	end_gadg()
{
	free(p_nom_alea[0]);
}


/*****************************************************************************/

void	press_gadg(pg,code,wnd)
struct	Gadget	*pg;
int	code;
struct	Window	*wnd;

{
	if(flg_sel[code]==FALSE){
		flg_sel[code]=TRUE;
		*p_nom_alea[code]='>';
	}else{
		flg_sel[code]=FALSE;
		*p_nom_alea[code]=' ';
	}
#ifndef	WB_13
	GT_RefreshWindow( wnd, 0l );
#endif
}

/*****************************************************************************/

void param_alea()
{
	LONG	old_dir;

	data_proc.p_texte=texte_alea;
#ifdef	WB_13
	create_list();
#endif
	Gere_gadg(gadg_alea,"Alea");
	old_dir=0;
	if(dir_save!=0){
		old_dir=CurrentDir(dir_save);
	}else if(old_lock!=-1){
		old_dir=CurrentDir(old_lock);
	}
	SaveConfig(p_name,gadg_alea);
	if(old_dir!=0){
		CurrentDir(old_dir);
	}
#ifdef	WB_13
	end_gadg();
#endif
}

/*****************************************************************************/

/************************************************/
/* Load the configuration of the random modules */
/* this configuration is saved in a file in the */
/* s: directory, an ASCII file...		*/
/************************************************/

void	load_alea_cfg()
{
	UWORD	flg_end;
	char	buffer[100];
	BPTR	alea_hd;
	int	i;

	alea_hd=Open(NAME_FILE_ALEA,MODE_OLDFILE);
	flg_end=FALSE;

	for(i=0;i<nb_module;i++){
		flg_sel[i]=FALSE;
	}

	if(alea_hd!=0){
		while(flg_end==FALSE){
			/* Lecture d'une ligne */
			i=-1;
			do{
				i++;
				if(Read(alea_hd,&buffer[i],1)!=1){
					flg_end=TRUE;
				}
			}while((buffer[i]!='\n')&&(flg_end==FALSE));
			buffer[i]=0;

			/* Recherche si l'element existe */
			if(flg_end==FALSE){
				for(i=0;i<nb_module;i++){
					if(strcmp(buffer,p_nom[i])==0){
						flg_sel[i]=TRUE;
					}
				}
			}
		}
		Close(alea_hd);
	}
}

/*****************************************************************************/

void	save_alea_cfg()
{
	BPTR	alea_hd;
	int	i,len;

	alea_hd=Open(NAME_FILE_ALEA,MODE_NEWFILE);
	if(alea_hd!=0){
		for(i=0;i<nb_module;i++){
			if(flg_sel[i]==TRUE){
				len=strlen(p_nom[i]);
				Write(alea_hd,p_nom[i],len);
				Write(alea_hd,"\n",1);
			}
		}
		Close(alea_hd);
	}
}
