Copyright (c) Hyperion Entertainment and contributors.

Difference between revisions of "ASL File Requester"

From AmigaOS Documentation Wiki
Jump to navigation Jump to search
Line 32: Line 32:
 
=== Specifying Requester Options With TagItems ===
 
=== Specifying Requester Options With TagItems ===
   
The file requester is specified with a TagItem list. There are several tags that are specific to the font requester:
+
The file requester is specified with a TagItem list. There are several tags that are specific to the file requester:
   
 
{| class="wikitable"
 
{| class="wikitable"
Line 76: Line 76:
 
| ASLFR_FilterDrawers || Makes ASLFR_RejectPattern and ASLFR_AcceptPattern apply to drawer names.
 
| ASLFR_FilterDrawers || Makes ASLFR_RejectPattern and ASLFR_AcceptPattern apply to drawer names.
 
|}
 
|}
 
Note that you are currently limited to about six characters for the replacement text if you use either the ASL_OKText or ASL_CancelText tags to change the text that appears in the OK and Cancel gadgets.
 
 
The contents of an ASL requester data structure are preserved across calls to AslRequest(). So, until the requester is freed, tag settings and user selections will remain in the data structure unless they are altered by tags in subsequent calls to AslRequest(). This is very useful because it allows the requester to remember and redisplay the user's previous selections. However, this also means that the programmer must assure that any addresses passed in ASL tags remain valid, or are refreshed on each call to AslRequest().
 
 
Generally, options that you wish to specify only once, such as the initial position and size, should be specified as tags when you allocate the requester. Options that you wish to control for each use of the requester should be passed as tags each time the requester is opened with AslRequest().
 
   
 
=== Simple ASL File Requester Example ===
 
=== Simple ASL File Requester Example ===

Revision as of 15:48, 25 April 2014

Creating a File Requester

The ASL library contains a file requester. First, allocate a requester structure with AllocAslRequest() or AllocAslRequestTags(). The type should be set to ASL_FileRequest in order to get a FileRequester structure:

struct FileRequester    {          /* (from <libraries/asl.h>)     */
        APTR    rf_Reserved1;
        BYTE    *rf_File;          /* Filename pointer             */
        BYTE    *rf_Dir;           /* Directory name pointer       */
        CPTR    rf_Reserved2;
        UBYTE   rf_Reserved3;
        UBYTE   rf_Reserved4;
        APTR    rf_Reserved5;
        WORD    rf_LeftEdge,rf_TopEdge; /* Preferred window pos   */
        WORD    rf_Width,rf_Height;     /* Preferred window size  */
        WORD    rf_Reserved6;
        LONG    rf_NumArgs;        /* A-la WB Args, for multiselects */
        struct WBArg *rf_ArgList;
        APTR    rf_UserData;       /* Applihandle (you may write!!)  */
        APTR    rf_Reserved7;
        APTR    rf_Reserved8;
        BYTE    *rf_Pat;           /* Pattern match pointer          */
        };                         /* note - more reserved fields follow */

Once the requester is set up, call AslRequest() or AslRequestTags() to make the requester appear on screen.

The ASL File Requester

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

Specifying Requester Options With TagItems

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

