/* 
 * tkAppInit.c --
 *
 *	Provides a default version of the Tcl_AppInit procedure for
 *	use in wish and similar Tk-based applications.
 *
 * Copyright (c) 1993 The Regents of the University of California.
 * All rights reserved.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the
 * above copyright notice and the following two paragraphs appear in
 * all copies of this software.
 * 
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 */

/*******************************************************************
 *   tkAppInit.c                                      Version 1.0  *
 *   Last Modification : today                                     *
 *-----------------------------------------------------------------*
 * Copyright (c) 1993, 1994                                        *
 * The Board of Trustees of the Leland Stanford Junior University. *
 * All rights reserved.                                            *
 * Author: Gene McKenna                                            *
 *******************************************************************/

#ifndef lint
static char rcsid[] = "$Header: /user6/ouster/wish/RCS/tkAppInit.c,v 1.8 93/08/26 14:38:24 ouster Exp $ SPRITE (Berkeley)";
#endif /* not lint */

#include "tk.h"
#include "plot.h"
#include "globals.h"

/*
 * The following variable is a special hack that allows applications
 * to be linked using the procedure "main" from the Tk library.  The
 * variable generates a reference to "main", which causes main to
 * be brought in from the library (and all of Tk and Tcl with it).
 */

extern int main();
int *tclDummyMainPtr = (int *) main;

int ReadHDFGeomData(ClientData clientData, Tcl_Interp *interp,
		    int argc, char *argv[]);

int ReadHDFMeshData(ClientData clientData, Tcl_Interp *interp,
		    int argc, char *argv[]);

int PlotGeometry(ClientData clientData, Tcl_Interp *interp,
		 int argc, char *argv[]);

int PlotMesh(ClientData clientData, Tcl_Interp *interp,
	     int argc, char *argv[]);

int PlotContours(ClientData clientData, Tcl_Interp *interp,
		 int argc, char *argv[]);

int PlotNodes(ClientData clientData, Tcl_Interp *interp,
	      int argc, char *argv[]);

int Plot1DContours(ClientData clientData, Tcl_Interp *interp,
		   int argc, char *argv[]);

int PlotSetUp(ClientData clientData, Tcl_Interp *interp,
	      int argc, char *argv[]);

int DrawGridLines (ClientData clientData, Tcl_Interp *interp,
		   int argc, char *argv[]);

int DrawGridValues (ClientData clientData, Tcl_Interp *interp,
		    int argc, char *argv[]);

int DrawRegionLegend(ClientData clientData, Tcl_Interp *interp,
		      int argc, char *argv[]);

int DrawContourLegend(ClientData clientData, Tcl_Interp *interp,
		      int argc, char *argv[]);

int SetRegionInfo(ClientData clientData, Tcl_Interp *interp,
		  int argc, char *argv[]);

int GetRegionNameNColor(ClientData clientData, Tcl_Interp *interp,
			int argc, char *argv[]);

int GetGroupContourInfo(ClientData clientData, Tcl_Interp *interp,
			int argc, char *argv[]);

int GetSingleContourInfo(ClientData clientData, Tcl_Interp *interp,
			 int argc, char *argv[]);

int PrintCanvas(ClientData clientData, Tcl_Interp *interp,
		int argc, char *argv[]);

int CreateContourNameLabel(ClientData clientData, Tcl_Interp *interp,
			   int argc, char *argv[]);

int CreateContourLabels(ClientData clientData, Tcl_Interp *interp,
			   int argc, char *argv[]);

int CreateContourNameEntry(ClientData clientData, Tcl_Interp *interp,
			   int argc, char *argv[]);

int CreateContourEntries(ClientData clientData, Tcl_Interp *interp,
			 int argc, char *argv[]);

int CreateNoofContoursSlider(ClientData clientData, 
			     Tcl_Interp *interp,
			     int argc, char *argv[]);

int CreateContourValueLabel(ClientData clientData, 
			    Tcl_Interp *interp,
			    int argc, char *argv[]);

int CreateContourValueEntry(ClientData clientData, 
			    Tcl_Interp *interp,
			    int argc, char *argv[]);

int CreateLogButtons(ClientData clientData, Tcl_Interp *interp,
		    int argc, char *argv[]);

int ContourParamChange(ClientData clientData, Tcl_Interp *interp,
		       int argc, char *argv[]);

int CreateContourListBox(ClientData clientData, Tcl_Interp *interp,
			 int argc, char *argv[]);

int UpdateContourList(ClientData clientData, Tcl_Interp *interp,
		       int argc, char *argv[]);

int UpdateContourMenus(ClientData clientData, Tcl_Interp *interp,
		       int argc, char *argv[]);

int ChangeDataSets(ClientData clientData, Tcl_Interp *interp,
		   int argc, char *argv[]);

