#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>
#include <proto/locale.h>

#include "prefs.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 long mytextlength(char *string, long len);

extern struct Catalog *catalog;
extern struct TextFont *win_font;

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;

extern struct Prefs 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;
	int mh;
	
	if(prefs.date)
		mh=screentype.s_bar+(font.y*2)+16;
	else
		mh=screentype.s_bar+font.y+8;

	// 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 !=mh) {
		// 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=mh;

		ClearMenuStrip(win_p);
		CloseWindow(win_p);

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

	// Get string lengths

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


	// Which one is bigger?

	if(length1 >= length2)
		newwidth=length1;
	else
		newwidth=length2;

	// Now the greater length is in length1

	newwidth+=16; /* Initial offset from LHS */
	newwidth+=16; /* Keep it even each side */
	newwidth+=15; /* 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(GetCatalogStr(catalog,28,"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(GetCatalogStr(catalog,28,"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(GetCatalogStr(catalog,28,"Sorry cannot autoadjust, screen too small!"));
						return;
					}
					x--;
				}
				ChangeWindowBox(win_p,x-(newwidth-width),y,newwidth,height);
			}			
		break;
		default:
			return;
		break;
	}
	Delay(5);
}	
