/***********************************************************************
 *
 * TITLE:
 *	export.c
 *
 * AUTHOR:
 *	Cassie Weissert
 *
 * DESCRIPTION:
 *	Dialog for the export functions: PICT and Metafile.
 *
 *				CHANGE HISTORY
 *
 * $Log: export.c,v $
 * Revision 1.2  1994/12/31  01:20:06  kevin
 * fixed export naming convention
 *
 * Revision 1.1  1994/08/26  21:51:09  cassie
 * Initial revision
 *
 *
 ***********************************************************************
 *
 * WARNINGS:
 *
 * EXTERNAL CALLABLE COMPONENTS (PUBLIC):
 *
 * GLOBALS:
 *
 * WAIVERS:
 *
 * NOTES:
 *
 * MANPAGE:
 *
 ***********************************************************************/

#ifndef lint
static char rcsid[] = 
    "$Id: export.c,v 1.2 1994/12/31 01:20:06 kevin OEL $";
#endif

#include "xopps.h"
#include "shellmgmt.h"
#include "dialog.h"
#include <time.h>

#define PAGES_NONE 0
#define PAGES_THIS 1
#define PAGES_ALL 2

#define PCT 0
#define WMF 1

/*
 * local variables
 */
static Widget parent;		/* parent of dialog */
static Widget exportDialog;	/* this dialog */
static char *appleFileTpl;	/* template for Apple file name */
static char *windowsFileTpl;	/* template for Windows file name */
static char *appleFile;		/* actual Apple file name */
static char *windowsFile;	/* actual Windows file name */
static int appleSingle;		/* TRUE if using AppleSingle format */
static DialogText *dlgPctName;	/* name of PICT file */
static DialogRadio *dlgPctPage;	/* page for PICT file */
static DialogText *dlgWmfName;	/* name of Windows file */
static DialogRadio *dlgWmfPage;	/* page for Windows file */

/*
 * local function prototypes
 */
static void createExportDialog(void);
static void exportDialogCB(Widget, XtPointer, XtPointer);
static void print_to_file(char *, int, int, int);

/***********************************************************************
 *
 * FUNCTION:
 *	InitExport
 *
 * INPUTS:
 *	parentP		- (Widget) parent widget
 *	appleFileP	- (char *) Apple file name template
 *	windowsFileP	- (char *) Windows file name template
 *	appleSingleP	- (int) TRUE if using AppleSingle format
 *
 * OUTPUTS:
 *	none
 *
 * RETURNS:
 *	none
 *
 * EXTERNALLY READ:
 *	none
 *
 * EXTERNALLY MODIFIED:
 *	parent		- (Widget) parent widget
 *	appleFileTpl	- (char *) Apple file name template
 *	windowsFileTpl	- (char *) Windows file name template
 *	appleSingle	- (int) TRUE if using AppleSingle format
 *
 * DESCRIPTION:
 *	Initialize the export popup.
 */

void
InitExport(Widget parentP, char *appleFileP, char *windowsFileP,
    int appleSingleP)
{
    parent = parentP;
    appleFileTpl = XtNewString(appleFileP);
    windowsFileTpl = XtNewString(windowsFileP);
    appleSingle = appleSingleP;
}

/***********************************************************************
 *
 * FUNCTION:
 *	createExportDialog
 *
 * INPUTS:
 *	none
 *
 * OUTPUTS:
 *	none
 *
 * RETURNS:
 *	none
 *
 * EXTERNALLY READ:
 *	parent		- (Widget) parent widget
 *	appleFile	- (char *) Apple file name template
 *	windowsFile	- (char *) Windows file name template
 *	appleSingle	- (int) TRUE if using AppleSingle format
 *
 * EXTERNALLY MODIFIED:
 *	none
 *
 * DESCRIPTION:
 *	Create the Export Dialog.
 */

