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

#include <X11/Shell.h>
#include <X11/Xaw/Form.h>
#include <X11/Xaw/Box.h>
#include <X11/Xaw/Command.h>
#include <X11/Xaw/AsciiText.h>
#include "FChooser.h"

#include "xregal.h"
#include "file.h"
#include "popup.h"
#include "LinkText.h"

#define DIALOG_WIDTH 400

Widget 	fs_shell,
		fs_form,
		fs_box,
		fs_chooser,
		fs_save,
		fs_cancel,
		fs_label,
		fs_text;

Boolean fs_popped_up=False;
char *filename;
char *directory;

static void Cancel(Widget w, XtPointer client_data, XtPointer call_data) {
	XtPopdown(fs_shell);
	fs_popped_up=False;
	}

static void Save(Widget w, XtPointer client_data, XtPointer call_data) {
	FILE *fp;
	char fullname[256];
	char errbuf[256];
	char *text;
	char *url;

	XtVaGetValues(fs_text, XtNstring, &filename, NULL);                                  
	XtVaGetValues(url_text, XtNstring, &url, NULL);                                  
	XtVaGetValues(fs_chooser,XtNcurrentDirectory,&directory, NULL);                                  

	strcpy(fullname, directory);
	strcat(fullname, filename);
	/* printf("Saving to %s\n", fullname); */

	fp=fopen(fullname, "w");
	if (fp==NULL) {
		sprintf(errbuf, "Failed to open file %s", fullname);
		Alert(errbuf);
		}
	else {
		text=HypertextToText(viewer);
		if (text!=NULL) {
			if (strlen(url)>0)
				fprintf(fp, "URL: %s\n\n", url);
			fprintf(fp, "%s", text);
			}
		fclose(fp);
		}
	XtPopdown(fs_shell);
	fs_popped_up=False;
	}

static void selectionChanged(Widget w, XtPointer client_data, XtPointer call_data) {
    XfwfFileChooserReturnStruct *ret =
        (XfwfFileChooserReturnStruct *)call_data;

    if (ret->directory == NULL || ret->file == NULL) {
		XtVaSetValues(fs_text,XtNstring,"", NULL);                                  
    } else {
		XtVaSetValues(fs_text,XtNstring,ret->file, NULL);                                  
		}
	}

void CreateFileSaveDialog(void) {
    static String myreturn = "#override <Key>Return:   set() notify() unset()\n";
    static String myescape = "#override <Key>Escape:   set() notify() unset()\n";
    XtAccelerators accel, escaccel;

    accel = XtParseAcceleratorTable(myreturn);
    escaccel = XtParseAcceleratorTable(myescape);


	fs_shell=XtVaCreatePopupShell(
                "fs_shell",
                transientShellWidgetClass,
                topLevel,
                XtNallowShellResize, False,
                NULL);

	fs_form=XtVaCreateManagedWidget("fs_form",formWidgetClass,fs_shell, NULL);
	fs_box=XtVaCreateManagedWidget("fs_box", boxWidgetClass, fs_form, 
		XtNwidth, 400,
		XtNborderWidth, 0,
		NULL);
	fs_save=XtVaCreateManagedWidget("fs_save",commandWidgetClass,fs_box,
		XtNlabel, "Save file",
        XtNaccelerators, accel,
		NULL);
	fs_cancel=XtVaCreateManagedWidget("fs_cancel",commandWidgetClass,fs_box,
		XtNlabel, "Cancel",
        XtNaccelerators, escaccel,
		NULL);

	fs_label=XtVaCreateManagedWidget("fs_label",labelWidgetClass,fs_form,
		XtNlabel, "File name: ",
		XtNfromVert, fs_box,
        XtNwidth, 80,
		XtNborderWidth, 0,
		XtNjustify, XtJustifyLeft,
		NULL);
	fs_text = XtVaCreateManagedWidget("fs_text",asciiTextWidgetClass,fs_form,
        XtNeditType, XawtextEdit,
        XtNwidth, DIALOG_WIDTH-80,
        XtNresizable, True,
		XtNfromVert, fs_box,
		XtNfromHoriz, fs_label,
		XtNhorizDistance, 0,
        XtNstring, filename,
        XtNuseStringInPlace, False,
        NULL);

	fs_chooser=XtVaCreateManagedWidget("fs_chooser",xfwfFileChooserWidgetClass,fs_form,
		XtNfromVert, fs_text,
		XtNvertDistance, 15,
		XtNborderWidth, 0, 
		XtNwidth, DIALOG_WIDTH,
		XtNheight, 2*DIALOG_WIDTH/3,
		NULL);

    XtAddCallback(fs_cancel, XtNcallback, Cancel, 0);
    XtAddCallback(fs_save, XtNcallback, Save, 0);
    XtAddCallback(fs_chooser,XtNcallback,selectionChanged,NULL);
    XtSetKeyboardFocus(fs_form, fs_text);
    XtInstallAccelerators(fs_text, fs_save);
    XtInstallAccelerators(fs_text, fs_cancel);
	}

void PopupFileSave(void) {
	SetPopupPosition(fs_shell, topLevel);
	XtPopup(fs_shell, XtGrabNone);
	fs_popped_up=True;
	}
