/***************************************************************************
 *		  Copyright (C) 1994  Charles P. Peterson                  *
 *	     4007 Enchanted Sun, San Antonio, Texas 78244-1254             *
 *              Email: Charles_P_Peterson@fcircus.sat.tx.us                *
 *                                                                         *
 *		  This is free software with NO WARRANTY.                  *
 *	      See gfft.c, or run program itself, for details.              *
 *		      Support is available for a fee.                      *
 ***************************************************************************
 *
 * Program:     gfft--General FFT analysis
 * File:        wbdialog.c
 * Purpose:     workbench GUI dialog
 * Author:      Charles Peterson (CPP)
 * History:     19-Nov-1993 CPP; Created.
 *              4-Aug-94 CPP (1.04); Filenames may contain spaces
 *              5-Aug-94 CPP (1.05); Make append apply to name shown
 *              5-Aug-94 CPP (1.10); Minor interface improvements
 * Comments:    Workbench GUI.  Amiga Dependent!
 *              We must exit from here on all fatal errors!
 *              (Note: might have been started from CLI.  In that case,
 *               don't expect to find any workbench message or icons.
 *               Read and write files and options might already have been
 *               selected through CLI arguments or commands.)
 */

#ifdef AMIGA  /* This module is AMIGA dependent */

#include <stdio.h>     /* sprintf() */
#include <string.h>

/*
 * Amiga includes
 */
#include <exec/types.h>
#include <workbench/workbench.h>
#include <intuition/intuition.h>
#include <clib/intuition_protos.h>
#include <clib/exec_protos.h>
#include <dos.h> /* chkabort() */

/*
 * GFFT includes
 */
#include "gfft.h"
#include "settings.h"  /* for testing */
#include "wbench.h"
#include "format.h"

/*
 * special global variables defined here
 */
struct Window *dialog_window_p = NULL;
struct Gadget *message_gadgetp;

/*
 * Semi-global variables
 */
extern LONG num_files;
extern LONG file_index;
extern double LoopTimeElapsed;


/*
 * Strings from messages[.c|.h]
 */
extern const char NameAuthor[];
extern const char VersionData[];
extern const char Copyright[];
extern char *Please_Select_Read_File_S;
extern char *Set_Parameters_Then_Ok_S;
extern char *Attempting_to_Perform_FFT_S;
extern char *Set_Sampling_Rate_S;
extern char *Verify_Sampling_Rate_S;
extern char *Dialog_Window_Title;

/*
 * From wbtools
 */
extern BOOLEAN Old_Intuition;
extern short Screen_Font_Height;

/*
 * From Amiga Hardware Reference Manual
 */
#define RAW_HELP_KEY 0x5f

/*
 * Window definitions
 */
#define FONT_SIZE 8

#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 200
#define MAX_TEXT_COLS 75
#define WINDOW_LEFT 0         /* Same as right edge */
#define WINDOW_TOP 0          /* Allow for WB title & click area above */
#define WINDOW_BOTTOM (FONT_SIZE * 5)  /* Allow for small console below */

#define SELECT_READ_FILE 1
#define SELECT_PARAMETERS 2
#define PERFORMING_FFT 3
#define REMEMBER_MESSAGE 4
#define SET_RATE 5
#define VERIFY_RATE 6

/*
 * Static variables for this file
 */
static struct Gadget *octave_2_gadgetp, *octave_3_gadgetp;
static struct Gadget *octave_4_gadgetp, *octave_5_gadgetp;
static struct Gadget *octave_6_gadgetp, *octave_7_gadgetp;
static struct Gadget *octave_low_gadgetp, *octave_hi_gadgetp;
static struct Gadget *ok_gadgetp, *CLI_gadgetp, *next_gadgetp;
static struct Gadget *copyright_gadgetp, *select_read_gadgetp;
static struct Gadget *read_file_gadgetp, *con_read_gadgetp;
static struct Gadget *one_shot_gadgetp, *repeat_only_gadgetp;
static struct Gadget *channel_gadgetp, *rate_gadgetp, *bins_gadgetp;
static struct Gadget *bins_max_gadgetp, *overlap_gadgetp, *pad_gadgetp;
static struct Gadget *interleave_gadgetp, *no_interleave_gadgetp;
static struct Gadget *rectangle_gadgetp, *welch_gadgetp, *parzen_gadgetp;
static struct Gadget *b_h_74db_gadgetp, *triangle_gadgetp;
static struct Gadget *b_h_92db_gadgetp;
static struct Gadget *hamming_gadgetp, *combine_plots_gadgetp;
static struct Gadget *hann_gadgetp, *pink_gadgetp, *def_write_gadgetp;
static struct Gadget *write_file_gadgetp, *cancel_write_gadgetp;
static struct Gadget *low_f_gadgetp, *high_f_gadgetp, *time_segs_gadgetp;
static struct Gadget *plot_gadgetp, *log_x_gadgetp, *log_y_gadgetp;
static struct Gadget *amplitude_gadgetp, *power_gadgetp, *db_gadgetp;
static struct Gadget *mean_gadgetp, *smoothing_gadgetp, *help_gadgetp;
static struct Gadget *no_smoothing_gadgetp, *sq_smoothing_gadgetp;
static struct Gadget *re_plot_gadgetp, *re_output_gadgetp;
static struct Gadget *time3d_gadgetp, *cut_plots_gadgetp;
static struct Gadget *append_gadgetp, *cal_gadgetp;
static struct Gadget *spectrum_select_gadgetp;

APTR *last_address = NULL;

#define DONT_KNOW -1
static ULONG bins_used = DONT_KNOW;
static wb_append_on = FALSE;

/*
 * local functions
 */
static void message__set (int message_number);
static void octave_buttons__set (void);
static void fdep_buttons__enable (void);
static void window_buttons__set (void);
static void read_parameters__set (void);
static void rate_gadget__update (void);
static void amplitude__set (void);
static void bins_gadget__update (BOOLEAN force_update);
static void bins_string__set (char *buffer);
static void high_f_gadget__update (void);
static void low_f_gadget__update (void);
static void do_help (struct Gadget *gadgetp);
static char *wb_set_append (char *arguments);
static char *wb_no_set_append (char *arguments);

