SizeTkl.c

/*
    File:       SizeTkl.c
 
    Contains:   izeTkl.c contains the code to calculate the size of a pull-down menu.  Also,
                services are provided through CalItemHeight and CalcItemWidth to calculate the
                height and width of a single menu item.
 
    Written by:     
 
    Copyright:  Copyright © 1991-1999 by Apple Computer, Inc., All Rights Reserved.
 
                You may incorporate this Apple sample source code into your program(s) without
                restriction. This Apple sample source code has been provided "AS IS" and the
                responsibility for its operation is yours. You are not permitted to redistribute
                this Apple sample source code as "Apple sample source code" after having made
                changes. If you're going to re-distribute the source, we require that you make
                it clear in the source that the code was descended from Apple sample source
                code, but that you've made changes.
 
    Change History (most recent first):
                8/10/1999   Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
                
 
*/
 
/******************************************************************************\
* Header Files
\******************************************************************************/
 
#include <Memory.h>
#include <Quickdraw.h>
#include <Script.h>
#include <Types.h>
#include "Concordia.h"
#include "DrawTkl.h"
#include "SizeTkl.h"
 
 
#pragma segment Main
/******************************************************************************\
* DoSizeMsg - Calculate the horizontal and vertical size of menu in pixels
*
* DoSizeMsg calculates the horizontal and vertical size in pixels of the menu
* specified specified by TheMenu.  The menuWidth and menuHeight fields are
* filled with this information.
\******************************************************************************/
 
void
DoSizeMsg (TheMenu)
    MenuHandle TheMenu; //=> Menu to size <>
    {
    Str255      *ItemString; //-> Item's string
    ItemInfoPtr ItemInfo;    //-> Item info
    short       TestWidth;   //Width of item to test
    short       Height;      //Height of menu
    short       Width;       //Width of menu
    short       ScrnHeight;  //Screen height in pixels w/o menu bar & margin
 
    Height = Width = 0;
    ScrnHeight = qd.screenBits.bounds.bottom - qd.screenBits.
            bounds.top - GetMBarHeight () - scrnMargin;
    HLock ((Handle) TheMenu);
    ItemString = (Str255 *) (**TheMenu).menuData;
    ItemString = (Str255 *) ((Byte *) ItemString + strSize (*ItemString));
    while ((*ItemString) [0] != (char) 0)
        {
        ItemInfo = (ItemInfoPtr) ((Byte *) *ItemString + strSize (*ItemString));
        Height += CalcItemHeight (*ItemString, ItemInfo);
        TestWidth = CalcItemWidth (*ItemString, ItemInfo);
        if (TestWidth > Width)
            Width = TestWidth;
        ItemString = (Str255 *) (ItemInfo + 1);
        }
    HUnlock ((Handle) TheMenu);
    (**TheMenu).menuWidth = Width;
    if (Height > ScrnHeight)
        (**TheMenu).menuHeight = ScrnHeight;
    else
        (**TheMenu).menuHeight = Height;
    }
 
 
#pragma segment Main
/******************************************************************************\
* CalcItemHeight - Calculate the height of a menu item in pixels
*
* CalcItemHeight calculates the height of the menu item (whose information is
* specified by ItemData) in pixels.  The size of an icon and separation bars
* are accounted for.  The height is returned.  ItemString is the item's string.
* CurrFont is a FontInfo record containing information about the current item's
* font specs, taking the current style into account.
*
* Coding Note:
* #A# - If the first character of the item string is a hyphen, DrawItem (source
*       in DrawTkl.c) will just draw a gray, horizontal line.  The height of any
*       "gray, horizontal line" items will always be stdItemHeight.
* #B# - These command-key equivalents indicate that the item's icon should be
*       scaled to half its size.
\******************************************************************************/
 
short
CalcItemHeight (ItemString, ItemData)
    Str255      ItemString; //Name of menu item >>
    ItemInfoPtr ItemData;   //Information for menu item >>
    {
    short        TotHeight; //Total height of menu item in pixels
    short        Height;    //Height of menu item in pixels
    short        IconSize;  //Size of icon, including vertical margins
    FontInfo     CurrFont;  //Current font's characteristics
    TextStateRec TextState; //Current port's text characteristics
 
    TotHeight = itemVertMarg << 1;
    if (ItemString [1] == '-') //#A#
        TotHeight += stdItemHeight;
    else
        {
        GetTextState (&TextState);
        TextFace (ItemData->charStyle);
        GetFontInfo (&CurrFont);
        Height = CurrFont.ascent + CurrFont.descent + CurrFont.leading;
        if (ItemData->iconNum != (Byte) 0)
            {
            if (ItemData->kbdEquiv == (char) 0x1D || ItemData->kbdEquiv ==
                    (char) 0x1F) //#B#
                IconSize = (short) (iconSize / 2 + iconVertMarg * 2);
            else
                IconSize = (short) (iconSize + iconVertMarg * 2);
            if (IconSize > Height)
                Height = IconSize;
            }
        if (ItemData->kbdEquiv == (char) hMenuCmd)
            if (sicnSize > Height)
                Height = sicnSize;
        TotHeight += Height;
        SetTextState (&TextState);
        }
    return TotHeight;
    }
 
 
#pragma segment Main
/******************************************************************************\
* CalcItemWidth - Calculate the width of a menu item in pixels
*
* CalcItemWidth calculates the width of the menu item whose item data is
* specified by ItemData.  The item's string is given in ItemString, the specs
* for the current font (with item styles) is given in CurrFont, and the
* maximum width in pixels of the command-key equivalents is given in CmmdWidth.
* The textFace field of the current GrafPort might be changed.
*
* Coding Notes
* #A# - These command-key equivalents indicate that the item's icon should be
*       scaled to half its size.
\******************************************************************************/
 
short
CalcItemWidth (ItemString, ItemData)
    Str255      ItemString; //Name of menu item >>
    ItemInfoPtr ItemData;   //Information for menu item >>
    {
    short        TotWidth;  //Total width of menu item in pixels (except mark)
    short        Width;     //Height of menu item in pixels
    FontInfo     CurrFont;  //Current font's characteristics
    TextStateRec TextState; //Current port's text characteristics
 
    Width = 0;
    GetFontInfo (&CurrFont);
    TotWidth = (short) (itemHorzMarg << 1) + CurrFont.widMax + (short)
            markItemGap;
    if (ItemString [1] != '-')
        {
        GetTextState (&TextState);
        if (ItemData->kbdEquiv > '!')
            Width = CharWidth (cmmdCharCode) + CurrFont.widMax + cmmdItemGap;
        TextFace (ItemData->charStyle);
        Width += StringWidth (ItemString);
        if (ItemData->iconNum != (Byte) 0)
            if (ItemData->kbdEquiv == (char) 0x1D || ItemData->kbdEquiv ==
                    (char) 0x1F) //#A#
                Width += (short) (iconSize / 2 + iconItemGap);
            else
                Width += (short) (iconSize + iconItemGap);
        if (ItemData->kbdEquiv == (char) hMenuCmd)
            Width += sicnSize;
        TotWidth += Width;
        SetTextState (&TextState);
        }
    return TotWidth;
    }