/*--------------------------------------------------------------------
 *    The MB-system:	mbeditviz_callbacks.c		4/27/2007
 *    $Id: mbeditviz_callbacks.c 1829 2010-02-05 02:53:39Z caress $
 *
 *    Copyright (c) 2007-2009 by
 *    David W. Caress (caress@mbari.org)
 *      Monterey Bay Aquarium Research Institute
 *      Moss Landing, CA 95039
 *    and Dale N. Chayes (dale@ldeo.columbia.edu)
 *      Lamont-Doherty Earth Observatory
 *      Palisades, NY 10964
 *
 *    See README file for copying and redistribution conditions.
 *--------------------------------------------------------------------*/
/*
 *
 * MBeditviz is an interactive swath bathymetry editor and patch
 * test tool for  MB-System.
 * It can work with any data format supported by the MBIO library.
 * This include file contains global control parameters shared with
 * the Motif interface code.
 *
 * Author:	D. W. Caress
 * Date:	April 27, 2007
 *
 * $Log: mbeditviz_callbacks.c,v $
 * Revision 5.9  2008/11/16 21:51:18  caress
 * Updating all recent changes, including time lag analysis using mbeditviz and improvements to the mbgrid footprint gridding algorithm.
 *
 * Revision 5.8  2008/05/16 22:59:42  caress
 * Release 5.1.1beta18.
 *
 * Revision 5.7  2008/03/14 19:04:32  caress
 * Fixed memory problems with route editing.
 *
 * Revision 5.6  2007/11/16 17:26:56  caress
 * Progress on MBeditviz
 *
 * Revision 5.5  2007/10/17 20:35:05  caress
 * Release 5.1.1beta11
 *
 * Revision 5.4  2007/10/08 16:32:08  caress
 * Code status as of 8 October 2007.
 *
 * Revision 5.3  2007/07/03 17:35:54  caress
 * Working on MBeditviz.
 *
 * Revision 5.2  2007/06/18 01:16:51  caress
 * Added MBeditviz.
 *
 *
 */

/**
 * README: This file is appended to at file generation time.
 * Edits can be made throughout the file
 */
/*
 * Generated by the ICS Builder Xcessory (BX).
 *
 * BuilderXcessory Version 6.1.3
 * Code Generator Xcessory 6.1.3 (08/19/04) CGX Scripts 6.1 Motif 2.1 
 *
 */
#include <Xm/Xm.h>


/*
 * Standard includes for builtins.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

/* MBIO include files */
#include "../../include/mb_status.h"
#include "../../include/mb_define.h"
#include "../../include/mb_format.h"
#include "../../include/mbsys_singlebeam.h"

/* GMT include files */
#include "gmt.h"

/* get NaN detector */
#if defined(isnanf)
#define check_fnan(x) isnanf((x))
#elif defined(isnan)
#define check_fnan(x) isnan((double)(x))
#elif HAVE_ISNANF == 1
#define check_fnan(x) isnanf(x)
extern int isnanf(float x);
#elif HAVE_ISNAN == 1
#define check_fnan(x) isnan((double)(x))
#elif HAVE_ISNAND == 1
#define check_fnan(x) isnand((double)(x))
#else
#define check_fnan(x) ((x) != (x))
#endif

/* mbview include file */
#include "mbview.h"
#include "mbeditviz.h"

/* fileSelectionBox modes */
#define MBEDITVIZ_OPENSWATH		1

/* Projection defines */
#define ModelTypeProjected	     1
#define ModelTypeGeographic	     2
#define GCS_WGS_84		  4326

/* widgets */
static XtAppContext	app;
static Widget	parent;
static int	mbview_id[MBV_MAX_WINDOWS];
static Widget	fileSelectionList;
static Widget	fileSelectionText;
int		mformat;
static Cardinal ac;
static Arg      args[256];
static char	value_text[MB_PATH_MAXLINE];

/* function prototypes */
void do_mbeditviz_update_gui();
void do_mbeditviz_fileSelectionBox( Widget w, XtPointer client_data, XtPointer call_data);
void do_mbeditviz_fileSelectionBox_openswath( Widget w, XtPointer client_data, XtPointer call_data);
void do_mbeditviz_fileselection_list( Widget w, XtPointer client_data, XtPointer call_data);
void get_text_string(Widget w, String str);
void set_label_multiline_string(Widget w, String str);
int do_mbeditviz_mbview_dismiss_notify(size_t instance);
void do_mbeditviz_viewgrid();
void do_mbeditviz_pickonepoint_notify(size_t instance);
void do_mbeditviz_picktwopoint_notify(size_t instance);
void do_mbeditviz_pickarea_notify(size_t instance);
void do_mbeditviz_pickregion_notify(size_t instance);
void do_mbeditviz_picksite_notify(size_t instance);
void do_mbeditviz_pickroute_notify(size_t instance);
void do_mbeditviz_picknav_notify(size_t instance);
void do_mbeditviz_regrid_notify( Widget w, XtPointer client_data, XtPointer call_data);
void do_mbeditviz_gridparameters( Widget w, XtPointer client_data, XtPointer call_data);
void do_mbeditviz_changecellsize( Widget w, XtPointer client_data, XtPointer call_data);
void do_mbeditviz_updategrid( Widget w, XtPointer client_data, XtPointer call_data);
	
XtPointer BX_CONVERT(Widget, char *, char *, int, Boolean *);

/*
 * Motif required Headers
 */
#include <X11/StringDefs.h>
#include <Xm/Xm.h>
#include <Xm/DialogS.h>
#include <Xm/Text.h>
#include <Xm/FileSB.h>
#include <Xm/ToggleB.h>
#include <Xm/List.h>
#if (XmVersion >= 1002)
#include <Xm/RepType.h>
#endif
#include <Xm/MwmUtil.h>
#include "mbeditviz_creation.h"

/*
 * Macros to make code look nicer between ANSI and K&R.
 */
#ifndef ARGLIST
#if (NeedFunctionPrototypes == 0)
#define PROTOTYPE(p)	()
#define ARGLIST(p)	p
#define ARG(a, b)	a b;
#define GRA(a, b)	a b;
#define UARG(a, b)      a b;
#define GRAU(a, b)      a b;
#else
#define PROTOTYPE(p)	p
#define ARGLIST(p)	(
#define ARG(a, b)	a b,
#define GRA(a, b)	a b)
#ifdef __cplusplus
#define UARG(a, b)      a,
#define GRAU(a, b)      a)
#else
#define UARG(a, b)      a b,
#define GRAU(a, b)      a b)
#endif
#endif
#endif

Widget		BxFindTopShell PROTOTYPE((Widget));
WidgetList	BxWidgetIdsFromNames PROTOTYPE((Widget, char*, char*));


/*---------------------------------------------------------------------------------------*/
/*      Function Name: 	BxManageCB
 *
 *      Description:   	Given a string of the form:
 *		       	"(WL)[widgetName, widgetName, ...]"
 *			BxManageCB attempts to convert the name to a Widget
 *			ID and manage the widget.
 *
 *      Arguments:     	Widget	    w:      the widget activating the callback.
 *		       	XtPointer   client: the list of widget names to attempt
 *					    to find and manage.
 *		       	XtPointer   call:   the call data (unused).
 *
 *      Notes:        *	This function expects that there is an application
 *		       	shell from which all other widgets are descended.
 */

/* ARGSUSED */
void
BxManageCB ARGLIST((w, client, call))
ARG( Widget, w)
ARG( XtPointer, client)
GRAU( XtPointer, call)
{
    WidgetList		widgets;
    int			i;

    /*
     * This function returns a NULL terminated WidgetList.  The memory for
     * the list needs to be freed when it is no longer needed.
     */
    widgets = BxWidgetIdsFromNames(w, "BxManageCB", (String)client);

    i = 0;
    while( widgets && widgets[i] != NULL )
    {
	XtManageChild(widgets[i]);
	i++;
    }
    XtFree((char *)widgets);
}

/*---------------------------------------------------------------------------------------*/
/*      Function Name: 	BxUnmanageCB
 *
 *      Description:   	Given a string of the form:
 *		       	"(WL)[widgetName, widgetName, ...]"
 *			BxUnmanageCB attempts to convert the name to a Widget
 *			ID and unmanage the widget.
 *
 *      Arguments:     	Widget	    w:      the widget activating the callback.
 *		       	XtPointer   client: the list of widget names to attempt
 *					    to find and unmanage.
 *		       	XtPointer   call:   the call data (unused).
 *
 *      Notes:        *	This function expects that there is an application
 *		       	shell from which all other widgets are descended.
 */

/* ARGSUSED */
void
BxUnmanageCB ARGLIST((w, client, call))
ARG( Widget, w)
ARG( XtPointer, client)
GRAU( XtPointer, call)
{
    WidgetList		widgets;
    int			i;

    /*
     * This function returns a NULL terminated WidgetList.  The memory for
     * the list needs to be freed when it is no longer needed.
     */
    widgets = BxWidgetIdsFromNames(w, "BxUnmanageCB", (String)client);

    i = 0;
    while( widgets && widgets[i] != NULL )
    {
	XtUnmanageChild(widgets[i]);
	i++;
    }
    XtFree((char *)widgets);
}

/*---------------------------------------------------------------------------------------*/
/*      Function Name:	BxExitCB
 *
 *      Description:   	This functions expects an integer to be passed in
 *		       	client data.  It calls the exit() system call with
 *			the integer value as the argument to the function.
 *
 *      Arguments:      Widget		w: 	the activating widget.
 *			XtPointer	client:	the integer exit value.
 *			XtPointer	call:	the call data (unused).
 */

#ifdef VMS
#include <stdlib.h>
#endif

/* ARGSUSED */
void
BxExitCB ARGLIST((w, client, call))
UARG( Widget, w)
ARG( XtPointer, client)
GRAU( XtPointer, call)
{
    long	exitValue = EXIT_FAILURE;
    exit(exitValue);
}

/*---------------------------------------------------------------------------------------*/

