/*
 * main.c - main routine for gened program
 *
 * $Log: main.c,v $
 * Revision 1.65  1995/01/05  03:43:44  kevin
 * removed extra shell definition / fixed shell management
 *
 * Revision 1.64  1994/12/31  05:05:25  kevin
 * updated shell management
 *
 * Revision 1.63  1994/12/31  01:20:06  kevin
 * removed redundant file loader
 *
 * Revision 1.62  1994/10/12  23:25:16  cassie
 * get resource names correctly for res_font and res_point
 *
 * Revision 1.61  1994/08/10  22:07:24  cassie
 * added export resources and called InitExport
 *
 * Revision 1.60  1994/06/01  22:29:16  clm
 * cleaned up arguments to init functions
 *
 * Revision 1.59  1994/05/20  23:31:43  clm
 * updated with HP, SGI changes
 *
 * Revision 1.58  1994/05/04  23:34:36  clm
 * ported to ANSI C
 *
 * Revision 1.57  1994/02/09  16:23:11  clm
 * added week_monday as resource to display weeks by Mondays
 *
 * Revision 1.56  1993/10/21  16:09:19  clm
 * updated to oellib help.c
 *
 * Revision 1.55  1993/10/19  14:40:50  clm
 * updated all oellib stuff
 *
 * Revision 1.54  1993/05/18  20:58:30  clm
 * added new message.c function
 *
 * Revision 1.53  1993/02/20  00:07:41  clm
 * added filename to top of chart
 *
 * Revision 1.52  1992/12/11  22:48:12  clm
 * fixed get_input for errors in load_file
 *
 * Revision 1.51  1992/12/11  21:29:34  clm
 * made RSRC_FONT and RSRC_POINT global
 *
 * Revision 1.50  1992/12/10  22:46:03  clm
 * added font and point as resources for init_chart
 *
 * Revision 1.49  1992/11/24  22:39:27  clm
 * made changes to get_input if load is not successful
 *
 * Revision 1.48  1992/11/24  21:15:17  clm
 * added loc_pline to get_input if file is loaded from command line
 *
 * Revision 1.47  1992/11/09  21:47:15  clm
 * removed unused variables
 *
 * Revision 1.46  1992/11/09  16:35:59  clm
 * changed OPPS Help to XOPPS help
 *
 * Revision 1.45  1992/10/06  17:06:17  clm
 * changed used of infile to filename from filemenu.c
 *
 * Revision 1.44  1992/10/05  22:16:48  clm
 * added ability to add data file on command line
 *
 * Revision 1.42  1992/09/16  14:06:47  clm
 * moved initialization of issue_disp to init_chart
 *
 * Revision 1.41  1992/09/11  23:00:12  clm
 * changed to GR_EXPAND mode
 *
 * Revision 1.40  1992/09/10  17:12:36  clm
 * added fileFilter resource
 * added init_utc_resol call
 *
 * Revision 1.39  1992/08/31  17:02:21  clm
 * added Objects, Operations, and HLine help
 *
 * Revision 1.38  1992/08/25  20:07:40  clm
 * added init_beware_dialog
 *
 * Revision 1.37  1992/08/25  15:47:15  clm
 * added init_line_object call
 *
 * Revision 1.36  1992/07/15  19:42:30  clm
 * deleted current_date since clock.c not included
 *
 * Revision 1.35  1992/07/08  19:50:28  clm
 * added current_date call
 *
 * Revision 1.34  1992/07/06  17:51:47  clm
 * added Option Menu help
 * added plot_dialog popup
 *
 * Revision 1.33  1992/06/24  17:13:47  clm
 * changed chart menu to option menu
 *
 * Revision 1.32  1992/06/12  18:14:44  clm
 * made chart and object
 *
 * Revision 1.31  1992/05/22  14:43:04  clm
 * fixed arguments for command string
 *
 * Revision 1.30  1992/05/21  16:21:31  clm
 * removed width, height settings from init_form
 *
 * Revision 1.28  1992/05/20  19:50:57  clm
 * cleaned up file and help format
 *
 * Revision 1.27  1992/05/11  23:44:01  kevin
 * renamed icon file
 *
 * Revision 1.26  1992/05/11  22:39:06  kevin
 * renamed project include file to xopps.h
 *
 * Revision 1.25  1991/10/01  00:00:00  tommy
 * added xopps specifics
 *
 * Revision 1.21  1991/06/19  22:14:15  tommy
 * Changed ichart() to init_chart()
 *
 * Revision 1.20  1991/06/18  18:29:44  tommy
 * Assigned fixed value GR_LANDSCAPE to mode
 *
 * Revision 1.18  1991/05/22  17:14:21  ana
 * Added call to init_fileio()
 *
 * Revision 1.17  1991/05/16  02:06:08  kevin
 * added title and release to title bar
 * added DEBUG for heap consistency check
 *
 * Revision 1.2  90/11/29  14:24:32  ana
 * First cut at motif.
 * 
 * Revision 1.1  90/11/02  08:41:06  ana
 * Initial revision
 * 
 */

