#include <clib/intuition_protos.h>
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <intuition/intuition.h>
#include <exec/exec.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


extern struct Window *win_p;
extern struct NewWindow win1;
extern struct Menu menu1;
extern char line1[200], line2[200];
extern void msg(char *text);
extern void closeall(void);

extern struct {
/* Font structure, set up by getfont() and used globally */

   int x;            /* Width of default graphic font */
   int y;            /* Height of default graphic font */
}font;

struct Event {
	BOOL used;
	int type;	/* Type of event */
	int hours;	/* Time of event */
	int minutes;
	int timecode;
	char message[256];
	int day;
	int month;
	int year;
	BOOL enabledate;
	int freq;
};

extern struct {
/* Prefs structure used for preferences file format */

   char header[10];  /* I.D. Header */
   int vers;         /* Version of preferences file */
   int x;            /* X dimension of window when saved */
   int y;            /* Y dimension of window when saved */
   int width;        /* Width of window when saved */
   int height;       /* Height of window when saved */
   int planguage;    /* Language selected (menu) when saved, low pri */
   int just;         /* Justification of text, 0=Left, 1=Centre */
   short date;       /* Display date?, 0 = No, 1=Yes */
   short wtf;	     /* Window to front? */
   int time_col[8];
   int date_col[8];
   short autoadjust;       /* Auto adjust ? */
   char backdrop[256];
   char pub[139];
	char accent[256];		/* Language file for tRanslate */
	char tkey[100];
	char hkey[100];
	struct Event events[11];  /* For the alarm! */ 
}prefs;
extern struct {
   int s_width;       /* Width of current screen */
   int s_height;      /* Height of current screen */
   int s_bar;         /* Bar height of windows.  Other font */
   int s_cols;        /* Number of colours available */
   int s_mode;        /* Mode according to s_cols */
}screentype;

void autoadjust(void) {
	WORD x,y,width,height;
	int length1, length2;
	WORD newwidth;

	// Get current dimensions

	// Before going any further we should make sure that the Window's
	//	MinWidth and MinHeight are set LOW LOW LOW!

	if(win_p->MinWidth != 40 || win_p->MinHeight != screentype.s_bar+font.y+5) {
		// Update win structure
		win1.LeftEdge=win_p->LeftEdge;
		win1.TopEdge=win_p->TopEdge;
		win1.Height=win_p->Height;
		win1.Width=win_p->Width;

		win1.MinWidth=40;
		win1.MinHeight=screentype.s_bar+font.y+5;

		ClearMenuStrip(win_p);
		CloseWindow(win_p);

		if(!(win_p=OpenWindow(&win1))) {
			// There is no reason why this should fail since we updated
			msg("Error re-opening window (system failure!!)");
			closeall();	
			exit(NULL);
		}
		SetMenuStrip(win_p,&menu1);
	}
	Forbid();
	x=win_p->LeftEdge; y=win_p->TopEdge;
	width=win_p->Width; height=win_p->Height;
	Permit();

	// Get string lengths

	length1=strlen(line1);
	length2=strlen(line2);


	// Which one is bigger?

	if(length2 > length1)
		length1=length2;

	// Now the greater length is in length1

	// What is the pixel requirements??

	newwidth=length1*font.x;
	newwidth+=16; /* Initial offset from LHS */
	newwidth+=16; /* Keep it even each side */
	newwidth+=30; /* RHS border */

	// Do we need to alter the window size?

	if(newwidth==width) {
		return;

	}
	// Ok then, what sort of stretch/shrink are we looking at?
	
	/* prefs.just == 0 is a stretch to the right,
		2 is a strech to the left
		1 is a proportional stretch */

	switch(prefs.just) {
		case 0: /* Left justification */
			// Stretch window to the right

			// Are we doing a stretch or a shrink

			if(newwidth < width) {
				// Shrink
				Forbid();
				ChangeWindowBox(win_p,x,y,newwidth,height);
				Permit();
			}
			else {
				// Stretch
				// Do we have enough room?

				while(x+newwidth > screentype.s_width) {
					// Try to solve the problem by reducing x
					if(x==0) {
						// Just too small a screen!
						msg("Sorry cannot autoadjust, screen too small!");
						return;
					}
					x--;
				}
				Forbid();
				ChangeWindowBox(win_p,x,y,newwidth,height);
				Permit();
			}			
		break;
		case 1: /* Centre justification */
			// Stretch window equally in both directions

			// Are we doing a stretch or a shrink

			if(newwidth < width) {
				// Shrink
				Forbid();
				ChangeWindowBox(win_p,x+((width-newwidth)/2),y,newwidth,height);
				Permit();
			}
			else {
				// Stretch
				// Do we have enough room?

				while(x-((newwidth-width)/2)+newwidth > screentype.s_width) {
					// Try to solve the problem by reducing x
					if(x==0) {
						// Just too small a screen!
						msg("Sorry cannot autoadjust, screen too small!");
						return;
					}
					x--;
				}
				Forbid();
				ChangeWindowBox(win_p,x-((newwidth-width)/2),y,newwidth,height);
				Permit();
			}			
		break;
		case 2: /* Right */
			// Stretch window to the left

			// Are we doing a stretch or a shrink

			if(newwidth < width) 
				// Shrink
				ChangeWindowBox(win_p,x+(width-newwidth),y,newwidth,height);
			else {
				// Stretch
				// Do we have enough room?

				while(x-(newwidth-width)+newwidth > screentype.s_width) {
					// Try to solve the problem by reducing x
					if(x==0) {
						// Just too small a screen!
						msg("Sorry cannot autoadjust, screen too small!");
						return;
					}
					x--;
				}
				ChangeWindowBox(win_p,x-(newwidth-width),y,newwidth,height);
			}			
		break;
		default:
			return;
		break;
	}
	Delay(5);
}	
