/***********************************************************************
 *
 * FILE:
 *	 plot.c
 * 
 * AUTHOR:
 *	Cassie Mulnix
 *
 * DESCRIPTION: creation and callbacks for the plot options dialog box
 *
 * $Log: plot.c,v $
 * Revision 1.36  1994/12/31  04:14:07  kevin
 * fixed memory allocation problem after DialogTextGet
 *
 * Revision 1.35  1994/08/10  22:03:27  cassie
 * updated to DIALOG_ constants for DialogFlagSet
 *
 * Revision 1.34  1994/07/20  19:06:35  clm
 * updated TRUE, ERROR etc in cancel & ok_button functions
 * to DIALOG_ constants
 *
 * Revision 1.33  1994/06/16  21:57:27  clm
 * updated to new dialog routines - extra parameter to DialogTextCreate
 *
 * Revision 1.32  1994/06/08  18:47:59  clm
 * removed Xm library includes since they are in dialog
 *
 * Revision 1.31  1994/06/07  16:39:26  clm
 * changed parameters to void
 *
 * Revision 1.30  1994/06/01  22:30:59  clm
 * changed to use new oellib dialogs
 *
 * Revision 1.29  1994/05/20  23:31:43  clm
 * updated with HP, SGI changes
 *
 * Revision 1.28  1994/05/04  23:34:36  clm
 * ported to ANSI C
 *
 * Revision 1.27  1993/05/18  22:39:41  clm
 * added meaningful error messages for check_num and check_day
 *
 * Revision 1.26  1993/05/18  20:58:30  clm
 * added new message.c function
 *
 * Revision 1.25  1993/02/22  18:44:13  clm
 * fixed plot ok_button to reset current_page if num_pages changed
 *
 * Revision 1.24  1993/02/08  16:16:30  clm
 * set_optionmenu for HGRID, MODE, and ISSUE
 *
 * Revision 1.23  1992/12/17  22:53:59  clm
 * fixed G_SOLID for vertical grid to V_SOLID so numbering correct
 *
 * Revision 1.22  1992/11/24  21:22:45  clm
 * added loc_pline to edit_pline
 *
 * Revision 1.20  1992/10/16  15:02:22  clm
 * error checking in ok_button for number of lines exceeding MAX_LINE
 *
 * Revision 1.19  1992/10/02  20:29:27  clm
 * housekeeping
 *
 * Revision 1.18  1992/10/02  18:42:46  clm
 * added locate for ylnary to ok_button
 *
 * Revision 1.17  1992/09/25  21:11:20  clm
 * check optionmenu for vgrid in ok_button
 *
 * Revision 1.16  1992/09/22  22:19:02  clm
 * fixed set_dsp_size to set ylnary[3] to match last line coordinate
 *
 * Revision 1.15  1992/09/10  21:02:58  clm
 * removed y_lines_end assignment from set_dsp_size
 * y_lines_end is now set according to ylnary[3]
 *
 * Revision 1.14  1992/09/02  15:34:52  clm
 * fixed refresh after set_dsp_size call
 *
 * Revision 1.13  1992/09/02  15:10:46  clm
 * fixed set_dsp_size for loop
 *
 * Revision 1.12  1992/08/27  16:13:56  clm
 * added vgrid option
 *
 * Revision 1.11  1992/08/26  15:06:09  clm
 * took exposeRectList out of set_dsp_size
 * fixed set_optionmenu for page_values
 *
 * Revision 1.10  1992/08/25  20:08:39  clm
 * added modified value to toggle changes
 *
 * Revision 1.9  1992/08/21  23:09:18  clm
 * added expose event in set_dsp_size
 *
 * Revision 1.8  1992/08/20  16:11:24  clm
 * changed hline grid values so that gr_routines can be used
 *
 * Revision 1.7  1992/08/14  21:23:53  clm
 * moved loc call in set_dsp_size
 *
 * Revision 1.6  1992/08/04  16:33:48  clm
 * fixed num_pages assignment value in set_dsp_size
 *
 * Revision 1.5  1992/08/03  21:35:31  clm
 * added number of pages text widget
 *
 * Revision 1.4  1992/07/13  20:47:22  clm
 * added locate for objects when lines per page is changed
 * changed color for add_hline call
 *
 * Revision 1.3  1992/07/08  19:55:09  clm
 * changed comment format
 *
 * Revision 1.2  1992/07/08  19:49:51  clm
 * changed add_hline to reflect color value
 * added issue_date display call
 *
 * Revision 1.1  1992/06/29  16:07:02  clm
 * Initial revision
 *
 */