int workbench_dialog (void)
{
    struct NewWindow dialog_window;

    int next_action = WORKBENCH_MODE;
    struct IntuiMessage *message;
    APTR *address = NULL;
    ULONG class = NULL;
    UWORD qualifier = NULL;
    UWORD code = NULL;
    APTR next_address = NULL;
    ULONG next_class = NULL;
    UWORD next_code = NULL;
    UWORD next_qualifier = NULL;
    BOOLEAN last_plot;
    char channel_string[20];
    char rate_string[32];
    char bins_string[20];
    char low_f_string[32];
    char high_f_string[32];
    char smoothing_string[20];
    char copyright_string[132];
    int copyright_indent;
    int copyright_delta;
    int last_command_mode = CommandMode;
    APTR string_gadget_being_set = NULL;
    double s_rate = (Rate == AUTO_RATE) ? FileRate : Rate;

    CommandMode = WORKBENCH_MODE;
    last_plot = Plot;
    Plot = ANY_PLOT;

    open_libraries ();

    if (dialog_window_p == NULL)
    {
	/*
	 * Define the Gadgets
	 */

	gadget__begin (NULL);  /* Ensure requester mode off */

	/* Top Row */

	strcpy (copyright_string, NameAuthor);
	strcat (copyright_string, VersionData);
	strcat (copyright_string, " "); /* One space */
	strcat (copyright_string, Copyright);
	copyright_delta = MAX_TEXT_COLS - strlen (copyright_string);
	if (1 & copyright_delta)
	{
	    strcpy (copyright_string, NameAuthor);
	    strcat (copyright_string, VersionData);
	    strcat (copyright_string, "  "); /* Two spaces */
	    strcat (copyright_string, Copyright);
	}
	copyright_delta = MAX_TEXT_COLS - strlen (copyright_string);
	copyright_indent = copyright_delta / 2;
	copyright_gadgetp = text_button__new (copyright_string,
					      copyright_indent, 0);
	copyright_gadgetp->UserData = "Workbench Copyright";

	/* Second Row */

	help_gadgetp = text_button__new ("Help", 0, 1);
	help_gadgetp->UserData = "Workbench Help";

	read_file_gadgetp = string_gadget__new ("Sample File:", ReadName,
						MAX_PATH, 49, 6, 1);
	read_file_gadgetp->UserData = "Workbench Sample-File";

	select_read_gadgetp = text_button__new ("Select", 57, 1);
	select_read_gadgetp->UserData = "Workbench Sample-Select";

	con_read_gadgetp = text_button__new ("con:", 65, 1);
	con_read_gadgetp->UserData = "Workbench Sample-Con:";

	next_gadgetp = text_button__new ("Next", 71, 1);
	next_gadgetp->UserData = "Workbench Next";
	if (num_files <= 0 ||
	    num_files <= file_index + 1)
	{
	    next_gadgetp->Flags |= GADGDISABLED;
	}



	/* Third Row */

	octave_low_gadgetp = radio_button__new ("Low Oct", 0, 2);
	octave_2_gadgetp = radio_button__new ("2", 9, 2);
	octave_3_gadgetp = radio_button__new ("3", 12, 2);
	octave_4_gadgetp = radio_button__new ("4", 15, 2);
	octave_5_gadgetp = radio_button__new ("5", 18, 2);
	octave_6_gadgetp = radio_button__new ("6", 21, 2);
	octave_7_gadgetp = radio_button__new ("7", 24, 2);
	octave_hi_gadgetp = radio_button__new ("Hi", 27, 2);
	octave_buttons__set ();

	octave_low_gadgetp->UserData = "Workbench Octave-Low";
	octave_2_gadgetp->UserData = "Workbench Octave-2";
	octave_3_gadgetp->UserData = "Workbench Octave-3";
	octave_4_gadgetp->UserData = "Workbench Octave-4";
	octave_5_gadgetp->UserData = "Workbench Octave-5";
	octave_6_gadgetp->UserData = "Workbench Octave-6";
	octave_7_gadgetp->UserData = "Workbench Octave-7";
	octave_hi_gadgetp->UserData = "Workbench Octave-Hi";

	one_shot_gadgetp = radio_button__new ("One-Shot Only", 32, 2);
	one_shot_gadgetp->UserData = "Workbench One-Shot-Only";
	if (OneShotOnly) one_shot_gadgetp->Flags |= SELECTED;

	repeat_only_gadgetp = radio_button__new ("Repeat Only", 48, 2);
	repeat_only_gadgetp->UserData = "Workbench Repeat-Only";
	if (RepeatOnly) repeat_only_gadgetp->Flags |= SELECTED;

	sprintf (channel_string,"%d",Channel);
	channel_gadgetp = string_gadget__new ("Channel:", channel_string,
					      3, 14, 61, 2);
	channel_gadgetp->UserData = "Workbench Channel";

	/* Fourth Row */

	
	if (s_rate != AUTO_RATE && s_rate != INVALID_RATE)
	{
	    sprintf (rate_string, "%g", s_rate);
	}
	else
	{
	    rate_string[0] = '\0';
	}
	rate_gadgetp = string_gadget__new ("S Rate:", rate_string,
					   30, 20, 0, 3);
	rate_gadgetp->UserData = "Workbench Rate";

	bins_string__set (bins_string);
	bins_gadgetp = string_gadget__new ("Bins:", bins_string,
					   12, 16, 22, 3);
	bins_gadgetp->UserData = "Workbench Bins";

	bins_max_gadgetp = radio_button__new ("MAX", 40, 3);
	bins_max_gadgetp->UserData = "Workbench Bins-Max";
	if (NumberBins == MAX_BINS) radio_button__in (bins_max_gadgetp, 
						      dialog_window_p);

	overlap_gadgetp = radio_button__new ("Overlap", 46, 3);
	overlap_gadgetp->UserData = "Workbench Overlap";
	if (Overlap) overlap_gadgetp->Flags |= SELECTED;

/*	pad_gadgetp = radio_button__new ("Pad", 56, 3); */
/*	pad_gadgetp->UserData = "Workbench Pad";        */
/*	if (Pad) pad_gadgetp->Flags |= SELECTED;        */

	cal_gadgetp = text_button__new ("Cal & Mag", 56, 3);
	cal_gadgetp->UserData = "Workbench Cal-&-Mag";

	time3d_gadgetp = text_button__new ("3D-Time", 68, 3);
	time3d_gadgetp->UserData = "Workbench 3D-Time";


	/* Fifth Row */

	rectangle_gadgetp = radio_button__new ("Window:   Rectangle", 0, 4);
	triangle_gadgetp = radio_button__new ("Triangle", 21, 4);
	parzen_gadgetp = radio_button__new ("Parzen", 31, 4);
	welch_gadgetp = radio_button__new ("Welch", 39, 4);
	hann_gadgetp = radio_button__new ("Hann", 46, 4);
	hamming_gadgetp = radio_button__new ("Hamming", 52, 4);
	b_h_74db_gadgetp = radio_button__new ("B-H 74dB", 61, 4);
	b_h_92db_gadgetp = radio_button__new ("92dB", 71, 4);
	window_buttons__set ();

	rectangle_gadgetp->UserData = "Workbench Rectangle";
	triangle_gadgetp->UserData = "Workbench Triangle";
	parzen_gadgetp->UserData = "Workbench Parzen";
	welch_gadgetp->UserData = "Workbench Welch";
	hann_gadgetp->UserData = "Workbench Hann";
	hamming_gadgetp->UserData = "Workbench Hamming";
	b_h_74db_gadgetp->UserData = "Workbench 74dB-Blackman-Harris";
	b_h_92db_gadgetp->UserData = "Workbench 92dB-Blackman-Harris";


	/* Sixth Row */


	if (LowFrequency != LOWEST_FREQUENCY)
	{
	    sprintf (low_f_string, "%g", LowFrequency);
	}
	else
	{
	    low_f_string[0] = '\0';
	}
	low_f_gadgetp = string_gadget__new ("Low Freq:", low_f_string,
					    30, 22, 0, 5);
	low_f_gadgetp->UserData = "Workbench Low-Frequency";

	if (HighFrequency != DBL_MAX)
	{
	    sprintf (high_f_string, "%g", HighFrequency);
	}
	else
	{
	    if (s_rate != AUTO_RATE && s_rate != INVALID_RATE)
	    {
		sprintf (high_f_string, "%g", s_rate / 2);
	    }
	    else
	    {
		high_f_string[0] = '\0';
	    }
	}
	high_f_gadgetp = string_gadget__new ("High Freq:", high_f_string,
					     30, 23, 25, 5);
	high_f_gadgetp->UserData = "Workbench High-Frequency";

	no_smoothing_gadgetp = radio_button__new ("NO", 73, 5);
	no_smoothing_gadgetp->UserData = "Workbench Smoothing-No";

	if (SmoothingSegments != INVALID_SMOOTHING &&
	    SmoothingSegments != NO_SMOOTHING)
	{
	    sprintf (smoothing_string, "%ld", SmoothingSegments);
	}
	else 
	{
	    smoothing_string[0] = '\0';
	    if (SmoothingSegments != INVALID_SMOOTHING)
	    {
		radio_button__in (no_smoothing_gadgetp, dialog_window_p);
	    }
	}
	smoothing_gadgetp = string_gadget__new ("Smooth:", smoothing_string,
						12, 17, 50, 5);
	smoothing_gadgetp->UserData = "Workbench Smoothing";

	sq_smoothing_gadgetp = radio_button__new ("Sq", 69, 5);
	sq_smoothing_gadgetp->UserData = "workbench Smoothing-Squared";
	if (SquaredSmoothing) sq_smoothing_gadgetp->Flags |= SELECTED;


	/* Seventh Row */

	plot_gadgetp = radio_button__new ("Plot", 0, 6);
	plot_gadgetp->UserData = "Workbench Plot";
	if (Plot != NO_PLOT) plot_gadgetp->Flags |= SELECTED;

	combine_plots_gadgetp = radio_button__new ("&", 6, 6);
	combine_plots_gadgetp->UserData = "Workbench Plot-Combine";
	if (CombinePlots) combine_plots_gadgetp->Flags |= SELECTED;

	cut_plots_gadgetp = text_button__new ("X", 9, 6);
	cut_plots_gadgetp->UserData = "Workbench Plot-Cut";

	if (is_temp_file (WriteName))
	{
	    write_file_gadgetp = string_gadget__new ("Spectrm File:", 
						     NullString, MAX_PATH, 
						     42, 12, 6);
	}
	else
	{
	    write_file_gadgetp = string_gadget__new ("Spectrm File:", 
						     WriteName, MAX_PATH, 
						     42, 12, 6);
	}
	write_file_gadgetp->UserData = "Workbench Spectrum-File";

	spectrum_select_gadgetp = text_button__new ("S", 56, 6);
	spectrum_select_gadgetp->UserData = 
	  "Workbench Spectrum-File-Select";

	append_gadgetp = radio_button__new ("Open", 59, 6);
        append_gadgetp->UserData = "Workbench Spectrum-File-Open";

	cancel_write_gadgetp = text_button__new ("Can", 65, 6);
	cancel_write_gadgetp->UserData = "Workbench Spectrum-File-Cancel";

	def_write_gadgetp = text_button__new ("*.fft", 70, 6);
	def_write_gadgetp->UserData = "Workbench *.fft";


	/* Eighth Row */

	CLI_gadgetp = text_button__new ("CLI", 0, 7);
	CLI_gadgetp->UserData = "Workbench CLI";

	pink_gadgetp = radio_button__new ("Pink", 6, 7);
	pink_gadgetp->UserData = "Workbench Pink";
	if (Pink) pink_gadgetp->Flags |= SELECTED;

	amplitude_gadgetp = radio_button__new ("Ampl", 13, 7);
	amplitude_gadgetp->UserData = "Workbench Amplitude";

	power_gadgetp = radio_button__new ("Power", 19, 7);
	power_gadgetp->UserData = "Workbench Power";
	amplitude__set ();

	mean_gadgetp = radio_button__new ("Mean", 27, 7);
	mean_gadgetp->UserData = "Workbench Mean";
	if (Mean) mean_gadgetp->Flags |= SELECTED;

	db_gadgetp = radio_button__new ("dB", 34, 7);
	db_gadgetp->UserData = "Workbench dB";
	if (Db) db_gadgetp->Flags |= SELECTED;

	log_x_gadgetp = radio_button__new ("LogX", 39, 7);
	log_x_gadgetp->UserData = "Workbench Logx";

	log_y_gadgetp = radio_button__new ("LogY", 46, 7);
	log_y_gadgetp->UserData = "Workbench Logy";

	re_plot_gadgetp = text_button__new ("RePlot", 53, 7);
	re_plot_gadgetp->UserData = "Workbench RePlot";

	re_output_gadgetp = text_button__new ("ReOutput", 62, 7);
	re_output_gadgetp->UserData = "Workbench ReOutput";

	ok_gadgetp = text_button__new ("OK", 73, 7);
	ok_gadgetp->UserData = "Workbench OK";


	/* Ninth Row */
	/* IMPORTANT!  LAST GADGET MUST APPEAR IN WINDOW INITIALIZATION */

	message_gadgetp = message_gadget__new (75, 0, 8);
	message_gadgetp->UserData = "Workbench Message";

    /*
     * Define the Window
     */
	dialog_window.LeftEdge = WINDOW_LEFT;
	dialog_window.TopEdge = WINDOW_TOP;
	dialog_window.Width = SCREEN_WIDTH - (2 * WINDOW_LEFT);
	dialog_window.Height = SCREEN_HEIGHT - WINDOW_TOP - WINDOW_BOTTOM
	  + Screen_Font_Height - 8;
	dialog_window.DetailPen = 0;
	dialog_window.BlockPen = 1;
	dialog_window.Title = Dialog_Window_Title;
	dialog_window.Flags = SMART_REFRESH | ACTIVATE | WINDOWCLOSE | 
	  WINDOWDRAG | WINDOWDEPTH | NOCAREREFRESH;
	dialog_window.IDCMPFlags = IDCMP_CLOSEWINDOW | IDCMP_GADGETUP |
	  IDCMP_GADGETDOWN | IDCMP_RAWKEY;
	if (Old_Intuition)
	{
	    dialog_window.Type = WBENCHSCREEN;
	}
	else
	{
	    dialog_window.Type = PUBLICSCREEN;  /* It's the latest thing */
	}
	dialog_window.FirstGadget = message_gadgetp;
	dialog_window.Screen = NULL;
	dialog_window.BitMap = NULL;
	dialog_window.MinWidth = 0;
	dialog_window.MinHeight = 0;
	dialog_window.MaxWidth = 0;
	dialog_window.MaxHeight = 0;
	
	/* Try to open the window.  Like the call to OpenLibrary(), if
	 * the OpenWindow call is successful, it returns a pointer to
	 * the structure for your new window.  If the OpenWindow() call
	 * fails, it returns a zero.
	 */
	
	if ((dialog_window_p = (struct Window *) OpenWindow 
	     (&dialog_window)) == NULL )
	{
	    gabort (EXIT_FAILURE);
	}
    }
    if (ReadName && strlen(ReadName))
    {
	read_parameters__set ();
	message__set (SELECT_PARAMETERS);
    }
    else
    {
	message__set (SELECT_READ_FILE);
    }

/* Handle events */

    while (TRUE)
    {
	chkabort ();
	Wait (1 << dialog_window_p->UserPort->mp_SigBit);
	while (message = (struct IntuiMessage *) GetMsg 
	       (dialog_window_p->UserPort))
	{
	    class = message->Class;
	    address = message->IAddress;
	    qualifier = message->Qualifier;
	    code = message->Code;
	    ReplyMsg ((struct Message *) message);
	/*
	 * IDCMP_CLOSEWINDOW doesn't have an IAddress
         * so must be handled here.
         */
	    if (class == IDCMP_CLOSEWINDOW) goto closewindow;
        /*
         * Finish unfinished string
         */

	    if (string_gadget_being_set &&
		string_gadget_being_set != address && !
		(string_gadget_being_set == read_file_gadgetp &&
		 (address == (APTR) select_read_gadgetp) ||
		 (address == (APTR) con_read_gadgetp)))
	    {
		next_address = address;
		next_class = class;
		next_qualifier = qualifier;
		next_code = code;

		address = string_gadget_being_set;
		class = IDCMP_GADGETUP;
		qualifier = NULL;
		code = NULL;
	    }
	    string_gadget_being_set = NULL;

	    while (address)
	    {
		CATCH_ERROR;
	    /*
             * Here begins massive compound if statement
             */
		if (class == IDCMP_RAWKEY)
		{
	        /* Test for HELP key on keyboard...
                 * At present, I'm not happy with the way this works.
		 * If a string gadget is activated, there is no response.
                 * So, I'm leaving it as undocumented, maybe it will
                 *   help someone anyway.  Meanwhile there is a help
                 *   button which does the same thing.
                 */
		    if (code == RAW_HELP_KEY)
		    {
			workbench_help_requester ();
		    }
		} /* end if class == IDCMP_RAWKEY */

		else if (qualifier & IEQUALIFIER_CONTROL)
		{
                /*
                 * CTRL and any gadget select gives help on that gadget
		 */
		    do_help ((struct Gadget *) address);
		}
	    /*
             * Check for string GADGETDOWN
	     */
		else if (class == IDCMP_GADGETDOWN && 
			 ((struct Gadget *) address)->GadgetType &
			 GTYP_STRGADGET)
		{
		    string_gadget_being_set = address;
		}
		else if (address == (APTR) help_gadgetp)
		{
		    workbench_help_requester ();
		}
		else if (address == (APTR) ok_gadgetp)
		{
		    ULONG check_frames = FileFrames;
		    message__set (PERFORMING_FFT);
		    CATCH_ERROR
		    {
			ok (NullString);
			loop_time_message_done (LoopTimeElapsed);
		    }
		    ON_ERROR
		    {
			message__set (REMEMBER_MESSAGE);
		    }
		    END_CATCH_ERROR;

		    if (check_frames != FileFrames)
		    {  /* A file scan has occurred */
			bins_gadget__update (FALSE);
		    }
		}
		else if (address == (APTR) CLI_gadgetp)
		{
		    next_action = INTERACTIVE_MODE;
		    goto closewindow;
		}
		else if (address == (APTR) copyright_gadgetp)
		{
		    copyright_requester ();
		}
		else if (address == (APTR) cal_gadgetp)
		{
		    calibration_requester ();
		}
		else if (address == (APTR) time3d_gadgetp)
		{
		    time3d_requester ();
		    bins_gadget__update (FALSE);
		}
		else if (address == (APTR) rectangle_gadgetp)
		{
		    set_rectangle (NullString);
		    window_buttons__set ();
		}
		else if (address == (APTR) triangle_gadgetp)
		{
		    set_triangle (NullString);
		    window_buttons__set ();
		}
		else if (address == (APTR) parzen_gadgetp)
		{
		    set_parzen (NullString);
		    window_buttons__set ();
		}
		else if (address == (APTR) welch_gadgetp)
		{
		    set_welch (NullString);
		    window_buttons__set ();
		}
		else if (address == (APTR) hann_gadgetp)
		{
		    set_hann (NullString);
		    window_buttons__set ();
		}
		else if (address == (APTR) hamming_gadgetp)
		{
		    set_hamming (NullString);
		    window_buttons__set ();
		}
		else if (address == (APTR) b_h_74db_gadgetp)
		{
		    set_blackman_harris_74db (NullString);
		    window_buttons__set ();
		}
		else if (address == (APTR) b_h_92db_gadgetp)
		{
		    set_blackman_harris_92db (NullString);
		    window_buttons__set ();
		}
		else if (address == (APTR) one_shot_gadgetp)
		{
		    if (one_shot_gadgetp->Flags & SELECTED)
		    {
			set_no_one_shot_only (NullString);
			radio_button__out (one_shot_gadgetp,
					   dialog_window_p);
		    }
		    else
		    {
			set_one_shot_only (NullString);
			radio_button__in (one_shot_gadgetp,
					  dialog_window_p);
			radio_button__out (repeat_only_gadgetp,
					   dialog_window_p);
		    }
		    bins_gadget__update (FALSE);
		}
		else if (address == (APTR) repeat_only_gadgetp)
		{
		    if (repeat_only_gadgetp->Flags & SELECTED)
		    {
			set_no_repeat_only (NullString);
			radio_button__out (repeat_only_gadgetp,
					   dialog_window_p);
		    }
		    else
		    {
			set_repeat_only (NullString);
			radio_button__in (repeat_only_gadgetp,
					  dialog_window_p);
			radio_button__out (one_shot_gadgetp,
					   dialog_window_p);
		    }
		    bins_gadget__update (FALSE);
		}
/*		else if (address == (APTR) pad_gadgetp) */
/*		{ */
/*		    radio_button__toggle (pad_gadgetp, set_pad,  */
/*					   set_no_pad, */
/*					   dialog_window_p); */
/*		    bins_gadget__update (FALSE); */
/*		} */
		else if (address == (APTR) channel_gadgetp)
		{
		    char *buffer = string_gadget__apply 
		      (channel_gadgetp, set_channel);
		    if (!strlen (buffer))
		    {
			sprintf (buffer, "%d", Channel);
			refresh_gadget (channel_gadgetp,
					dialog_window_p);
		    }
		}
		else if (address == (APTR) rate_gadgetp)
		{
		    char *buffer = string_gadget__apply 
		      (rate_gadgetp, set_rate);
		    if (!strlen (buffer))
		    {
			rate_gadget__update ();
		    }
		    high_f_gadget__update ();
		    message__set (SELECT_PARAMETERS);
		}
		else if (address == (APTR) bins_max_gadgetp)
		{
		    set_bins (NullString);
		    radio_button__in (bins_max_gadgetp,
				      dialog_window_p);
		    bins_gadget__update (TRUE);  /* Force */
		}
		else if (address == (APTR) bins_gadgetp)
		{
		    char *buffer = string_gadget__apply 
		      (bins_gadgetp, set_bins);
		    if (strlen (buffer))
		    {
			radio_button__out (bins_max_gadgetp,
					   dialog_window_p);
		    }
		    else
		    {
			radio_button__in (bins_max_gadgetp,
					  dialog_window_p);
		    }
		    
		    bins_gadget__update (TRUE);  /* Force */
		}
		else if (address == (APTR) no_smoothing_gadgetp)
		{
		    struct StringInfo *string_infop = 
		      smoothing_gadgetp->SpecialInfo;
		    char *buffer = string_infop->Buffer;
		    buffer[0] = '\0';
		    refresh_gadget (smoothing_gadgetp,
				    dialog_window_p);
		    radio_button__in (no_smoothing_gadgetp,
				      dialog_window_p);
		    set_smoothing_segments (NullString);
		}
		else if (address == (APTR) smoothing_gadgetp)
		{
		    char *buffer = string_gadget__apply 
		      (smoothing_gadgetp, set_smoothing_segments);
		    if (strlen (buffer))
		    {
			radio_button__out (no_smoothing_gadgetp,
					   dialog_window_p);
		    }
		    else
		    {
			radio_button__in (no_smoothing_gadgetp,
					  dialog_window_p);
		    }
		    
		}
		else if (address == (APTR) sq_smoothing_gadgetp)
		{
		    radio_button__toggle (sq_smoothing_gadgetp,
					   set_squared_smoothing, 
					   set_no_squared_smoothing,
					   dialog_window_p);
		}
		else if (address == (APTR) octave_low_gadgetp)
		{
		    Octave = 1;
		    octave_buttons__set ();
		    bins_gadget__update (FALSE);
		}
		else if (address == (APTR) octave_2_gadgetp)
		{
		    Octave = 2;
		    octave_buttons__set ();
		    bins_gadget__update (FALSE);
		}
		else if (address == (APTR) octave_3_gadgetp)
		{
		    Octave = 3;
		    octave_buttons__set ();
		    bins_gadget__update (FALSE);
		}
		else if (address == (APTR) octave_4_gadgetp)
		{
		    Octave = 4;
		    octave_buttons__set ();
		    bins_gadget__update (FALSE);
		}
		else if (address == (APTR) octave_5_gadgetp)
		{
		    Octave = 5;
		    octave_buttons__set ();
		    bins_gadget__update (FALSE);
		}
		else if (address == (APTR) octave_6_gadgetp)
		{
		    Octave = 6;
		    octave_buttons__set ();
		    bins_gadget__update (FALSE);
		}
		else if (address == (APTR) octave_7_gadgetp)
		{
		    Octave = 7;
		    octave_buttons__set ();
		    bins_gadget__update (FALSE);
		}
		else if (address == (APTR) octave_hi_gadgetp)
		{
		    Octave = 0;
		    octave_buttons__set ();
		    bins_gadget__update (FALSE);
		}
		else if (address == (APTR) next_gadgetp)
		{
		    next_action = NEXT_FILE;
		    goto closewindow;
		}
		else if (address == (APTR) select_read_gadgetp)
		{
		    char *read_file = file_requestor 
		      ("Select Sample File");
		    if (read_file)
		    {
			string_gadget__reset (read_file_gadgetp, read_file,
					      dialog_window_p);
			name_string_gadget__apply (read_file_gadgetp,
						   set_read);
			read_parameters__set ();
		    }
		    else
		    {
			message__set (SELECT_READ_FILE);
		    }
		    gfree (read_file);
		}
		else if (address == (APTR) con_read_gadgetp)
		{
		    set_read ("con:");
		    string_gadget__reset (read_file_gadgetp, "con:",
					  dialog_window_p);
		    read_parameters__set ();
		}
		else if (address == (APTR) read_file_gadgetp)
		{
		    CATCH_ERROR
		    {
			name_string_gadget__apply (read_file_gadgetp, 
					      set_read);
			read_parameters__set ();
		    }
		    ON_ERROR
		    {
			message__set (SELECT_READ_FILE);
		    }
		    END_CATCH_ERROR;
		}
		else if (address == (APTR) low_f_gadgetp)
		{
		    char *buffer = string_gadget__apply 
		      (low_f_gadgetp, set_low_frequency);
		    if (!strlen (buffer))
		    {
			low_f_gadget__update ();
		    }
		}
		else if (address == (APTR) high_f_gadgetp)
		{
		    char *buffer = string_gadget__apply 
		      (high_f_gadgetp, set_high_frequency);
		    if (!strlen (buffer))
		    {
			high_f_gadget__update ();
		    }
		}
		else if (address == (APTR) write_file_gadgetp)
		{
		    if (wb_append_on)
		    {
			name_string_gadget__apply (write_file_gadgetp,
						   set_append);
		    }
		    else
		    {
			name_string_gadget__apply (write_file_gadgetp,
						   set_write);
		    }
		}
		else if (address == (APTR) spectrum_select_gadgetp)
		{
		    char *spectrum_file = file_requestor
		      ("Select Spectrum File");
		    if (spectrum_file)
		    {
			/* default now to append mode; usually helps */
			radio_button__in (append_gadgetp, dialog_window_p);
			wb_set_append (NullString);
			string_gadget__reset (write_file_gadgetp, 
					      spectrum_file,
					      dialog_window_p);
			if (wb_append_on)
			{
			    name_string_gadget__apply (write_file_gadgetp,
						       set_append);
			}
			else
			{
			    name_string_gadget__apply (write_file_gadgetp,
						       set_write);
			}
		    }
		}
		else if (address == (APTR) append_gadgetp)
		{
		    radio_button__toggle (append_gadgetp,
					   wb_set_append, wb_no_set_append,
					   dialog_window_p);
		}
		else if (address == (APTR) cancel_write_gadgetp)
		{
		    string_gadget__reset (write_file_gadgetp,
					  NullString, dialog_window_p);
		    if (wb_append_on)
		    {
			set_append (NullString);
		    }
		    else
		    {
			set_write (NullString);
		    }
		}
		else if (address == (APTR) def_write_gadgetp)
		{
		    struct StringInfo *string_infop = 
		      write_file_gadgetp->SpecialInfo;
		    char *buffer = string_infop->Buffer;
		    if (ReadName)
		    {
			strcpy (buffer, ReadName);
			strcat (buffer, ".fft");
		    }
		    else
		    {
			strcpy (buffer, ".fft");
		    }
		    string_infop->NumChars = strlen (buffer);
		    if (wb_append_on)
		    {
			name_string_gadget__apply (write_file_gadgetp,
						   set_append);
		    }
		    else
		    {
			name_string_gadget__apply (write_file_gadgetp,
						   set_write);
		    }
		    refresh_gadget (write_file_gadgetp, 
				    dialog_window_p);
		}
		else if (address == (APTR) amplitude_gadgetp)
		{
		    set_amplitude (NullString);
		    amplitude__set ();
		}
		else if (address == (APTR) power_gadgetp)
		{
		    set_power (NullString);
		    amplitude__set ();
		}
		else if (address == (APTR) overlap_gadgetp)
		{
		    radio_button__toggle (overlap_gadgetp,
					   set_overlap, set_no_overlap,
					   dialog_window_p);
		}
		else if (address == (APTR) db_gadgetp)
		{
		    radio_button__toggle (db_gadgetp, 
					   set_db, set_no_db,
					   dialog_window_p);
		}
		else if (address == (APTR) pink_gadgetp)
		{
		    radio_button__toggle (pink_gadgetp, set_pink, 
					   set_no_pink,
					   dialog_window_p);
		}
		else if (address == (APTR) mean_gadgetp)
		{
		    radio_button__toggle (mean_gadgetp, set_mean, 
					   set_sum, 
					   dialog_window_p);
		}
		else if (address == (APTR) plot_gadgetp)
		{
		    radio_button__toggle (plot_gadgetp, set_plot, 
					   set_no_plot,
					   dialog_window_p);
		    last_plot = Plot;
		}
		else if (address == (APTR) combine_plots_gadgetp)
		{
		    radio_button__toggle (combine_plots_gadgetp,
					   set_combine_plots,
					   set_no_combine_plots,
					   dialog_window_p);
		}
		else if (address == (APTR) cut_plots_gadgetp)
		{
		    cut_combined_plot (NullString);
		}
		else if (address == (APTR) re_output_gadgetp)
		{
		    re_output (NullString);
		}
		else if (address == (APTR) re_plot_gadgetp)
		{
		    re_plot (NullString);
		}
		else if (address == (APTR) log_x_gadgetp)
		{
		    radio_button__toggle (log_x_gadgetp, set_logx, 
					   set_no_logx,
					   dialog_window_p);
		}
		else if (address == (APTR) log_y_gadgetp)
		{
		    radio_button__toggle (log_y_gadgetp, set_logy,
					   set_no_logy,
					   dialog_window_p);
		} /* end massive compound if*/
		END_CATCH_ERROR;

		last_address = address;
		address = next_address;
		class = next_class;
		qualifier = next_qualifier;
		code = next_code;

		next_address = NULL;
		next_class = NULL;
		next_qualifier = NULL;
		next_code = NULL;

	    } /* end while (address) */
	} /* end while (message...) */
    } /* end while (TRUE) */

closewindow:    
    CloseWindow (dialog_window_p);
    dialog_window_p = NULL;
    if (class == IDCMP_CLOSEWINDOW)
    {
	quit (NullString);  /* it all ends here, no matter where started */
    }
    CommandMode = last_command_mode;
    Plot = last_plot;  /* reset previous default, unless changed */
    return next_action;
}