#ifndef lint
static char rcsid[] = "$Id: main.c,v 1.65 1995/01/05 03:43:44 kevin OEL $";
#endif

/*************************************************************************

**************************************************************************/

#define MAIN_MOD 1
#include "xopps.h"

#include <unistd.h>

#include <Xm/Form.h>
#include <Xm/Frame.h>
#include <Xm/LabelG.h>
#include <Xm/RowColumn.h>
#include <X11/StringDefs.h>

#include "xopps.bm"

static void init_form(Display *, Widget, int);

char filename[_POSIX_PATH_MAX];
static char title[128];
static Widget app_shell;
extern char *optarg;

static struct help_list help_menu[] = {
    { "Overview", "overview" },
    { "File Menu", "fileMenu" },
    { "Option Menu", "optionMenu" },
    { "Objects", "objects" },
    { "Operations", "operations" },
    { "HLine", "Hline" }
};

/*
 * application resources
 */

typedef struct {
    String fileFilter;
    String res_font;
    String res_point;
    Boolean week_monday;
    Boolean appleSingle;
    String appleFile;
    String windowsFile;
} AppResData, *AppResDataPtr;

static AppResData rdata;

static XtResource resources[] = {
    {
        "fileFilter", "FileFilter", XtRString, sizeof(String),
        XtOffset(AppResDataPtr, fileFilter), XtRString, "*.xopps"
    }, {
	"res_font", "Font", XtRString, sizeof(String),
	XtOffset(AppResDataPtr, res_font), XtRString, "TIMES"
    }, {
	"res_point", "Point", XtRString, sizeof(String),
	XtOffset(AppResDataPtr, res_point), XtRString, "10"
    }, {
	"week_monday", "Week_Monday", XtRBoolean, sizeof(Boolean),
	XtOffset(AppResDataPtr, week_monday), XtRString, "TRUE"
    }, {
        "appleFile", "AppleFile", XtRString, sizeof(String),
        XtOffset(AppResDataPtr, appleFile), XtRString, NULL
    }, {
        "windowsFile", "WindowsFile", XtRString, sizeof(String),
        XtOffset(AppResDataPtr, windowsFile), XtRString, NULL
    }, {
	"appleSingle", "AppleSingle", XtRBoolean, sizeof(Boolean),
        XtOffset(AppResDataPtr, appleSingle), XtRString, "True"
    }
};

/***********************************************************************
 *
 * FUNCTION:
 *	main()
 *
 * INPUTS:
 *	argc	(int)
 *	argv	(char *)
 *
 * OUTPUTS:
 *
 * RETURNS:
 *
 * EXTERNALLY READ:
 *
 * EXTERNALLY MODIFIED:
 *
 * DESCRIPTION:
 *	defines tool window for xopps program
 */

