Amiga C Tutorial

Easy Requesters

AmigaOS supports 3 types of requesters: System, true and ASL type requesters. One of these types on the Easy Requester which replaces the old AutoRequest() and the intuition Request() functions. It is a very easy to use and just needs a Window to attach to, an EasyStruct structure, a pointer to a list of IDCMP flags and optional arguments.

struct EasyStruct {
   ULONG es_StructSize;  /* should be sizeof (struct EasyStruct )*/
   ULONG es_Flags;       /* should be 0 for now */
   UBYTE *es_Title;      /* title of requester window */
   UBYTE *es_TextFormat; /* 'printf' style formatting string */
   UBYTE *es_GadgetFormat; /* 'printf' style formatting string */
   };

The EasyStruct structure requires the following, the size of the structure (see above), some Flags which is usually set to 0, the title of the requester as a string, the main text of the requester with optional parameters to fill in by EasyRequest(), and a list of gadget button text names seperated by the '|' character. The text strings can use printf style parameters such as %c for character, %d for decimal, %s for string, \n for new line and so on. You can have as many gadget buttons as you like such as OK, OK|Cancel, Retry|Cancel, Yes|No, Yes|No|Cancel and so on. First gadget returns a 1, second a 2, third a 3 but the last gadget returns a 0.

To display a Requester, use the EasyRequest function. Format is:

LONG EasyRequestArgs( struct Window *window, struct EasyStruct *easyStruct, ULONG *idcmpPtr, APTR args );
LONG EasyRequest( struct Window *window, struct EasyStruct *easyStruct, ULONG *idcmpPtr, ... );

Here you need a pointer to a parent Window structure, a pointer to an EasyStruct structure, some IDCMP pointers and optional arguments for the EasyStruct structure such as a text message.

For example, the following program will display a question in a requester and display the answer in the requester.

/* Request Example 1 */
#include <proto/intuition.h>
#include <proto/gadtools.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <intuition/intuition.h>
#define NO (0)
#define YES (1)
/* Easy Requester question */
   struct EasyStruct myreq = {
     sizeof(struct EasyStruct),
     0,
     "Easy Request Example",
     "Do you find this interesting?",
     "Yes|No"
   };
/* Answer goes into %s parameter */
   struct EasyStruct YesNoReply = {
     sizeof(struct EasyStruct),
     0,
     "Answer",
     "%s",
     "OK"
   };
   int main() {
    struct Window *myWindow;
   
    myWindow = OpenWindowTags(NULL,
      WA_Left, 20, WA_Top, 20,
      WA_Width, 200, WA_Height, 150,
      WA_IDCMP, IDCMP_CLOSEWINDOW,
      WA_Flags, WFLG_SIZEGADGET | WFLG_DRAGBAR | WFLG_DEPTHGADGET    | WFLG_CLOSEGADGET | WFLG_ACTIVATE,
      WA_Title, "My Window",
      WA_PubScreenName, "Workbench",
      TAG_DONE);
  /* Display question and depending on answer give a response in another requester */
    if (EasyRequest(myWindow, &myreq, NULL, NULL)==YES) 
       EasyRequest(myWindow, &YesNoReply, NULL, "Good, I'm glad you like it.");
    else
       EasyRequest(myWindow, &YesNoReply, NULL, "Oh dear, Let's try something else");
   
    if (myWindow) CloseWindow(myWindow);
   
    return(0);
   }
 

For AmigaOS 4, prefix OpenWindowTags, EasyRequest and CloseWindow with 'IIntuition->' interface text.
e.g. IIntuition->EasyRequest(myWindow, &myreq, NULL, NULL)

Here, a window is opened and the first requester is displayed using myreq which has a title of Easy Request Example, a main body text saying Do you find this interesting? and two possible replies of Yes or No (seperated by a | character). Selecting Yes will display another requester using YesNoReply and changes the %s parameter with Good, I'm glad you like it. otherwise it will use the same YesNoReply structure and display Oh dear, Let's try something else. Clicking OK will then end the program.

