/*********************************
*  WINDOW  09/29/90
*  Source file for STV
*  © Copyright 1990 Timm Martin
*  All Rights Reserved Worldwide
**********************************/

#include <exec/types.h>
#include <functions.h>
#include <intuition/intuition.h>
#include "func.h"
#include "main.h"

/*************
*  FONT USED
**************/

struct TextAttr text_attr =  /* GLOBAL */
{
  /* STRPTR ta_Name */ LATER,
  /* UWORD ta_YSize */ LATER,
  /* UBYTE ta_Style */ FS_NORMAL,
  /* UBYTE ta_Flags */ NULL,
};

/********************
*  WINDOW STRUCTURE
*********************/

#define WINDOW_TOP 2

#define MIN_WIDTH 300
#define MIN_HEIGHT 30

extern struct Gadget vprop_gadget;  /* gadget.c */

struct NewWindow new_window =
{
  /* SHORT           LeftEdge    */ LATER,
  /* SHORT           TopEdge     */ LATER,
  /* SHORT           Width       */ LATER,
  /* SHORT           Height      */ LATER,
  /* UBYTE           DetailPen   */ BLUE,
  /* UBYTE           BlockPen    */ WHITE,
  /* ULONG           IDCMPFlags  */ CLOSEWINDOW|GADGETDOWN|GADGETUP|MENUPICK|MOUSEBUTTONS|RAWKEY|REFRESHWINDOW,
  /* ULONG           Flags       */ ACTIVATE|SMART_REFRESH|WINDOWCLOSE|WINDOWDEPTH|WINDOWDRAG|WINDOWSIZING,
  /* struct Gadget * FirstGadget */ LATER,
  /* struct Image *  CheckMark   */ NULL,
  /* UBYTE *         Title       */ NULL,
  /* struct Screen * Screen      */ LATER,
  /* struct BitMap * BitMap      */ NULL,
  /* SHORT           MinWidth    */ MIN_WIDTH,
  /* SHORT           MinHeight   */ MIN_HEIGHT,
  /* USHORT          MaxWidth    */ 1024,
  /* USHORT          MaxHeight   */ 1024,
  /* USHORT          Type        */ LATER,
};

struct Window   *win = NULL;  /* GLOBAL */
struct RastPort *rp;          /* GLOBAL */

#define LEFT   new_window.LeftEdge
#define TOP    new_window.TopEdge
#define WIDTH  new_window.Width
#define HEIGHT new_window.Height

/****************
*  WINDOW CLOSE
*****************/

/*
This procedure closes the text window if opened.  It removes the gadgets
beforehand so Intuition doesn't try to free the memory associated with my
fake system gadgets.
*/

void window_close( void )
{
  if (win)
  {
    ClearMenuStrip( win );
    CloseWindow( win );
  }
}

/***************
*  WINDOW OPEN
****************/

/*
This procedure opens the window.
*/

void window_open( void )
{
  int height;            /* height of screen in which window will open */
  struct Screen screen;  /* to grab Workbench screen data */
  int width;             /* width of screen in which window will open */

  /* if custom screen desired */
  if (arguments.Screen)
  {
    width  = arguments.Screen->Width;
    height = arguments.Screen->Height;
    new_window.Screen = arguments.Screen;
    new_window.Type   = CUSTOMSCREEN;
  }
  /* else opening it on the Workbench screen */
  else
  {
    GetScreenData( &screen, (long)sizeof(struct Screen), WBENCHSCREEN, NULL );
    width  = screen.Width;
    height = screen.Height;
    new_window.Type = WBENCHSCREEN;
  }

  /* either use defaults or specified left and top edges */
  LEFT = arguments.Left >= 0 ? arguments.Left : 0;
  TOP  = arguments.Top  >= 0 ? arguments.Top  : WINDOW_TOP;

  /* left and top edge have priority -- make sure they are valid */
  if (LEFT + MIN_WIDTH > width)
    LEFT = width - MIN_WIDTH;
  if (TOP + MIN_HEIGHT > height)
    TOP = height - MIN_HEIGHT;

  /* either use default or specified width and height */
  WIDTH  = arguments.Width  > 0 ? arguments.Width  : width;
  HEIGHT = arguments.Height > 0 ? arguments.Height : height;

  /* now make sure the width and height are not too large */
  if (LEFT + WIDTH > width)
    WIDTH = width - LEFT;
  if (TOP + HEIGHT > height)
    HEIGHT = height - TOP;

  /* if window opened OK */
  if (win = OpenWindow( &new_window ))
  {
    SET_POINTER;

    rp = win->RPort;

    /* grab font information */
    stats.Font       = rp->Font;
    stats.TextWidth  = rp->TxWidth;
    stats.TextHeight = rp->TxHeight;
    stats.Baseline   = rp->TxBaseline;

    /* prepare the TextAttr structure */
    text_attr.ta_Name  = (STRPTR)stats.Font->tf_Message.mn_Node.ln_Name;
    text_attr.ta_YSize = stats.TextHeight;

    /* prevent this sucker from crashing */
    WindowLimits( win, 0L, (long)(win->BorderTop + stats.TextHeight + 4), 0L, 0L );

    gadget_add();
    menu_add();

    /* adjust dimensions based on window size */
    window_resized();
  }
  else
    program_end( "window" );
}

/******************
*  WINDOW RESIZED
*******************/

/*
This procedure redraws the window border and recalculates all of the
values dependent on the window width and height.
*/

void window_resized( void )
{
  /* start just inside left and top border */
  stats.LeftEdge = win->BorderLeft + 1;
  stats.TopEdge  = win->BorderTop + 1;

  /* calculate new number of rows and columns based on window size */
  stats.Cols = (win->Width - stats.LeftEdge - win->BorderRight) /
               stats.TextWidth;
  stats.Rows = (win->Height - stats.TopEdge - win->BorderBottom) /
               stats.TextHeight;

  /* find right and bottom edges based on text */
  stats.RightEdge = stats.LeftEdge + stats.Cols * stats.TextWidth - 1;
  stats.BottomEdge = stats.TopEdge + stats.Rows * stats.TextHeight - 1;

  stats.VCenter = ((stats.BottomEdge - stats.TopEdge) >> 1) + stats.TopEdge;

  /* makes sure Top is still valid after resizing window */
  line_check();
}