int 
main(int argc, char *argv[])
{
    XtAppContext app;
    Arg args[20];
    Cardinal n;
    int mode;
    Pixmap icon_pixmap;

    /*
     * set program name, and use malloc debug if in DEBUG mode
     */
#ifdef DEBUG
    (void)sprintf(title, "%s V%s (DEBUG) %s", PROGRAM, RELEASE, COPYRIGHT);
#else
    (void)sprintf(title, "%s V%s %s", PROGRAM, RELEASE, COPYRIGHT);
#endif

    XtToolkitInitialize();
    app = XtCreateApplicationContext();
    dw.display = XtOpenDisplay(app, NULL, NULL, "XOpps", NULL, 0, &argc,
        argv);

    if (!dw.display) {
        XtWarning("xopps: Can't open display");
        exit(1);
    }

    dw.screen = DefaultScreen(dw.display);
    dw.iscolor = (XDisplayCells(dw.display, dw.screen) > 2);

    n = 0;
    XtSetArg(args[n], XmNallowShellResize, True); n++;
    XtSetArg(args[n], XmNtitle, title); n++;
    app_shell = XtAppCreateShell(NULL, "XOpps", applicationShellWidgetClass,
	dw.display, args, n);

    ShellAddClose(app_shell, (XtCallbackProc)exit_button, NULL);

    /*
     * load application resources 
     */
    XtGetApplicationResources(app_shell, &rdata, resources, XtNumber(resources),
 	NULL, 0);

    mode = GR_LANDSCAPE | GR_EXPAND;

    /* set time resolution to mm/dd/yy */
    init_utc_resol(UTC_DAYS);

    init_form(dw.display, app_shell, mode);

    InitBeware(app_shell, "Warning");

    init_fileio();
    InitHelp(app_shell, "XOPPS Help", "xopps.info");
    InitFileSelect(app_shell, "XOpps File Select");
    init_line_object();
    init_font(rdata.res_font);
    init_point(rdata.res_point);
    week_monday = rdata.week_monday;
    init_objects(app_shell, dw.display, app, rdata.appleSingle);
    InitExport(app_shell, rdata.appleFile, rdata.windowsFile, 
	rdata.appleSingle);
    init_plot_dialog(app_shell, dw.display);
    issue_date();
   
    /*
     * load input file if one is entered on command line
     */ 
    if (argc == 2) {
	load_the_file(argv[1]);
    }
    modified = FALSE;

    n = 0;
    icon_pixmap = XCreateBitmapFromData(dw.display, dw.win, xopps_bits,
	xopps_width, xopps_height);
    XtSetArg(args[n], XmNiconPixmap, (XtArgVal)icon_pixmap); n++;
    XtSetValues(app_shell, args, n);
    XtAppMainLoop(app);
    exit(0);
}

/***********************************************************************
 *
 * FUNCTION:
 *	init_form()
 *
 * INPUTS:
 *	display 	(Display)
 *	parent		(Widget)
 *	mode		(int)
 *
 * OUTPUTS:
 *
 * RETURNS:
 *
 * EXTERNALLY READ:
 *
 * EXTERNALLY MODIFIED:
 *
 * DESCRIPTION:
 *	initializes the form of the display
 */

static void 
init_form(Display *display, Widget parent, int mode)
{
    Widget form;
    Widget menubar;
    Widget frame;
    Widget messageArea;
    Arg args[20];
    Cardinal n;
    Widget InitMessage(Widget parent, ...);

    n = 0;
    form = XmCreateForm(parent, "mainform", args, n);
    XtManageChild(form);

    n = 0;
    XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
    XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
    XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
    menubar = XmCreateMenuBar(form, "menubar", args, n);
    XtManageChild(menubar);

    init_file_menu(menubar, rdata.fileFilter);
    init_option_menu(menubar);
    InitHelpMenu(menubar, help_menu, XtNumber(help_menu));

    messageArea = InitMessage(form);

    n = 0;
    XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
    XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
    XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
    XtSetValues(messageArea, args, n);

    n = 0;
    XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
    XtSetArg(args[n], XmNtopWidget, menubar); n++;
    XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
    XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
    XtSetArg(args[n], XmNbottomAttachment, XmATTACH_WIDGET); n++;
    XtSetArg(args[n], XmNbottomWidget, messageArea); n++;
    frame = XmCreateFrame(form, "frame", args, n);
    XtManageChild(frame);

    init_drawing_1(dw.display, frame, mode);
    XtRealizeWidget(parent);
    init_drawing_2();

}

/***********************************************************************
 *
 * FUNCTION:
 *      set_title()
 *
 * INPUTS:
 *
 * OUTPUTS:
 *
 * RETURNS:
 *
 * EXTERNALLY READ:
 *
 * EXTERNALLY MODIFIED:
 *
 * DESCRIPTION:
 *      Adds the open file name to the tool title.
 */

void 
set_title(char *name, char *dir)
{
    char banner[256];
    Cardinal n;
    Arg args[2];

    n = 0;
    if (name != NULL) {
        (void)sprintf(banner, "%s V%s         File: %s%s", PROGRAM, RELEASE, 
	    dir, name);
        XtSetArg(args[n], XmNtitle, banner);  n++;
    } else {
        XtSetArg(args[n], XmNtitle, title);  n++;
    }
    XtSetValues(app_shell, args, n);
}
