/* This file is part of `mogul'
 * Copyright (c) 1995 David Deaven
 *
 * In the following, "you" and "You" refer to any entity other than 
 * the Microsoft corporation and its subsidiaries.  All rights are
 * reserved by the author in connection with Microsoft and its
 * subsidiaries.  The Microsoft Network is explicity denied the right
 * to copy, reproduce, or transmit this software.
 *
 *   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.
 *
 * $Author: deaven $
 * $Id: dialogs.c,v 2.1 1996/07/11 18:39:57 deaven Exp deaven $
 */

#include <stdio.h>
#include <string.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>

#include <X11/Xaw/Box.h>
#include <X11/Xaw/Command.h>
#include <X11/Xaw/Dialog.h>
#include <X11/Xaw/AsciiText.h>
#include <X11/Xaw/Text.h>
#include <X11/Xaw/SimpleMenu.h>
#include <X11/Xaw/MenuButton.h>
#include <X11/Xaw/SmeBSB.h>
#include <X11/Xaw/SmeLine.h>
#include "FileSelect/FileSelect.h"
#include "dmd/memory.h"
#include "xmogul.h"

struct info_s {
	int option;
	Boolean done;
	Widget shell;
	Widget widget;
	String *presult;
};

enum {CANCEL, ACTION, FSW_ACTION, YES_ACTION};

static void do_callback(int option, Widget widget, struct info_s *info, XtPointer call_data)
{
	switch(option) {

	  case ACTION:
	    *(info->presult) = FSCompletePath(info->widget);
	    break;

	  case FSW_ACTION:
	    *(info->presult) = (String) call_data;
	    break;

	  case YES_ACTION:
	    *(info->presult) = "yes";
	    break;

	  default:
	  case CANCEL:
	    *(info->presult) = NULL;
	    break;

	}
	info->done = True;
}

static void cancel_callback(Widget widget, XtPointer client_data, XtPointer call_data) {
	do_callback(CANCEL, widget, (struct info_s *) client_data, call_data); }

static void action_callback(Widget widget, XtPointer client_data, XtPointer call_data) {
	do_callback(ACTION, widget, (struct info_s *) client_data, call_data); }

static void fsw_callback(Widget widget, XtPointer client_data, XtPointer call_data) {
	do_callback(FSW_ACTION, widget, (struct info_s *) client_data, call_data); }

static void yes_callback(Widget widget, XtPointer client_data, XtPointer call_data) {
	do_callback(YES_ACTION, widget, (struct info_s *) client_data, call_data); }

String MPGetFilenameDialog(XtAppContext app, Widget parent, String action, String fn, String filter)
{
	Widget shell, form, fsw, button;
	String result = NULL;
	struct info_s *info = NewPointer(info);

	info->presult = &result;
	info->done = False;

        shell =
	  XtVaCreatePopupShell("FilePopup", shellWidgetClass, parent,
	    NULL);
	info->shell = shell;

	form =
	  XtVaCreateManagedWidget("form", formWidgetClass, shell,
	    NULL);

	fsw =
	  XtVaCreateManagedWidget("fileSelect", fileSelectWidgetClass, form,
	    XtNfilename, fn,
	    XtNfilter, filter,
	    NULL);
	info->widget = fsw;

	button =
	  XtVaCreateManagedWidget(action, commandWidgetClass, form,
	    XtNfromVert, fsw,
	    NULL);
	XtAddCallback(button, XtNcallback, action_callback, info);
	XtAddCallback(fsw, XtNcallback, fsw_callback, info);

	button = XtVaCreateManagedWidget("Cancel", commandWidgetClass, form,
	  XtNfromVert, fsw,
	  XtNfromHoriz, button,
	  NULL);
	XtAddCallback(button, XtNcallback, cancel_callback, info);

	XtPopup(shell, XtGrabExclusive); /* nothing else can happen until we fill this in */
	XStoreName(XtDisplay(shell), XtWindow(shell), action);

	while(!info->done)
	{ XEvent event;
	  XtAppNextEvent(app, &event);
	  XtDispatchEvent(&event);
	}

	XtPopdown(shell);
	XtDestroyWidget(shell);
	myfree(info);

	return result;
}

Boolean MPVerifyDialog(XtAppContext app, Widget parent, String action)
{
	Widget shell, form, button, label;
	String result = NULL;
	struct info_s *info = NewPointer(info);

	info->presult = &result;
	info->done = False;

        shell =
	  XtVaCreatePopupShell("VerifyPopup", shellWidgetClass, parent,
	    NULL);
	info->shell = shell;

	form =
	  XtVaCreateManagedWidget("form", formWidgetClass, shell,
	    NULL);

	label = 
	  XtVaCreateManagedWidget("label", labelWidgetClass, form,
	    XtNlabel, action,
	    NULL);

	button =
	  XtVaCreateManagedWidget("Yes", commandWidgetClass, form,
	    XtNfromVert, label,
	    NULL);
	XtAddCallback(button, XtNcallback, yes_callback, info);

	button =
	  XtVaCreateManagedWidget("Cancel", commandWidgetClass, form,
	    XtNfromVert, label,
	    XtNfromHoriz, button,
	    NULL);
	XtAddCallback(button, XtNcallback, cancel_callback, info);

	XtPopup(shell, XtGrabExclusive); /* nothing else can happen until we fill this in */
	XStoreName(XtDisplay(shell), XtWindow(shell), "xmogul verification dialog");

	while(!info->done)
	{ XEvent event;
	  XtAppNextEvent(app, &event);
	  XtDispatchEvent(&event);
	}

	XtPopdown(shell);
	XtDestroyWidget(shell);
	myfree(info);

	if(result && strcmp(result, "yes") == 0) return True;

	return False;
}

void MPInformationDialog(XtAppContext app, Widget parent, String action)
{
	Widget shell, form, button, label;
	String result = NULL;
	struct info_s *info = NewPointer(info);

	info->presult = &result;
	info->done = False;

        shell =
	  XtVaCreatePopupShell("InformationPopup", shellWidgetClass, parent,
	    NULL);
	info->shell = shell;

	form =
	  XtVaCreateManagedWidget("form", formWidgetClass, shell,
	    NULL);

	label = 
	  XtVaCreateManagedWidget("label", labelWidgetClass, form,
	    XtNlabel, action,
	    NULL);

	button =
	  XtVaCreateManagedWidget("OK", commandWidgetClass, form,
	    XtNfromVert, label,
	    NULL);
	XtAddCallback(button, XtNcallback, yes_callback, info);

	XtPopup(shell, XtGrabExclusive); /* nothing else can happen until we fill this in */
	XStoreName(XtDisplay(shell), XtWindow(shell), "xmogul information");

	while(!info->done)
	{ XEvent event;
	  XtAppNextEvent(app, &event);
	  XtDispatchEvent(&event);
	}

	XtPopdown(shell);
	XtDestroyWidget(shell);
	myfree(info);
}