int do_mbeditviz_init(Widget parentwidget, XtAppContext appcon)
{
	char function_name[] = "do_mbeditviz_init";
	int	i;

	/* print input debug statements */
	if (mbev_verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",function_name);
		fprintf(stderr,"dbg2  Input arguments:\n");
		fprintf(stderr,"dbg2       mbev_verbose:   %d\n",mbev_verbose);
		fprintf(stderr,"dbg2       parentwidget:   %lu\n",(size_t)parentwidget);
		fprintf(stderr,"dbg2       appcon:         %lu\n",(size_t)appcon);
		}

	parent = parentwidget;
	app = appcon;
	mbev_message_on = MB_NO;
    
if (mbev_verbose > 0)
fprintf(stderr,"do_mbeditviz_init\n");
			
	/* set about version label */
	sprintf(value_text, "::#TimesMedium14:t\"MB-System Release %s\"#TimesMedium14\"%s\"", 
		MB_VERSION, MB_BUILD_DATE);
	set_mbview_label_multiline_string(label_about_version, value_text);
   
	/* set file selection widgets */
	fileSelectionList = (Widget) 
	    XmFileSelectionBoxGetChild(fileSelectionBox, 
					XmDIALOG_LIST);
	fileSelectionText = (Widget) 
	    XmFileSelectionBoxGetChild(fileSelectionBox, 
					XmDIALOG_TEXT);
	XtAddCallback(fileSelectionList, 
		XmNbrowseSelectionCallback, 
		do_mbeditviz_fileselection_list, NULL);
	XtUnmanageChild(
		(Widget) XmFileSelectionBoxGetChild(
					fileSelectionBox, 
					XmDIALOG_HELP_BUTTON));		
	mformat = -1;
	sprintf(value_text,"%d",mformat);
	XmTextSetString(
	    text_format, 
	    value_text);

	/* set the output mode */
	if (mbev_mode_output == MBEV_OUTPUT_MODE_EDIT)
	    {
	    XmToggleButtonSetState(toggleButton_mode_edit, 
			TRUE, FALSE);
	    XmToggleButtonSetState(toggleButton_mode_browse,  
			FALSE, FALSE);
	    XmToggleButtonSetState(toggleButton_openmodeedit, 
			TRUE, FALSE);
	    XmToggleButtonSetState(toggleButton_openmodebrowse, 
			FALSE, FALSE);
	    }
	else
	    {
	    XmToggleButtonSetState(toggleButton_mode_edit, 
			FALSE, FALSE);
	    XmToggleButtonSetState(toggleButton_mode_browse,  
			TRUE, FALSE);
	    XmToggleButtonSetState(toggleButton_openmodeedit, 
			FALSE, FALSE);
	    XmToggleButtonSetState(toggleButton_openmodebrowse, 
			TRUE, FALSE);
	    }

	/* initialize mbview_id list */
	for (i=0;i<MBV_MAX_WINDOWS;i++)
	    {
	    mbview_id[i] = MB_NO;
	    }
	    
	/* set sensitivity of widgets that require an mbview instance to be active */
	do_mbeditviz_update_gui( );
	
if (mbev_verbose > 0)
fprintf(stderr,"return do_mbeditviz_init status:%d\n", mbev_status);

	return(0);
}
/*---------------------------------------------------------------------------------------*/

void
do_mbeditviz_mode_change( Widget w, XtPointer client_data, XtPointer call_data)
{
	char function_name[] = "do_mbeditviz_mode_change";
	XmToggleButtonCallbackStruct *acs;
	acs = (XmToggleButtonCallbackStruct*)call_data;

	/* print input debug statements */
	if (mbev_verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",function_name);
		fprintf(stderr,"dbg2  Input arguments:\n");
		fprintf(stderr,"dbg2       w:           %lu\n",(size_t)w);
		fprintf(stderr,"dbg2       client_data: %lu\n",(size_t)client_data);
		fprintf(stderr,"dbg2       call_data:   %lu\n",(size_t)call_data);
		}

if (mbev_verbose > 0)
fprintf(stderr,"do_mbeditviz_mode_change\n");

    /* set values if needed */
    if (acs->reason == XmCR_VALUE_CHANGED)
	    {
	    if (acs->set) 
	    	mbev_mode_output = MBEV_OUTPUT_MODE_EDIT;
	    else 
	    	mbev_mode_output = MBEV_OUTPUT_MODE_BROWSE;
	    if (mbev_mode_output == MBEV_OUTPUT_MODE_EDIT)
		    {
		    XmToggleButtonSetState(toggleButton_mode_edit, 
			     TRUE, FALSE);
		    XmToggleButtonSetState(toggleButton_mode_browse,  
			     FALSE, FALSE);
		    XmToggleButtonSetState(toggleButton_openmodeedit, 
			     TRUE, FALSE);
		    XmToggleButtonSetState(toggleButton_openmodebrowse, 
			     FALSE, FALSE);
		    }
	    else
		    {
		    XmToggleButtonSetState(toggleButton_mode_edit, 
			     FALSE, FALSE);
		    XmToggleButtonSetState(toggleButton_mode_browse,  
			     TRUE, FALSE);
		    XmToggleButtonSetState(toggleButton_openmodeedit, 
			     FALSE, FALSE);
		    XmToggleButtonSetState(toggleButton_openmodebrowse, 
			     TRUE, FALSE);
		    }
if (mbev_verbose > 0)
fprintf(stderr,"acs->set:%d mode:%d\n",acs->set,mbev_mode_output);
	    }

if (mbev_verbose > 0)
fprintf(stderr,"return do_mbeditviz_mode_change status:%d\n", mbev_status);

}

/*---------------------------------------------------------------------------------------*/
void
do_mbeditviz_openfile( Widget w, XtPointer client_data, XtPointer call_data)
{
	char function_name[] = "do_mbeditviz_openfile";
	XmFileSelectionBoxCallbackStruct *acs;
	acs = (XmFileSelectionBoxCallbackStruct*)call_data;
	char    *file_ptr;
	int	format;

	/* print input debug statements */
	if (mbev_verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",function_name);
		fprintf(stderr,"dbg2  Input arguments:\n");
		fprintf(stderr,"dbg2       w:           %lu\n",(size_t)w);
		fprintf(stderr,"dbg2       client_data: %lu\n",(size_t)client_data);
		fprintf(stderr,"dbg2       call_data:   %lu\n",(size_t)call_data);
		}
    
if (mbev_verbose > 0)
if (mbev_verbose > 0)
fprintf(stderr,"do_mbeditviz_openfile\n");
	/* read the mbio format number */
	get_text_string(text_format, value_text);
	sscanf(value_text, "%d", &format);

	/* read the input file name */
	XmStringGetLtoR(acs->value, XmSTRING_DEFAULT_CHARSET, 
			&file_ptr);
	if (strlen(file_ptr) <= 0 && file_ptr != NULL)
		{
		XtFree(file_ptr);
		file_ptr = NULL;
		}
	
	/* open data */
	mbev_status = do_mbeditviz_opendata(file_ptr, format);
	
if (mbev_verbose > 0)
fprintf(stderr,"return do_mbeditviz_openfile status:%d\n", mbev_status);
}

/*---------------------------------------------------------------------------------------*/
void
do_mbeditviz_fileselection_list( Widget w, XtPointer client_data, XtPointer call_data)
{
	char function_name[] = "do_mbeditviz_fileselection_list";
	XmAnyCallbackStruct *acs;
	acs = (XmAnyCallbackStruct*)call_data;

	/* print input debug statements */
	if (mbev_verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",function_name);
		fprintf(stderr,"dbg2  Input arguments:\n");
		fprintf(stderr,"dbg2       w:           %lu\n",(size_t)w);
		fprintf(stderr,"dbg2       client_data: %lu\n",(size_t)client_data);
		fprintf(stderr,"dbg2       call_data:   %lu\n",(size_t)call_data);
		}
    
if (mbev_verbose > 0)
fprintf(stderr,"do_mbeditviz_fileselection_list\n");

    static char selection_text[128];
    int	form;
    char value_text[10];

    /* get selected text */
    get_text_string(fileSelectionText, (String) selection_text);

    /* get output file */
    if((int)strlen(selection_text) > 0)
	    {
	    /* look for MB suffix convention */
	    form = mformat;
	    if ((mbev_status = mbeditviz_get_format(selection_text, 
			    &form)) == MB_SUCCESS)
		{
		mformat = form;
		sprintf(value_text,"%d",mformat);
		XmTextSetString(
		    text_format, 
		    value_text);
		}
	    }
if (mbev_verbose > 0)
fprintf(stderr,"return do_mbeditviz_fileselection_list status:%d\n", mbev_status);
}
/*---------------------------------------------------------------------------------------*/
void
do_mbeditviz_fileSelectionBox_openswath( Widget w, XtPointer client_data, XtPointer call_data)
{
	char function_name[] = "do_mbeditviz_fileSelectionBox_openswath";
	XmAnyCallbackStruct *acs;
	acs = (XmAnyCallbackStruct*)call_data;

	/* print input debug statements */
	if (mbev_verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",function_name);
		fprintf(stderr,"dbg2  Input arguments:\n");
		fprintf(stderr,"dbg2       w:           %lu\n",(size_t)w);
		fprintf(stderr,"dbg2       client_data: %lu\n",(size_t)client_data);
		fprintf(stderr,"dbg2       call_data:   %lu\n",(size_t)call_data);
		}
    
if (mbev_verbose > 0)
fprintf(stderr,"do_mbeditviz_fileSelectionBox_openswath\n");
        XmString	tmp0;
	Boolean	argok;
 	
	/* set title to open swath data */
	ac = 0;
	XtSetArg(args[ac], XmNtitle, "Open Swath Data"); ac++;
	XtSetValues(dialogShell_open, args, ac);
	BxManageCB(w, (XtPointer)"fileSelectionBox", call_data);

	
	/* set filter */
	ac = 0;
	tmp0 = (XmString) BX_CONVERT(dialogShell_open, "*.mb*", 
                				XmRXmString, 0, &argok);
        XtSetArg(args[ac], XmNpattern, tmp0); ac++;
	XtSetValues(fileSelectionBox, args, ac);
        XmStringFree((XmString)tmp0);
if (mbev_verbose > 0)
fprintf(stderr,"return do_mbeditviz_fileSelectionBox_openswath status:%d\n", mbev_status);
    
}