static void 
createExportDialog(void)
{
    exportDialog = DialogInit(parent, "Export Graphics Files", "export",
	exportDialogCB, (XtPointer)DIALOG_CANCEL);

    dlgPctName = DialogTextCreate("Mac PICT file name template: ", 20, 20);
    dlgPctPage = DialogRadioCreate("Pages: ", 3, PAGES_ALL, NULL,
	DIALOG_RAD_STR_VA, "None", PAGES_NONE, "This", PAGES_THIS,
	"All", PAGES_ALL);

    (void)DialogSeparatorCreate(XmSINGLE_LINE);

    dlgWmfName = DialogTextCreate("Windows Metafile name template: ", 12, 12);
    dlgWmfPage = DialogRadioCreate("Pages: ", 3, PAGES_ALL, NULL,
	DIALOG_RAD_STR_VA, "None", PAGES_NONE, "This", PAGES_THIS,
	"All", PAGES_ALL);

    DialogStdButtons(exportDialogCB, (XtPointer)DIALOG_UPDATE, exportDialogCB,
	(XtPointer)DIALOG_CANCEL, (XtCallbackProc)ShowHelp, "export");
}

/***********************************************************************
 *
 * FUNCTION:
 *	Export
 *
 * INPUTS:
 *	none
 *
 * OUTPUTS:
 *	none
 *
 * RETURNS:
 *	none
 *
 * EXTERNALLY READ:
 *	none
 *
 * EXTERNALLY MODIFIED:
 *	none
 *
 * DESCRIPTION:
 *	Export graphics files.
 */

void 
Export(void)
{
    int pages;
    int frompage, topage;
    char *tmpPtr;
    char tmpRoot[9];


    frompage = 1;
    topage = 0;

    if (exportDialog == NULL) {
	createExportDialog();
    }

    if (filename == NULL) {
	strcpy(tmpRoot, "unknown");
    } else {
	tmpPtr = filename;
	while(strchr(tmpPtr, '/') != NULL) {
	    tmpPtr = strchr(tmpPtr, '/') + 1;
	}
	strncpy(tmpRoot, tmpPtr, 8);
	tmpRoot[8] = '\0';
	if (strchr(tmpRoot, '.') != NULL) {
	    *strchr(tmpRoot, '.') = '\0';
	}
    }

    if (appleFile != NULL) XtFree(appleFile);
    if (appleFileTpl != NULL) {
	appleFile = XtNewString(appleFileTpl);
    } else {
	appleFile = (char *)XtMalloc(strlen(tmpRoot) + 6);
	strcpy(appleFile, tmpRoot);
	strcat(appleFile, ".pict");
    }
    DialogTextSet(dlgPctName, appleFile);

    if (windowsFile != NULL) XtFree(windowsFile);
    if (windowsFileTpl != NULL) {
	windowsFile = XtNewString(windowsFileTpl);
    } else {
	windowsFile = (char *)XtMalloc(strlen(tmpRoot) + 5);
	strcpy(windowsFile, tmpRoot);
	strcat(windowsFile, ".wmf");
    }
    DialogTextSet(dlgWmfName, windowsFile);

    if (DialogManage(exportDialog) == DIALOG_UPDATE) {

	pages = DialogRadioGet(dlgPctPage);
	XtFree(appleFile);
	appleFile = DialogTextGet(dlgPctName);
	if (pages == PAGES_ALL) {
	    frompage = 1;
	    topage = num_pages;
	} else if (pages == PAGES_THIS) {
	    frompage = current_page;
	    topage = current_page;
	}
	print_to_file(appleFile, frompage, topage, PCT);

	frompage = 1;
	topage = 0;
	pages = DialogRadioGet(dlgWmfPage);
	XtFree(windowsFile);
	windowsFile = DialogTextGet(dlgWmfName);
	if (pages == PAGES_ALL) {
	    frompage = 1;
	    topage = num_pages;
	} else if (pages == PAGES_THIS) {
	    frompage = current_page;
	    topage = current_page;
	}
	print_to_file(windowsFile, frompage, topage, WMF);

	Message("Completed exporting files");
    }
}

