MODULE TestFileReq;


(*==========================================================================*)
(*                                                                          *)
(*                      Demo of ARP File Requester                          *)
(*                                                                          *)
(*==========================================================================*)
(*                 Written for TDI Modula-2/Amiga V3.01a                    *)
(*==========================================================================*)
(*                                                                          *)
(*   Original Author : Martin Taillefer.  28-Feb-88                         *)
(*                                                                          *)
(*   Version         : 1.00a  28-Feb-88  Martin Taillefer                   *)
(*                       Original.                                          *)
(*                                                                          *)
(*==========================================================================*)
(*                                                                           *)
(* INFO: This short program shows how the ARP file requester can be used from*)
(*       within any M2 program. We must first open the ARP lib, this is done *)
(*       by using the standard ARP startup code. Once this is done, we       *)
(*       initialize a FileRequester record. This example wants to modify the *)
(*       NewWindow record of the requester before it opens (so that the      *)
(*       window borders are white instead of orange). To do this, we must set*)
(*       the NewWindowFunc flag in the frFuncFlags field of the FileRequester*)
(*       record. We must also set a pointer to the function to call when the *)
(*       file requester is ready to open the window. When this function is   *)
(*       called, we are passed a pointer to the NewWindow record which we    *)
(*       are free to modify. We then return from the function and the file   *)
(*       requester proceeds.                                                 *)
(*       The function called by the file requester MUST simulate a C         *)
(*       function, that is: save CPU registers, and give its return value in *)
(*       register D0. This is done and commented in our example function, so *)
(*       use this function as a skeleton for any of your own functions       *)
(*       Note that we could've set frFunction to NULL and set frFuncFlags to *)
(*       {}. this would heva given a VERY simple program, and VERY easy      *)
(*       access to the file requester                                        *)
(*                                                                           *)
(*****************************************************************************)

(*$S-,$T-,$A+*)

FROM SYSTEM    IMPORT ADR, ADDRESS, BYTE, NULL, CODE, SETREG;
FROM Intuition IMPORT NewWindow;
FROM ArpMisc   IMPORT FRFunction, FRFlags, FRFlagSet, FileRequester,
                      FileRequest;
FROM ArpStartupCode    IMPORT StartUp, CloseUp;
FROM ArpConsoleHandler IMPORT Puts;


VAR
  fr : FileRequester;
  dir,hail,file : ARRAY [0..35] OF CHAR;


PROCEDURE ChangeNewWindow(object:ADDRESS;mask:LONGCARD);
VAR
  nwp   : POINTER TO NewWindow;
  flags : FRFlagSet;
BEGIN
  CODE(048E7H,0FFFFH);                  (* This saves the CPU registers *)
  
  nwp:=ADDRESS(object);                 (* object points to the NewWindow record *)
  flags:=FRFlagSet(mask);               (* Why we got called *)
  IF NewWindFunc IN flags THEN          (* If we got called because of a NewWindow, then change border color *)
    nwp^.BlockPen:=BYTE(1);
  ELSE
    Puts("We were called with something we don't handle!");
  END;

  CODE(04CDFH,0FFFFH);                  (* This restores the CPU registers   *)
  SETREG(0,0);                          (* This sets the return value (D0=0) *)
END ChangeNewWindow;


BEGIN
  StartUp("None","Just hit RETURN dummy");    (* ARP startup code *)
  
  hail:="Enter anything you want!"; 
  file:="";
  dir:="";
  WITH fr DO
    frHail      := ADR(hail);                 (* Title of requester    *)
    frFile      := ADR(file);                 (* Initial filename      *)
    frDir       := ADR(dir);                  (* Initial pathname      *)
    frWindow    := NULL;                      (* No WindowPtr means on WB screen             *)
    frFuncFlags := FRFlagSet{NewWindFunc};    (* Call our function before opening the window *)
    frReserved1 := BYTE(0);                   (* Reserved so set to 0  *)
    frFunction  := ChangeNewWindow;           (* Function to call      *)
    frReserved2 := 0;                         (* Reserved so set to 0  *)
  END;
  
  IF FileRequest(fr) # NULL THEN
    Puts("You entered:");
    Puts(dir);
    Puts(file);
  ELSE                                  (* User clicked CANCEL  *)
    Puts("You clicked Cancel");
  END;

  CloseUp;                              (* ARP termination code *)
END TestFileReq.