#ifndef lint
static char rcsid[] =
    "$Id: ";
#endif

#include "xopps.h"

enum dialog_type { DONE_BUTTON, HELP_BUTTON, CANCEL_BUTTON, PAGE_LAB,
		   LINE_PAGE, NUM_PAGE, HGRID, VGRID, MODE, ISSUE,
		   LINE, N_ITEMS
}; 

static Display *display;		/* display for popup */

static void create_dialog(void);
static void ok_button(void);		/* callback for ok button */
static void cancel_button(void);	/* callback for cancel button */
static void change_hgrid(Widget, XtPointer, XtPointer);
static void change_vgrid(Widget, XtPointer, XtPointer);
static void change_mode(Widget, XtPointer, XtPointer);
static void change_issue(Widget, XtPointer, XtPointer);
static void change_page(Widget, XtPointer, XtPointer);
static void verify_fun(Widget, XtPointer, XtPointer);

static DialogText *nlines;
static DialogOption *npages;
static DialogOption *hgrid, *vgridw;
static DialogOption *mode, *issue;

Widget popupplot;				/* dialog shell for popup */
static Widget parent_shell;
static int new_hgrid;				/* new horizontal grid value */
static int old_vgrid;				/* new vertical grid value */
static int old_issue;				/* old issue date */
static int old_hgrid;				/* previous hgrid value */
static int old_mode;				/* previous mode value */
static int old_page;				/* previous mode value */
static int oldval;				/* previous # of lines */	

/***********************************************************************
 *
 * FUNCTION:
 *      init_plot_dialog()
 *
 * INPUTS:
 *	parent		(Widget)
 *	popup_display	(Display *)
 *
 * OUTPUTS:
 *	none
 *
 * RETURNS:
 *	none
 *
 * EXTERNALLY READ:
 *	none
 *
 * EXTERNALLY MODIFIED:
 *	none
 *
 * DESCRIPTION:
 *	registers the display and parent
 */

void 
init_plot_dialog(Widget parent, Display *popup_display)
{
	/* initial options set */
    old_hgrid = all_grid_dsp;
    old_mode = 0;
    old_issue = TOG_ON;
    old_page = num_pages;

    display = popup_display;
    parent_shell = parent;
}
    
/***********************************************************************
 *
 * FUNCTION:
 *      create_dialog()
 *
 * INPUTS:
 *	none
 *
 * OUTPUTS:
 *	none
 *
 * RETURNS:
 *	none
 *
 * EXTERNALLY READ:
 *	none
 *
 * EXTERNALLY MODIFIED:
 *	none
 *
 * DESCRIPTION:
 *	This function initializes the plot box options dialog.  It
 *      Creates the shell, form, label, and text widgets needed for a
 *      popup dialog.
 */