static void amplitude__set (void)
{
    if (Amplitude)
    {
	radio_button__in (amplitude_gadgetp, dialog_window_p);
	radio_button__out (power_gadgetp, dialog_window_p);
    }
    else
    {
	radio_button__in (power_gadgetp, dialog_window_p);
	radio_button__out (amplitude_gadgetp, dialog_window_p);
    }
}


static void window_buttons__set ()
{
    switch (WindowType)
    {
    case RECTANGLE_WINDOW:
	radio_button__in (rectangle_gadgetp, dialog_window_p);
	radio_button__out (triangle_gadgetp, dialog_window_p);
	radio_button__out (parzen_gadgetp, dialog_window_p);
	radio_button__out (welch_gadgetp, dialog_window_p);
	radio_button__out (hann_gadgetp, dialog_window_p);
	radio_button__out (hamming_gadgetp, dialog_window_p);
	radio_button__out (b_h_74db_gadgetp, dialog_window_p);
	radio_button__out (b_h_92db_gadgetp, dialog_window_p);
	break;
    case TRIANGLE_WINDOW:
	radio_button__out (rectangle_gadgetp, dialog_window_p);
	radio_button__in (triangle_gadgetp, dialog_window_p);
	radio_button__out (parzen_gadgetp, dialog_window_p);
	radio_button__out (welch_gadgetp, dialog_window_p);
	radio_button__out (hann_gadgetp, dialog_window_p);
	radio_button__out (hamming_gadgetp, dialog_window_p);
	radio_button__out (b_h_74db_gadgetp, dialog_window_p);
	radio_button__out (b_h_92db_gadgetp, dialog_window_p);
	break;
    case PARZEN_WINDOW:
	radio_button__out (rectangle_gadgetp, dialog_window_p);
	radio_button__out (triangle_gadgetp, dialog_window_p);
	radio_button__in (parzen_gadgetp, dialog_window_p);
	radio_button__out (welch_gadgetp, dialog_window_p);
	radio_button__out (hann_gadgetp, dialog_window_p);
	radio_button__out (hamming_gadgetp, dialog_window_p);
	radio_button__out (b_h_74db_gadgetp, dialog_window_p);
	radio_button__out (b_h_92db_gadgetp, dialog_window_p);
	break;
    case WELCH_WINDOW:
	radio_button__out (rectangle_gadgetp, dialog_window_p);
	radio_button__out (triangle_gadgetp, dialog_window_p);
	radio_button__out (parzen_gadgetp, dialog_window_p);
	radio_button__in (welch_gadgetp, dialog_window_p);
	radio_button__out (hann_gadgetp, dialog_window_p);
	radio_button__out (hamming_gadgetp, dialog_window_p);
	radio_button__out (b_h_74db_gadgetp, dialog_window_p);
	radio_button__out (b_h_92db_gadgetp, dialog_window_p);
	break;
    case HANN_WINDOW:
	radio_button__out (rectangle_gadgetp, dialog_window_p);
	radio_button__out (triangle_gadgetp, dialog_window_p);
	radio_button__out (parzen_gadgetp, dialog_window_p);
	radio_button__out (welch_gadgetp, dialog_window_p);
	radio_button__in (hann_gadgetp, dialog_window_p);
	radio_button__out (hamming_gadgetp, dialog_window_p);
	radio_button__out (b_h_74db_gadgetp, dialog_window_p);
	radio_button__out (b_h_92db_gadgetp, dialog_window_p);
	break;
    case HAMMING_WINDOW:
	radio_button__out (rectangle_gadgetp, dialog_window_p);
	radio_button__out (triangle_gadgetp, dialog_window_p);
	radio_button__out (parzen_gadgetp, dialog_window_p);
	radio_button__out (welch_gadgetp, dialog_window_p);
	radio_button__out (hann_gadgetp, dialog_window_p);
	radio_button__in (hamming_gadgetp, dialog_window_p);
	radio_button__out (b_h_74db_gadgetp, dialog_window_p);
	radio_button__out (b_h_92db_gadgetp, dialog_window_p);
	break;
    case BLACKMAN_HARRIS_74DB_WINDOW:
	radio_button__out (rectangle_gadgetp, dialog_window_p);
	radio_button__out (triangle_gadgetp, dialog_window_p);
	radio_button__out (parzen_gadgetp, dialog_window_p);
	radio_button__out (welch_gadgetp, dialog_window_p);
	radio_button__out (hann_gadgetp, dialog_window_p);
	radio_button__out (hamming_gadgetp, dialog_window_p);
	radio_button__in (b_h_74db_gadgetp, dialog_window_p);
	radio_button__out (b_h_92db_gadgetp, dialog_window_p);
	break;
    case BLACKMAN_HARRIS_92DB_WINDOW:
	radio_button__out (rectangle_gadgetp, dialog_window_p);
	radio_button__out (triangle_gadgetp, dialog_window_p);
	radio_button__out (parzen_gadgetp, dialog_window_p);
	radio_button__out (welch_gadgetp, dialog_window_p);
	radio_button__out (hann_gadgetp, dialog_window_p);
	radio_button__out (hamming_gadgetp, dialog_window_p);
	radio_button__out (b_h_74db_gadgetp, dialog_window_p);
	radio_button__in (b_h_92db_gadgetp, dialog_window_p);
	break;
    }
}

