/*********************************************/
/* File example_gadgs.dark.c   		     */
/*					     */
/* This exampleshow you how to use different */
/* kind of 'gadgets' available with superdark*/
/*********************************************/

#include <exec/types.h>
#include <exec/memory.h>
#include <intuition/intuition.h>
#include <intuition/gadgetclass.h>
#include <graphics/gfx.h>
#include <graphics/gfxbase.h>
#include <dos/dos.h>
#include <graphics/gfxmacros.h>
#include <graphics/rastport.h>
#include <graphics/displayinfo.h>
#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/dos_protos.h>
#include <clib/graphics_protos.h>
#include <clib/diskfont_protos.h>


#include "/includes/struct.h"
#include "/includes/tom_gadget.h"
#include "image.h"

struct Library		*DiskfontBase;
extern  struct  GfxBase *GfxBase;
extern	struct	appel_proc	*p_data_proc;

#define	VOID_ARG	void	__regargs

char	*p_text_info=
"This is a part of programmers.doc\n"
"You can see here the different   \n"
"kind of gadgets available\n"
"including 'high level' gadg like\n"
"screen and font\n"
"Note that all corresponding param.\n"
"are saved in the .info file....\n"
"\n"
" @ Thomas Landspurg\n";

struct	RastPort	*rp;
struct	Screen		*s;

#define	WIDTH	640
#define	HEIGHT	256

UWORD  ScreenColors[] = {
    0x000,
    0xFFF,
    0xFE2,
    0x68B,
    0xF90,
    0x0F0,
    0x00F,
    0xF0F,
    0x000 };

/* Note that you should put a string with enough place to put the */
/* larger font name						  */
struct	TextAttr myta={"topaz                   ",0,0,0};

#define	TAILLE_BUF	100
char	buffer_text[TAILLE_BUF]="";
char *CyclesNames[] = {"This","is","a","list","of","items",NULL};
char *MxNames[]     = {"Item 1","Item 2","Item 3",NULL};
int	__saveds	press_button();

struct	tom_gadget	my_gadg[]={
	{"_Button",      BUTTON,  100,10,100, 11,0,0,0,0,(char *)press_button},
	{"Checkbo_x",    CHECKBOX,280,10, 10, 10,0,0,0,0,0},
	{"C_ycle",	CYCLE,   100,25,100, 12,        0,1,0,0,(char *)CyclesNames},
	{"_String",	STRING,  100, 45,250,14,0,TAILLE_BUF,0,0,buffer_text},
	{"_Fonts",	FONT,    100,60,100,12,0,         0,0,0,(char *)&myta},
	{"Scr_een",	SCREEN,  220,60,100,12,0,         0,0,0,0},
	{"Sli_der",	SLIDER,  100,75,200,10,1,0,10,0,0},
	{"_Mx",		MX,	 150,90,100,30,0,0, 0,0,(char *)MxNames},
	{"image...",    IMAGE,   250,90, 80,30,0,0,0 ,0,(char *)&image_iffImage},
	{0,		END_LISTE,  0,  0,   0, 0, 0,0,0,0,0}};

/********** a little proc to print text on screen ************/
void	my_print(rp,x,y,pc)
struct	RastPort *rp;
int	x,y;
char	*pc;
{
	Move(rp,x,y);
	Text(rp,pc,strlen(pc));
}

/* The callback procedure, called when the button is pressed, */
/* Note that this function use the keyword __saved. If you    */
/* don't have this one, try geta4() function		      */

int	__saveds	press_button(pg,p_t,ctx)
struct	Gadget	*pg;
struct	tom_gadget	*p_t;
struct	contexe	*ctx;
{
	static struct EasyStruct ES={
		sizeof(struct EasyStruct),
		0,"Example",
		"You've pressed the button gadget","I know,thanks",};

	EasyRequest(ctx->Wnd, &ES, NULL);
	/* If you return TRUE, this mean that you want to close the */
	/* config window.					    */
	return FALSE;
}

/********************* Just to show you something on your screen *****/