/*---------------------------------------------------------------------------------------*/
void
do_mbeditviz_quit( Widget w, XtPointer client_data, XtPointer call_data)
{
	char function_name[] = "do_mbeditviz_quit";
	XmAnyCallbackStruct *acs;
	acs = (XmAnyCallbackStruct*)call_data;

	/* print input debug statements */
	if (mbev_verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",function_name);
		fprintf(stderr,"dbg2  Input arguments:\n");
		fprintf(stderr,"dbg2       w:           %lu\n",(size_t)w);
		fprintf(stderr,"dbg2       client_data: %lu\n",(size_t)client_data);
		fprintf(stderr,"dbg2       call_data:   %lu\n",(size_t)call_data);
		}
    
if (mbev_verbose > 0)
fprintf(stderr,"do_mbeditviz_quit\n");
	
	/* destroy any mbview window */
	if (mbev_grid.status == MBEV_GRID_VIEWED)
		{
		/* destroy any mb3dsoundings window */
		mbev_status = mb3dsoundings_end(mbev_verbose, &mbev_error);
		mbeditviz_mb3dsoundings_dismiss();

		mbev_status = mbview_destroy(mbev_verbose, 0, MB_YES, &mbev_error);
		mbev_grid.status = MBEV_GRID_NOTVIEWED;
		}
	
	/* destroy the grid */
	if (mbev_grid.status != MBEV_GRID_NONE)
		mbeditviz_destroy_grid();
	
	/* reset the gui */
	do_mbeditviz_update_gui();

if (mbev_verbose > 0)
fprintf(stderr,"return do_mbeditviz_quit status:%d\n", mbev_status);
    
}

/*---------------------------------------------------------------------------------------*/
void
do_mbeditviz_viewall( Widget w, XtPointer client_data, XtPointer call_data)
{
	char function_name[] = "do_mbeditviz_viewall";
	XmAnyCallbackStruct *acs;
	acs = (XmAnyCallbackStruct*)call_data;
	struct mbev_file_struct *file;
	int	ifile;
	int	loadcount;

	/* print input debug statements */
	if (mbev_verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",function_name);
		fprintf(stderr,"dbg2  Input arguments:\n");
		fprintf(stderr,"dbg2       w:           %lu\n",(size_t)w);
		fprintf(stderr,"dbg2       client_data: %lu\n",(size_t)client_data);
		fprintf(stderr,"dbg2       call_data:   %lu\n",(size_t)call_data);
		}
    
if (mbev_verbose > 0)
fprintf(stderr,"do_mbeditviz_viewall\n");
	
	/* destroy any mbview window */
	if (mbev_grid.status == MBEV_GRID_VIEWED)
		{
		/* destroy any mb3dsoundings window */
		mbev_status = mb3dsoundings_end(mbev_verbose, &mbev_error);
		mbeditviz_mb3dsoundings_dismiss();

		mbev_status = mbview_destroy(mbev_verbose, 0, MB_YES, &mbev_error);
		mbev_grid.status = MBEV_GRID_NOTVIEWED;
if (mbev_verbose > 0)
fprintf(stderr,"do_mbeditviz_viewall destroyed previous windows\n");
		}

	/* destroy old grid */
	if (mbev_grid.status != MBEV_GRID_NONE)
		{
		mbeditviz_destroy_grid();
if (mbev_verbose > 0)
fprintf(stderr,"do_mbeditviz_viewall destroyed old grid\n");
		}
	
	/* loop over all files to be sure all files are loaded */
if (mbev_verbose > 0)
fprintf(stderr,"do_mbeditviz_viewall loading files...\n");
	do_mbeditviz_message_on("Loading files...");
	loadcount = 0;
	for (ifile=0;ifile<mbev_num_files;ifile++)
		{
if (mbev_verbose > 0)
fprintf(stderr,"do_mbeditviz_viewall loading file %d\n",ifile);
		file = &(mbev_files[ifile]);
		if (file->load_status == MB_NO)
			{
			sprintf(value_text, "Loading file %d of %d...", ifile+1, mbev_num_files);
			do_mbeditviz_message_on(value_text);
if (mbev_verbose > 0)
fprintf(stderr,"do_mbeditviz_viewall loading file %d of %d...\n", ifile+1, mbev_num_files);
			mbeditviz_load_file(ifile);
			}
		loadcount++;
if (mbev_verbose > 0)
fprintf(stderr,"do_mbeditviz_viewall mbev_status:%d loadcount:%d\n",mbev_status,loadcount);
		}
	do_mbeditviz_message_off();
		
	/* put up dialog on grid parameters */
if (mbev_verbose > 0)
fprintf(stderr,"do_mbeditviz_viewall mbev_status:%d loadcount:%d\n",mbev_status,loadcount);
	if (mbev_status == MB_SUCCESS && loadcount > 0)
		{
if (mbev_verbose > 0)
fprintf(stderr,"do_mbeditviz_viewall calling do_mbeditviz_gridparameters\n");
		do_mbeditviz_gridparameters(w, client_data, call_data);
		}
	else
		{
		XBell(XtDisplay(list_filelist),100);
		}
	
	/* reset the gui */
if (mbev_verbose > 0)
fprintf(stderr,"do_mbeditviz_viewall calling do_mbeditviz_update_gui\n");
	do_mbeditviz_update_gui();
	
if (mbev_verbose > 0)
fprintf(stderr,"return do_mbeditviz_viewall status:%d\n", mbev_status);
}

/*---------------------------------------------------------------------------------------*/
void
do_mbeditviz_viewselected( Widget w, XtPointer client_data, XtPointer call_data)
{
	char function_name[] = "do_mbeditviz_viewselected";
	XmAnyCallbackStruct *acs;
	acs = (XmAnyCallbackStruct*)call_data;
	struct mbev_file_struct *file;
	int	*position_list = NULL;
	int position_count = 0;
	int	selected;
	int	ifile;
	int	loadcount;
	int	i;

	/* print input debug statements */
	if (mbev_verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",function_name);
		fprintf(stderr,"dbg2  Input arguments:\n");
		fprintf(stderr,"dbg2       w:           %lu\n",(size_t)w);
		fprintf(stderr,"dbg2       client_data: %lu\n",(size_t)client_data);
		fprintf(stderr,"dbg2       call_data:   %lu\n",(size_t)call_data);
		}
    
if (mbev_verbose > 0)
fprintf(stderr,"do_mbeditviz_viewselected\n");
	
	/* destroy any mbview window */
	if (mbev_grid.status == MBEV_GRID_VIEWED)
		{
		/* destroy any mb3dsoundings window */
		mbev_status = mb3dsoundings_end(mbev_verbose, &mbev_error);
		mbeditviz_mb3dsoundings_dismiss();

		mbev_status = mbview_destroy(mbev_verbose, 0, MB_YES, &mbev_error);
		mbev_grid.status = MBEV_GRID_NOTVIEWED;
		}

	/* destroy old grid */
	if (mbev_grid.status != MBEV_GRID_NONE)
		mbeditviz_destroy_grid();

	/* get positions of selected list items */
	ac = 0;
	XtSetArg(args[ac], XmNselectedPositionCount, (XtPointer) &position_count); ac++;
	XtSetArg(args[ac], XmNselectedPositions, (XtPointer) &position_list); ac++;
	XtGetValues(list_filelist, args, ac);
if (mbev_verbose > 0)
{
fprintf(stderr,"position_count:%d\n",position_count);
for(i=0;i<position_count;i++)
fprintf(stderr,"  %d %d\n",i,position_list[i]);
}
	
	/* loop over all files to be sure selected files are loaded */
	do_mbeditviz_message_on("Loading files...");
	loadcount = 0;
	for (ifile=0;ifile<mbev_num_files;ifile++)
		{
		/* find out if file is in selected list */
		selected = MB_NO;
		for (i=0;i<position_count;i++)
			{
			if (ifile == position_list[i] - 1)
				selected = MB_YES;
			}
			
		/* load unloaded selected files, unload loaded unselected files */
		file = &(mbev_files[ifile]);
		if (selected == MB_YES && file->load_status == MB_NO)
			{
			loadcount++;
			sprintf(value_text, "Loading file %d of %d...", loadcount, position_count);
			do_mbeditviz_message_on(value_text);
			mbeditviz_load_file(ifile);
			}
		else if (selected == MB_YES && file->load_status == MB_YES)
			{
			loadcount++;
			}
		else if (selected == MB_NO && file->load_status == MB_YES)
			{
			mbeditviz_unload_file(ifile);
			}
		}
	do_mbeditviz_message_off();
		
	/* put up dialog on grid parameters */
if (mbev_verbose > 0)
fprintf(stderr," mbev_status:%d loadcount:%d\n",mbev_status,loadcount);
	if (mbev_status == MB_SUCCESS && loadcount > 0)
		{
		do_mbeditviz_gridparameters(w, client_data, call_data);
		}
	else
		{
		XBell(XtDisplay(list_filelist),100);
		}
	
	/* reset the gui */
	do_mbeditviz_update_gui();
	
	/* reset status */
	mbev_status = MB_SUCCESS;
	mbev_error = MB_ERROR_NO_ERROR;
	
if (mbev_verbose > 0)
fprintf(stderr,"return do_mbeditviz_viewselected status:%d\n", mbev_status);
}
/*---------------------------------------------------------------------------------------*/
void
do_mbeditviz_regrid( Widget w, XtPointer client_data, XtPointer call_data)
{
	char function_name[] = "do_mbeditviz_regrid";
	XmAnyCallbackStruct *acs;
	acs = (XmAnyCallbackStruct*)call_data;
	struct mbev_file_struct *file;
	int	ifile;
	int	loadcount;

	/* print input debug statements */
	if (mbev_verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",function_name);
		fprintf(stderr,"dbg2  Input arguments:\n");
		fprintf(stderr,"dbg2       w:           %lu\n",(size_t)w);
		fprintf(stderr,"dbg2       client_data: %lu\n",(size_t)client_data);
		fprintf(stderr,"dbg2       call_data:   %lu\n",(size_t)call_data);
		}
    
if (mbev_verbose > 0)
fprintf(stderr,"do_mbeditviz_regrid\n");
	
	/* destroy any mbview window */
	if (mbev_grid.status == MBEV_GRID_VIEWED)
		{
		/* destroy any mb3dsoundings window */
		mbev_status = mb3dsoundings_end(mbev_verbose, &mbev_error);
		mbeditviz_mb3dsoundings_dismiss();

		mbev_status = mbview_destroy(mbev_verbose, 0, MB_YES, &mbev_error);
		mbev_grid.status = MBEV_GRID_NOTVIEWED;
		}

	/* destroy old grid */
	if (mbev_grid.status != MBEV_GRID_NONE)
		mbeditviz_destroy_grid();
	
	/* loop over all files to be count loaded files */
	loadcount = 0;
	for (ifile=0;ifile<mbev_num_files;ifile++)
		{
		file = &(mbev_files[ifile]);
		if (file->load_status == MB_YES)
			loadcount++;
		}
		
	/* put up dialog on grid parameters */
if (mbev_verbose > 0)
fprintf(stderr," mbev_status:%d loadcount:%d\n",mbev_status,loadcount);
	if (mbev_status == MB_SUCCESS && loadcount > 0)
		{
		do_mbeditviz_gridparameters(w, client_data, call_data);
		}
	else
		{
		XBell(XtDisplay(list_filelist),100);
		}
	
	/* reset the gui */
	do_mbeditviz_update_gui();
	
	/* reset status */
	mbev_status = MB_SUCCESS;
	mbev_error = MB_ERROR_NO_ERROR;
	
if (mbev_verbose > 0)
fprintf(stderr,"return do_mbeditviz_regrid status:%d\n", mbev_status);
}
/*---------------------------------------------------------------------------------------*/