static void
create_dialog(void)
{
    static char *pages[] = { "1", "2", "3", "4", "5", "6", "7" };
    static int page_vals[] = { 1, 2, 3, 4, 5, 6, 7 };
    static char *hgrids[] = { "Solid", "Thick", "Dotted", "Short Dash",
	"Long Dash", "None" };
    static int hgrid_vals[] = { G_SOLID, G_THICK, G_DOT_LINE, G_SHORT_DASH, 
	G_LONG_DASH, G_NONE };
    static char *vgrids[] = { "Solid", "None" };
    static int vgrid_vals[] = { V_SOLID, V_NONE };
    static char *modes[] = { "Turn On All Numbers", "Turn On All Lines",
	"Turn Off All Numbers", "Turn Off All Lines", "Renumber On Lines" };
    static char *issues[] = { "No", "Yes" };

    popupplot = DialogInit(parent_shell, "Plot Dialog", "plot", verify_fun,
	(XtPointer)DIALOG_CANCEL);

    nlines = DialogTextCreate("Number of Lines Per Page ", 2, 2);

    npages = DialogOptionCreate("Number of Pages ", 7, 0, change_page,
	DIALOG_OPT_STR_ARY, pages, page_vals);

    hgrid = DialogOptionCreate("Horizontal Grid ", 6, 0, change_hgrid,
	DIALOG_OPT_STR_ARY, hgrids, hgrid_vals);

    vgridw = DialogOptionCreate("Vertical Grid ", 2, 0, change_vgrid,
	DIALOG_OPT_STR_ARY, vgrids, vgrid_vals);

    mode = DialogOptionCreate("Display Mode", 5, 0, change_mode,
	DIALOG_OPT_STR_ARY, modes, NULL);

    issue = DialogOptionCreate("Display Issue Date", 2, 1, change_issue,
	DIALOG_OPT_STR_ARY, issues, NULL);

    DialogStdButtons(verify_fun, (XtPointer)DIALOG_UPDATE, verify_fun,
        (XtPointer)DIALOG_CANCEL, (XtCallbackProc)ShowHelp, "plot");

}

/***********************************************************************
 *
 * FUNCTION:
 *      ok_button()
 *
 * INPUTS:
 *	none
 *
 * OUTPUTS:
 *	none
 *
 * RETURNS:
 *	none
 *
 * EXTERNALLY READ:
 *	none
 *
 * EXTERNALLY MODIFIED:
 *	none
 *
 * DESCRIPTION:	
 *	checks the edited values to see if they have been changed
 *
 */

static void 
ok_button(void)
{
    int change = DIALOG_NO_CHANGE;
    int pgchange;
    int temp;				/* temp old value */
    int i;				/* temp loop index */
    int val;

    change |= modified;
    if (num_pages != old_page) {
	change |= DIALOG_UPDATE;
    }
    old_page = DialogOptionGet(npages);

    val = DialogTextGetInt(nlines);
    if ((val < 1) || (val > 99)) {
	Warning("Improper Lines Per Page Value");
	pgchange = DIALOG_ERROR;
    }
    if (val != pg_lines) {
	pgchange = DIALOG_UPDATE;
    }
    pg_lines = val;

    if ((pg_lines * num_pages) > MAX_LINE) {
	Warning("Number of lines on all pages exceeds the maximum of 253");
	pgchange |= DIALOG_ERROR;
    }
    if (num_pages < current_page) {
	current_page = num_pages;
    }
    if (pgchange == DIALOG_UPDATE) {
	set_dsp_size();
	for (i = 0; i < nyln; i++) {
	    (*(ylnary[i].opsptr->loc))(i, LINE_Y);
	}
    }
    change |= pgchange;

    if (old_hgrid != new_hgrid) {
	change |= DIALOG_UPDATE;
    }
    old_hgrid = DialogOptionGet(hgrid);

    if (old_vgrid != vgrid) {
	change |= DIALOG_UPDATE;
    }
    old_vgrid = DialogOptionGet(vgridw);

    temp = DialogOptionGet(mode);

    if (old_issue != (int)issue_disp) {
	change |= DIALOG_UPDATE;
    }
    old_issue = DialogOptionGet(issue);

    object_change = change;
    DialogFlagSet(change);
}

/***********************************************************************
 *
 * FUNCTION:
 *	cancel_button
 *
 * INPUTS:
 *
 * OUTPUTS:
 *
 * RETURNS:
 *
 * EXTERNALLY READ:
 *
 * EXTERNALLY MODIFIED:
 *
 * DESCRIPTION:
 *	discards any edits and unmanages popup
 *
 */