static void fdep_buttons__enable (void)
{
    BOOLEAN changed_value = FALSE;
    BOOLEAN no_one_shot_frames = TRUE;
    BOOLEAN no_repeat_frames = TRUE;

    if (!ReadPtr) return;

    switch (FileOctaves)
    {
    case 7:
	gadget__enable (octave_7_gadgetp, dialog_window_p);
    case 6:
	gadget__enable (octave_6_gadgetp, dialog_window_p);
    case 5:
	gadget__enable (octave_5_gadgetp, dialog_window_p);
    case 4:
	gadget__enable (octave_4_gadgetp, dialog_window_p);
    case 3:
	gadget__enable (octave_3_gadgetp, dialog_window_p);
    case 2:
	gadget__enable (octave_2_gadgetp, dialog_window_p);
    }

    switch (FileOctaves)
    {
    case 1:
	gadget__disable (octave_2_gadgetp, dialog_window_p);
    case 2:
	gadget__disable (octave_3_gadgetp, dialog_window_p);
    case 3:
	gadget__disable (octave_4_gadgetp, dialog_window_p);
    case 4:
	gadget__disable (octave_5_gadgetp, dialog_window_p);
    case 5:
	gadget__disable (octave_6_gadgetp, dialog_window_p);
    case 6:
	gadget__disable (octave_7_gadgetp, dialog_window_p);
    }

    if (FileChannels > 1)
    {
	gadget__enable  (channel_gadgetp, dialog_window_p);
    }
    else
    {
	if (Channel > 1)
	{
	    set_channel (NullString);
	    string_gadget__reset (channel_gadgetp, "1", dialog_window_p);
	    changed_value = TRUE;
	}
	gadget__disable (channel_gadgetp, dialog_window_p);
    }

    if (FileFormat == ID_8SVX)
    {
	if (FileOneShotHiFrames)
	{
	    gadget__enable (one_shot_gadgetp, dialog_window_p);
	    no_one_shot_frames = FALSE;
	}
	if (FileRepeatHiFrames)
	{
	    gadget__enable (repeat_only_gadgetp, dialog_window_p);
	    no_repeat_frames = FALSE;
	}
    }
    if (no_one_shot_frames)
    {
	if (OneShotOnly)
	{
	    set_no_one_shot_only (NullString);
	    radio_button__out (one_shot_gadgetp, dialog_window_p);
	    changed_value = TRUE;
	}
	gadget__disable (one_shot_gadgetp, dialog_window_p);
    }
    if (no_repeat_frames)
    {
	if (RepeatOnly)
	{
	    set_no_repeat_only (NullString);
	    radio_button__out (repeat_only_gadgetp, dialog_window_p);
	    changed_value = TRUE;
	}
	gadget__disable (repeat_only_gadgetp, dialog_window_p);
    }

    if (changed_value)
    {
	error_message (CHANGED_FORMAT_DEP_VALUE);
    }
}