File Requester Tag Name Used For
ASLFR_Window Specifies the parent window of the requester
ASLFR_TitleText String to place in the title bar of the requester window
ASLFR_InitialWidth Requester window width
ASLFR_InitialHeight Requester window height
ASLFR_InitialLeftEdge Requester window y origin
ASLFR_InitialTopEdge Requester window x origin
ASLFR_PositiveText String to place in OK gadget of requester
ASLFR_NegativeText String to place in Cancel gadget of requester
ASLFR_InitialFile Default file name (for file requesters only)
ASLFR_InitialDrawer Default directory name (for file requesters only)
ASLFR_InitialPattern Initial pattern-matching string
ASLFR_DoSaveMode Specifies that this file requester is a save requester (this replaces the FILF_SAVE flag in the ASL_FuncFlags tag used in V37).
ASLFR_DoMultiSelect Enables multiple selection of files (this replaces the FILF_MULTISELECT flag in ASL_FuncFlags used in V37).
ASLFR_DoPatterns Causes a pattern gadget to be included in the requester (this replaces the FILF_PATGAD flag in ASL_FuncFlags used in V37).
ASLFR_DrawersOnly Causes the requester to only display drawers (this replaces the FIL1F_NOFILES flag in ASL_ExtFlags1 used in V37).
ASLFR_RejectIcons Causes the requester not to display Workbench icons.
ASLFR_RejectPattern Specifies an AmigaDOS pattern used to reject files.
ASLFR_AcceptPattern Specifies an AmigaDOS pattern used to accept files.
ASLFR_FilterDrawers Makes ASLFR_RejectPattern and ASLFR_AcceptPattern apply to drawer names.

Simple ASL File Requester Example

Here's a short example showing how to create a file requester with asl.library. If AslRequest() returns TRUE then the rf_File and rf_Dir fields of the requester data structure contain the name and directory of the file the user selected. Note that the user can type in the a name for the file and directory, which makes it possible for a file requester to return a file and directory that do not (currently) exist.

// filereq.c
#include <exec/types.h>
#include <exec/libraries.h>
#include <libraries/asl.h>
 
#include <proto/dos.h>
#include <proto/exec.h>
#include <proto/asl.h>
 
#define MYLEFTEDGE 0
#define MYTOPEDGE  0
#define MYWIDTH    320
#define MYHEIGHT   400
 
struct AslIFace *IAsl = NULL;
 
struct TagItem frtags[] =
{
    ASL_Hail,       (uint32)"The Amiga file requester",
    ASL_Height,     MYHEIGHT,
    ASL_Width,      MYWIDTH,
    ASL_LeftEdge,   MYLEFTEDGE,
    ASL_TopEdge,    MYTOPEDGE,
    ASL_OKText,     (uint32)"O KAY",
    ASL_CancelText, (uint32)"not OK",
    ASL_File,       (uint32)"asl.library",
    ASL_Dir,        (uint32)"libs:",
    TAG_END
};
 
int main(int argc, char **argv)
{
    struct Library *AslBase = IExec->OpenLibrary("asl.library", 50);
    IAsl = (struct AslIFace*)IExec->GetInterface(AslBase, "main", 1, NULL);
    if (IAsl != NULL)
    {
        struct FileRequester *fr = IAsl->AllocAslRequest(ASL_FileRequest, frtags);
        if (fr != NULL)
        {
            if (IAsl->AslRequest(fr, NULL))
            {
                IDOS->Printf("PATH=%s  FILE=%s\n", fr->rf_Dir, fr->rf_File);
                IDOS->Printf("To combine the path and filename, copy the path\n");
                IDOS->Printf("to a buffer, add the filename with Dos AddPart().\n");
            }
            IAsl->FreeAslRequest(fr);
        }
        else IDOS->Printf("User Cancelled\n");
    }
 
    DropInterface((struct Interface*)IAsl);
    CloseLibrary(AslBase);
    return 0;
}

File Pattern Matching and Multiple Selects

A file requester can filter out certain file and directory entries using the "wildcard" feature of AmigaDOS. To activate the wildcard feature for a file requester, you use the ASL_FuncFlags tag. Each bit in the ASL_FuncFlags tag item controls a special option of the requester depending on its type (file or font). See <libraries/asl.h> for a complete listing of the options that the ASL_FuncFlags tag controls.

File Requester Flag Used For
FILF_PATGAD Enables the file name pattern matching gadget
FILF_MULTISELECT Enables multiple selection of files
FILF_NEWIDCMP Use separate IDCMP for requester sharing a custom screen (see below)
FILF_SAVE Makes the file requester a save requester (see below)

