Copyright (c) Hyperion Entertainment and contributors.

ASL Font Requester

From AmigaOS Documentation Wiki
Jump to navigation Jump to search

The ASL library also contains a font requester. Using the font requester is very similar to using the file requester. First, allocate a requester structure with AllocAslRequest() or AllocAslRequestTags(). The type should be set to ASL_FontRequest in order to get a FontRequester structure:

struct FontRequester    {
    APTR    fo_Reserved1[2];
    struct TextAttr fo_Attr;    /* Returned TextAttr            */
    UBYTE   fo_FrontPen;        /* Returned pens, if selected   */
    UBYTE   fo_BackPen;
    UBYTE   fo_DrawMode;
    APTR    fo_UserData;
    /* missing from asl.h but present in this structure */
    SHORT   fo_LeftEdge, fo_TopEdge, fo_Width, fo_Height;
    };

Once the requester is set up, call AslRequest() or AslRequestTags() to make the requester appear on screen. These functions return TRUE if the user makes a selection. In that case, the font selected is returned as a TextAttr structure in the fo_Attr field of the FontRequester structure. (The TextAttr structure is defined in <graphics/text.h>. See the SDK for a complete listing.) If the user cancels the font requester FALSE is returned.

The ASL Font Requester

When the requester is no longer needed, call FreeAslRequest() to deallocate the requester data structure.

Specifying Font Requester Options With TagItems

As with a file requester, the font requester is specified with a TagItem list. There are several tags that are specific to the font requester:

Font Requester Tag Name Used For
ASLFO_InitialName Initial font name selection
ASLFO_InitialSize Initial font size
ASLFO_InitialStyle Initial setting of font Style gadget
ASLFO_Flags Various font requester options
ASLFO_InitialFrontPen Initial setting of Front Color gadget
ASLFO_InitialBackPen Initial setting of Back Color gadget
ASLFO_ModeList Alternate strings for Mode cycle gadget (see below)
ASLFO_MinHeight Specifies the minimum height of fonts to be listed
ASLFO_MaxHeight Specifies the maximum height of fonts to be listed
ASLFO_DoFrontPen Causes the requester to display the Front Color selection gadget (this replaces the FONF_FRONTCOLOR flag in ASL_FuncFlags used in V37).
ASLFO_DoBackPen Causes the requester to display the Back Color selection gadget (this replaces the FONF_BACKCOLOR flag in ASL_FuncFlags used in V37).
ASLFO_DoStyle Causes the requester to display Style checkbox gadgets (this replaces the FONF_STYLES flag in ASL_FuncFlags used in V37).
ASLFO_DoDrawMode Causes the requester to display the Mode cycle gadget.(this replaces the FONF_DRAWMODE flag in ASL_FuncFlags used in V37).
ASLFO_FixedWidthOnly Causes the requester to list only fixed-width fonts (this replaces the FONF_FIXEDWIDTH flag in ASL_FuncFlags used in V37).
ASLFO_InitialDrawMode Initial setting of the font Mode gadget.

Note that the last two tags only limit the range of font sizes that the font requester displays, the user is free to type in any value.

Font requesters have additional special options that are controlled through the ASL_FuncFlags tag. This tag works the same way as it does with file requesters but with different options available. Recall that the data for this tag is divided into bit fields, each of which controls a requester option. The flags used with the ASL_FuncFlags tag in a font requester are defined in <libraries/asl.h>:

Font Requester Flags Used For
FONF_FRONTCOLOR Enables font color selection gadgets
FONF_BACKCOLOR Enables font background color selection gadget
FONF_STYLES Enables font style selection gadget
FONF_FIXEDWIDTH Limits display to fixed width fonts only
FONF_DRAWMODE Enables font draw mode gadget

A simple font requester (one without any of the above FONF_ flags set) only lets the user choose a font and a Y size. Setting the flags above adds options to the font requester. FONF_FRONTCOLOR and FONF_BACKCOLOR add color selection gadgets to the requester, one for choosing a font's foreground color (labeled "Text") and the other for choosing the background color (labeled "Field"). The font requester records the user's setting in the FontRequester's fo_FrontPen and fo_BackPen fields.