void	aff_texte(rp)
struct	RastPort *rp;
{
	BYTE	old_flg;
        struct TextFont *myfont;
	int	len,tx,ty,x,y;
	struct	TextExtent	result;
	char	buffer[100];

	/* Note that there is SPrintF equivalent in proc_main,	*/
	/* so it's better to use this one: SPrintF		*/
	SetAPen(rp,1);

	SPrintF(buffer,"Cycle value: %ld",my_gadg[2].value);
	my_print(rp,10,20,buffer);

	SPrintF(buffer,"String value: %s",my_gadg[3].p_data);
	my_print(rp,10,30,buffer);

	SPrintF(buffer,"Slider value: %ld",my_gadg[6].value);
	my_print(rp,10,40,buffer);

	SetAPen(rp,1);
	if(DiskfontBase){
	       /* We stop CPU watchdog, because opening a textfont can take a */
	       /* long time						      */
	       old_flg=p_data_proc->enable_watchdog;
	       p_data_proc->enable_watchdog=FALSE;
               if (myfont = OpenDiskFont(&myta))
                    {
	                    SetFont(rp, myfont);
                            SetSoftStyle(rp,   myta.ta_Style ^ myfont->tf_Style,
                                      (FSF_BOLD | FSF_UNDERLINED | FSF_ITALIC));
		    }
	       p_data_proc->enable_watchdog=old_flg;
	}
	len=strlen(my_gadg[3].p_data);
	ty=myta.ta_YSize;
	tx=TextLength(rp,my_gadg[3].p_data,len);
	if(tx+10>s->Width){
		tx=s->Width-10;
		len=TextFit(rp,my_gadg[3].p_data,len,&result,NULL,1,s->Width-10,ty);
	}
	x=(s->Width-tx)/2;
	y=(s->Height-ty)/2;
	Move(rp,x,y+ty);
	ty+=8;
	Text(rp,my_gadg[3].p_data,len);
	
}

/***************************************************/
/* This is the main entry point of this blanker....*/
/***************************************************/
LONG	dark()
{
  struct	tom_gadget *pg;
  struct	Window	*win;
  WORD		width,height;

  pg=&my_gadg[5];

  s=0;
  if((GfxBase->LibNode.lib_Version>=37)&&(pg->d3!=0)){
	s=OpenScreenTags(  NULL,
			   SA_Width,(ULONG)pg->d1,
			   SA_Height,(ULONG)pg->d2,
                           SA_Depth,(ULONG)pg->d3,
			   SA_Overscan,(ULONG)pg->value,
                           SA_DisplayID,(ULONG)pg->p_data,
                           SA_Quiet,TRUE,TAG_DONE);
   }
   if(s==0){
	s=OpenScreenTags(NULL,
                           SA_Depth,(ULONG)2,
			   SA_Width, (ULONG)320,
/*			   SA_Type,CUSTOMSCREEN,*/
			   SA_ShowTitle, FALSE,
                           SA_Quiet,TRUE,TAG_DONE);
   }
   if(s){
     SetRGB4 (&s->ViewPort,0,0,0,0);

     width=s->Width;
     height=s->Height;
     win=OpenWindowTags(	NULL,
				WA_Left,	0,
				WA_Top,		0,
				WA_Width,	width,
				WA_Height,	height,
				WA_CustomScreen,s,
				WA_Flags,	WFLG_SIMPLE_REFRESH|WFLG_BACKDROP|WFLG_BORDERLESS|WFLG_ACTIVATE,
				TAG_DONE);
     if(win){
	ClearPtr(win);
	rp = win->RPort;

	SetRast(rp,0);
	FreeSprite (0);
	GfxBase->SpriteReserved|=1;
	LoadRGB4(&s->ViewPort,ScreenColors,1<<3);

	aff_texte(rp);
        wait_end();
      }
      DCloseScreen(s);
   }
   return(NULL);
}

void	proc_init()
{
        DiskfontBase = OpenLibrary("diskfont.library", 37L);
	p_data_proc->code_ret=DARK_WB_20;
}

void	proc_save()
{
}
void	proc_end()
{
	if(DiskfontBase)CloseLibrary(DiskfontBase);
}