If the FILF_PATGAD bit of the ASL_FuncFlags tag is set, the file requester will appear with a "Pattern" gadget in addition to the usual file name and directory name gadgets. The user can type an AmigaDOS wildcard pattern into this gadget and the pattern will be used to limit the file names that appear in the requester. An application can also supply a default pattern using the ASL_Pattern tag item. A hidden unchangeable pattern can be created by supplying an ASL_Pattern without a FILF_PATGAD gadget. Such invisible patterns should not be used if there is any reason that the user may need to access a file which does not match the pattern.

Another feature of the ASL file requester is multiple selection. When multiple selection is enabled, the user can choose more than one file name in a single directory by selecting names in the requester's scrolling list gadget with the mouse. This option, like pattern matching, is set up with the ASL_FuncFlags tag.

If the FILF_MULTISELECT bit of the ASL_FuncFlags tag is set, the file requester will allow multiple selection. When the user selects several file names through the multiple selection feature, the FileRequester's rf_NumArgs field contains the number of files selected and the rf_ArgList field contains a pointer to an array of WBArg structures (defined in <workbench/startup.h>). There is a WBArg structure containing a file name for each file the user selected.

The following example illustrates a file requester with both a pattern matching gadget and multiple selection enabled.

/*
** filepat.c
*/
#include <exec/types.h>
#include <intuition/intuition.h>
#include <intuition/screens.h>
#include <graphics/displayinfo.h>
#include <libraries/asl.h>
#include <workbench/startup.h>
 
#include <proto/asl.h>
#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/dos.h>
 
struct AslIFace *IAsl = NULL;
struct IntuitionIFace *IIntuition = NULL;
 
struct Screen *screen = NULL;
struct Window *window = NULL;
 
int main(int argc, char **argv)
{
    struct FileRequester *fr;
    struct WBArg *frargs;
    int x;
 
    struct Library *AslBase = IExec->OpenLibrary("asl.library", 50);
    IAsl = (struct AslIFace*)IExec->GetInterface(AslBase, "main", 1, NULL);
 
    struct Library *IntuitionBase = IExec->OpenLibrary("intuition.library", 50);
    IIntuition = (struct IntuitionIFace*)IExec->GetInterface(IntuitionBase, "main", 1, NULL);
 
    if (IAsl != NULL && IIntuition != NULL)
    {
       if (screen = IIntuition->OpenScreenTags(NULL,
               SA_DisplayID, HIRESLACE_KEY,
               SA_Title, "ASL Test Screen",
               TAG_END))
       {
           if (window = IIntuition->OpenWindowTags(NULL,
                   WA_CustomScreen, screen,
                   WA_Title, "Demo Customscreen, File Pattern, Multi-select",
                   WA_Flags, WINDOWDEPTH | WINDOWDRAG,
                   TAG_END))
           {
               if (fr = (struct FileRequester *)
                   IAsl->AllocAslRequestTags(ASL_FileRequest,
                       ASL_Hail, "FilePat/MultiSelect Demo",
                       ASL_Dir,  "libs:",
                       ASL_File, "asl.library",
 
                       /* Initial pattern string for pattern matching */
                       ASL_Pattern, "~(rexx#?|math#?)",
 
                       /* Enable multiselection and pattern match gadget */
                       ASL_FuncFlags, FILF_MULTISELECT | FILF_PATGAD,
 
                       /* This requester comes up on the screen of this
                       ** window (and uses window's message port, if any).
                       */
                       ASL_Window, window,
                       TAG_END))
               {
                   /* Put up file requester */
                   if (IAsl->AslRequest(fr, 0L))
                   {
                       /* If the file requester's rf_NumArgs field
                       ** is not zero, the user multiselected. The
                       ** number of files is stored in rf_NumArgs.
                       */
                       if (fr->rf_NumArgs)
                       {
                           /* rf_ArgList is an array of WBArg structures
                           ** (see <workbench/startup.h>). Each entry in
                           ** this array corresponds to one of the files
                           ** the user selected (in alphabetical order).
                           */
                           frargs = fr->rf_ArgList;
 
                           /* The user multiselected, step through
                           ** the list of selected files.
                           */
                           for ( x=0;  x < fr->rf_NumArgs;  x++ )
                               IDOS->Printf("Argument %ld: PATH=%s FILE=%s\n",
                                   x, fr->rf_Dir, frargs[x].wa_Name);
                       }
                       else
                           /* The user didn't multiselect, use the
                           ** normal way to get the file name.
                           */
                           IDOS->Printf("PATH=%s FILE=%s\n", fr->rf_Dir, fr->rf_File);
                   }
                   /* Done with the FileRequester, better return it */
                   IAsl->FreeAslRequest(fr);
               }
               IIntuition->CloseWindow(window);
           }
           IIntuition->CloseScreen(screen);
        }
    }
 
    IExec->DropInterface((struct Interface*)IIntuition);
    IExec->CloseLibrary(IntuitionBase);
 
    IExec->DropInterface((struct Interface*)IAsl);
    IExec->CloseLibrary(AslBase);
 
    return 0;
}