FONF_STYLES sets up several gadgets to choose the style of the font (bold, italics, underline). The font requester saves these settings in the fo_Attr.ta_Style bit field according to the style flags defined in <graphics/text.h>. FONF_FIXEDWIDTH limits the font name display to fixed width (non-proportional) fonts (note that this does not prevent the user from typing in a proportional font name).

FONF_DRAWMODE adds a cycle gadget to the font requester so the user can choose the draw mode. The draw mode is saved in the requester's fo_DrawMode field. The number stored there corresponds to the draw mode's position in the gadget's cycle.

The draw mode cycle gadget initially is labeled "Mode" and has three elements in its cycle: "JAM1", "JAM2", and "Complement". These yield a result of 0, 1, and 2, respectively. It is possible to change the names and number of draw modes with the ASL_ModeList tag. This tag accepts a pointer to an array of strings. The first string replaces "Mode" as the label for the draw mode cycle gadget. The strings that follow replace the elements of the cycle gadget. The last entry in the array has to be NULL to tell the requester where the list of entries ends.

Example Font Requester

The following example illustrates how to use a font requester.

/*
** fontreq.c
*/
 
#include <exec/types.h>
#include <libraries/asl.h>
 
#include <proto/asl.h>
#include <proto/exec.h>
#include <proto/dos.h>
 
struct AslIFace *IAsl = NULL;
 
/* Our replacement strings for the "mode" cycle gadget.  The
** first string is the cycle gadget's label.  The other strings
** are the actual strings that will appear on the cycle gadget.
*/
CONST_STRPTR modelist[] =
{
    "Amiga Modes",
    "Mode 0",
    "Mode 1",
    "Mode 2",
    "Mode 3",
    "Mode 4",
    NULL
};
 
 
int main(int argc, char **argv)
{
    struct FontRequester *fr;
 
    struct Library *AslBase = IExec->OpenLibrary("asl.library", 50);
    IAsl = (struct AslIFace*)IExec->GetInterface(AslBase, "main", 1, NULL);
 
    if (IAsl != NULL)
    {
        if (fr = (struct FontRequester *)
            IAsl->AllocAslRequestTags(ASL_FontRequest,
                /* tell the requester to use my custom mode names */
                ASL_ModeList, modelist,
 
                /* Supply initial values for requester */
                ASL_FontName, "topaz.font",
                ASL_FontHeight, 11,
                ASL_FontStyles, FSF_BOLD | FSF_ITALIC,
                ASL_FrontPen,  0x00,
                ASL_BackPen,   0x01,
 
                /* Only display font sizes between 8 and 14, inclusive. */
                ASL_MinHeight, 8,
                ASL_MaxHeight, 14,
 
                /* Give all the gadgetry, but only display fixed width fonts */
                ASL_FuncFlags, FONF_FRONTCOLOR | FONF_BACKCOLOR |
                    FONF_DRAWMODE | FONF_STYLES | FONF_FIXEDWIDTH,
                TAG_END))
        {
            /* Pop up the requester */
            if (IAsl->AslRequest(fr, NULL))
            {
                /* The user selected something,  report their choice */
                IDOS->Printf("%s\n  YSize = %ld  Style = 0x%lx   Flags = 0x%lx\n"
                       "  FPen = 0x%lx   BPen = 0x%lx   DrawMode = 0x%lx\n",
                               fr->fo_Attr.ta_Name,
                               fr->fo_Attr.ta_YSize,
                               fr->fo_Attr.ta_Style,
                               fr->fo_Attr.ta_Flags,
                               fr->fo_FrontPen,
                               fr->fo_BackPen,
                               fr->fo_DrawMode);
            }
            else
                /* The user cancelled the requester, or some kind of error
                ** occurred preventing the requester from opening. */
                IDOS->Printf("Request Cancelled\n");
 
            IAsl->FreeAslRequest(fr);
        }
    }
 
    IExec->DropInterface((struct Interface*)IAsl);
    IExec->CloseLibrary(AslBase);
 
    return 0;
}