static void 
cancel_button(void)
{
    object_change = DIALOG_CANCEL; 
    DialogFlagSet(DIALOG_CANCEL);
}

/***********************************************************************
 *
 * FUNCTION:
 *      change_page()
 *
 * INPUTS:
 *      w                       (Widget)
 *      option                  (XtPointer)
 *      any_data                (XtPointer)
 *
 * OUTPUTS:
 *
 * RETURNS:
 *
 * EXTERNALLY READ:
 *
 * EXTERNALLY MODIFIED:
 *
 * DESCRIPTION:
 *      sets new number of pages value
 */

static void 
change_page(Widget w, XtPointer option, XtPointer any_data)
{
    old_page = (int)option;
    num_pages = (int)option + 1;
    modified = DIALOG_UPDATE;
}

/***********************************************************************
 *
 * FUNCTION:
 *	change_hgrid()
 *
 * INPUTS:
 *	w                       (Widget)
 *      option             	(XtPointer)
 *      any_data               	(XtPointer)
 *
 * OUTPUTS:
 *
 * RETURNS:
 *
 * EXTERNALLY READ:
 *
 * EXTERNALLY MODIFIED:
 *
 * DESCRIPTION:
 *	sets new horizontal grid value 
 */

static void 
change_hgrid(Widget w, XtPointer option, XtPointer any_data)
{
    int i;				/* temp index */

    for (i = 0; i < MAX_LINE; i++) {
        Hobjary[i].z.hlnptr->grid = (int)option;
    }
    old_hgrid = (int)option;
    all_grid_dsp = (int)option;
    modified = DIALOG_UPDATE;
}

/***********************************************************************
 *
 * FUNCTION:
 *	change_vgrid()
 *
 * INPUTS:
 *	w                       (Widget)
 *      option		        (XtPointer)
 *      any_data		(XtPointer)
 *
 * OUTPUTS:
 *
 * RETURNS:
 *
 * EXTERNALLY READ:
 *
 * EXTERNALLY MODIFIED:
 *
 * DESCRIPTION:
 *	sets new vertical grid value
 */

static void 
change_vgrid(Widget w, XtPointer option, XtPointer any_data)
{
    old_vgrid = vgrid;
    vgrid = (int)option;
}

/***********************************************************************
 *
 * FUNCTION:
 *	change_mode()
 *
 * INPUTS:
 *	w                       (Widget)
 *      option			(XtPointer) 
 *      any_data		(XtPointer)
 *
 * OUTPUTS:
 *
 * RETURNS:
 *
 * EXTERNALLY READ:
 *
 * EXTERNALLY MODIFIED:
 *
 * DESCRIPTION:
 *	sets new display mode value
 */

static void 
change_mode(Widget w, XtPointer option, XtPointer any_data)
{
    int i;				/* temp index */

    switch((int)option) {
    case 0:
	for (i = 0; i < MAX_LINE; i++) {
	    Hobjary[i].z.hlnptr->dsp_num = TOG_ON;
	}
	all_num_dsp = TOG_ON;
	break;
    case 1:
	for (i = 0; i < MAX_LINE; i++) {
	    Hobjary[i].z.hlnptr->dsp_lin = TOG_ON;
	}
	all_line_dsp = TOG_ON;
	break;
    case 2:
	for (i = 0; i < MAX_LINE; i++) {
	    Hobjary[i].z.hlnptr->dsp_num = TOG_OFF;
	}
   	all_num_dsp = TOG_OFF;
	break;
    case 3:
	for (i = 0; i < MAX_LINE; i++) {
	    Hobjary[i].z.hlnptr->dsp_lin = TOG_OFF;
	}
	all_line_dsp = TOG_OFF;
	break;
    default:
	ren_hline();
    }
    modified = DIALOG_UPDATE;
    old_mode = (int)option;
}

