/******************************************************************************
 JXPlotRangeDialog.cc

	BASE CLASS = JXDialogDirector

	Copyright @ 1999 by Glenn W. Bach. All rights reserved.

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

#include "JXPlotRangeDialog.h"
#include <JXWindow.h>
#include <JXTextButton.h>
#include <JXFloatInput.h>
#include <JXStaticText.h>
#include <JXColormap.h>

#include <JMinMax.h>

#include <jAssert.h>

/******************************************************************************
 Constructor

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

JXPlotRangeDialog::JXPlotRangeDialog
	(
	JXWindowDirector* supervisor, 
	const JFloat xMax,
	const JFloat xMin,
	const JFloat yMax,
	const JFloat yMin
	)
	:
	JXDialogDirector(supervisor, kTrue)
{
	BuildWindow();
	itsXMax->SetValue(xMax);
	itsXMin->SetValue(xMin);
	itsYMax->SetValue(yMax);
	itsYMin->SetValue(yMin);

	itsClearRangeFlag = kFalse;
}

/******************************************************************************
 Destructor

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

JXPlotRangeDialog::~JXPlotRangeDialog()
{
}

/******************************************************************************
 BuildWindow (private)

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

void
JXPlotRangeDialog::BuildWindow()
{
	
// begin JXLayout

    JXWindow* window = new JXWindow(this, 390,110, "");
    assert( window != NULL );
    SetWindow(window);

    JXStaticText* obj1 =
        new JXStaticText("X Range:", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 10,15, 65,20);
    assert( obj1 != NULL );

    JXStaticText* obj2 =
        new JXStaticText("to:", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 205,15, 35,20);
    assert( obj2 != NULL );

    JXStaticText* obj3 =
        new JXStaticText("Y Range:", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 10,50, 65,20);
    assert( obj3 != NULL );

    JXStaticText* obj4 =
        new JXStaticText("to:", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 205,50, 35,20);
    assert( obj4 != NULL );

    JXTextButton* okButton =
        new JXTextButton("OK", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 285,80, 70,20);
    assert( okButton != NULL );
    okButton->SetShortcuts("^M");

    JXTextButton* cancelButton =
        new JXTextButton("Cancel", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 45,80, 70,20);
    assert( cancelButton != NULL );
    cancelButton->SetShortcuts("^[");

    itsXMin =
        new JXFloatInput(window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 80,15, 120,20);
    assert( itsXMin != NULL );

    itsXMax =
        new JXFloatInput(window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 250,15, 120,20);
    assert( itsXMax != NULL );

    itsYMin =
        new JXFloatInput(window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 80,50, 120,20);
    assert( itsYMin != NULL );

    itsYMax =
        new JXFloatInput(window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 250,50, 120,20);
    assert( itsYMax != NULL );

	itsClearButton =
		new JXTextButton("Clear", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 160,80, 70,20);
    assert( itsClearButton != NULL );

// end JXLayout

	window->SetTitle("Edit Plot Range");
	SetButtons(okButton, cancelButton);
	ListenTo(itsClearButton);
}

/******************************************************************************
 GetRangeValues (public)

	Returns kTrue if the range should not be cleared.

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

JBoolean
JXPlotRangeDialog::GetRangeValues
	(
	JFloat* xMax, 
	JFloat* xMin, 
	JFloat* yMax, 
	JFloat* yMin
	)
{
	JFloat min;
	JFloat max;
	itsXMax->GetValue(&max);
	itsXMin->GetValue(&min);
	*xMax = JMax(max,min);
	*xMin = JMin(max,min);
	itsYMax->GetValue(&max);
	itsYMin->GetValue(&min);
	*yMax = JMax(max,min);
	*yMin = JMin(max,min);
	return JNegate(itsClearRangeFlag);
}

/*******************************************************************************
 Receive

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

void
JXPlotRangeDialog::Receive
	(
	JBroadcaster*	sender,
	const Message&	message
	)
{
	if (sender == itsClearButton && message.Is(JXButton::kPushed))
		{
		itsClearRangeFlag = kTrue;
		EndDialog(kTrue);
		}
	else
		{
		JXDialogDirector::Receive(sender, message);
		}
}