ASL Requesters

Besides the Inution type requesters, there is the ASL.library requesters that have built in requesters for File Open/Save requesters, Font requester, Public Screen selection and so on. The following example uses the following functions to setup and display a requester.

APTR AllocAslRequestTags( ULONG reqType, Tag tag1, ... );
VOID FreeAslRequest( APTR requester );
BOOL AslRequest( APTR requester, struct TagItem *tagList );

You need to use AllocAslRequestTags to set up an ASL requester, the reqType can be ASL_FileRequest, ASL_FontRequest or ASL_ScreenModeRequest, you can then optional specify parameters for the ASL requester here or in the AslRequest function such as size of requester, title text, initial file, drawer and file pattern, font information or initial screen information as defined in libraries/asl.h header file.
The AllocAslRequster will return a pointer and can be a pointer to a FileRequester, FontRequester or ScreenModeRequester structure (these are read only). Once a structure is set up, you can call the AslRequest function to display the requester and retreive data from the structure afterwards such as Filename, Font or ScreenMode for use in your program. Once you have finished with the requester structure, you should call FreeAslRequest.

The example below will display a file requester and return the full path of the file you selected.

/* ASL File Request Example 
   by Peter Hutchison
*/
#include <proto/intuition.h>
#include <proto/gadtools.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/asl.h>
#include <intuition/intuition.h>
#include <string.h>
/* Answer goes into %s parameter */
   struct EasyStruct ResultReq = {
     sizeof(struct EasyStruct),
     0,
     "Answer",
     "%s",
     "OK"
   };
   struct FileRequester *request;
   UBYTE fname[255];
int main(void) {
    struct Window *myWindow;
   
    myWindow = OpenWindowTags(NULL,
      WA_Left, 20, WA_Top, 20,
      WA_Width, 200, WA_Height, 150,
      WA_IDCMP, IDCMP_CLOSEWINDOW,
      WA_Flags, WFLG_SIZEGADGET | WFLG_DRAGBAR | WFLG_DEPTHGADGET    | WFLG_CLOSEGADGET | WFLG_ACTIVATE,
      WA_Title, "My Window",
      WA_PubScreenName, "Workbench",
      TAG_DONE);

    /* Create a FileRequester structure */
    request = (struct FileRequester *)AllocAslRequestTags(ASL_FileRequest, 
      ASL_Hail, "Open File", (struct TagItem *)TAG_DONE);
   
    /* Now show file requester */
    if (AslRequestTags(request,
      ASLFR_Window, myWindow, 
      ASLFR_InitialLeftEdge, 20,
      ASLFR_InitialTopEdge, 20,
      ASLFR_InitialWidth, 300,
      ASLFR_InitialHeight, 350,
      ASLFR_InitialDrawer, "Sys:Work",
      ASLFR_InitialPattern, "#?.txt",
      ASLFR_PositiveText, "Open",
      (struct TagItem *)TAG_DONE)) {
   
       /* Build filename */
   
      strcat( fname, request->rf_Dir);
      if (fname[strlen(fname)-1] != (UBYTE)58) /* Check for : */
         strcat(fname, "/");
      strcat(fname, request->rf_File);
   
      /* Display filename */
      EasyRequest(myWindow, &ResultReq, NULL, fname);
   }
   else {
        EasyRequest(myWindow, &ResultReq, NULL, "File request cancelled");
   }
   /* Free requester */
   if (request) FreeAslRequest(request);
   if (myWindow) CloseWindow(myWindow);
   
   return(0);
   }
 

For AmigaOS 4, prefix OpenWindowTags, EasyRequest and CloseWindow with 'IIntuition->'.
Replace, ASL_Hail with ASLFR_TitleText, rf_Dir with fc_Drawer and rf_File with fc_File.
Prefix AllocAslRequestTags, AslRequestTags, FreeAslRequest with 'IAsl->' Interface text
e.g. IAsl->AslRequestTags( request, ... )

Next Page