void
do_mbeditviz_updategrid( Widget w, XtPointer client_data, XtPointer call_data)
{
	char function_name[] = "do_mbeditviz_updategrid";
	XmAnyCallbackStruct *acs;
	acs = (XmAnyCallbackStruct*)call_data;
	struct mbev_file_struct *file;
	int	loadcount;
	int	ifile;

	/* print input debug statements */
	if (mbev_verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",function_name);
		fprintf(stderr,"dbg2  Input arguments:\n");
		fprintf(stderr,"dbg2       w:           %lu\n",(size_t)w);
		fprintf(stderr,"dbg2       client_data: %lu\n",(size_t)client_data);
		fprintf(stderr,"dbg2       call_data:   %lu\n",(size_t)call_data);
		}

if (mbev_verbose > 0)
fprintf(stderr,"do_mbeditviz_updategrid\n");
	do_mbeditviz_mbview_dismiss_notify(0);
	
	/* loop over all files to be sure all files are loaded */
	loadcount = 0;
	for (ifile=0;ifile<mbev_num_files;ifile++)
		{
		file = &(mbev_files[ifile]);
		if (file->load_status == MB_YES)
			loadcount++;
		}
		
	/* make the grid and display grid */
	if (mbev_status == MB_SUCCESS && loadcount > 0)
		{
		/* make the grid */
		do_mbeditviz_message_on("Making grid...");
		mbev_status = mbeditviz_setup_grid();	
		mbeditviz_project_soundings();
		mbev_status = mbeditviz_make_grid();	
	
		/* display grid */
		do_mbeditviz_viewgrid();
		
		do_mbeditviz_message_off();
		}
	else
		{
		do_mbeditviz_message_off();
		XBell(XtDisplay(list_filelist),100);
		}
	
	/* reset the gui */
	do_mbeditviz_update_gui();
	
	/* reset status */
	mbev_status = MB_SUCCESS;
	mbev_error = MB_ERROR_NO_ERROR;
	
if (mbev_verbose > 0)
fprintf(stderr,"return do_mbeditviz_updategrid status:%d\n", mbev_status);

}

/*--------------------------------------------------------------------*/
void
do_mbeditviz_changecellsize( Widget w, XtPointer client_data, XtPointer call_data)
{
	char function_name[] = "do_mbeditviz_changecellsize";
	XmAnyCallbackStruct *acs;
	acs = (XmAnyCallbackStruct*)call_data;
	char	string[MB_PATH_MAXLINE];
	int	icellsize;

	/* print input debug statements */
	if (mbev_verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",function_name);
		fprintf(stderr,"dbg2  Input arguments:\n");
		fprintf(stderr,"dbg2       w:           %lu\n",(size_t)w);
		fprintf(stderr,"dbg2       client_data: %lu\n",(size_t)client_data);
		fprintf(stderr,"dbg2       call_data:   %lu\n",(size_t)call_data);
		}

if (mbev_verbose > 0)
fprintf(stderr,"do_mbeditviz_changecellsize\n");
    
    	/* get cell size value */
	ac = 0;
        XtSetArg(args[ac], XmNvalue, &icellsize); ac++;
	XtGetValues(scale_cellsize, args, ac);
	mbev_grid_cellsize = 0.1 * icellsize;
		
	/* get updated grid dimensions */
	mbev_grid_nx = (mbev_grid_boundsutm[1] - mbev_grid_boundsutm[0]) / mbev_grid_cellsize + 1;
	mbev_grid_ny = (mbev_grid_boundsutm[3] - mbev_grid_boundsutm[2]) / mbev_grid_cellsize + 1;
if (mbev_verbose > 0)
{
fprintf(stderr,"Grid bounds: %f %f %f %f    %f %f %f %f\n",
mbev_grid_bounds[0], mbev_grid_bounds[1], mbev_grid_bounds[2], mbev_grid_bounds[3],
mbev_grid_boundsutm[0], mbev_grid_boundsutm[1], mbev_grid_boundsutm[2], mbev_grid_boundsutm[3]);
fprintf(stderr,"cell size:%f dimensions: %d %d\n",
mbev_grid_cellsize, mbev_grid_nx, mbev_grid_ny);
}
	
	/* reset the widgets */
	sprintf(string,":::t\"Selected Grid Parameters:\":t\"    Cell Size: %.1f m\":t\"    Dimensions: %d %d\"", 
		mbev_grid_cellsize, mbev_grid_nx, mbev_grid_ny);
	set_label_multiline_string(label_implied, (String) string);

}


/*--------------------------------------------------------------------*/
void
do_mbeditviz_gridparameters( Widget w, XtPointer client_data, XtPointer call_data)
{
	char function_name[] = "do_mbeditviz_gridparameters";
	XmAnyCallbackStruct *acs;
	acs = (XmAnyCallbackStruct*)call_data;
	int	icellsize;
	char	string[MB_PATH_MAXLINE];

	/* print input debug statements */
	if (mbev_verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",function_name);
		fprintf(stderr,"dbg2  Input arguments:\n");
		fprintf(stderr,"dbg2       w:           %lu\n",(size_t)w);
		fprintf(stderr,"dbg2       client_data: %lu\n",(size_t)client_data);
		fprintf(stderr,"dbg2       call_data:   %lu\n",(size_t)call_data);
		}

if (mbev_verbose > 0)
fprintf(stderr,"do_mbeditviz_gridparameters\n");

	/* get calculated grid parameters */
	mbeditviz_get_grid_bounds();
	
	/* set the widgets */
	ac = 0;
	icellsize = (int)(10 * mbev_grid_cellsize);
        XtSetArg(args[ac], XmNvalue, icellsize); ac++;
	XtSetValues(scale_cellsize, args, ac);
	sprintf(string,":::t\"Grid Bounds:\":t\"    Longitude: %10.5f %10.5f  | %6.3f km\":t\"    Latitude: %9.5f %9.5f | %6.3f km\":t\"Suggested Grid Parameters:\":t\"    Cell Size: %.2f m\":t\"    Dimensions: %d %d\"", 
		mbev_grid_bounds[0], mbev_grid_bounds[1], 0.001 * (mbev_grid_boundsutm[1] - mbev_grid_boundsutm[0]),
		mbev_grid_bounds[2], mbev_grid_bounds[3], 0.001 * (mbev_grid_boundsutm[3] - mbev_grid_boundsutm[2]),
		mbev_grid_cellsize, mbev_grid_nx, mbev_grid_ny);
	set_label_multiline_string(label_current, (String) string);
	sprintf(string,":::t\"Selected Grid Parameters:\":t\"    Cell Size: %.2f m\":t\"    Dimensions: %d %d\"", 
		mbev_grid_cellsize, mbev_grid_nx, mbev_grid_ny);
	set_label_multiline_string(label_implied, (String) string);

}

