MAG Disk (Jun 1990) : source / Convert.c

#include "PopUpMenu.h"

/****************************************************************
 * FinalSelect()  Do checkmarking and calculate Menupick value. *
 *								*
 * Input:							*
 *   none							*
 * Output:							*
 *   return  - MenuPick-value or MENUNULL.			*
 ****************************************************************/
WORDBITS FinalSelect()
{
  IMPORT const UWORD  CurrentMenuNr;
  IMPORT struct MenuItem *const CurrentItem, *const CurrentSubItem;
  IMPORT struct WindowData ItemWindow, SubWindow;
  IMPORT struct RastPort Rp;

  UWORD  SubItemNr = 31;
  UWORD  ItemNr;

  if ((CurrentSubItem) AND (CurrentSubItem->Flags & ITEMENABLED)) {

    /* subitem selected -> checkmark and draw all subitems again */

    CheckMark(CurrentSubItem,SubWindow.Items);
    DrawAllItems(&SubWindow);
    HighLightItem(CurrentSubItem,&SubWindow,HIGHLIGHTON);

    /* clear nextselect field */
    CurrentSubItem->NextSelect = MENUNULL;

    /* find the number of the selected subitem */

    SubItemNr = FindItemNr(CurrentSubItem,SubWindow.Items);

    /* remove the subitem-window so we can checkmark the items */
    SwapBits(&SubWindow);
  }
  else
    if ((CurrentItem) AND (CurrentItem->Flags & ITEMENABLED) AND
	!(CurrentItem->SubItem))
      CurrentItem->NextSelect = MENUNULL;
    else
      /* nothing is selected -> exit */
      return (MENUNULL);

  CheckMark(CurrentItem,ItemWindow.Items);

  /* find number of selected item */
  ItemNr = FindItemNr(CurrentItem,ItemWindow.Items);

  DrawAllItems(&ItemWindow);
  HighLightItem(CurrentItem,&ItemWindow,HIGHLIGHTON);

  /* restore subwindow */
  SwapBits(&SubWindow);

  return (SHIFTMENU((CurrentMenuNr - 1)) |
	  SHIFTITEM(ItemNr) |
	  SHIFTSUB(SubItemNr));
}

/********************************************************
 * CheckMark(Selected,Items) - Checkmark selected item. *
 *							*
 * Input:						*
 *   Selected - The selected item.			*
 *   Items    - All items on the same level.		*
 * Output:						*
 *   none						*
 ********************************************************/
VOID CheckMark(Selected,Items)
  struct MenuItem *const Selected;
  struct MenuItem *Items;
{
  if (Selected->Flags & CHECKIT) {
    if (Selected->Flags & CHECKED) {
      if (Selected->Flags & MENUTOGGLE)
	Selected->Flags &= ~CHECKED;
    }
    else {
      LONGBITS	Exclude = Selected->MutualExclude;

      Selected->Flags |= CHECKED;

      /* Handle mutual exclusion */

      if (Exclude)
	do {
	  if (Exclude & 1)
	    Items->Flags &= ~CHECKED;
	  Exclude >>= 1;
	}
	while ((Items = Items->NextItem) != NULL);
    } /* if CHECKED */
  } /* if CHECKIT */
}

/**********************************************
 * FindMenuPtr(Number) - Find Menu structure. *
 *					      *
 * INPUT				      *
 *   Number - Number of the menu to look for. *
 * OUTPUT				      *
 *   return - Ptr to the Menu structure.      *
 **********************************************/
struct Menu *FindMenuPtr(Number)
  UWORD  Number;
{
  IMPORT struct Menu *const Menues;

  struct Menu *MenuPtr = Menues;

  while ((MenuPtr) AND (--Number))
    MenuPtr = MenuPtr->NextMenu;

  return (MenuPtr);
}

/************************************************
 * FindItemNr(Item,ItemList) - Find nr of item. *
 *  WARNING: The item MUST exist		*
 *						*
 * Input:					*
 *   Item - Item to look for.			*
 *   ItemList - all items on same level.	*
 * OUTPUT					*
 *   return - Nr of Item.			*
 ************************************************/
UWORD  FindItemNr(Item,ItemList)
  struct MenuItem *const Item;
  struct MenuItem *ItemList;
{
  UWORD Count = 0;

  while (ItemList != Item) {
    Count++;
    ItemList = ItemList->NextItem;
  }
  return (Count);
}