/******************************************************************************
 JXPlotScaleDialog.cc

	BASE CLASS = JXDialogDirector

	Written by John Lindal.

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

#include "JXPlotScaleDialog.h"
#include <JXWindow.h>
#include <JXTextButton.h>
#include <JXFloatInput.h>
#include <JXRadioGroup.h>
#include <JXStaticText.h>
#include <JXTextRadioButton.h>
#include <JXColormap.h>

#include <JMinMax.h>

#include <fitplot.h>

#include <jAssert.h>

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

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

JXPlotScaleDialog::JXPlotScaleDialog
	(
	JXWindowDirector* supervisor, 
	const JFloat xMax,
	const JFloat xMin,
	const JFloat xInc,
	const JBoolean xLinear,
	const JFloat yMax,
	const JFloat yMin,
	const JFloat yInc,
	const JBoolean yLinear
	)
	:
	JXDialogDirector(supervisor, kTrue)
{
	BuildWindow();
	itsXMax->SetValue(xMax);
	itsXMin->SetValue(xMin);
	itsXInc->SetValue(xInc);
	itsYMax->SetValue(yMax);
	itsYMin->SetValue(yMin);
	itsYInc->SetValue(yInc);
	if (xLinear)
		{
		itsXAxisTypeRG->SelectItem(1);
		}
	else
		{
		itsXAxisTypeRG->SelectItem(2);
		}
	if (yLinear)
		{
		itsYAxisTypeRG->SelectItem(1);
		}
	else
		{
		itsYAxisTypeRG->SelectItem(2);
		}
		
	ListenTo(itsXAxisTypeRG);
	ListenTo(itsYAxisTypeRG);
	AdjustXScaleActivation();
	AdjustYScaleActivation();

	JFloat min;
	JFloat max;
	JFloat inc;
	if (!xLinear)
		{
		FindRange(xMax, xMin, &inc, &max, &min);
		itsXInc->SetValue(inc);
		}
	if (!yLinear)
		{
		FindRange(yMax, yMin, &inc, &max, &min);
		itsYInc->SetValue(inc);
		}
}

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

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

JXPlotScaleDialog::~JXPlotScaleDialog()
{
}

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

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

void
JXPlotScaleDialog::BuildWindow()
{
	JXTextRadioButton* xrb[3];
	JXTextRadioButton* yrb[3];
	
// begin JXLayout

    JXWindow* window = new JXWindow(this, 540,180, "");
    assert( window != NULL );
    SetWindow(window);

    itsXIncLabel =
        new JXStaticText("Increment:", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 190,45, 70,20);
    assert( itsXIncLabel != NULL );

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

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

    itsYIncLabel =
        new JXStaticText("Increment:", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 190,110, 70,20);
    assert( itsYIncLabel != NULL );

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

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

    itsYAxisTypeRG =
        new JXRadioGroup(window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 405,75, 120,60);
    assert( itsYAxisTypeRG != NULL );

    yrb[0] =
        new JXTextRadioButton(1, "Linear", itsYAxisTypeRG,
                    JXWidget::kHElastic, JXWidget::kVElastic, 10,5, 98,20);
    assert( yrb[0] != NULL );

    yrb[1] =
        new JXTextRadioButton(2, "Logarithmic", itsYAxisTypeRG,
                    JXWidget::kHElastic, JXWidget::kVElastic, 10,25, 98,20);
    assert( yrb[1] != NULL );

    itsXAxisTypeRG =
        new JXRadioGroup(window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 405,10, 120,60);
    assert( itsXAxisTypeRG != NULL );

    xrb[0] =
        new JXTextRadioButton(1, "Linear", itsXAxisTypeRG,
                    JXWidget::kHElastic, JXWidget::kVElastic, 10,5, 98,20);
    assert( xrb[0] != NULL );

    xrb[1] =
        new JXTextRadioButton(2, "Logarithmic", itsXAxisTypeRG,
                    JXWidget::kHElastic, JXWidget::kVElastic, 10,25, 98,20);
    assert( xrb[1] != NULL );

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

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

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

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

    itsXInc =
        new JXFloatInput(window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 270,45, 120,20);
    assert( itsXInc != NULL );

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

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

    itsYInc =
        new JXFloatInput(window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 270,110, 120,20);
    assert( itsYInc != NULL );

// end JXLayout

	window->SetTitle("Edit Plot Scale");
	SetButtons(okButton, cancelButton);
}

/******************************************************************************
 GetScaleValues 

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

void
JXPlotScaleDialog::GetScaleValues
	(
	JFloat* xMax, 
	JFloat* xMin, 
	JFloat* xInc, 
	JBoolean* xLinear,	
	JFloat* yMax, 
	JFloat* yMin, 
	JFloat* yInc, 
	JBoolean* yLinear
	)
{
	JFloat min;
	JFloat max;
	itsXMax->GetValue(&max);
	itsXMin->GetValue(&min);
	itsXInc->GetValue(xInc);
	*xMax = JMax(max,min);
	*xMin = JMin(max,min);
	itsYMax->GetValue(&max);
	itsYMin->GetValue(&min);
	itsYInc->GetValue(yInc);
	*yMax = JMax(max,min);
	*yMin = JMin(max,min);
	if (itsXAxisTypeRG->GetSelectedItem() == 1)
		{
		*xLinear = kTrue;
		}
	else
		{
		*xLinear = kFalse;
		}
	if (itsYAxisTypeRG->GetSelectedItem() == 1)
		{
		*yLinear = kTrue;
		}
	else
		{
		*yLinear = kFalse;
		}
}

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

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

void
JXPlotScaleDialog::Receive
	(
	JBroadcaster* sender, 
	const Message& message
	)
{
	if (sender == itsXAxisTypeRG && message.Is(JXRadioGroup::kSelectionChanged))
		{
		AdjustXScaleActivation();
		}
		
	else if (sender == itsYAxisTypeRG && message.Is(JXRadioGroup::kSelectionChanged))
		{
		AdjustYScaleActivation();
		}
		
	else
		{
		JXDialogDirector::Receive(sender,message);
		}
}

/******************************************************************************
 AdjustXScaleActivation (private)

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

void
JXPlotScaleDialog::AdjustXScaleActivation()
{
	JIndex type = itsXAxisTypeRG->GetSelectedItem();
	JXColormap* colormap = GetColormap();	
	
	if (type == linear)
		{
		itsXInc->Show();
		itsXIncLabel->Show();
		}
	else
		{
		itsXInc->Hide();
		itsXIncLabel->Hide();
		}
}

/******************************************************************************
 AdjustYScaleActivation (private)

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

void
JXPlotScaleDialog::AdjustYScaleActivation()
{
	JIndex type = itsYAxisTypeRG->GetSelectedItem();
	JXColormap* colormap = GetColormap();	
	
	if (type == linear)
		{
//		itsYInc->Activate();
//		JFontStyle style(colormap->GetBlackColor());
//		itsYInc->SetFontStyle(style);
		itsYInc->Show();
		itsYIncLabel->Show();
		}
		
	else
		{
//		itsYInc->Deactivate();
//		JFontStyle style(colormap->GetInactiveLabelColor());
//		itsYInc->SetFontStyle(style);
		itsYInc->Hide();
		itsYIncLabel->Hide();
		}
}

/******************************************************************************
 EditXAxis (public);

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

void
JXPlotScaleDialog::EditXAxis
	(
	const JBoolean xAxis
	)
{
	if (!xAxis)
		{
		itsYMin->Focus();
		}
}