/*--------------------------------------------------------------------*/
void
do_mbeditviz_viewgrid()
{
	char function_name[] = "do_mbeditviz_viewgrid";
	struct mbev_file_struct *file;
	struct mbev_ping_struct *ping;
	char	mbv_title[MB_PATH_MAXLINE];
	int	mbv_xo;
	int	mbv_yo;
	int	mbv_width;
	int	mbv_height;
	int	mbv_lorez_dimension;
	int	mbv_hirez_dimension;
	int	mbv_lorez_navdecimate;
	int	mbv_hirez_navdecimate;
	int	mbv_display_mode;
	int	mbv_mouse_mode;
	int	mbv_grid_mode;
	int	mbv_primary_histogram;
	int	mbv_primaryslope_histogram;
	int	mbv_secondary_histogram;
	int	mbv_primary_shade_mode;
	int	mbv_slope_shade_mode;
	int	mbv_secondary_shade_mode;
	int	mbv_grid_contour_mode;
	int	mbv_site_view_mode;
	int	mbv_route_view_mode;
	int	mbv_nav_view_mode;
	int	mbv_navdrape_view_mode;
	int	mbv_primary_colortable;
	int	mbv_primary_colortable_mode;
	double	mbv_primary_colortable_min;
	double	mbv_primary_colortable_max;
	int	mbv_slope_colortable;
	int	mbv_slope_colortable_mode;
	double	mbv_slope_colortable_min;
	double	mbv_slope_colortable_max;
	int	mbv_secondary_colortable;
	int	mbv_secondary_colortable_mode;
	double	mbv_secondary_colortable_min;
	double	mbv_secondary_colortable_max;
	double	mbv_exageration;
	double	mbv_modelelevation3d;
	double	mbv_modelazimuth3d;
	double	mbv_viewelevation3d;
	double	mbv_viewazimuth3d;
	double	mbv_illuminate_magnitude;
	double	mbv_illuminate_elevation;
	double	mbv_illuminate_azimuth;
	double	mbv_slope_magnitude;
	double	mbv_overlay_shade_magnitude;
	double	mbv_overlay_shade_center;
	int	mbv_overlay_shade_mode;
	double	mbv_contour_interval;
	double	*navtime_d = NULL;
	double	*navlon = NULL;
	double	*navlat = NULL;
	double	*navz = NULL;
	double	*navheading = NULL;
	double	*navspeed = NULL;
	double	*navportlon = NULL;
	double	*navportlat = NULL;
	double	*navstbdlon = NULL;
	double	*navstbdlat = NULL;
	int	*navline = NULL;
	int	*navshot = NULL;
	int	*navcdp = NULL;
	int	ifile, iping;

	/* print input debug statements */
	if (mbev_verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",function_name);
		}
    
if (mbev_verbose > 0)
fprintf(stderr,"do_mbeditviz_viewgrid\n");
	
	/* display grid */
	if (mbev_status == MB_SUCCESS 
		&& mbev_grid.status == MBEV_GRID_NOTVIEWED)
		{
		/* set parameters */
		sprintf(mbv_title, "MBeditviz Survey Viewer");
		mbv_xo = 200;
		mbv_yo = 200;
		mbv_width = 560;
		mbv_height = 500;
		mbv_lorez_dimension = 100;
		mbv_hirez_dimension = 500;
		mbv_lorez_navdecimate = 5;
		mbv_hirez_navdecimate = 1;
		mbv_display_mode = MBV_DISPLAY_2D;
		mbv_mouse_mode = MBV_MOUSE_MOVE;
		mbv_grid_mode = MBV_GRID_VIEW_PRIMARY;
		mbv_primary_histogram = MB_NO;
		mbv_primaryslope_histogram = MB_NO;
		mbv_secondary_histogram = MB_NO;
		mbv_primary_shade_mode = MBV_SHADE_VIEW_SLOPE;
		mbv_slope_shade_mode = MBV_SHADE_VIEW_NONE;
		mbv_secondary_shade_mode = MBV_SHADE_VIEW_NONE;
		mbv_grid_contour_mode = MBV_VIEW_OFF;
		mbv_site_view_mode = MBV_VIEW_OFF;
		mbv_route_view_mode = MBV_VIEW_OFF;
		mbv_nav_view_mode = MBV_VIEW_OFF;
		mbv_navdrape_view_mode = MBV_VIEW_OFF;
		mbv_primary_colortable = MBV_COLORTABLE_HAXBY;
		mbv_primary_colortable_mode = MBV_COLORTABLE_NORMAL;
		mbv_primary_colortable_min = mbev_grid.min;
		mbv_primary_colortable_max = mbev_grid.max;
		mbv_slope_colortable = MBV_COLORTABLE_HAXBY;
		mbv_slope_colortable_mode = MBV_COLORTABLE_REVERSED;
		mbv_slope_colortable_min = 0.0;
		mbv_slope_colortable_max = 0.5;
		mbv_secondary_colortable = MBV_COLORTABLE_HAXBY;
		mbv_secondary_colortable_mode = MBV_COLORTABLE_NORMAL;
		mbv_secondary_colortable_min = mbev_grid.smin;
		mbv_secondary_colortable_max = mbev_grid.smax;
		mbv_exageration = 1.0;
		mbv_modelelevation3d = 90.0;
		mbv_modelazimuth3d = 0.0;
		mbv_viewelevation3d = 90.0;
		mbv_viewazimuth3d = 0.0;
		mbv_illuminate_magnitude = 5.0;
		mbv_illuminate_elevation = 30.0;
		mbv_illuminate_azimuth = 90.0;
		mbv_slope_magnitude = 1.0;
		mbv_overlay_shade_magnitude = 1.0;
		mbv_overlay_shade_center = 0.0;
		mbv_overlay_shade_mode = MBV_COLORTABLE_NORMAL;
		mbv_contour_interval
			= pow(10.0, floor(log10(mbev_grid.max 
						- mbev_grid.min)) - 1.0);
		
		/* initialize mbview window */
		mbev_status = mbview_init(mbev_verbose, &mbev_instance, &mbev_error);

		/* set basic mbview window parameters */
		mbev_status = mbview_setwindowparms(mbev_verbose, mbev_instance,
					&do_mbeditviz_mbview_dismiss_notify,
					mbv_title,
					mbv_xo,
					mbv_yo,
					mbv_width,
					mbv_height,
					mbv_lorez_dimension,
					mbv_hirez_dimension,
					mbv_lorez_navdecimate,
					mbv_hirez_navdecimate,
					&mbev_error);
		

		/* set basic mbview view controls */
		if (mbev_status == MB_SUCCESS)
		mbev_status = mbview_setviewcontrols(mbev_verbose, mbev_instance,
					mbv_display_mode,
					mbv_mouse_mode,
					mbv_grid_mode,
					mbv_primary_histogram,
					mbv_primaryslope_histogram,
					mbv_secondary_histogram,
					mbv_primary_shade_mode,
					mbv_slope_shade_mode,
					mbv_secondary_shade_mode,
					mbv_grid_contour_mode,
					mbv_site_view_mode,
					mbv_route_view_mode,
					mbv_nav_view_mode,
					mbv_navdrape_view_mode,
					mbv_exageration,
					mbv_modelelevation3d,
					mbv_modelazimuth3d,
					mbv_viewelevation3d,
					mbv_viewazimuth3d,
					mbv_illuminate_magnitude,
					mbv_illuminate_elevation,
					mbv_illuminate_azimuth,
					mbv_slope_magnitude,
					mbv_overlay_shade_magnitude,
					mbv_overlay_shade_center,
					mbv_overlay_shade_mode,
					mbv_contour_interval,
					MBV_PROJECTION_PROJECTED,
					mbev_grid.projection_id,
					&mbev_error);
		
		/* set primary grid data */
		if (mbev_status == MB_SUCCESS)
		mbev_status = mbview_setprimarygrid(mbev_verbose, mbev_instance,
					MBV_PROJECTION_PROJECTED,
					mbev_grid.projection_id,
					mbev_grid.nodatavalue,
					mbev_grid.nx,
					mbev_grid.ny,
					mbev_grid.min,
					mbev_grid.max,
					mbev_grid.boundsutm[0],
					mbev_grid.boundsutm[1],
					mbev_grid.boundsutm[2],
					mbev_grid.boundsutm[3],
					mbev_grid.dx,
					mbev_grid.dy,
					mbev_grid.val,
					&mbev_error);
		
		/* set more mbview control values */
		if (mbev_status == MB_SUCCESS)
		mbev_status = mbview_setprimarycolortable(mbev_verbose, mbev_instance,
					mbv_primary_colortable,
					mbv_primary_colortable_mode,
					mbv_primary_colortable_min,
					mbv_primary_colortable_max,
					&mbev_error);
		if (mbev_status == MB_SUCCESS)
		mbev_status = mbview_setslopecolortable(mbev_verbose, mbev_instance,
					mbv_slope_colortable,
					mbv_slope_colortable_mode,
					mbv_slope_colortable_min,
					mbv_slope_colortable_max,
					&mbev_error);

		/* open up mbview window */
		mbev_status = mbview_open(mbev_verbose, mbev_instance, &mbev_error);
		
		/* set grid status */
		if (mbev_status == MB_SUCCESS)
			mbev_grid.status = MBEV_GRID_VIEWED;

		/* set more mbview control values */
		if (mbev_status == MB_SUCCESS)
		mbev_status = mbview_setsecondarygrid(mbev_verbose, mbev_instance,
					MBV_PROJECTION_PROJECTED,
					mbev_grid.projection_id,
					mbev_grid.nodatavalue,
					mbev_grid.nx,
					mbev_grid.ny,
					mbev_grid.smin,
					mbev_grid.smax,
					mbev_grid.boundsutm[0],
					mbev_grid.boundsutm[1],
					mbev_grid.boundsutm[2],
					mbev_grid.boundsutm[3],
					mbev_grid.dx,
					mbev_grid.dy,
					mbev_grid.sgm,
					&mbev_error);
		if (mbev_status == MB_SUCCESS)
		mbev_status = mbview_setsecondarycolortable(mbev_verbose, mbev_instance,
					mbv_secondary_colortable,
					mbv_secondary_colortable_mode,
					mbv_secondary_colortable_min,
					mbv_secondary_colortable_max,
					mbv_overlay_shade_magnitude,
					mbv_overlay_shade_center,
					mbv_overlay_shade_mode,
					&mbev_error);
		mbev_status = mbview_setsecondaryname(mbev_verbose, mbev_instance,
				"Standard Deviation", &mbev_error);
					
		/* update widgets */
		if (mbev_status == MB_SUCCESS)
		mbev_status = mbview_update(mbev_verbose, mbev_instance, &mbev_error);
			
		/* add navigation to view */
		for (ifile=0;ifile<mbev_num_files;ifile++)
			{
			file = &(mbev_files[ifile]);
			if (file->load_status == MB_YES && file->num_pings > 0)
				{
				/* set message */
				sprintf(value_text, "Loading nav %d of %d...", ifile+1, mbev_num_files);
				do_mbeditviz_message_on(value_text);
				
				/* allocate arrays */
				if ((navtime_d = (double *) malloc(file->num_pings*sizeof(double))) == NULL)
					mbev_status = MB_FAILURE;
				if (mbev_status == MB_SUCCESS)
				if ((navlon = malloc(file->num_pings*sizeof(double))) == NULL)
					mbev_status = MB_FAILURE;
				if (mbev_status == MB_SUCCESS)
				if ((navlat = malloc(file->num_pings*sizeof(double))) == NULL)
					mbev_status = MB_FAILURE;
				if (mbev_status == MB_SUCCESS)
				if ((navz = malloc(file->num_pings*sizeof(double))) == NULL)
					mbev_status = MB_FAILURE;
				if (mbev_status == MB_SUCCESS)
				if ((navheading = malloc(file->num_pings*sizeof(double))) == NULL)
					mbev_status = MB_FAILURE;
				if (mbev_status == MB_SUCCESS)
				if ((navspeed = malloc(file->num_pings*sizeof(double))) == NULL)
					mbev_status = MB_FAILURE;
				if (mbev_status == MB_SUCCESS)
				if ((navportlon = malloc(file->num_pings*sizeof(double))) == NULL)
					mbev_status = MB_FAILURE;
				if (mbev_status == MB_SUCCESS)
				if ((navportlat = malloc(file->num_pings*sizeof(double))) == NULL)
					mbev_status = MB_FAILURE;
				if (mbev_status == MB_SUCCESS)
				if ((navstbdlon = malloc(file->num_pings*sizeof(double))) == NULL)
					mbev_status = MB_FAILURE;
				if (mbev_status == MB_SUCCESS)
				if ((navstbdlat = malloc(file->num_pings*sizeof(double))) == NULL)
					mbev_status = MB_FAILURE;
				if (mbev_status == MB_SUCCESS)
				if ((navline = malloc(file->num_pings*sizeof(double))) == NULL)
					mbev_status = MB_FAILURE;
				if (mbev_status == MB_SUCCESS)
				if ((navshot = malloc(file->num_pings*sizeof(double))) == NULL)
					mbev_status = MB_FAILURE;
				if (mbev_status == MB_SUCCESS)
				if ((navcdp = malloc(file->num_pings*sizeof(double))) == NULL)
					mbev_status = MB_FAILURE;
				
				/* copy nav data */
				if (mbev_status == MB_SUCCESS)
				for (iping=0;iping<file->num_pings;iping++)
					{
					ping = (struct mbev_ping_struct *) &(file->pings[iping]);
					navtime_d[iping] = ping->time_d;
					navlon[iping] = ping->navlon;
					navlat[iping] = ping->navlat;
					navz[iping] = -ping->sonardepth;
					navheading[iping] = ping->heading;
					navspeed[iping] = ping->speed;
					navportlon[iping] = ping->portlon;
					navportlat[iping] = ping->portlat;
					navstbdlon[iping] = ping->stbdlon;
					navstbdlat[iping] = ping->stbdlat;
					navline[iping] = 0;
					navshot[iping] = iping;
					navcdp[iping] = 0;
					}
					
				/* add nav data to mbview */
				if (mbev_status == MB_SUCCESS)
				mbev_status = mbview_addnav(mbev_verbose, mbev_instance,
						file->num_pings,
						navtime_d,
						navlon,
						navlat,
						navz,
						navheading,
						navspeed,
						navportlon,
						navportlat,
						navstbdlon,
						navstbdlat,
						navline,
						navshot,
						navcdp,
						MBV_COLOR_BLACK,
						2,
						file->name,
						MB_PROCESSED_NONE,
						file->path,
						file->path,
						file->format,
						MB_YES,
						MB_NO,
						MB_NO,
						MB_NO,
						1,
						&mbev_error);

				/* deallocate memory used for data arrays */
				free(navtime_d);
				navtime_d = NULL;
				free(navlon);
				navlon = NULL;
				free(navlat);
				navlat = NULL;
				free(navz);
				navz = NULL;
				free(navheading);
				navheading = NULL;
				free(navspeed);
				navspeed = NULL;
				free(navportlon);
				navportlon = NULL;
				free(navportlat);
				navportlat = NULL;
				free(navstbdlon);
				navstbdlon = NULL;
				free(navstbdlat);
				navstbdlat = NULL;
				free(navline);
				navline = NULL;
				free(navshot);
				navshot = NULL;
				free(navcdp);
				navcdp = NULL;
				}
			}
		mbview_enableviewnavs(mbev_verbose, mbev_instance, &mbev_error);
		mbev_status = mbview_update(mbev_verbose, mbev_instance, &mbev_error);
		do_mbeditviz_message_off();
		
		/* add pick notify functions */
		mbview_addpicknotify(mbev_verbose, 0,
					MBV_PICK_ONEPOINT,
					do_mbeditviz_pickonepoint_notify,
					&mbev_error);
		mbview_addpicknotify(mbev_verbose, 0,
					MBV_PICK_TWOPOINT,
					do_mbeditviz_picktwopoint_notify,
					&mbev_error);
		mbview_addpicknotify(mbev_verbose, 0,
					MBV_PICK_AREA,
					do_mbeditviz_pickarea_notify,
					&mbev_error);
		mbview_addpicknotify(mbev_verbose, 0,
					MBV_PICK_REGION,
					do_mbeditviz_pickregion_notify,
					&mbev_error);
		mbview_addpicknotify(mbev_verbose, 0,
					MBV_PICK_SITE,
					do_mbeditviz_picksite_notify,
					&mbev_error);
		mbview_addpicknotify(mbev_verbose, 0,
					MBV_PICK_ROUTE,
					do_mbeditviz_pickroute_notify,
					&mbev_error);
		mbview_addpicknotify(mbev_verbose, 0,
					MBV_PICK_NAV,
					do_mbeditviz_picknav_notify,
					&mbev_error);

		/* add action button */
		mbview_addaction(mbev_verbose, mbev_instance,
			do_mbeditviz_regrid_notify,
			"Update Bathymetry Grid", 
			MBV_PICKMASK_NONE, &mbev_error);
		}
	
	/* reset the gui */
	do_mbeditviz_update_gui();
	
if (mbev_verbose > 0)
fprintf(stderr,"return do_mbeditviz_viewgrid status:%d\n", mbev_status);

}
/*---------------------------------------------------------------------------------------*/

