/*
 *  INITNEWWINDOW.C
 */

#include "newlook.h"

#ifndef AUTOSCROLL
#define AUTOSCROLL 0x4000  /* V36+ */
#endif /* !AUTOSCROLL */

extern BOOL GetScreenData( APTR, UWORD, UWORD, struct Screen * );

/*
 * BUGS
 *   Ich habe keinen Plan, wie ich die Breite des rechten WindowBorders
 *   rausfinden soll, wenn das Window ein SizeGadget hat.
 */

struct NewWindow *InitNewWindow(nw, title, w,h, idcmp,flags, glist)

struct NewWindow *nw;  /* ... will be allocated if nw == NULL */
UBYTE *title;
WORD w,h;              /* inner (!) window dimensions */
ULONG idcmp, flags;
struct Gadget *glist;
{
  if(!nw)
  {
    if(nw= (struct NewWindow *)SmartAllocate(NEWWINDOWSIZE))
      nw->Screen= (struct Screen *)NULL;
  }

  if(nw)
  {
    struct Screen sbuf, *s= nw->Screen;

    if(!s)
    {
      nw->Type= (UWORD)WBENCHSCREEN;

      if(GetScreenData(&sbuf, sizeof(struct Screen), nw->Type, NULL))
      {
        /* GetScreenData() lies about dimensions */
        sbuf.Width  = sbuf.ViewPort.DWidth;
        sbuf.Height = sbuf.ViewPort.DHeight;
        s= &sbuf;
      }
    }
    /* else nw->Type must be set by the caller !!! */

    /* set parameter values */
    nw->Width  = w;
    nw->Height = h;

    if(s)
    {
      nw->Width  += s->WBorLeft + s->WBorRight;
      nw->Height += s->WBorTop  + s->RastPort.TxHeight+1 + s->WBorBottom;

      /*
       *  If we're going to open on an AUTOSCROLL Screen it is not
       *  wise to appear at a fix position so we'll try to pop up
       *  near the current location of the mouse pointer...
       */

      if(s->Flags & AUTOSCROLL)
      {
        /* pop up under the mouse */
        if((nw->LeftEdge= s->MouseX - nw->Width/2) < 0)
          nw->LeftEdge= 0;
        else if(nw->LeftEdge + nw->Width > s->Width)
          nw->LeftEdge= s->Width - nw->Width;

        if((nw->TopEdge= s->MouseY - nw->Height/2) < 0)
          nw->TopEdge= 0;
        else if(nw->TopEdge+nw->Height > s->Height)
          nw->TopEdge= s->Height - nw->Height;
      }
      else /* !AUTOSCROLL */
      {
        /* center window on screen */
        nw->LeftEdge = (s->Width - nw->Width)/2;
        nw->TopEdge  = (s->Height - nw->Height)/2;
      }
    }
    else /* !Screen */
    {
      nw->LeftEdge  =
      nw->TopEdge   = (WORD)0L;
    }

    /* parameter values */
    nw->IDCMPFlags  = idcmp;
    nw->Flags       = flags;
    nw->FirstGadget = glist;
    nw->Title       = title;

    /* default values */
    nw->DetailPen   = (UBYTE)0L;
    nw->BlockPen    = (UBYTE)1L;
    nw->CheckMark   = (struct Image *)NULL;
    nw->BitMap      = (struct BitMap *)NULL;
    nw->MinWidth    = 
    nw->MinHeight   = (WORD)0L;
    nw->MaxWidth    =
    nw->MaxHeight   = (UWORD)0L;
  }

  return nw;
}
