
/*
 *	Function	SpoolWindow
 *	Programmer	N.d'Alterio
 *	Date		09/09/95
 *
 *  Synopsis:	Opens window (title bar size) with a title that gives
 *		information about the number of files spooled.
 *
 *  Arguments:	struct SWArgs *swa		Prog args structure.
 *		struct FullInfo *mfi		Mail FullInfo structure.
 *		struct FullInfo *nfi		News FullInfo structure.
 *
 *  Returns:	*spoolw_win			Pointer to window opened.
 *		NULL				If error.
 *
 *  Variables:	 swin_width			Min window width needed.
 *		 swin_height			Min window height needed.
 *		 bwin_width			Max window width needed.
 *		 bwin_height			Max window height needed
 *		 num_mail			Number of mail msgs.
 *		 num_news			Number of news msgs.
 *		 max_lines			Maximum number of messages.
 *		 exit_code			Function return code.
 *		*size_str			Temp string to get width.
 *		*pub_scr			Pointer to public screen.
 *		*spoolw_win			Pointer to window opened.
 *		*blank				String of spaces.
 *		 it				IntuiText structure.
 *		 tat				Text attribute struct.
 *		 backpen			Background pen colour.
 *		 textpen			Text pen colour.
 *		*di				Screen drawinfo struct.
 *		*vi				Visual info struct.
 *		*gad				Pointer to gadget.
 *		*gad_list			Pointer to gadgets linked list.
 *		 ng				New gadget struct.
 * 
 *  Functions:	OpenLibrary			Opens a library (EXEC)
 *		OpenWindowTags			Opens a window (INT)
 *		LockPubScreen			Locks default public screen (INT)
 *		UnLockPubScreen			Unlocks public screen (INT)
 *		IntuiTextLength			Finds length of string in pixels (INT)
 *
 *  $Id: SpoolWindow.c 2.5 1995/10/02 22:25:17 daltern Exp $
 *
 */

#include "SpoolWatch.h"

extern struct Library *IntuitionBase;
extern struct GfxBase *GfxBase;
extern struct Library *GadToolsBase;

int SpoolWindow( struct SWArgs *swa, struct FullInfo *nfi, struct FullInfo *mfi ) 