int do_mbeditviz_mbview_dismiss_notify(size_t instance)
{
	char function_name[] = "do_mbeditviz_mbview_dismiss_notify";

	/* print input debug statements */
	if (mbev_verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",function_name);
		fprintf(stderr,"dbg2  Input arguments:\n");
		fprintf(stderr,"dbg2       instance:   %ld\n",instance);
		}
    
if (mbev_verbose > 0)
fprintf(stderr,"do_mbeditviz_mbview_dismiss_notify status:%d\n", mbev_status);
	
	/* destroy any mb3dsoundings window */
if (mbev_verbose > 0)
fprintf(stderr,"1 do_mbeditviz_mbview_dismiss_notify status:%d\n", mbev_status);
	mbev_status = mb3dsoundings_end(mbev_verbose, &mbev_verbose);
if (mbev_verbose > 0)
fprintf(stderr,"2 do_mbeditviz_mbview_dismiss_notify status:%d\n", mbev_status);
	mbeditviz_mb3dsoundings_dismiss();
if (mbev_verbose > 0)
fprintf(stderr,"3 do_mbeditviz_mbview_dismiss_notify status:%d\n", mbev_status);
	
	/* destroy the grid */
if (mbev_verbose > 0)
fprintf(stderr,"mbev_grid.status:%d\n",mbev_grid.status);
	if (mbev_grid.status != MBEV_GRID_NONE)
		mbeditviz_destroy_grid();
if (mbev_verbose > 0)
fprintf(stderr,"4 do_mbeditviz_mbview_dismiss_notify status:%d\n", mbev_status);
	
	/* reset the gui */
	do_mbeditviz_update_gui();

if (mbev_verbose > 0)
fprintf(stderr,"return do_mbeditviz_mbview_dismiss_notify status:%d\n", mbev_status);

	return(mbev_status);
}
/*---------------------------------------------------------------------------------------*/
void
do_mbeditviz_deleteselected( Widget w, XtPointer client_data, XtPointer call_data)
{
	char function_name[] = "do_mbeditviz_deleteselected";
	XmAnyCallbackStruct *acs;
	acs = (XmAnyCallbackStruct*)call_data;
	int	*position_list = NULL;
	int position_count = 0;
	int	i;

	/* print input debug statements */
	if (mbev_verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",function_name);
		fprintf(stderr,"dbg2  Input arguments:\n");
		fprintf(stderr,"dbg2       w:           %lu\n",(size_t)w);
		fprintf(stderr,"dbg2       client_data: %lu\n",(size_t)client_data);
		fprintf(stderr,"dbg2       call_data:   %lu\n",(size_t)call_data);
		}
    
if (mbev_verbose > 0)
fprintf(stderr,"do_mbeditviz_deleteselected\n");

	/* get positions of selected list items */
	ac = 0;
	XtSetArg(args[ac], XmNselectedPositionCount, (XtPointer) &position_count); ac++;
	XtSetArg(args[ac], XmNselectedPositions, (XtPointer) &position_list); ac++;
	XtGetValues(list_filelist, args, ac);
	
	/* delete the selected files */
	for (i=position_count-1;i>=0;i--)
		mbeditviz_delete_file(position_list[i]-1);
	
	/* reset the gui */
	do_mbeditviz_update_gui();

if (mbev_verbose > 0)
fprintf(stderr,"return do_mbeditviz_deleteselected status:%d\n", mbev_status);

}

/*---------------------------------------------------------------------------------------*/
void
do_mbeditviz_changeoutputmode( Widget w, XtPointer client_data, XtPointer call_data)
{
	char function_name[] = "do_mbeditviz_changeoutputmode";
	XmToggleButtonCallbackStruct *acs;
	acs = (XmToggleButtonCallbackStruct*)call_data;

	/* print input debug statements */
	if (mbev_verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",function_name);
		fprintf(stderr,"dbg2  Input arguments:\n");
		fprintf(stderr,"dbg2       w:           %lu\n",(size_t)w);
		fprintf(stderr,"dbg2       client_data: %lu\n",(size_t)client_data);
		fprintf(stderr,"dbg2       call_data:   %lu\n",(size_t)call_data);
		}

if (mbev_verbose > 0)
fprintf(stderr,"do_mbeditviz_changeoutputmode\n");

    /* set values if needed */
    if (acs->reason == XmCR_VALUE_CHANGED)
	    {
	    if (acs->set) 
	    	mbev_mode_output = MBEV_OUTPUT_MODE_EDIT;
	    else 
	    	mbev_mode_output = MBEV_OUTPUT_MODE_BROWSE;
	    if (mbev_mode_output == MBEV_OUTPUT_MODE_EDIT)
		{
		XmToggleButtonSetState(toggleButton_mode_edit, 
			    TRUE, FALSE);
		XmToggleButtonSetState(toggleButton_mode_browse,  
			    FALSE, FALSE);
		XmToggleButtonSetState(toggleButton_openmodeedit, 
			    TRUE, FALSE);
		XmToggleButtonSetState(toggleButton_openmodebrowse, 
			    FALSE, FALSE);
		}
	    else
		{
		XmToggleButtonSetState(toggleButton_mode_edit, 
			    FALSE, FALSE);
		XmToggleButtonSetState(toggleButton_mode_browse,  
			    TRUE, FALSE);
		XmToggleButtonSetState(toggleButton_openmodeedit, 
			    FALSE, FALSE);
		XmToggleButtonSetState(toggleButton_openmodebrowse, 
			    TRUE, FALSE);
		}
if (mbev_verbose > 0)
fprintf(stderr,"acs->set:%d mbev_mode_output:%d\n",acs->set,mbev_mode_output);
	    }

if (mbev_verbose > 0)
fprintf(stderr,"return do_mbeditviz_changeoutputmode status:%d\n", mbev_status);

}
/*---------------------------------------------------------------------------------------*/