The previous example demonstrates two alternate functions for creating and using ASL requesters:

APTR AllocAslRequestTags( uint32 type, Tag Tag1, ... );
BOOL AslRequestTags( APTR request, Tag Tag1, ... );

AllocAslRequestTags() can be used instead of AllocAslRequest() to allocate and set up the file requester. This is a function that will accept TagItems directly in its parameter list, rather than a pointer to an array of TagItems.

Similarly, AslRequestTags() will accept TagItems directly instead of requiring a pointer to an array of TagItems as AslRequest() does.

ASL Requesters and Custom Screens

An application that uses a custom screen normally wants its requesters to open on its screen. Using the ASL_Window tag, a program can associate a requester with a specific window so that the requester appears on the same screen as the window. The ASL_Window tag is followed by a pointer to a window structure. ASL_Window works with both file and font requesters. The example above shows how the ASL_Window tag is used with a file requester.

Normally, a requester associated with a window (using ASL_Window) shares that window's IDCMP port for its communication. An application may not want to share an IDCMP port with the requester. Using the ASL_FuncFlags tag, a program can ask for a requester that creates its own IDCMP port. There are two flags that accomplish this. The first, FILF_NEWIDCMP, is used on file requesters. The other, FONF_NEWIDCMP, is used on font requesters.

The Save Requester

The save requester is a special type of file requester used for save operations. It differs from the regular ASL file requester in several ways.

A save requester does not allow the user to select an existing file name by double-clicking on an entry in the scrolling list gadget. This helps prevent the user from accidentally overwriting the wrong file.

Save requesters can also create directories. If the user types a directory name into the save requester and the directory doesn't exist, the save requester will create that directory (after getting the user's permission via another requester).

To create a save requester, set the ASLFR_DoSaveMode tag to TRUE. Note that it does not make sense to have multiselection in a save requester, so the ASLFR_DoSaveMode tag overrides the ASLFR_DoMultiSelect tag.

The Directory Requester

Sometimes a program may only require a directory name from the user. There is another variation on asl.library's file requester that allows this. If the ASLFR_DrawersOnly tag is set to TRUE, the requester will appear without a string gadget for file names and will display only directory names in the scrolling list gadget. When AslRequest() (or AslRequestTags() ) returns successfully, the rf_Dir field of the FileRequester structure contains the name of the directory the user selected.

Another flag defined for ASL_ExtFlags1 is FIL1F_MATCHDIRS. If file pattern matching is on (see the FILF_PATGAD flag for ASL_FuncFlags), setting FIL1F_MATCHDIRS tells the file requester to pattern match directory names as well as file names. Of course, if both of these ASL_ExtFlags1 flags are set, the requester will only pattern match directory names.