{

  int i;
  int max_lines;
  int exit_code;

  int num_mail;
  int num_news;

  long swin_width;
  long swin_height;
  long bwin_width;
  long bwin_height;

  char *size_str   = "Mail: 000 News: 000\0";
  char blank[FULLINFO_LEN+1];

  struct Screen *pub_scr;
  struct Window *spoolw_win;
  
  struct IntuiText it;

  struct TextAttr   tat;

  ULONG backpen;
  ULONG textpen;

  struct DrawInfo *di;

  long int gad_width;
  long int gad_height;

  struct Gadget    *gad;
  struct Gadget    *gadlist = NULL;
  struct NewGadget  ng;

  void		   *vi;

/*
 *   Find out max files in spooldir.
 */

  num_mail = CountSpool( mfi );
  num_news = CountSpool( nfi );

  max_lines = (num_news>num_mail) ? num_news : num_mail;
		
  for ( i = 0; i < FULLINFO_LEN; i++ ) blank[i] = ' ';
  blank[FULLINFO_LEN-1] = '\0';

 if ( GfxBase = (struct GfxBase *)OpenLibrary( "graphics.library", 37 ) ) {
  if ( IntuitionBase = OpenLibrary( "intuition.library", 37 ) ) {
   if ( GadToolsBase = OpenLibrary( "gadtools.library", 37 ) ) {

/*
 *   Find public screen and lock until window has been opened.
 */

	if ( pub_scr = LockPubScreen( NULL ) ) {

/*
 *  Get width of window needed for title in default font for screen.
 */

		it.ITextFont = pub_scr->Font;
		it.IText     = size_str;

/*
 *   IntuiTextLength does not seem to work for fixed width fonts so use
 *   a different method for getting window size.
 */

		if ( pub_scr->Font->ta_Flags & FPF_PROPORTIONAL ) {
			swin_width  = IntuiTextLength( &it ) + ( pub_scr->FirstGadget->Width * 2 ) + 7;
		} else {
			swin_width  = ( pub_scr->RastPort.Font->tf_XSize * 21 ) + ( pub_scr->FirstGadget->Width * 2 ) + 7;
		}    /* end if */

		swin_height = pub_scr->WBorTop + pub_scr->Font->ta_YSize + 1;

		if ( di = GetScreenDrawInfo( pub_scr ) ) {

			backpen = di->dri_Pens[BACKGROUNDPEN];
			textpen = di->dri_Pens[TEXTPEN];

			FreeScreenDrawInfo( pub_scr, di );

		}   /* end if di */

		tat.ta_Name  = GfxBase->DefaultFont->tf_Message.mn_Node.ln_Name;
  		tat.ta_YSize = GfxBase->DefaultFont->tf_YSize;
		tat.ta_Style = GfxBase->DefaultFont->tf_Style;
		tat.ta_Flags = GfxBase->DefaultFont->tf_Flags;

		it.FrontPen  = textpen;
		it.BackPen   = backpen;
		it.DrawMode  = JAM2;
		it.LeftEdge  = 0;
		it.TopEdge   = 0;
		it.ITextFont = &tat;
		it.NextText  = NULL;

		it.IText     = blank;

		gad_width    = IntuiTextLength( &it ) + 8;
		gad_height   = it.ITextFont->ta_YSize + 6;

	  	bwin_width   = IntuiTextLength( &it ) + pub_scr->WBorLeft + pub_scr->WBorRight + 15;
  		bwin_height  = ( pub_scr->WBorTop + pub_scr->Font->ta_YSize + 1 ) + pub_scr->WBorBottom + ( it.ITextFont->ta_YSize * (max_lines+2) ) + 15 + gad_height;

		if ( vi  = GetVisualInfo( pub_scr, TAG_END ) ) {

			gad = CreateContext( &gadlist );

			ng.ng_TextAttr   = &tat;
			ng.ng_VisualInfo = vi;
			ng.ng_LeftEdge   = (bwin_width/2)-(gad_width/2);
			ng.ng_TopEdge    = pub_scr->WBorTop + pub_scr->Font->ta_YSize + 6;
			ng.ng_Height     = gad_height;
			ng.ng_Width      = gad_width;
			ng.ng_GadgetText = "News/Mail";
			ng.ng_GadgetID   = 2;
			ng.ng_Flags	 = PLACETEXT_IN;

			if ( gad = CreateGadget( BUTTON_KIND, gad, &ng, TAG_END ) ) {


/*
 *   Open the window.
 */

				spoolw_win = OpenWindowTags( 
					NULL,
					WA_Left,            swa->win_left,       
					WA_Top,             swa->win_top,
					WA_Width,           swin_width,   
					WA_Height,	    swin_height,
					WA_MinHeight,	    swin_height,
					WA_MinWidth,	    swin_width,
					WA_MaxHeight,	    bwin_height,
					WA_MaxWidth,	    bwin_width,
					WA_AutoAdjust,	    TRUE,
					WA_DragBar,         TRUE,
					WA_CloseGadget,     TRUE,
					WA_Gadgets,	    gadlist,
					WA_RMBTrap,	    TRUE,
					WA_SmartRefresh,    TRUE,
					WA_IDCMP,           IDCMP_CLOSEWINDOW | 
							    IDCMP_MOUSEBUTTONS |
							    IDCMP_NEWSIZE |
						 	    IDCMP_GADGETUP |
						    	    IDCMP_REFRESHWINDOW,
					WA_Title,           "",
					WA_ScreenTitle,	    SCREEN_TITLE,
					WA_PubScreen,       pub_scr,
					WA_DepthGadget,	    TRUE,
							    TAG_END );


/*
 *   Go into event loop.
 */

				if ( spoolw_win ) {

					GT_RefreshWindow( spoolw_win, NULL );
					exit_code = HandleEvents( spoolw_win, gad, &it, swa, nfi, mfi );
					CloseWindow( spoolw_win );

				} else {

					SWError( swa, NULL, "Could not open window" );
					exit_code = FAIL;

				}   /* end if spoolw_win */

				FreeGadgets( gadlist );

			} else {

				SWError( swa, NULL, "Could not create gadget" );
				exit_code = FAIL;

			}   /* end if gad */

			FreeVisualInfo( vi );

		} else {

			SWError( swa, NULL, "Could not get visual info" );
			exit_code = FAIL;

		}   /* end if vi */

		UnlockPubScreen( NULL, pub_scr );

  	} else {
		SWError( swa, NULL, "Failed to lock public screen" );
		exit_code = FAIL;
	}  /* end if lock screen */

	CloseLibrary( GadToolsBase );

   } else {
	SWError( swa, NULL, "Could not open library" );
	exit_code = FAIL;
   }   /* end if GadToolsBase */

   CloseLibrary( IntuitionBase );

  } else {
	SWError( swa, NULL, "Could not open library" );
   	exit_code = FAIL;
  }  /* end if IntuitionBase */

  CloseLibrary( (struct Library *)GfxBase );

 } else {
	SWError( swa, NULL, "Could not open library" );
	exit_code = FAIL;
 }  /* end if GfxBase */

 return exit_code;

}   /* end function SpoolWindow */

/*========================================================================*
                        END FUNCTION SpoolWindow
 *========================================================================*/