static void octave_buttons__set (void)
{
    switch (Octave)
    {
    case 0:
	radio_button__in (octave_hi_gadgetp, dialog_window_p);
	radio_button__out (octave_low_gadgetp, dialog_window_p);
	radio_button__out (octave_2_gadgetp, dialog_window_p);
	radio_button__out (octave_3_gadgetp, dialog_window_p);
	radio_button__out (octave_4_gadgetp, dialog_window_p);
	radio_button__out (octave_5_gadgetp, dialog_window_p);
	radio_button__out (octave_6_gadgetp, dialog_window_p);
	radio_button__out (octave_7_gadgetp, dialog_window_p);
	break;
    case 1:
	radio_button__out (octave_hi_gadgetp, dialog_window_p);
	radio_button__in (octave_low_gadgetp, dialog_window_p);
	radio_button__out (octave_2_gadgetp, dialog_window_p);
	radio_button__out (octave_3_gadgetp, dialog_window_p);
	radio_button__out (octave_4_gadgetp, dialog_window_p);
	radio_button__out (octave_5_gadgetp, dialog_window_p);
	radio_button__out (octave_6_gadgetp, dialog_window_p);
	radio_button__out (octave_7_gadgetp, dialog_window_p);
	break;
    case 2:
	radio_button__out (octave_hi_gadgetp, dialog_window_p);
	radio_button__out (octave_low_gadgetp, dialog_window_p);
	radio_button__in (octave_2_gadgetp, dialog_window_p);
	radio_button__out (octave_3_gadgetp, dialog_window_p);
	radio_button__out (octave_4_gadgetp, dialog_window_p);
	radio_button__out (octave_5_gadgetp, dialog_window_p);
	radio_button__out (octave_6_gadgetp, dialog_window_p);
	radio_button__out (octave_7_gadgetp, dialog_window_p);
	break;
    case 3:
	radio_button__out (octave_hi_gadgetp, dialog_window_p);
	radio_button__out (octave_low_gadgetp, dialog_window_p);
	radio_button__out (octave_2_gadgetp, dialog_window_p);
	radio_button__in (octave_3_gadgetp, dialog_window_p);
	radio_button__out (octave_4_gadgetp, dialog_window_p);
	radio_button__out (octave_5_gadgetp, dialog_window_p);
	radio_button__out (octave_6_gadgetp, dialog_window_p);
	radio_button__out (octave_7_gadgetp, dialog_window_p);
	break;
    case 4:
	radio_button__out (octave_hi_gadgetp, dialog_window_p);
	radio_button__out (octave_low_gadgetp, dialog_window_p);
	radio_button__out (octave_2_gadgetp, dialog_window_p);
	radio_button__out (octave_3_gadgetp, dialog_window_p);
	radio_button__in (octave_4_gadgetp, dialog_window_p);
	radio_button__out (octave_5_gadgetp, dialog_window_p);
	radio_button__out (octave_6_gadgetp, dialog_window_p);
	radio_button__out (octave_7_gadgetp, dialog_window_p);
	break;
    case 5:
	radio_button__out (octave_hi_gadgetp, dialog_window_p);
	radio_button__out (octave_low_gadgetp, dialog_window_p);
	radio_button__out (octave_2_gadgetp, dialog_window_p);
	radio_button__out (octave_3_gadgetp, dialog_window_p);
	radio_button__out (octave_4_gadgetp, dialog_window_p);
	radio_button__in (octave_5_gadgetp, dialog_window_p);
	radio_button__out (octave_6_gadgetp, dialog_window_p);
	radio_button__out (octave_7_gadgetp, dialog_window_p);
	break;
    case 6:
	radio_button__out (octave_hi_gadgetp, dialog_window_p);
	radio_button__out (octave_low_gadgetp, dialog_window_p);
	radio_button__out (octave_2_gadgetp, dialog_window_p);
	radio_button__out (octave_3_gadgetp, dialog_window_p);
	radio_button__out (octave_4_gadgetp, dialog_window_p);
	radio_button__out (octave_5_gadgetp, dialog_window_p);
	radio_button__in (octave_6_gadgetp, dialog_window_p);
	radio_button__out (octave_7_gadgetp, dialog_window_p);
	break;
    case 7:
	radio_button__out (octave_hi_gadgetp, dialog_window_p);
	radio_button__out (octave_low_gadgetp, dialog_window_p);
	radio_button__out (octave_2_gadgetp, dialog_window_p);
	radio_button__out (octave_3_gadgetp, dialog_window_p);
	radio_button__out (octave_4_gadgetp, dialog_window_p);
	radio_button__out (octave_5_gadgetp, dialog_window_p);
	radio_button__out (octave_6_gadgetp, dialog_window_p);
	radio_button__in (octave_7_gadgetp, dialog_window_p);
	break;
    }
}

