/*
    FileCompT.c : test program for the FileComplete widget
    Copyright (C) 1992 Robert Forsman

    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 2 of the License, 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 the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

static char rcsid[] = "$Id: FileCompT.c,v 0.18 93/01/19 12:01:06 thoth Exp $";

/*
 * $Log:	FileCompT.c,v $
 * Revision 0.18  93/01/19  12:01:06  thoth
 * important stuff is now prefixed with xfwf (I hope)
 * 
 * Revision 0.17  92/08/24  13:59:29  thoth
 * Totty's initial stab at the Xfwf version of
 * the release.
 * 
 * Revision 0.16  92/07/12  15:08:27  thoth
 * adjusted test program to follow new FWF conventions.
 * 
 * Revision 0.15  92/06/08  22:17:11  thoth
 * portability enhancements using the S_IS* macros.
 * 
 * Revision 0.14  92/05/20  17:13:12  thoth
 * this was released with FWF 3.0
 * 
 * Revision 0.13  92/05/07  12:34:27  thoth
 * Added menu entries to test out the tilde backup filters.
 * 
 * Revision 0.12  92/05/06  11:08:58  thoth
 * adding rcsid[]
 * 
 */

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>

#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Xaw/Form.h>
#include <X11/Xaw/MenuButton.h>
#include <X11/Xaw/SimpleMenu.h>
#include <X11/Xaw/SmeBSB.h>

#include <Xfwf/FileComp.h>

Widget outer, fc, form, fileproc, statsproc;
XtAppContext	app_con;

void print_filename(w, client_data, call_data)
     Widget	w;
     XtPointer  client_data, call_data;
{
  printf("the user has selected file \"%s\"\n", call_data);
}

void print_filename_2(w, client_data, call_data)
     Widget	w;
     XtPointer  client_data, call_data;
{
  char	*s;
  XtVaGetValues(client_data, XtNfilename, &s, NULL);
  printf("current value is \"%s\"\n", s);
}

void quit()
{
  exit(0);
}

void change_file_proc(w, client_data, call_data)
     Widget	w;
     XtPointer	call_data, client_data;
{
  XtVaSetValues(fc, XtNacceptableFilenameProc, client_data, NULL);
}

void add_file_proc(parent, label, proc)
     Widget	parent;
     char	*label;
     XfwfFCAcceptableFilenameProc	proc;
{
  Widget	temp;
  temp = XtVaCreateManagedWidget
    ("entry", smeBSBObjectClass, parent,
     XtNlabel, label,
     NULL);
  XtAddCallback(temp, XtNcallback, change_file_proc, proc);
}

Boolean noprograms(filename, filestats)
     char	*filename;
     struct stat	*filestats;
{
  if ( S_ISREG(filestats->st_mode) &&
      (filestats->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0)
    return False;
  else
    return True;
}

void change_stat_proc(w, client_data, call_data)
     Widget	w;
     XtPointer	call_data, client_data;
{
  XtVaSetValues(fc, XtNacceptableStatsProc, client_data, NULL);
}

void add_stat_proc(parent, label, proc)
     Widget	parent;
     char	*label;
     XfwfFCAcceptableStatsProc	proc;
{
  Widget	temp;
  temp = XtVaCreateManagedWidget
    ("entry", smeBSBObjectClass, parent,
     XtNlabel, label,
     NULL);
  XtAddCallback(temp, XtNcallback, change_stat_proc, proc);
}

int main (argc,argv)
     int	argc;
     char	**argv;
{
  XtTranslations	xlations;
  Widget	temp;

  outer = XtAppInitialize(&app_con,"FileCompT",NULL,0,
			  &argc,argv,NULL,NULL,0);

  form = XtVaCreateManagedWidget("form", formWidgetClass, outer,
				 NULL);
  fc = XtVaCreateManagedWidget("selector", xfwffileCompleteWidgetClass, form,
			       NULL);
  XtAddCallback(fc, XtNcallback, print_filename, NULL);
  XtAddCallback(fc, XtNcancelCallback, quit, NULL);
  XfwfFileCompleteAddButton(fc, "check", print_filename_2, fc);

  fileproc = XtVaCreateManagedWidget
    ("filename", menuButtonWidgetClass, form,
     XtNfromVert, fc,
     NULL);
  temp = XtVaCreatePopupShell ("menu", simpleMenuWidgetClass, fileproc,
			       NULL);
  add_file_proc(temp, "any", NULL);
  add_file_proc(temp, "no .files", XfwfFCNoDotFiles);
  add_file_proc(temp, "no tilde backups", XfwfFCNoTildeBackups);
  add_file_proc(temp, "no tilde backups or .files",
		XfwfFCNoTildeBackupsOrDotFiles);

  statsproc = XtVaCreateManagedWidget
    ("stats", menuButtonWidgetClass, form,
     XtNfromVert, fc,
     XtNfromHoriz, fileproc,
     NULL);
  temp = XtVaCreatePopupShell ("menu", simpleMenuWidgetClass, statsproc,
			       NULL);
  add_stat_proc(temp, "any", NULL);
  add_stat_proc(temp, "no programs", noprograms);

  XtRealizeWidget(outer);

  XtAppMainLoop(app_con);

}
