/*********************************
*  MENU  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"

/*******************
*  MENU STRUCTURES
********************/

/* left edge of text             */  #define MENU_TEXT_LEFT 4
/* top edge of text              */  #define MENU_TEXT_TOP 1
/* how long is each text string? */  #define MENU_TEXT_LENGTH 18

struct IntuiText prev_mtext =
{
  /* UBYTE              FrontPen  */ BLACK,
  /* UBYTE              BackPen   */ WHITE,
  /* UBYTE              DrawMode  */ JAM1,
  /* SHORT              LeftEdge  */ MENU_TEXT_LEFT,
  /* SHORT              TopEdge   */ MENU_TEXT_TOP,
  /* struct TextAttr *  ITextFont */ &text_attr,
  /* UBYTE *            IText     */ (STRPTR)"Previous File    P",
  /* struct IntuiText * NextText  */ NULL,
};
struct IntuiText next_mtext =
{
  /* UBYTE              FrontPen  */ BLACK,
  /* UBYTE              BackPen   */ WHITE,
  /* UBYTE              DrawMode  */ JAM1,
  /* SHORT              LeftEdge  */ MENU_TEXT_LEFT,
  /* SHORT              TopEdge   */ MENU_TEXT_TOP,
  /* struct TextAttr *  ITextFont */ &text_attr,
  /* UBYTE *            IText     */ (STRPTR)"Next File        N",
  /* struct IntuiText * NextText  */ NULL,
};
struct IntuiText search_mtext =
{
  /* UBYTE              FrontPen  */ BLACK,
  /* UBYTE              BackPen   */ WHITE,
  /* UBYTE              DrawMode  */ JAM1,
  /* SHORT              LeftEdge  */ MENU_TEXT_LEFT,
  /* SHORT              TopEdge   */ MENU_TEXT_TOP,
  /* struct TextAttr *  ITextFont */ &text_attr,
  /* UBYTE *            IText     */ (STRPTR)"Search           S",
  /* struct IntuiText * NextText  */ NULL,
};
struct IntuiText again_mtext =
{
  /* UBYTE              FrontPen  */ BLACK,
  /* UBYTE              BackPen   */ WHITE,
  /* UBYTE              DrawMode  */ JAM1,
  /* SHORT              LeftEdge  */ MENU_TEXT_LEFT,
  /* SHORT              TopEdge   */ MENU_TEXT_TOP,
  /* struct TextAttr *  ITextFont */ &text_attr,
  /* UBYTE *            IText     */ (STRPTR)"Search Again     A",
  /* struct IntuiText * NextText  */ NULL,
};
struct IntuiText quit_mtext =
{
  /* UBYTE              FrontPen  */ BLACK,
  /* UBYTE              BackPen   */ WHITE,
  /* UBYTE              DrawMode  */ JAM1,
  /* SHORT              LeftEdge  */ MENU_TEXT_LEFT,
  /* SHORT              TopEdge   */ MENU_TEXT_TOP,
  /* struct TextAttr *  ITextFont */ &text_attr,
  /* UBYTE *            IText     */ (STRPTR)"Quit             Q",
  /* struct IntuiText * NextText  */ NULL,
};

/* left edge of each menu item */
#define MENU_LEFT 0