static void read_parameters__set (void)
{
    if (!ReadName || ReadName == NullString) return;
    fdep_buttons__enable ();
    rate_gadget__update ();
    bins_gadget__update (FALSE);
/*    low_f_gadget__update (); now included in bins_gadget__update... */
    high_f_gadget__update ();
    if (FileFormat != UNFORMATTED)
    {
	message__set (SELECT_PARAMETERS);
    }
    else if (Rate == AUTO_RATE)
    {
	message__set (SET_RATE);
    }
    else
    {
	message__set (VERIFY_RATE);
    }
}

static void rate_gadget__update (void)
{
    double rate;
    struct StringInfo *string_infop = 
      rate_gadgetp->SpecialInfo;
    char *buffer = string_infop->Buffer;

    rate = (Rate == AUTO_RATE) ? FileRate : Rate;
    if (rate != AUTO_RATE)
    {
        sprintf (buffer,"%g",rate);
    }
    else
    {
	buffer[0] = '\0';
    }
    refresh_gadget (rate_gadgetp, dialog_window_p);
}


static void high_f_gadget__update (void)
{
    double rate;
    struct StringInfo *string_infop = 
      high_f_gadgetp->SpecialInfo;
    char *buffer = string_infop->Buffer;

    if (HighFrequency == DBL_MAX)
    {
        rate = (Rate == AUTO_RATE) ? FileRate : Rate;
	if (rate != AUTO_RATE)
	{
	    sprintf (buffer, "%g", rate / (2 * Interleave));
	    refresh_gadget (high_f_gadgetp, dialog_window_p);
	}
    }
}

