/******************************************************************************
 JXCursorMarkTableDir.cc

	BASE CLASS = JXWindowDirector

	Copyright © 1997 by Glenn Bach. All rights reserved.

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

#include <JXCursorMarkTableDir.h>
#include <JXCursorMarkTable.h>

#include <JXTextButton.h>
#include <JXScrollbarSet.h>
#include <JXWindow.h>
#include <JXScrollbar.h>

#include <J2DPlotWidget.h>
#include <JString.h>
#include <jAssert.h>

// setup information

const JFileVersion kCurrentSetupVersion = 1;	// must begin with digit >= 1

	// version 1:  added version number

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

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

JXCursorMarkTableDir::JXCursorMarkTableDir
	(
	JXWindowDirector* 	supervisor,
	J2DPlotWidget* 		plot
	)
	:
	JXWindowDirector(supervisor)
{
	JXWindow* window = new JXWindow(this, 250,300, "Cursor Mark Values");
    assert( window != NULL );
    SetWindow(window);
    window->SetMinSize(250,300);
    
	JXScrollbarSet* scrollbarSet = 
		new JXScrollbarSet(window,
			JXWidget::kHElastic, JXWidget::kVElastic, 
			0,0,250,260);
	assert(scrollbarSet != NULL);
	
	itsTable = 
		new JXCursorMarkTable(plot, 
			scrollbarSet, scrollbarSet->GetScrollEnclosure(),
			JXWidget::kHElastic, JXWidget::kVElastic, 
			0,0,250,260);
	assert (itsTable != NULL);
	
	itsCloseButton = 
		new JXTextButton("Close", window,
			JXWidget::kFixedLeft, JXWidget::kFixedBottom, 
			10, 270, 70, 20);
	assert( itsCloseButton != NULL );
	itsCloseButton->SetShortcuts("#W^[");
	ListenTo(itsCloseButton);
}

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

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

JXCursorMarkTableDir::~JXCursorMarkTableDir()
{
}

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

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

void
JXCursorMarkTableDir::Receive
	(
	JBroadcaster* sender,
	const JBroadcaster::Message& message
	)
{
	if (sender == itsCloseButton && message.Is(JXButton::kPushed))
		{
		Deactivate();
		}
		
	else
		{
		JXWindowDirector::Receive(sender, message);
		}
}

/******************************************************************************
 ReadSetup

	This uses assert() to check the version number because we cannot use
	a marker to skip the data (since it contains objects), so it is the
	client's responsibility to check whether or not the file can be
	read (via a higher level version number).

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

void
JXCursorMarkTableDir::ReadSetup
	(
	istream& input
	)
{
	JFileVersion vers = 0;
	input >> ws;
	if (input.peek() != '0')	// version 0 => leave it for JXWindow
		{
		input >> vers;
		assert( vers <= kCurrentSetupVersion );
		}

	GetWindow()->ReadGeometry(input);

	itsTable->AdjustTable();

	if (vers == 0)
		{
		JXScrollbar *hScrollbar, *vScrollbar;
		const JBoolean hasScrollbars = itsTable->GetScrollbars(&hScrollbar, &vScrollbar);
		assert( hasScrollbars );

		vScrollbar->ReadSetup(input);
		hScrollbar->ReadSetup(input);
		}
	else
		{
		itsTable->ReadScrollSetup(input);
		}
}

/******************************************************************************
 WriteSetup

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

void
JXCursorMarkTableDir::WriteSetup
	(
	ostream& output
	)
	const
{
	output << kCurrentSetupVersion;

	output << ' ';
	(GetWindow())->WriteGeometry(output);

	// must be last so the table can adjust before the scrollbars are adjusted

	output << ' ';
	itsTable->WriteScrollSetup(output);
}
