/* *****                                                            *****
** *****           Program created by Ken Farinsky, 1986.           *****
** *****                                                            *****
**
** All material contained herein may be used in any way desired.
**                    >>>>> i.e.  PUBLIC DOMAIN. <<<<<
** Not limited to any single bulletin board - can be freely transferred.
**
** Original distribution through:
**     Slipped Disk, Inc.
**     Madison Heights,  MI  48071
**     (313) 583-9803
**
** Distributed in the hopes of increasing the level of understanding of the
** Amiga personal computer.  Even the best computer can only succeed with
** the proper software support.
*/
#include <exec/types.h>
#include <intuition/intuition.h>
#include <functions.h>
#include <libraries/diskfont.h>
#include <exec/memory.h>
#include <stdio.h>
#include "view.h"

#define AVAILFONT_MEM		2000L
#define BIG_MENU_SIZE		((long)sizeof(struct big_MenuItem))
#define TEXT_SIZE			((long)sizeof(struct IntuiText))
#define MENU_WIDTH			180
#define MENU_HEIGHT			10
#define MAX_MENUCOLUMN		15
#define MENU_FLAGS			(ITEMENABLED | ITEMTEXT | HIGHCOMP | CHECKIT) 


struct	big_MenuItem
	{
	struct MenuItem		menu ;
	struct TextAttr	   *text ;
	} ;

/*---------------------------------------------------------------------
** font load procedure
**     the trick to this routine is the big_MenuItem structure.  the
** menu items are set up with the names of the fonts that they represent.
** these are then paired in the big_MenuItem structure with the actual
** TextAttr structure for that font.  so when the user picks one of the
** fonts from the menu (given the addr of the MenuItem), you can find
** the correct TextAttr and open the disk font!
**
** win is the window into which this is all taking place.
** load_menu is the Menu to which all of the fonts get attached.
** memory_key is used to remember what was allocated for later de-allocation.
*/
void font_load( Win, load_menu, memory_key)
struct Window		*Win ;
struct Menu			*load_menu ;
struct Remember    **memory_key ;
{
struct Menu				*head_menu ;
struct AvailFontsHeader	*afh ;
struct AvailFonts		*af ;

struct big_MenuItem		*top_item ;
struct big_MenuItem		*cur_item ;
struct big_MenuItem		*last_item ;
struct IntuiText		*cur_text ;

short	ktr ;
short	index ;
short	item_num ;
UBYTE  *cur_name ;
UBYTE	str_num[10] ;


top_item = NULL ;
last_item = NULL ;
item_num = 0 ;

head_menu = Win->MenuStrip ;
ClearMenuStrip( Win) ;

/*  allocate a big chunk of memory for the font info.  see the RKM for
** details.  once the fonts are read in, go through them (selecting the
** correct ones only), and build a list of MenuItems to attach to the
** menu strip.
**
** when all is completed the Items will be attached to the Menu structure
** that was passed as an arguement.
*/
if ( NULL != (afh = (struct AvailFontsHeader *)
		AllocRemember(memory_key,AVAILFONT_MEM,MEMF_CLEAR)))
	{
	AvailFonts( afh, AVAILFONT_MEM, 0xFFL) ;
	af = (struct AvailFonts *)&afh[1] ;
	for ( ktr = 0;
		  ktr < afh->afh_NumEntries;
		  af++,ktr++)
		{
		if ( ! ( (af->af_Attr.ta_Flags & FPF_REMOVED) ||
			     ( (af->af_Type & AFF_MEMORY) &&
				   (af->af_Attr.ta_Flags & FPF_DISKFONT))))
			{
			if ( ( NULL != (cur_item = (struct MenuItem *)
					AllocRemember(memory_key,BIG_MENU_SIZE,MEMF_CLEAR))) &&
				 ( NULL != (cur_name = (struct MenuItem *)
					AllocRemember(memory_key,100L,MEMF_CLEAR))) &&
				 ( NULL != (cur_text = (struct MenuItem *)
					AllocRemember(memory_key,TEXT_SIZE,MEMF_CLEAR))))
				{
				/* save the text attr with the menu item */
				cur_item->text = &(af->af_Attr) ;
				/* fix up last item pointer to next... */
				if (last_item != NULL)
					last_item->menu.NextItem = &(cur_item->menu) ;
				/* menu item */
				cur_item->menu.NextItem = NULL ;
				if ( item_num >= MAX_MENUCOLUMN)
					{
					cur_item->menu.LeftEdge = MENU_WIDTH ;
					cur_item->menu.TopEdge =
						(item_num - MAX_MENUCOLUMN) * MENU_HEIGHT ;
					}
				else
					{
					cur_item->menu.LeftEdge = 0 ;
					cur_item->menu.TopEdge = item_num * MENU_HEIGHT ;
					}
				cur_item->menu.Width = MENU_WIDTH ;
				cur_item->menu.Height = MENU_HEIGHT ;
				cur_item->menu.Flags = MENU_FLAGS ;
				cur_item->menu.MutualExclude = ~((long)(1<<item_num)) ;
				cur_item->menu.ItemFill = (APTR)cur_text ;
				cur_item->menu.SelectFill = NULL ;
				cur_item->menu.Command = (BYTE)' ' ;
				cur_item->menu.SubItem = NULL ;
				cur_item->menu.NextSelect = 0 ;
				/* menu item intuitext structure */
				cur_text->FrontPen = 0 ;
				cur_text->BackPen = 1 ;
				cur_text->DrawMode = JAM1 ;
				cur_text->LeftEdge = 0 ;
				cur_text->TopEdge = 1 ;
				cur_text->ITextFont = NULL ;
				cur_text->IText = cur_name ;
				cur_text->NextText = NULL ;
				/* fill in the name (menu entry string) */
				strcpy( cur_name, "    ") ;
				strcat( cur_name, af->af_Attr.ta_Name) ;
				for ( index = 0 ;
					  index < strlen(cur_name) ;
					  index++)
					if(*(cur_name + index) == '.')
						*(cur_name + index) = '\0' ;
				sprintf(str_num, " %d", af->af_Attr.ta_YSize) ;
				strcat( cur_name, str_num) ;

				item_num++ ;

				if ( top_item == NULL)
					top_item = last_item = cur_item ;
				else
					last_item = cur_item ;
				}
			}
		}
	}
if (top_item != NULL)
	load_menu->FirstItem = top_item ;
SetMenuStrip( Win, head_menu) ;
}