/***********************************************************************
 *
 * FUNCTION:
 *	exportDialogCB
 *
 * INPUTS:
 *	w		- (Widget) shell or button
 *	client_data	- (XtPointer) reason
 *	callback_data	- (XtPointer) not used
 *
 * OUTPUTS:
 *	none
 *
 * RETURNS:
 *	none
 *
 * EXTERNALLY READ:
 *	none
 *
 * EXTERNALLY MODIFIED:
 *	none
 *
 * DESCRIPTION:
 *	Callback for OK and Cancel buttons.
 */

static void
exportDialogCB(Widget w, XtPointer client_data, XtPointer callback_data)
{
    if ((int)client_data == DIALOG_CANCEL) {
	if (ShellValidateClose(w)) {
	    DialogFlagSet(DIALOG_CANCEL);
	}
    } else if ((int)client_data == DIALOG_UPDATE) {
	DialogFlagSet(DIALOG_UPDATE);
    }
}

/***********************************************************************
 *
 * FUNCTION:
 *      print_to_file()
 *
 * INPUTS:
 *      name            - (char *) name of file
 *      startpage       - (int) first page to be printed
 *      endpage         - (int) last page to be printed
 *
 * OUTPUTS:
 *      none
 *
 * RETURNS:
 *      none
 *
 * EXTERNALLY READ:
 *      none
 *
 * EXTERNALLY MODIFIED:
 *      none
 *
 * DESCRIPTION:
 *      This function outputs to a Mac PICT or Windows META file for 
 *	each page.
 */

static void
print_to_file(char *name, int startpage, int endpage, int type)
{
    char fname[256];		/* temp buffer for filename */
    char *ext;			/* extension of filename */
    int i;                      /* temp loop index */
    int currpage;               /* current page of display */

    /*
     * save current page since variable must be updated for set_dsp_size
     */
    currpage = current_page;

    /*
     * split file name for multiple pages
     */
    if (startpage != endpage) {
	if (strchr(name, '.') == NULL) {
	    ext = (type == PCT) ? "pict" : "wmf";
	} else {
	    ext = name + strlen(name);
	    while (*ext != '.') --ext;
	    *ext = '\0';
	    ++ext;
	}
    }

    /*
     * cycle through pages
     */
    for (current_page = startpage; current_page <= endpage; ++current_page) {
	set_dsp_size();
	for (i = 0; i < Hnobj; i++) {
            (*(Hobjary[i].opsptr->loc))(i, OBJECT);
            (*(Hobjary[i].opsptr->dsp))(i);
        }
        ren_hline();
        for (i = 0; i < nobj; i++) {
            (*(objary[i].opsptr->loc))(i, OBJECT);
            (*(objary[i].opsptr->dsp))(i);
        }
        for (i = 0; i < npln; i++) {
            loc_pline(i, OBJECT);
        }
	if (startpage == endpage) {
            (void)strcpy(fname, name);
	} else {
	    (void)sprintf(fname, "%s%d.%s", name, current_page, ext);
	}
	if (type == PCT) {
	    Message("Creating file: %s", fname);
	    if (gr_pict_on(fname, TRUE) < 0) {
		Warning("ERROR: Could not create file.");
		return;
	    }
	    draw_outline(TRUE);
	    gr_pict_off();
	} else {
	    Message("Creating file: %s", fname);
	    if (gr_wmf_on(fname) < 0) {
		Warning("ERROR: Could not create file.");
		return;
	    }
	    draw_outline(TRUE);
	    gr_wmf_off();
	}
    }

    /* set back to original status */
    current_page = currpage;
    set_dsp_size();
    for (i = 0; i < Hnobj; i++) {
        (*(Hobjary[i].opsptr->loc))(i, OBJECT);
        (*(Hobjary[i].opsptr->dsp))(i);
    }
    ren_hline();
    for (i = 0; i < nobj; i++) {
        (*(objary[i].opsptr->loc))(i, OBJECT);
        (*(objary[i].opsptr->dsp))(i);
    }
    for (i = 0; i < npln; i++) {
        loc_pline(i, OBJECT);
    }
}