int do_mbeditviz_opendata(char *input_file_ptr, int format)
{
	char function_name[] = "do_mbeditviz_opendata";

	/* print input debug statements */
	if (mbev_verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",function_name);
		fprintf(stderr,"dbg2  Input arguments:\n");
		fprintf(stderr,"dbg2       input_file_ptr:    %s\n",input_file_ptr);
		fprintf(stderr,"dbg2       format:            %d\n",format);
		}
	
if (mbev_verbose > 0)
fprintf(stderr,"do_mbeditviz_opendata:%s %d\n", input_file_ptr, format);
	do_mbeditviz_message_on("Reading datalist...");
	
	mbeditviz_open_data(input_file_ptr, format);
	
	do_mbeditviz_message_off();

if (mbev_verbose > 0)
fprintf(stderr,"return do_mbeditviz_opendata status:%d\n", mbev_status);
	
	return(mbev_status);
}	
/*---------------------------------------------------------------------------------------*/
void
do_mbeditviz_update_gui( )
{
	char function_name[] = "do_mbeditviz_update_gui";
	struct mbev_file_struct *file;
	struct mbev_ping_struct *ping;
	char	string[MB_PATH_MAXLINE];
	char	loadchar;
	char	athchar;
	char	atschar;
	char	atachar;
    	XmString *xstr;
	int	i, j, k;

	/* print input debug statements */
	if (mbev_verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",function_name);
		}
    
if (mbev_verbose > 0)
fprintf(stderr,"do_mbeditviz_update_gui status:%d\n", mbev_status);

	/* set status text */
	mbev_num_files_loaded = 0;
	mbev_num_pings_loaded = 0;
	mbev_num_soundings_loaded = 0;
	for (i=0;i<mbev_num_files;i++)
		{
		file = &(mbev_files[i]);
		if (file->load_status == MB_YES)
			{
			mbev_num_files_loaded++;
			mbev_num_pings_loaded += file->num_pings;
			for (j=0;j<file->num_pings;j++)
				{
				ping = &(file->pings[j]);
				for (k=0;k<ping->beams_bath;k++)
					{
					if (ping->beamflag[k] != MB_FLAG_NULL)
						mbev_num_soundings_loaded ++;
					}
				}
			}
		}
	if (mbev_grid.status == MBEV_GRID_NONE) 
	sprintf(string,":::t\"Available Files: %d\":t\"Loaded Files: %d\":t\"Grid Not Generated\"", 
		mbev_num_files,mbev_num_files_loaded);
	else
	sprintf(string,":::t\"Available Files: %d\":t\"Loaded Files: %d\":t\"Grid:\":t\"  Lon: %f %f\":t\"  Lat: %f %f\":t\"  Cell Size: %f m\":t\"  Dimensions: %d %d\"", 
		mbev_num_files,mbev_num_files_loaded,
		mbev_grid.bounds[0],mbev_grid.bounds[1],mbev_grid.bounds[2],mbev_grid.bounds[3],
		mbev_grid.dx,mbev_grid.nx,mbev_grid.ny);
	set_label_multiline_string(label_mbeditviz_status, (String) string);
	
	/* build available file list */
	XmListDeleteAllItems(list_filelist);
	if (mbev_num_files > 0)
		{
		xstr = (XmString *) malloc(mbev_num_files * sizeof(XmString));
		for (i=0;i<mbev_num_files;i++)
			{
			file = &(mbev_files[i]);
			if (file->load_status == MB_YES)
				loadchar = '*';
			else
				loadchar = ' ';
			if (file->n_async_heading > 0)
				athchar = 'H';
			else
				athchar = ' ';
			if (file->n_async_sonardepth > 0)
				atschar = 'S';
			else
				atschar = ' ';
			if (file->n_async_attitude > 0)
				atachar = 'A';
			else
				atachar = ' ';
			sprintf(string,"%c%c%c%c %s", 
				loadchar, athchar, atschar, atachar, 
				mbev_files[i].name);
   			xstr[i] = XmStringCreateLocalized(string);
 			}
    		XmListAddItems(list_filelist,xstr,mbev_num_files,0);
		for (i=0;i<mbev_num_files;i++)
			{
    			XmStringFree(xstr[i]);
    			}
    		free(xstr);
		}
	
	/*set sensitivity */
	if (mbev_grid.status == MBEV_GRID_NONE)
		{
		XtVaSetValues(pushButton_openswath,
			XmNsensitive, True,
			NULL);
		}
	else
		{
		XtVaSetValues(pushButton_openswath,
			XmNsensitive, False,
			NULL);
		}
	if (mbev_num_files > 0 && mbev_grid.status == MBEV_GRID_NONE)
		{
		XtVaSetValues(pushButton_deleteselected,
			XmNsensitive, True,
			NULL);
		XtVaSetValues(pushButton_viewselected,
			XmNsensitive, True,
			NULL);
		XtVaSetValues(pushButton_viewall,
			XmNsensitive, True,
			NULL);
		XtVaSetValues(toggleButton_mode_edit,
			XmNsensitive, True,
			NULL);
		XtVaSetValues(toggleButton_mode_browse,
			XmNsensitive, True,
			NULL);
		XtVaSetValues(pushButton_updategrid,
			XmNsensitive, False,
			NULL);
		}
	else
		{
		XtVaSetValues(pushButton_deleteselected,
			XmNsensitive, False,
			NULL);
		XtVaSetValues(pushButton_viewselected,
			XmNsensitive, False,
			NULL);
		XtVaSetValues(pushButton_viewall,
			XmNsensitive, False,
			NULL);
		XtVaSetValues(toggleButton_mode_edit,
			XmNsensitive, False,
			NULL);
		XtVaSetValues(toggleButton_mode_browse,
			XmNsensitive, False,
			NULL);
		XtVaSetValues(pushButton_updategrid,
			XmNsensitive, True,
			NULL);
		}

if (mbev_verbose > 0)
fprintf(stderr,"return do_mbeditviz_update_gui status:%d\n", mbev_status);
}
/*---------------------------------------------------------------------------------------*/
void do_mbeditviz_pickonepoint_notify(size_t instance)
{
	char function_name[] = "do_mbeditviz_pickonepoint_notify";

	/* print input debug statements */
	if (mbev_verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",function_name);
		fprintf(stderr,"dbg2  Input arguments:\n");
		fprintf(stderr,"dbg2       instance:    %ld\n",instance);
		}
	
if (mbev_verbose > 0)
fprintf(stderr,"do_mbeditviz_pickonepoint_notify:%ld\n", instance);
	

if (mbev_verbose > 0)
fprintf(stderr,"return do_mbeditviz_pickonepoint_notify status:%d\n", mbev_status);
}	
/*------------------------------------------------------------------------------*/

void do_mbeditviz_picktwopoint_notify(size_t instance)
{
	char function_name[] = "do_mbeditviz_picktwopoint_notify";

	/* print input debug statements */
	if (mbev_verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",function_name);
		fprintf(stderr,"dbg2  Input arguments:\n");
		fprintf(stderr,"dbg2       instance:    %ld\n",instance);
		}
	
if (mbev_verbose > 0)
fprintf(stderr,"do_mbeditviz_picktwopoint_notify:%ld\n", instance);
	

if (mbev_verbose > 0)
fprintf(stderr,"return do_mbeditviz_picktwopoint_notify status:%d\n", mbev_status);
}	
/*------------------------------------------------------------------------------*/

void do_mbeditviz_pickarea_notify(size_t instance)
{
	char function_name[] = "do_mbeditviz_pickarea_notify";

	/* print input debug statements */
	if (mbev_verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",function_name);
		fprintf(stderr,"dbg2  Input arguments:\n");
		fprintf(stderr,"dbg2       instance:    %ld\n",instance);
		}
	
if (mbev_verbose > 0)
fprintf(stderr,"do_mbeditviz_pickarea_notify:%ld\n", instance);

	mbeditviz_selectarea(instance);
	mbev_status = mb3dsoundings_open(mbev_verbose, &mbev_selected, &mbev_error);
	mbev_status = mb3dsoundings_set_dismiss_notify(mbev_verbose, &mbeditviz_mb3dsoundings_dismiss, &mbev_error);
	mbev_status = mb3dsoundings_set_edit_notify(mbev_verbose, &mbeditviz_mb3dsoundings_edit, &mbev_error);
	mbev_status = mb3dsoundings_set_info_notify(mbev_verbose, &mbeditviz_mb3dsoundings_info, &mbev_error);
	mbev_status = mb3dsoundings_set_bias_notify(mbev_verbose, &mbeditviz_mb3dsoundings_bias, &mbev_error);
	mbev_status = mb3dsoundings_set_biasapply_notify(mbev_verbose, &mbeditviz_mb3dsoundings_biasapply, &mbev_error);

if (mbev_verbose > 0)
fprintf(stderr,"return do_mbeditviz_picktwopoint_notify status:%d\n", mbev_status);
	
}	
/*------------------------------------------------------------------------------*/