struct MenuItem quit_item =
{
  /* struct MenuItem * NextItem      */ NULL,
  /* SHORT             LeftEdge      */ MENU_LEFT,
  /* SHORT             TopEdge       */ LATER,
  /* SHORT             Width         */ LATER,
  /* SHORT             Height        */ LATER,
  /* USHORT            Flags         */ HIGHCOMP|ITEMENABLED|ITEMTEXT,
  /* LONG              MutualExclude */ NULL,
  /* APTR              ItemFill      */ (APTR)&quit_mtext,
  /* APTR              SelectFill    */ NULL,
  /* BYTE              Command       */ NULL,
  /* struct MenuItem * SubItem       */ NULL,
  /* USHORT            NextSelect    */ MENUNULL,
};
struct MenuItem again_item =
{
  /* struct MenuItem * NextItem      */ &quit_item,
  /* SHORT             LeftEdge      */ MENU_LEFT,
  /* SHORT             TopEdge       */ LATER,
  /* SHORT             Width         */ LATER,
  /* SHORT             Height        */ LATER,
  /* USHORT            Flags         */ HIGHCOMP|ITEMENABLED|ITEMTEXT,
  /* LONG              MutualExclude */ NULL,
  /* APTR              ItemFill      */ (APTR)&again_mtext,
  /* APTR              SelectFill    */ NULL,
  /* BYTE              Command       */ NULL,
  /* struct MenuItem * SubItem       */ NULL,
  /* USHORT            NextSelect    */ MENUNULL,
};
struct MenuItem search_item =
{
  /* struct MenuItem * NextItem      */ &again_item,
  /* SHORT             LeftEdge      */ MENU_LEFT,
  /* SHORT             TopEdge       */ LATER,
  /* SHORT             Width         */ LATER,
  /* SHORT             Height        */ LATER,
  /* USHORT            Flags         */ HIGHCOMP|ITEMENABLED|ITEMTEXT,
  /* LONG              MutualExclude */ NULL,
  /* APTR              ItemFill      */ (APTR)&search_mtext,
  /* APTR              SelectFill    */ NULL,
  /* BYTE              Command       */ NULL,
  /* struct MenuItem * SubItem       */ NULL,
  /* USHORT            NextSelect    */ MENUNULL,
};
struct MenuItem next_item =
{
  /* struct MenuItem * NextItem      */ &search_item,
  /* SHORT             LeftEdge      */ MENU_LEFT,
  /* SHORT             TopEdge       */ LATER,
  /* SHORT             Width         */ LATER,
  /* SHORT             Height        */ LATER,
  /* USHORT            Flags         */ HIGHCOMP|ITEMENABLED|ITEMTEXT,
  /* LONG              MutualExclude */ NULL,
  /* APTR              ItemFill      */ (APTR)&next_mtext,
  /* APTR              SelectFill    */ NULL,
  /* BYTE              Command       */ NULL,
  /* struct MenuItem * SubItem       */ NULL,
  /* USHORT            NextSelect    */ MENUNULL,
};
struct MenuItem prev_item =
{
  /* struct MenuItem * NextItem      */ &next_item,
  /* SHORT             LeftEdge      */ MENU_LEFT,
  /* SHORT             TopEdge       */ LATER,
  /* SHORT             Width         */ LATER,
  /* SHORT             Height        */ LATER,
  /* USHORT            Flags         */ HIGHCOMP|ITEMENABLED|ITEMTEXT,
  /* LONG              MutualExclude */ NULL,
  /* APTR              ItemFill      */ (APTR)&prev_mtext,
  /* APTR              SelectFill    */ NULL,
  /* BYTE              Command       */ NULL,
  /* struct MenuItem * SubItem       */ NULL,
  /* USHORT            NextSelect    */ MENUNULL,
};
#define FIRST_ITEM prev_item

/* menu item numbers */
#define STV_PREV   0
#define STV_NEXT   1
#define STV_SEARCH 2
#define STV_AGAIN  3
#define STV_QUIT   4
/* menu number */
#define STV_MENU 0

/* length of the menu name */
#define MENU_NAME_LENGTH 18

struct Menu stv_menu =
{
  /* struct Menu *     NextMenu  */ NULL,
  /* SHORT             LeftEdge  */ 0,
  /* SHORT             TopEdge   */ 0,
  /* SHORT             Width     */ LATER,
  /* SHORT             Height    */ 10,
  /* USHORT            Flags     */ MENUENABLED,
  /* BYTE *            MenuName  */ "STV by Timm Martin",
  /* struct MenuItem * FirstItem */ &FIRST_ITEM,
};
#define FIRST_MENU stv_menu

/* menu item ordinal numbers for enabling/disabling them */
#define PREV_MENU MENUNUMBER(STV_MENU,STV_PREV,0)
#define NEXT_MENU MENUNUMBER(STV_MENU,STV_NEXT,0)

/************
*  MENU ADD
*************/

/*
This procedure calculates the TopEdge, Width, and Height of each menu item
and adds the menus to the window.
*/

void menu_add( void )
{
  int height;             /* height of each menu item */
  struct MenuItem *item;  /* pointer to current menu item */
  int top;                /* top edge of current menu item */
  int width;              /* width of each menu item */

  height = stats.TextHeight + MENU_TEXT_TOP * 2;
  width  = stats.TextWidth * MENU_TEXT_LENGTH + MENU_TEXT_LEFT * 2;

  /* calculate TopEdge, Width, and Height of each menu item */
  for (item = &FIRST_ITEM, top = 0; item; item = item->NextItem, top += height)
  {
    item->TopEdge = top;
    item->Width   = width;
    item->Height  = height;
  }
  stv_menu.Width = stats.TextWidth * MENU_NAME_LENGTH + 8;

  /* add menus to window */
  SetMenuStrip( win, &FIRST_MENU );
}

/*************
*  MENU PICK
**************/

/*
This function handles menu selections.  The 'menu' number was provided for
future expansion.
*/

SELECT menu_pick( int menu, int item )
{
  SELECT file = SELECT_SOME;

  switch (item)
  {
    case STV_PREV:   file = SELECT_PREV; break;
    case STV_NEXT:   file = SELECT_NEXT; break;
    case STV_QUIT:   file = SELECT_STOP; break;
    case STV_SEARCH: search();           break;
    case STV_AGAIN:  search_again();     break;
  }

  return (file);
}

/*****************
*  MENU PREVNEXT
******************/

/*
This procedure enables/disables the menu items based on whether a previous or
next file exists.
*/

void menu_prevnext( SELECT prev, SELECT next )
{
  if (prev == SELECT_NONE)
    OffMenu( win, PREV_MENU );
  else
    OnMenu( win, PREV_MENU );

  if (next == SELECT_NONE)
    OffMenu( win, NEXT_MENU );
  else
    OnMenu( win, NEXT_MENU );
}