static void low_f_gadget__update (void)
{
    double rate;
    struct StringInfo *string_infop = 
      low_f_gadgetp->SpecialInfo;
    char *buffer = string_infop->Buffer;
    BOOLEAN reset = FALSE;

    if (LowFrequency == LOWEST_FREQUENCY)
    {
        rate = (Rate == AUTO_RATE) ? FileRate : Rate;
	if (rate != AUTO_RATE)
	{

/*
 * Ahah, a beautiful divide by zero handling
 * if TRAP_TEST defined, just enter 0 into Bins gadget
 * Unfortunately, ffp math forces a trap on divide by zero, and
 * this cannot be intercepted without writing assembly code.  Damn.
 */

/* #define TRAP_TEST */

#ifdef TRAP_TEST
	    if (bins_used != DONT_KNOW)  /* May divide by 0 */
#else
	    if (bins_used != DONT_KNOW && bins_used > 0) /* Safe */
#endif

	    {
		double low_f = rate / (2.0 * bins_used * Interleave);
		sprintf (buffer, "%g", low_f);
		reset = TRUE;
	    }
	}
	if (!reset)
	{
	    buffer[0] = '\0';
	}
	refresh_gadget (low_f_gadgetp, dialog_window_p);
    }
}

static void bins_gadget__update (BOOLEAN force_update)
{
    struct StringInfo *string_infop;
    char *buffer;

    if (NumberBins == MAX_BINS || force_update)
    {
	string_infop = bins_gadgetp->SpecialInfo;
	buffer = string_infop->Buffer;
	bins_string__set (buffer);
	refresh_gadget (bins_gadgetp, dialog_window_p);
    }
    low_f_gadget__update ();
}