void do_mbeditviz_pickregion_notify(size_t instance)
{
	char function_name[] = "do_mbeditviz_pickregion_notify";

	/* print input debug statements */
	if (mbev_verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",function_name);
		fprintf(stderr,"dbg2  Input arguments:\n");
		fprintf(stderr,"dbg2       instance:    %ld\n",instance);
		}
	
if (mbev_verbose > 0)
fprintf(stderr,"do_mbeditviz_pickregion_notify:%ld\n", instance);

	mbeditviz_selectregion(instance);
	mbev_status = mb3dsoundings_open(mbev_verbose, &mbev_selected, &mbev_error);
	mbev_status = mb3dsoundings_set_dismiss_notify(mbev_verbose, &mbeditviz_mb3dsoundings_dismiss, &mbev_error);
	mbev_status = mb3dsoundings_set_edit_notify(mbev_verbose, &mbeditviz_mb3dsoundings_edit, &mbev_error);
	mbev_status = mb3dsoundings_set_info_notify(mbev_verbose, &mbeditviz_mb3dsoundings_info, &mbev_error);	
	mbev_status = mb3dsoundings_set_bias_notify(mbev_verbose, &mbeditviz_mb3dsoundings_bias, &mbev_error);
	mbev_status = mb3dsoundings_set_biasapply_notify(mbev_verbose, &mbeditviz_mb3dsoundings_biasapply, &mbev_error);

if (mbev_verbose > 0)
fprintf(stderr,"return do_mbeditviz_pickregion_notify status:%d\n", mbev_status);
}	
/*------------------------------------------------------------------------------*/

void do_mbeditviz_picksite_notify(size_t instance)
{
	char function_name[] = "do_mbeditviz_picksite_notify";

	/* print input debug statements */
	if (mbev_verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",function_name);
		fprintf(stderr,"dbg2  Input arguments:\n");
		fprintf(stderr,"dbg2       instance:    %ld\n",instance);
		}
	
if (mbev_verbose > 0)
fprintf(stderr,"do_mbeditviz_picksite_notify:%ld\n", instance);
	

if (mbev_verbose > 0)
fprintf(stderr,"return do_mbeditviz_picksite_notify status:%d\n", mbev_status);
}	
/*------------------------------------------------------------------------------*/

void do_mbeditviz_pickroute_notify(size_t instance)
{
	char function_name[] = "do_mbeditviz_pickroute_notify";

	/* print input debug statements */
	if (mbev_verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",function_name);
		fprintf(stderr,"dbg2  Input arguments:\n");
		fprintf(stderr,"dbg2       instance:    %ld\n",instance);
		}
	
if (mbev_verbose > 0)
fprintf(stderr,"do_mbeditviz_pickroute_notify:%ld\n", instance);
	

if (mbev_verbose > 0)
fprintf(stderr,"return do_mbeditviz_pickroute_notify status:%d\n", mbev_status);
}	
/*------------------------------------------------------------------------------*/

void do_mbeditviz_picknav_notify(size_t instance)
{
	char function_name[] = "do_mbeditviz_picknav_notify";

	/* print input debug statements */
	if (mbev_verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",function_name);
		fprintf(stderr,"dbg2  Input arguments:\n");
		fprintf(stderr,"dbg2       instance:    %ld\n",instance);
		}
	
if (mbev_verbose > 0)
fprintf(stderr,"do_mbeditviz_picknav_notify:%ld\n", instance);
	

	mbeditviz_selectnav(instance);
	
	/* open ping editor */
	/* BxManageCB(parent, (XtPointer)"form_pingedit", NULL); */
	
	/* open mb3dsoundings 3d editor */
	mbev_status = mb3dsoundings_open(mbev_verbose, &mbev_selected, &mbev_error);
	mbev_status = mb3dsoundings_set_dismiss_notify(mbev_verbose, &mbeditviz_mb3dsoundings_dismiss, &mbev_error);
	mbev_status = mb3dsoundings_set_edit_notify(mbev_verbose, &mbeditviz_mb3dsoundings_edit, &mbev_error);
	mbev_status = mb3dsoundings_set_info_notify(mbev_verbose, &mbeditviz_mb3dsoundings_info, &mbev_error);	
	mbev_status = mb3dsoundings_set_bias_notify(mbev_verbose, &mbeditviz_mb3dsoundings_bias, &mbev_error);
	mbev_status = mb3dsoundings_set_biasapply_notify(mbev_verbose, &mbeditviz_mb3dsoundings_biasapply, &mbev_error);

if (mbev_verbose > 0)
fprintf(stderr,"return do_mbeditviz_picknav_notify status:%d\n", mbev_status);
}	
/*------------------------------------------------------------------------------*/
void
do_mbeditviz_regrid_notify( Widget w, XtPointer client_data, XtPointer call_data)
{
	char function_name[] = "do_mbeditviz_regrid_notify";
	XmAnyCallbackStruct *acs;
	acs = (XmAnyCallbackStruct*)call_data;
	double	rollbias;
	double	pitchbias;
	double	headingbias;
	double	timelag;

	/* print input debug statements */
	if (mbev_verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",function_name);
		fprintf(stderr,"dbg2  Input arguments:\n");
		fprintf(stderr,"dbg2       w:           %lu\n",(size_t)w);
		fprintf(stderr,"dbg2       client_data: %lu\n",(size_t)client_data);
		fprintf(stderr,"dbg2       call_data:   %lu\n",(size_t)call_data);
		}
    
if (mbev_verbose > 0)
fprintf(stderr,"do_mbeditviz_regrid_notify\n");

	/* get current bias parameters */
	mb3dsoundings_get_bias_values(mbev_verbose, &rollbias, &pitchbias, &headingbias, &timelag, &mbev_error);

	/* regrid the bathymetry */
	mbeditviz_mb3dsoundings_biasapply(rollbias, pitchbias, headingbias, timelag);
	
	/* reset the gui */
	do_mbeditviz_update_gui();

if (mbev_verbose > 0)
fprintf(stderr,"return do_mbeditviz_regrid_notify status:%d\n", mbev_status);

}

/*--------------------------------------------------------------------*/
/* Mbeditviz message functions */
/*------------------------------------------------------------------------------*/

int
do_mbeditviz_message_on(char *message)
{
	char function_name[] = "do_mbeditviz_message_on";
  	Widget  diashell, topshell;
    	Window  diawindow, topwindow;
    	XWindowAttributes	xwa;
    	XEvent  event;

	/* print input debug statements */
	if (mbev_verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",function_name);
		fprintf(stderr,"dbg2  Input arguments:\n");
		fprintf(stderr,"dbg2       message:   %s\n",message);
		}
	
	mbev_message_on = MB_YES;

    	set_mbview_label_string(label_mbeditviz_message, message);
    	XtManageChild(bulletinBoard_mbeditviz_message);
    
    	/* force the label to be visible */
    	for (diashell = label_mbeditviz_message; 
	    	!XtIsShell(diashell); 
	    	diashell = XtParent(diashell));
    	for (topshell = diashell; 
	    	!XtIsTopLevelShell(topshell);
	    	topshell = XtParent(topshell));
    	if (XtIsRealized(diashell) && XtIsRealized(topshell))
		{
		diawindow = XtWindow(diashell);
		topwindow = XtWindow(topshell);
	
		/* wait for the dialog to be mapped */
		while (XGetWindowAttributes(XtDisplay(label_mbeditviz_message), 
						diawindow, &xwa)
			&& xwa.map_state != IsViewable)
	    		{
	    		if (XGetWindowAttributes(XtDisplay(label_mbeditviz_message), 
							topwindow, &xwa)
		    		&& xwa.map_state != IsViewable)
			break;
		
	    		XtAppNextEvent(app, &event);
	    		XtDispatchEvent(&event);
	    		}
		}
	
    	XmUpdateDisplay(topshell);
	
    	return(1);
}

/*------------------------------------------------------------------------------*/

int
do_mbeditviz_message_off()
{
	char function_name[] = "do_mbeditviz_message_off";

	/* print input debug statements */
	if (mbev_verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",function_name);
		}
	
	mbev_message_on = MB_NO;

     	XtUnmanageChild(bulletinBoard_mbeditviz_message);
    	XSync(XtDisplay(bulletinBoard_mbeditviz_message), 0);
    	XmUpdateDisplay(bulletinBoard_mbeditviz_message);
   
    	return(1);
}

/*--------------------------------------------------------------------*/
/* Change label string cleanly, no memory leak */
/*--------------------------------------------------------------------*/

void set_label_string(Widget w, String str)
{
    XmString xstr;
    
    xstr = XmStringCreateLocalized( str );
    if ( xstr != NULL ) 
	XtVaSetValues(w, 
	    XmNlabelString, xstr, 
	    NULL);
    else 
	XtWarning("Failed to update labelString");

    XmStringFree( xstr );
}
/*--------------------------------------------------------------------*/
/* Change multiline label string cleanly, no memory leak */
/*--------------------------------------------------------------------*/

void set_label_multiline_string(Widget w, String str)
{
    XmString xstr;
    Boolean      argok;

    xstr = (XtPointer)BX_CONVERT(w, str, XmRXmString, 0, &argok);
    if ( xstr != NULL && argok)
        XtVaSetValues(w,
            XmNlabelString, xstr,
            NULL);
    else
        XtWarning("Failed to update labelString");

    XmStringFree( xstr );
}
/*--------------------------------------------------------------------*/
/* Get text item string cleanly, no memory leak */
/*--------------------------------------------------------------------*/

void get_text_string(Widget w, String str)
{
    char	*str_tmp;
    
    str_tmp = (char *) XmTextGetString(w);
    strcpy(str, str_tmp);
    XtFree(str_tmp);
}

/*--------------------------------------------------------------------*/

int
do_wait_until_viewed()
{
    Widget  topshell;
    Window  topwindow;
    XWindowAttributes	xwa;
    XEvent  event;

    /* find the top level shell */
    for (topshell = parent;
	    !XtIsTopLevelShell(topshell);
	    topshell = XtParent(topshell))
	;
	
    /* keep processing events until it is viewed */
    if (XtIsRealized(topshell))
	{
	topwindow = XtWindow(topshell);
	
	/* wait for the window to be mapped */
	while (XGetWindowAttributes(
			XtDisplay(parent),
			topwindow, &xwa)
		&& xwa.map_state != IsViewable)
	    {
	    XtAppNextEvent(app, &event);
	    XtDispatchEvent(&event);
	    }
	}
	
    XmUpdateDisplay(topshell);
		
    return(MB_SUCCESS);
}

/*--------------------------------------------------------------------*/
