(******************************************************************************
 *
 * COPYRIGHT: Unless otherwise noted, all files are Copyright (c) 1992, 1993
 * Commodore-Amiga, Inc.  All rights reserved.
 *
 * DISCLAIMER: This software is provided "as is".  No representations or
 * warranties are made with respect to the accuracy, reliability, performance,
 * currentness, or operation of this software, and all use is at your own risk.
 * Neither commodore nor the authors assume any responsibility or liability
 * whatsoever with respect to your use of this software.
 *
 ******************************************************************************
 * filter.c
 * This example shows how to use the ASL file requester to only
 * show files that are of a certain data type---in this example
 * it only shows pictures.
 * Written by David N. Junod
 * ported to Oberon by Albert Weinert (Feb. 1994)
 *
 *)
MODULE filter;

IMPORT
  ASL,
  Dos,
  e := Exec,
  dt := Datatypes,
  u := Utility,
  y := SYSTEM;

(*****************************************************************************)

  PROCEDURE Filter( h : u.HookPtr; fr : ASL.FileRequesterPtr; ap : Dos.AnchorPathPtr ):LONGINT;
  (* $StackChk- *)
    VAR dtn    : dt.DataTypePtr;
        buffer : ARRAY 300 OF CHAR;
        lock   : Dos.FileLockPtr;
        use    : e.LONGBOOL;
    BEGIN
      use := e.LFALSE;
      IF ap.info.dirEntryType > 0 THEN
        use := e.LTRUE;
      ELSE
        (* $OddChk- *)
        COPY( fr.dir^, buffer );
        (* $OddChk= *)
        IF Dos.AddPart( buffer, ap.info.fileName, SIZE( buffer ) ) THEN END;
        lock := Dos.Lock( buffer, Dos.accessRead );
        IF lock # NIL THEN
          dtn := dt.ObtainDataType( dt.stFile, y.VAL( e.APTR, lock ) );
          IF dtn # NIL THEN
            IF dtn.Header.groupID = dt.picture THEN;
              use := e.LTRUE;
            END;
          dt.ReleaseDataType( dtn );
          END;
          Dos.UnLock( lock );
        END;
      END;
      RETURN (use);
    END Filter;

(*****************************************************************************)

VAR fr     : ASL.FileRequesterPtr;
    filter : u.HookPtr;

BEGIN
  NEW( filter );
  IF dt.base = NIL THEN HALT( 20 ) END;
  IF ASL.base.version < 38 THEN HALT( 20 ) END;
  u.InitHook( filter, y.VAL( u.HookFunc, Filter ) );
  fr := ASL.AllocAslRequestTags( ASL.fileRequest,
                                 ASL.titleText,   y.ADR( "Select Picture to open" ),
                                 ASL.positiveText,y.ADR( "Open" ),
                                 ASL.filterFunc,  filter,
                                 ASL.rejectIcons,    e.LTRUE,
                                 u.done );
  IF fr # NIL THEN
    IF ASL.AslRequestTags( fr ) THEN END;
    ASL.FreeAslRequest( fr );
  ELSE
    Dos.PrintF( "couldn't allocate als requester\n" );
  END;
END filter.
