/******************************************************************************
 JXCurveOptionsDialog.cc

	BASE CLASS = JXDialogDirector

	Copyright @ 1997 by Glenn W. Bach.

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

#include "JXCurveOptionsDialog.h"
#include <JXCurveNameList.h>

#include <JXWindow.h>
#include <JXTextButton.h>
#include <JXRadioGroup.h>
#include <JXTextRadioButton.h>
#include <JXStaticText.h>
#include <JXTextCheckbox.h>
#include <JXScrollbarSet.h>

#include <JString.h>
#include <JColormap.h>

#include <jGlobals.h>

#include <jAssert.h>

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

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

JXCurveOptionsDialog::JXCurveOptionsDialog
	(
	JXWindowDirector* 			supervisor,
	const JArray<JCurveInfo>&	array, 
	const JIndex 				startIndex
	)
	:
	JXDialogDirector(supervisor, kTrue),
	itsCurrentIndex(startIndex)
{
	const JSize count = array.GetElementCount();
	assert(startIndex <= count);

	itsCurvesInfo = new JArray<JCurveInfo>;
	assert(itsCurvesInfo != NULL);
	
	for (JSize i = 1; i <= count; i++)
		{
		JCurveInfo info = array.GetElement(i);
		info.name = new JString(*info.name);
		assert(info.name != NULL);
		itsCurvesInfo->AppendElement(info);
		}

	BuildWindow();
}

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

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

JXCurveOptionsDialog::~JXCurveOptionsDialog()
{
	const JSize count = itsCurvesInfo->GetElementCount();
	for (JIndex i = 1; i <= count; i++)
		{		
		JCurveInfo info = itsCurvesInfo->GetElement(i);
		delete info.name;
		}
	delete itsCurvesInfo;
}

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

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

void
JXCurveOptionsDialog::BuildWindow()
{
	
	JXTextRadioButton* rb[5];
	
// begin JXLayout

    JXWindow* window = new JXWindow(this, 370,260, "");
    assert( window != NULL );
    SetWindow(window);

    itsRG =
        new JXRadioGroup(window,
                    JXWidget::kFixedRight, JXWidget::kFixedTop, 160,10, 200,125);
    assert( itsRG != NULL );

    rb[0] =
        new JXTextRadioButton(1, "Hide curve", itsRG,
                    JXWidget::kFixedRight, JXWidget::kFixedTop, 15,15, 120,20);
    assert( rb[0] != NULL );

    rb[1] =
        new JXTextRadioButton(2, "Show lines only", itsRG,
                    JXWidget::kFixedRight, JXWidget::kFixedTop, 15,35, 125,20);
    assert( rb[1] != NULL );

    rb[2] =
        new JXTextRadioButton(3, "Show symbols only", itsRG,
                    JXWidget::kFixedRight, JXWidget::kFixedTop, 15,55, 140,20);
    assert( rb[2] != NULL );

    rb[3] =
        new JXTextRadioButton(4, "Show points only", itsRG,
                    JXWidget::kFixedRight, JXWidget::kFixedTop, 15,75, 165,20);
    assert( rb[3] != NULL );

    JXTextButton* okButton =
        new JXTextButton("OK", window,
                    JXWidget::kFixedRight, JXWidget::kFixedBottom, 280,230, 70,20);
    assert( okButton != NULL );

    itsCancelButton =
        new JXTextButton("Cancel", window,
                    JXWidget::kFixedRight, JXWidget::kFixedBottom, 175,230, 70,20);
    assert( itsCancelButton != NULL );
    itsCancelButton->SetShortcuts("^[");

    itsShowXErrorsCB =
        new JXTextCheckbox("Show X Errors", window,
                    JXWidget::kFixedRight, JXWidget::kFixedTop, 200,150, 120,20);
    assert( itsShowXErrorsCB != NULL );

    itsShowYErrorsCB =
        new JXTextCheckbox("Show Y Errors", window,
                    JXWidget::kFixedRight, JXWidget::kFixedTop, 200,170, 120,20);
    assert( itsShowYErrorsCB != NULL );

    rb[4] =
        new JXTextRadioButton(5, "Show lines and symbols", itsRG,
                    JXWidget::kFixedRight, JXWidget::kFixedTop, 15,95, 165,20);
    assert( rb[4] != NULL );

    JXScrollbarSet* scrollbarset =
        new JXScrollbarSet(window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 10,10, 135,240);
    assert( scrollbarset != NULL );

// end JXLayout

    itsNameList =
        new JXCurveNameList(scrollbarset, scrollbarset->GetScrollEnclosure(),
                    JXWidget::kHElastic, JXWidget::kVElastic, 0,0, 135,240);
    assert( itsNameList != NULL );
	ListenTo(itsNameList);
	itsNameList->SetBackColor(JGetCurrColormap()->GetWhiteColor());
	
	window->SetTitle("Curve Options");
	SetButtons(okButton, NULL);

	UseModalPlacement(kFalse);
	window->PlaceAsDialogWindow();
	window->LockCurrentMinSize();

	AdjustWidgets();
	
	const JSize count = itsCurvesInfo->GetElementCount();
	for (JSize i = 1; i <= count; i++)
		{
		JCurveInfo info = itsCurvesInfo->GetElement(i);
		itsNameList->AppendName(info.name);
		}

	itsNameList->SetCurrentName(itsCurrentIndex);

	ListenTo(itsRG);
	ListenTo(itsShowXErrorsCB);
	ListenTo(itsShowYErrorsCB);
	ListenTo(itsCancelButton);
}

/******************************************************************************
 Receive (protected)

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

void 
JXCurveOptionsDialog::Receive
	(
	JBroadcaster* 	sender, 
	const Message&	message
	)
{
	if (sender == itsNameList && message.Is(JXCurveNameList::kNameSelected))
		{
		const JXCurveNameList::NameSelected* newName =
			dynamic_cast(const JXCurveNameList::NameSelected*, &message);
		assert(newName != NULL);
		JIndex index = newName->GetIndex();
		JCurveInfo info = itsCurvesInfo->GetElement(itsCurrentIndex);
		info.xerrors = itsShowXErrorsCB->IsChecked();
		info.yerrors = itsShowYErrorsCB->IsChecked();
		JIndex showIndex = itsRG->GetSelectedItem();
		if (showIndex == kHideCurve)
			{
			info.curve 		= kFalse;
			}
		else if (showIndex == kShowLines)
			{
			info.curve 		= kTrue;
			info.symbols 	= kFalse;
			info.lines 		= kTrue;
			info.points 	= kFalse;
			}
		else if (showIndex == kShowSymbols)
			{
			info.curve 		= kTrue;
			info.symbols 	= kTrue;
			info.lines 		= kFalse;
			info.points 	= kFalse;
			}
		else if (showIndex == kShowPoints)
			{
			info.curve 		= kTrue;
			info.symbols 	= kFalse;
			info.lines 		= kFalse;
			info.points 	= kTrue;
			}
		else if (showIndex == kShowBoth)
			{
			info.curve 		= kTrue;
			info.symbols 	= kTrue;
			info.lines 		= kTrue;
			info.points 	= kFalse;
			}
		itsCurvesInfo->SetElement(itsCurrentIndex, info);
		itsCurrentIndex = index;
		AdjustWidgets();
		}
	else if (sender == itsRG && message.Is(JXRadioGroup::kSelectionChanged))
		{
		JCurveInfo info = itsCurvesInfo->GetElement(itsCurrentIndex);
		JIndex showIndex = itsRG->GetSelectedItem();
		if (showIndex == kHideCurve)
			{
			info.curve 		= kFalse;
			}
		else if (showIndex == kShowLines)
			{
			info.curve 		= kTrue;
			info.symbols 	= kFalse;
			info.lines 		= kTrue;
			info.points 	= kFalse;
			}
		else if (showIndex == kShowSymbols)
			{
			info.curve 		= kTrue;
			info.symbols 	= kTrue;
			info.lines 		= kFalse;
			info.points 	= kFalse;
			}
		else if (showIndex == kShowPoints)
			{
			info.curve 		= kTrue;
			info.symbols 	= kFalse;
			info.lines 		= kFalse;
			info.points 	= kTrue;
			}
		else if (showIndex == kShowBoth)
			{
			info.curve 		= kTrue;
			info.symbols 	= kTrue;
			info.lines 		= kTrue;
			info.points 	= kFalse;
			}
		itsCurvesInfo->SetElement(itsCurrentIndex, info);
		}
	else if (sender == itsShowXErrorsCB && message.Is(JXCheckbox::kPushed))
		{
		JCurveInfo info = itsCurvesInfo->GetElement(itsCurrentIndex);
		info.xerrors 	= itsShowXErrorsCB->IsChecked();
		itsCurvesInfo->SetElement(itsCurrentIndex, info);
		}
	else if (sender == itsShowYErrorsCB && message.Is(JXCheckbox::kPushed))
		{
		JCurveInfo info = itsCurvesInfo->GetElement(itsCurrentIndex);
		info.yerrors 	= itsShowYErrorsCB->IsChecked();
		itsCurvesInfo->SetElement(itsCurrentIndex, info);
		}
	else if (sender == itsCancelButton && message.Is(JXButton::kPushed))
		{
		if (itsNameList->IsEditing())
			{
			itsNameList->CancelEditing();
			}
		else
			{
			EndDialog(kFalse);
			}
		}
	else
		{
		JXDialogDirector::Receive(sender, message);
		}
}

/******************************************************************************
 AdjustWidgets (private)

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

void
JXCurveOptionsDialog::AdjustWidgets()
{
	JCurveInfo info = itsCurvesInfo->GetElement(itsCurrentIndex);
	JIndex option;
	if (!info.curve)
		{
		option = kHideCurve;
		}
	else if (info.lines)
		{
		if (info.symbols)
			{
			option = kShowBoth;
			}
		else
			{
			option = kShowLines;
			}
		}
	else if (info.symbols)
		{
		option = kShowSymbols;
		}
	else
		{
		option = kShowPoints;
		}
	itsRG->SelectItem(option);
	itsShowXErrorsCB->SetState(info.xerrors);
	itsShowYErrorsCB->SetState(info.yerrors);
}

/******************************************************************************
 OKToDeactivate (virtual protected)

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

JBoolean
JXCurveOptionsDialog::OKToDeactivate()
{
	if (Cancelled())
		{
		return kTrue;
		}
	if (itsNameList->IsEditing())
		{
		if (itsNameList->EndEditing())
			{
			return kTrue;
			}
		return kFalse;
		}
	return kTrue;
}