int AddWindowItem(ClientData clientData, Tcl_Interp *interp,
		  int argc, char *argv[]);

int CreateInfoLabels(ClientData clientData, Tcl_Interp *interp,
		     int argc, char *argv[]);

int UpdateInfoLabels(ClientData clientData, Tcl_Interp *interp,
		     int argc, char *argv[]);

int Create1DCrossSection(ClientData clientData, Tcl_Interp *interp,
			 int argc, char *argv[]);

int ResetScale(ClientData clientData, Tcl_Interp *interp,
	       int argc, char *argv[]);

int PrintThis(ClientData clientData, Tcl_Interp *interp,
	      int argc, char *argv[]);

/*
 *----------------------------------------------------------------------
 *
 * Tcl_AppInit --
 *
 *	This procedure performs application-specific initialization.
 *	Most applications, especially those that incorporate additional
 *	packages, will have their own version of this procedure.
 *
 * Results:
 *	Returns a standard Tcl completion code, and leaves an error
 *	message in interp->result if an error occurs.
 *
 * Side effects:
 *	Depends on the startup script.
 *
 *----------------------------------------------------------------------
 */

int Tcl_AppInit(Tcl_Interp* interp)
{
  int i;
  Tk_Window main;
  
  main = Tk_MainWindow(interp);
  
  /*
   * Call the init procedures for included packages.  Each call should
   * look like this:
   *
   * if (Mod_Init(interp) == TCL_ERROR) {
   *     return TCL_ERROR;
   * }
   *
   * where "Mod" is the name of the module.
   */
  
  if (Tcl_Init(interp) == TCL_ERROR) {
    return TCL_ERROR;
  }
  if (Tk_Init(interp) == TCL_ERROR) {
    return TCL_ERROR;
  }
  
  /*
   * Call Tcl_CreateCommand for application-specific commands, if
   * they weren't already created by the init procedures called above.
   */

  Tcl_CreateCommand(interp, "C_ReadHDFGeomData", ReadHDFGeomData,
		    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);

  Tcl_CreateCommand(interp, "C_ReadHDFMeshData", ReadHDFMeshData,
		    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);

  Tcl_CreateCommand(interp, "C_PlotGeometry", PlotGeometry,
		    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
  
  Tcl_CreateCommand(interp, "C_PlotMesh", PlotMesh,
		    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);

  Tcl_CreateCommand(interp, "C_PlotContours", PlotContours,
		    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);

  Tcl_CreateCommand(interp, "C_PlotNodes", PlotNodes,
		    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);

  Tcl_CreateCommand(interp, "C_Plot1DContours", Plot1DContours,
		    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);

  Tcl_CreateCommand(interp, "C_PlotSetUp", PlotSetUp,
		    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);

  Tcl_CreateCommand(interp, "C_DrawGridLines", DrawGridLines,
		    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);

  Tcl_CreateCommand(interp, "C_DrawGridValues", DrawGridValues,
		    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);

  Tcl_CreateCommand(interp, "C_DrawRegionLegend", DrawRegionLegend,
		    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);

  Tcl_CreateCommand(interp, "C_DrawContourLegend", DrawContourLegend,
		    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);

  Tcl_CreateCommand(interp, "C_SetRegionInfo", SetRegionInfo,
		    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
  
  Tcl_CreateCommand(interp, "C_GetRegionNameNColor", GetRegionNameNColor,
		    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);

  Tcl_CreateCommand(interp, "C_GetGroupContourInfo", GetGroupContourInfo,
		    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);

  Tcl_CreateCommand(interp, "C_GetSingleContourInfo", GetSingleContourInfo,
		    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);

  Tcl_CreateCommand(interp, "C_PrintCanvas", PrintCanvas,
		    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);

  Tcl_CreateCommand(interp, "C_CreateContourNameLabel", 
		    CreateContourNameLabel,
		    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);

  Tcl_CreateCommand(interp, "C_CreateContourLabels", 
		    CreateContourLabels,
		    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);

  Tcl_CreateCommand(interp, "C_CreateContourNameEntry",
		    CreateContourNameEntry,
		    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);

  Tcl_CreateCommand(interp, "C_CreateContourEntries",
		    CreateContourEntries,
		    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);

  Tcl_CreateCommand(interp, "C_CreateNoofContoursSlider",
		    CreateNoofContoursSlider,
		    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);

  Tcl_CreateCommand(interp, "C_CreateContourValueLabel",
		    CreateContourValueLabel,
		    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
  
  Tcl_CreateCommand(interp, "C_CreateContourValueEntry",
		    CreateContourValueEntry,
		    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);

  Tcl_CreateCommand(interp, "C_CreateLogButtons", CreateLogButtons,
		    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);

  Tcl_CreateCommand(interp, "C_ContourParamChange", ContourParamChange,
		    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);

  Tcl_CreateCommand(interp, "C_CreateContourListBox", CreateContourListBox,
		    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
  
  Tcl_CreateCommand(interp, "C_UpdateContourList", UpdateContourList,
		    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);

  Tcl_CreateCommand(interp, "C_UpdateContourMenus", UpdateContourMenus,
		    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
  
  Tcl_CreateCommand(interp, "C_ChangeDataSets", ChangeDataSets,
		    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
  
  Tcl_CreateCommand(interp, "C_AddWindowItem", AddWindowItem,
		    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
  
  Tcl_CreateCommand(interp, "C_CreateInfoLabels", CreateInfoLabels,
		    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
  
  Tcl_CreateCommand(interp, "C_UpdateInfoLabels", UpdateInfoLabels,
		    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
  
  Tcl_CreateCommand(interp, "C_Create1DCrossSection", Create1DCrossSection,
		    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
  
  Tcl_CreateCommand(interp, "C_ResetScale", ResetScale,
		    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
  
  Tcl_CreateCommand(interp, "C_PrintThis", PrintThis,
		    (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
  
  /*
   * Data Initialization
   */

  current_gridsize_x = 0.5;
  current_gridsize_y = 0.5;

  for (i=0; i<MAX_NUM_DATA_SETS; i++)
    IPlotDataSets[i].SetCalcMinMax(1);

  Tcl_LinkVar(interp, "ipl_min_x",      (char *) &min_x,     TCL_LINK_DOUBLE);
  Tcl_LinkVar(interp, "ipl_min_y",      (char *) &min_y,     TCL_LINK_DOUBLE);
  Tcl_LinkVar(interp, "ipl_max_x",      (char *) &max_x,     TCL_LINK_DOUBLE);
  Tcl_LinkVar(interp, "ipl_max_y",      (char *) &max_y,     TCL_LINK_DOUBLE);
  Tcl_LinkVar(interp, "ipl_scale_x",    (char *) &x_scale,   TCL_LINK_DOUBLE);
  Tcl_LinkVar(interp, "ipl_scale_y",    (char *) &y_scale,   TCL_LINK_DOUBLE);
  Tcl_LinkVar(interp, "ipl_trans_x",    (char *) &x_trans,   TCL_LINK_INT);
  Tcl_LinkVar(interp, "ipl_trans_y",    (char *) &y_trans,   TCL_LINK_INT);

  Tcl_LinkVar(interp, "ipl_gridsize_x", (char *) &current_gridsize_x,
	      TCL_LINK_DOUBLE);
  Tcl_LinkVar(interp, "ipl_gridsize_y", (char *) &current_gridsize_y,
	      TCL_LINK_DOUBLE);

  Tcl_LinkVar(interp, "ipl_toplabel",   (char *) &current_title,
	      TCL_LINK_STRING);

  Tcl_LinkVar(interp, "ipl_bottomlabel",   (char *) &current_xaxis,
	      TCL_LINK_STRING);

  Tcl_LinkVar(interp, "ipl_leftlabel",   (char *) &current_yaxis,
	      TCL_LINK_STRING);

  Tcl_LinkVar(interp, "ipl_plotgeom",    (char *) &plot_geom,    TCL_LINK_INT);
  Tcl_LinkVar(interp, "ipl_plotmesh",    (char *) &plot_mesh,    TCL_LINK_INT);
  Tcl_LinkVar(interp, "ipl_plotcontour", (char *) &plot_contour, TCL_LINK_INT);
  Tcl_LinkVar(interp, "ipl_plotnode",    (char *) &plot_node,    TCL_LINK_INT);
  Tcl_LinkVar(interp, "ipl_plot1d",      (char *) &plot_1d,      TCL_LINK_INT);

  Tcl_LinkVar(interp, "ipl_noofregions",(char *) &current_noof_regions,
	      TCL_LINK_INT);
  Tcl_LinkVar(interp, "ipl_colormode",  (char *) &color_mode,TCL_LINK_INT);
  Tcl_LinkVar(interp, "ipl_noofdatasets", (char *) &noof_data_sets, TCL_LINK_INT);
  Tcl_LinkVar(interp, "ipl_dimension", (char *) &current_dimen, TCL_LINK_INT);
  Tcl_LinkVar(interp, "ipl_scaletype", (char *) &scale_type, TCL_LINK_INT);

  /*
   * Specify a user-specific startup file to invoke if the application
   * is run interactively.  Typically the startup file is "~/.apprc"
   * where "app" is the name of the application.  If this line is deleted
   * then no user-specific startup file will be run under any conditions.
   */
  
  tcl_RcFileName = "~/.wishrc";
  return TCL_OK;
}