/***********************************************************************
 *
 * FUNCTION:
 *	change_issue()
 *
 * INPUTS:
 *	w                       (Widget)
 *      client_data             (XtPointer)
 *      call_data               (XtPointer)
 *
 * OUTPUTS:
 *
 * RETURNS:
 *
 * EXTERNALLY READ:
 *
 * EXTERNALLY MODIFIED:
 *
 * DESCRIPTION:
 *	sets new issue date display value
 */

static void 
change_issue(Widget w, XtPointer option, XtPointer any_data)
{
    if ((int)option == 1) {
	issue_disp = TOG_ON;
    } else {
	issue_disp = TOG_OFF;
    }
    modified = DIALOG_UPDATE;
    old_issue = (int)option;
}

/***********************************************************************
 *
 * FUNCTION:
 *	edit_plot
 *
 * INPUTS:
 *	box	(Widget)
 *
 * OUTPUTS:
 *
 * RETURNS:
 *
 * EXTERNALLY READ:
 *
 * EXTERNALLY MODIFIED:
 *
 * DESCRIPTION:
 *	pops up the plot dialog box
 *
 */

void 
edit_plot(void)
{
    int i;				/* temp index */

    if (popupplot == NULL) {
	create_dialog();
    }

    object_change = DIALOG_ERROR;

    DialogOptionSet(npages, num_pages);
    DialogOptionSet(vgridw, vgrid);
    DialogOptionSet(hgrid, old_hgrid);
    DialogOptionSet(mode, old_mode);
    DialogOptionSet(issue, old_issue);
    DialogTextSetInt(nlines, pg_lines);

    DialogManage(popupplot);

    if (object_change == DIALOG_UPDATE) {
	for (i = page_begin; i <= page_end; i++) {
	    (*(Hobjary[i].opsptr->loc))(i, OBJECT);
 	}
	for (i = 0; i < nobj; i++) {
	    (*(objary[i].opsptr->loc))(i, OBJECT);
 	}
	for (i = 0; i < npln; i++) {
	    loc_pline(i, OBJECT);
	}
	exposeRectList(0, objary[0].rlist);
    }
}

/***********************************************************************
 *
 * FUNCTION:
 *	set_dsp_size()
 *
 * INPUTS:      
 *
 * OUTPUTS:
 *
 * RETURNS:
 *
 * EXTERNALLY READ:
 *
 * EXTERNALLY MODIFIED:
 *
 * DESCRIPTION:
 *	sets the new number of lines displayed on screen per page
 *
 */

void 
set_dsp_size(void)
{
    int i;				/* temp index */

    oldval = pg_lines;
    page_end = current_page * pg_lines;
    page_begin = page_end - pg_lines + 1;
    y_gap = (int)((y_lines_end - y_act_bottom) / pg_lines);
    ylnary[3].z.lnptr->cord = y_gap * pg_lines + y_act_bottom;
    for (i = oldval; i < pg_lines; i++) {
	add_hline(i, i, all_line_dsp, all_num_dsp, objary[0].z.chtptr->color, 0,
	     10, all_grid_dsp, "");
	if (Hobjary[i-1].rlist == NULL) {
	    Hobjary[i-1].rlist = newRectList(0);
 	}
    }
    for (i = 0; i < Hnobj; i++) {
	(*(Hobjary[i].opsptr->loc))(i, OBJECT);
    }
    modified = DIALOG_UPDATE;
}

/***********************************************************************
 *
 * FUNCTION:
 *      verify_fun()
 *
 * INPUTS:
 *      w       (Widget)
 *      x, y    (XtPointer)
 *
 * OUTPUTS:
 *      none
 *
 * RETURNS:
 *      none
 *
 * EXTERNALLY READ:
 *      none
 *
 * EXTERNALLY MODIFIED:
 *      none
 *
 * DESCRIPTION:
 *      validates that the popup dialog can be closed
 */

static void
verify_fun(Widget w, XtPointer x, XtPointer y)
{
    if ((int)x == DIALOG_CANCEL) {
        if (!ShellValidateClose(w)) return;
        cancel_button();
    } else if ((int)x == DIALOG_UPDATE) {
        ok_button();
    }
}
