/* NAME = jukebox.func */
/* Sat 5-03-2025 (anyflo 2025.03) Michel Bret */

/*
in main program:
	#include jukebox.func()
	externs()
	{
	#include "jukebox_externs()"
		...
	}
	init(w)
	{
		...
		jukebox_init(w);
		...
	}
	F0()
	{
		jukebox_F0();
	dont define:
	//	interaction ascii("j")func("j_");
	}
	F5()
	{
		...
		jukebox_F5();
		...
	}
Juk_Val = -1 (off), 1 (on)
Must be defined Sem_D = 1 (mouse on), -1 (mouse off)
*/
jukebox(w)
{
	jukebox_init(w);
	interaction func(0,"jukebox_F0")func(5,"jukebox_F5");
}
/* func jukebox_externs */
/* -------------------- */
jukebox_externs()
{
extern Juk_Dir,Juk_List,Juk_Val,Juk_Mus,Juk_Nb,Juk_Num;
extern Juk_Col,Juk_Font,Juk_Frame,Juk_Rectangle;
}
/* func jukeboxinit */
/* ---------------- */
jukebox_init(w)
{
#include "jukebox_externs()"

	Juk_Dir="Gregory_Jarrige";
	Juk_Col=1,1;Juk_Mus=-1;Juk_Nb=0;Juk_Num=-1;
	Juk_Font="ROMAN_24";
	Juk_Frame=0,1;
	Juk_Rectangle=0,0,0;
	if(w==NIL)
		jukebox_REA();
	else
		jukebox_option(w);
	directory list exec(1);
	Juk_List=1;Juk_Val=-1;
}
/* func jukebox_REA */
/* ---------------- */
jukebox_REA()
{
#include "jukebox_externs()"

	w=exec(clean(read("PARAM_JUK")));
}
/* func jukebox_F0 */
/* --------------- */
jukebox_F0()
{
	interaction ascii("j")func("jukebox_inv_stat");
}
/* func jukebox_F5 */
/* --------------- */
jukebox_F5()
{
#include "jukebox_externs()"

	if(Juk_Val>0)
		displ directory list(Juk_List)font(Juk_Font)
			col(Juk_Col)rectangle(Juk_Rectangle)
			frame(Juk_Frame)message("click on -> OK");
}
/* func jukebox_inv_stat */
/* --------------------- */
jukebox_inv_stat()
{
extern Juk_Val,Sem_D;

	Juk_Val=-Juk_Val;Sem_D=1;D_();
	if(Juk_Val>0)
	{
		directory list exec(1);
	}
	else
	{
		directory list exec(0);
	}

yes mouse;

}

/* func option */
/* ----------- */
jukebox_option(option)
{
#include "jukebox_externs()"
extern Juk_Mus;	/*??? BUG ???*/
	$"jukebox_option = ",option;NL;
	if((n1=dim(option))==0)return;	
	k=0;
	while(1)
	{
		w=jukebox_get_option(option[k,n1-1]);
		if((ni=dim(w))==0)return;
		if(ni==1)
		{
			switch(w)
			{
			default: ;
			}
			goto next;
		}
		if(ni>2 && w[1]=="=")
		{
			switch(w[0])
			{
			default:
			}
			goto next;
		}
		if(ni==3)
		{
			switch(w)
			{
			default: ;
			}
			goto next;
		}
		if(ni>4)
		{
			if((w[3])=="=")
			{
				w02=w[0,2];w4n=w[4,ni-1];
				switch(w02)
				{
				case("col"):
					Juk_Col=atof(w4n);break;
				case("dir"):
					np=NP(w4n);
					for(i=0,np-1)
						read directory(w4n{i})list(i+1);
					break;

				case("mus"):
					Juk_Mus=atoi(w4n);break;
				case("font"):
					Juk_Font=w4n;break;
				case("frame"):
					Juk_Frame=atof(w4n);break;
				case("rectangle"):
					Juk_Rectangle=atof(w4n);break;
				default: ;
				}
			}
		}
next:
		k+=ni+1;if(k>=n1)return;
	}
}
/* func jukebox_get_option */
/* ----------------------- */
jukebox_get_option(w)
{
	n=dim(w);w1=NIL;k=0;
	while(n-->0)
	{
		wk=w[k];
		if(wk==";")return(w1);
		w1,=wk;k++;
	}
	return(w1);
}
play_one(n)
{
extern Juk_Dir,Juk_Mus,Juk_Nb;

	d=read directory(Juk_Dir);Juk_Nb=NP(d);
	if(n==NIL)
	{
		if(Juk_Mus>=0)
		{
			n=Juk_Mus;Juk_Mus=-1;
			if(n>=Juk_Nb)n=Juk_Nb-1;
		}
		else
			n=rand(Juk_Nb);
	}			
	system(Juk_Dir,"/",d{n});
}
test()
{
	jukebox("dir=ima1,ima2");
}

/* END */
