#include "ezlib.h"

extern struct GfxBase *GfxBase;
extern struct IntuitionBase *IntuitionBase;
extern int openlibs();
void *AllocMem();


struct Window *createwindow(screen, flags, idcmp, leftedge, topedge, width, height)
 struct Screen *screen;
 ULONG flags, idcmp;
 int leftedge, topedge, width, height;
{
 struct Window *OpenWindow();
 int wb_height;
 struct NewWindow *tempwin;
 struct Window *win;

 /* some sanity checking - short and fast */
 if(GfxBase == NULL || IntuitionBase == NULL)
  if ( openlibs(GFX | INTUITION) == NULL)
    return NULL;

 tempwin = (struct NewWindow *)AllocMem(sizeof(struct NewWindow), MEMF_CLEAR);
 if (tempwin == NULL)
   return NULL;

 if (width < 0)                            /* more checking */
   width = GfxBase->NormalDisplayColumns;
 if (height < 0)
    height = 1500;

 tempwin->Flags    = flags;	       tempwin->IDCMPFlags = idcmp;
 tempwin->LeftEdge = (SHORT)leftedge;  tempwin->Width      = (SHORT)width;
 tempwin->TopEdge  = (SHORT)topedge;   tempwin->Height     = (SHORT)height;
 tempwin->MinWidth = 60;	       tempwin->MinHeight  = 30;
 tempwin->DetailPen= -1;	       tempwin->BlockPen   = -1;
 tempwin->Type	   = WBENCHSCREEN;
 tempwin->Screen = screen;    /* if it's null that's o.k. too */

 /* if user has a custom screen, open up on that screen */
 if (screen) {
   tempwin->Type = CUSTOMSCREEN;
   if (leftedge + width > screen->Width)
     tempwin->Width = (screen->Width - leftedge);

   if (topedge + height > screen->Height)
     tempwin->Height = (screen->Height - topedge);
  }
 else {
   if (leftedge+width > GfxBase->NormalDisplayColumns)
     tempwin->Width = (GfxBase->NormalDisplayColumns - leftedge);

   /* gotta make sure to check for interlace */
   wb_height = (GfxBase->NormalDisplayRows * (1 + laced_wb()) );
   if (topedge + height > wb_height )
     tempwin->Height = wb_height - topedge;
  }

 win = OpenWindow(tempwin);

 FreeMem(tempwin, sizeof(struct NewWindow));
 return win;
}   /* end of makewindow() */



/* return TRUE (1) if wb screen is interlaced otherwise,
 * we return FALSE (0) if it is NOT interlaced
 */
laced_wb()
{
 struct Preferences prefs;

 GetPrefs(&prefs, sizeof(struct Preferences));
 if (prefs.LaceWB)
   return 1;
 else
   return 0;
}  /*  end of laced_wb()  */


killwindow(window)
 register struct Window *window;
{
 register struct IntuiMessage *msg, *GetMsg();

 if (window < 100)
   return;

 /* make sure this is NULL if you have already cleared out your menus. */
 if (window->MenuStrip != NULL)
   ClearMenuStrip(window);

 /* make sure there aren't any junk messages hanging here */
 while(window->UserPort != NULL && (msg = GetMsg(window->UserPort)) != NULL )
   ReplyMsg(msg);

 /* then just close the window */
 CloseWindow(window);
}

/* this routine will make your standard Intuition Window.  It creates
 * a window with all the standard system gadgets on it, and makes it
 * the appropriate size (with sanity checking)
 */

struct Window *makewindow(screen, leftedge, topedge, width, height)
 struct Screen *screen;
 int leftedge, topedge, width, height;
{
  static ULONG idcmp =	CLOSEWINDOW,		/* IDCMP  Classes	*/
						/* system flags 	*/
	 flags =  WINDOWCLOSE|WINDOWDEPTH|WINDOWDRAG|WINDOWSIZING|NOCAREREFRESH|SMART_REFRESH|ACTIVATE;

 return (struct Window *)createwindow(screen, flags, idcmp, leftedge, topedge, width, height);
}


