/**FileSelect.c**********************************************************
 *									*
 *   SeePix -- by Hank Schafer						*
 *									*
 *   SeePix is an IFF Picture Viewer.  It works with Lo-Res,		*
 *   Med-Res, Hi-Res, HAM, and EHB formats.  I'm working on support     *
 *   for X-Specs, and Anim5 formats.					*
 *									*
 *   SeePix is based on a program by Olaf Barthel, called		*
 *   'LoadImage'.  As released, LoadImage had a couple of bugs. 	*
 *   The Amiga-Key alternatives to menu use didn't all work.  That's	*
 *   been fixed.  There were two separate print functions in LoadImage	*
 *   which behaved exactly the same.  The redundancy was eliminated.	*
 *   LoadImage used the Topaz ROMFONT, and made no allowances for a	*
 *   different system default font (under 2.04).  I added a built-in	*
 *   font.  I've reworked the code considerably from the original       *
 *   release, to allow for optimizations based on time and space.	*
 *									*
 *   SeePix features a palette tool which allows "tweaking" of colors	*
 *   prior to printing, allowing you to modify the color printout to	*
 *   more closely resemble the original graphics (Blue is Blue, not	*
 *   Purple).  SeePix can be Iconified.  SeePix features an ARP 	*
 *   interface.  SeePix now uses the PathMaster File Selector.		*
 *									*
 ************************************************************************
 *                                                                      *
 *   SeePix Copyright © 1992 by Hank Schafer; all rights reserved.      *
 *                                                                      *
 *   This program is free software; you can redistribute it and/or      *
 *   modify it under the terms of the GNU General Public License as     *
 *   published by the Free Software Foundation, either version 1, or    *
 *   (at your option) any later version.                                *
 *                                                                      *
 *   This program is distributed in the hope that it will be useful,    *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of     *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  *
 *   General Public License for more details.                           *
 *                                                                      *
 *   You should have received a copy of the GNU General Public License  *
 *   along with this program; if not, write to:                         *
 *                 Free Software Foundation, Inc.                       *
 *                 675 Massachusetts Ave.                               *
 *                 Cambridge  MA  02139, USA                            *
 *                                                                      *
 **************** Special Function Copyright Notices ********************
 *									*
 ****LoadImage Copyright Notice:					*
 *									*
 *   LoadImage is © Copyright 1988, 1989, 1990 by MXM, all rights	*
 *   reserved, written by Olaf Barthel.  No guarantees of any kind are	*
 *   made that this program is 100% reliable.  Use this program on	*
 *   your own risk!							*
 *									*
 ****Iconify Copyright Notice:						*
 *									*
 *   Copyright 1987 by Leo L. Schwab.					*
 *   Permission is hereby granted for use in any and all programs,	*
 *   both Public Domain and commercial in nature, provided this 	*
 *   Copyright notice is left intact.					*
 *									*
 ****ColorWindow (Palette) Copyright Notice:				*
 *									*
 * ColorWindow Routine	--  Color Window Routines			*
 *     from Book 1 of the Amiga Programmers' Suite by RJ Mical          *
 *									*
 * Copyright (C) 1986, 1987, Robert J. Mical				*
 * All Rights Reserved. 						*
 *									*
 ****PathMaster Copyright Notice:					*
 *									*
 * -------------------------------------------------------------------- *
 *   Copyright © 1989 Justin V. McCormick.  All Rights Reserved.	*
 * -------------------------------------------------------------------- *
 *									*
 *    The PathMaster name is a trademark of Justin V. McCormick.	*
 *									*
 *   PathMaster File Selector source and documentation written by:	*
 *									*
 *			 Justin V. McCormick.				*
 *	       Copyright © 1989 by Justin V. McCormick. 		*
 *			 All Rights Reserved.				*
 *									*
 * -------------------------------------------------------------------- *
 ************************************************************************
 *									*
 *   Hope this is something you can use and enjoy.			*
 *									*
 ************************************************************************/

#ifdef	PATHMASTER

/*
 * FileSelect
 */

LONG
FileSelect(fsrequest)
    struct FSRequest *fsrequest;
{
    LONG	    i, status, newpat;
    ULONG	    signals;

    /* (Re)Initialize things */

    FSReq = fsrequest;		/* Init global filereq struct pointer */

    /* Set pattern to match files */

    newpat = 0;			/* Assume no pattern changes at first */
    FSPatternInfo.Buffer = (UBYTE *) fsrequest->matchpattern;
    FSPatternInfo.BufferPos = strlen(FSPatternInfo.Buffer);
    if (FSPatternInfo.Buffer[0] == 0)	/* Put splat back if no matchpattern  */
	(VOID) strcpy(FSPatternInfo.Buffer, SplatStr);

    if (lstrcmp(FSPatternUndoBuffer, FSPatternInfo.Buffer) != 0) {
	safestrcpy(FSPatternUndoBuffer, FSPatternInfo.Buffer, (LONG) (MATCHSTRSIZE - 1));
	newpat = 1;
    }
    /* Set pattern to ignore files */

    if (lstrcmp(FSIgnorePat, fsrequest->ignorepattern) != 0) {
	safestrcpy(FSIgnorePat, fsrequest->ignorepattern, (LONG) (FILESTRSIZE - 1));
	newpat = 1;
    }
    /* Update fsflags if FSReq->flags has changed, reset state if needed */

    if (FSCheckFlagChange() != 0)
	newpat = 1;

    /* Set initial directory */

    FSPathInfo.Buffer = (UBYTE *) fsrequest->dirname;
    FSPathInfo.BufferPos = strlen(FSPathInfo.Buffer);
    if (lstrcmp(FSPathInfo.Buffer, FSPathBuf) != 0) {
	fsvirgindir = 1;
    }
    /* Set initial filename */

    FSFileInfo.Buffer = (UBYTE *) fsrequest->filename;
    FSFileInfo.BufferPos = strlen(FSFileInfo.Buffer);

    /* Open and initialize everything, start IDCMP processing */

    status = InitFSWindow();
    if (status != 0) {
	FSWinTitle();

	/* Redisplay buffered data if still valid */

	if (FSTestOldLock() != 0 && fsvirgindir <= 0) {
	    if (newpat != 0) {	/* Pattern changed, but still have old data */
		FSMatchPattern();
	    } else {

		/* Otherwise just redraw them from static data */

		FSSetFileGads();
		FSDisableAllFGads();
	    }
	}
	FSDone = 0;
	while (FSDone == 0) {
	    if (fsvirgindir != 0) {
		if (FSGrabEntry() == 0)
		    goto FSFailed;
		signals = FSSignal | FSUserSignal;
	    } else {
		signals = Wait((LONG) FSSignalMask);
	    }

	    if ((signals & FSUserSignal) != 0) {
		i = (FSReq->userfunc) (FSReq, FSWin);
		if (i != 0)
		    fsvirgindir = 1;
	    }
	    if ((signals & FSSignal) != 0)
		CheckFSIDCMP();
	}
    }
  FSFailed:

    if (FSRPort != 0) {
	FreeMem(FSRPort, (LONG) sizeof(struct RastPort));
	FSRPort = 0;
    }
    if (MainFont != 0) {
	CloseFont(MainFont);
	MainFont = 0;
    }
    if (FSWin != 0) {
	FSReq->leftedge = FSWin->LeftEdge;
	FSReq->topedge = FSWin->TopEdge;
	CloseWindow(FSWin);
	FSWin = 0;
    }
    FreeFSVBDelay();
    return (status);
}

#endif	/* PATHMASTER */