static void bins_string__set (char *buffer)
{
    BOOLEAN done = FALSE;

    if (NumberBins != MAX_BINS)
    {
	bins_used = NumberBins;
	sprintf (buffer, "%ld", NumberBins);
	done = TRUE;
    }
    else if (ReadPtr != NULL && ReadPtr != stdin)
    {
        BOOLEAN save_quiet = Quiet;
	Quiet = TRUE;

	bins_used = DONT_KNOW;
	CATCH_ERROR;
	bins_used = do_ok (FALSE);
	sprintf (buffer, "%ld", bins_used);
	done = TRUE;
	END_CATCH_ERROR;

	Quiet = save_quiet;
    }
    if (!done) buffer[0] = '\0';
}


static void message__set (int message_number)
{
    static int remember_message = SELECT_PARAMETERS;

    switch (message_number)
    {
    case SELECT_READ_FILE:
	message_gadget__write (message_gadgetp,
			       Please_Select_Read_File_S,
			       dialog_window_p);
	ActivateGadget (read_file_gadgetp, dialog_window_p, NULL);
	remember_message = SELECT_READ_FILE;
	break;
    case SELECT_PARAMETERS:
	message_gadget__write (message_gadgetp,
			       Set_Parameters_Then_Ok_S,
			       dialog_window_p);
	remember_message = SELECT_PARAMETERS;
	break;
    case PERFORMING_FFT:
	message_gadget__write (message_gadgetp,
			       Attempting_to_Perform_FFT_S,
			       dialog_window_p);
	break;
    case SET_RATE:
	message_gadget__write (message_gadgetp,
			       Set_Sampling_Rate_S,
			       dialog_window_p);
	remember_message = SET_RATE;
	ActivateGadget (rate_gadgetp, dialog_window_p, NULL);
	break;
    case VERIFY_RATE:
	message_gadget__write (message_gadgetp,
			       Verify_Sampling_Rate_S,
			       dialog_window_p);
	ActivateGadget (rate_gadgetp, dialog_window_p, NULL);
	remember_message = VERIFY_RATE;
	break;
    case REMEMBER_MESSAGE:
	message__set (remember_message);
	break;
    }
}

static void do_help (struct Gadget *gadgetp)
{
    if (gadgetp->UserData)
    {
	help_message ( (char *) gadgetp->UserData);
    }
    else
    {
	error_message (HELP_MESSAGE_UNAVAILABLE);
    }
}

static char *wb_set_append (char *arguments)
{
    wb_append_on = TRUE;
    name_string_gadget__apply (write_file_gadgetp, set_append);
    return arguments;
}

static char *wb_no_set_append (char *arguments)
{
    wb_append_on = FALSE;
    if (last_address != (APTR) spectrum_select_gadgetp)
    {
	string_gadget__reset (write_file_gadgetp, NullString,
			      dialog_window_p);
    }
    name_string_gadget__apply (write_file_gadgetp, set_write);
    return arguments;
}







/*
 * Obsolete Gadgets (not enough room; deemed less important)
 */
#if FALSE
    char ileave_string[20];

	no_interleave_gadgetp = radio_button__new ("NO", 73, 3);
	if (Interleave > 1 && Interleave != INVALID_INTERLEAVE)
	{
	    sprintf (ileave_string, "%ld", Interleave);
	}
	else
	{
	    ileave_string[0] = '\0';
	    if (Interleave != INVALID_INTERLEAVE)
	    {
		radio_button__in (no_interleave_gadgetp, dialog_window_p);
	    }
	}
	interleave_gadgetp = string_gadget__new ("Interleave:", 
						 ileave_string,
						 12, 20, 51, 3);

			else if (address == (APTR) no_interleave_gadgetp)
			{
			    struct StringInfo *string_infop = 
			      interleave_gadgetp->SpecialInfo;
			    char *buffer = string_infop->Buffer;
			    buffer[0] = '\0';
			    refresh_gadget (interleave_gadgetp,
					    dialog_window_p);
			    radio_button__in (no_interleave_gadgetp, 
					      dialog_window_p);
			    set_no_interleave (NullString);
			    bins_gadget__update (FALSE);
			}
			else if (address == (APTR) interleave_gadgetp)
			{
			    CATCH_ERROR
			    {
				string_gadget__apply (interleave_gadgetp,
						      set_interleave);
			    }
			    END_CATCH_ERROR;
			    radio_button__out (no_interleave_gadgetp,
					       dialog_window_p);
			    bins_gadget__update (FALSE);
			}

#endif /* obsolete section */


#endif  /* ifdef AMIGA */

