/******************************************************************************
 SyGChooseSaveFile.cc

	Class to test overriding of JXChooseSaveFile dialog windows.

	BASE CLASS = JXChooseSaveFile

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

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

#include "SyGChooseSaveFile.h"
#include "SyGChoosePathDialog.h"

#include <jStreamUtil.h>
#include <jStrStreamUtil.h>

#include <jAssert.h>

const JFileVersion kCurrentPrefsVersion	= 1;

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

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

SyGChooseSaveFile::SyGChooseSaveFile
	(
	JPrefsManager*	prefsMgr,
	const JPrefID&	id
	)
	:
	JXChooseSaveFile(),
	JPrefObject(prefsMgr, id),
	itsOpenInNewWindow(kTrue)
{
	itsPathDialog 	= NULL;

	JPrefObject::ReadPrefs();
}

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

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

SyGChooseSaveFile::~SyGChooseSaveFile()
{
	JPrefObject::WritePrefs();
}

/******************************************************************************
 CreateSaveFileDialog (virtual protected)

 ******************************************************************************

JXSaveFileDialog*
SyGChooseSaveFile::CreateSaveFileDialog
	(
	JXDirector*			supervisor,
	JUNIXDirInfo*		dirInfo,
	const JCharacter*	fileFilter,
	const JCharacter*	origName,
	const JCharacter*	prompt,
	const JCharacter*	message
	)
{
	itsSaveDialog =
		new GMSaveFileDialog(supervisor, dirInfo, fileFilter, origName, prompt, message);
	assert( itsSaveDialog != NULL );
	itsSaveDialog->ShouldSaveHeaders(itsSaveHeaders);
	return itsSaveDialog;
}*/

/******************************************************************************
 CreateChooseFileDialog (virtual protected)

 ******************************************************************************

JXChooseFileDialog*
SyGChooseSaveFile::CreateChooseFileDialog
	(
	JXDirector*			supervisor,
	JUNIXDirInfo*		dirInfo,
	const JCharacter*	fileFilter,
	const JCharacter* 	origName,
	const JBoolean		allowSelectMultiple,
	const JCharacter*	message
	)
{
	itsChooseDialog =
		new GMChooseFileDialog(supervisor, dirInfo, fileFilter, allowSelectMultiple);
	assert( itsChooseDialog != NULL );
	itsChooseDialog->BuildWindow(origName, message);
	itsChooseDialog->ShouldSaveHeaders(itsSaveHeaders);
	return itsChooseDialog;
}*/

/******************************************************************************
 CreateChoosePathDialog (virtual protected)

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

JXChoosePathDialog*
SyGChooseSaveFile::CreateChoosePathDialog
	(
	JXDirector*			supervisor,
	JUNIXDirInfo*		dirInfo,
	const JCharacter*	fileFilter,
	const JBoolean 		selectOnlyWritable,
	const JCharacter*	message
	)
{
	itsPathDialog =
		new SyGChoosePathDialog(supervisor, dirInfo, fileFilter, selectOnlyWritable);
	assert( itsPathDialog != NULL );
	itsPathDialog->BuildWindow(itsOpenInNewWindow, message);
	return itsPathDialog;
}

/******************************************************************************
 NewWindow

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

JBoolean
SyGChooseSaveFile::IsOpeningInNewWindow()
	const
{
	return itsOpenInNewWindow;
}

void
SyGChooseSaveFile::ShouldOpenInNewWindow
	(
	const JBoolean newWindow
	)
{
	itsOpenInNewWindow = newWindow;
}


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

	Listen for response from itsSaveDialog.

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

void
SyGChooseSaveFile::Receive
	(
	JBroadcaster*	sender,
	const Message&	message
	)
{
	if (sender == itsPathDialog && message.Is(JXDialogDirector::kDeactivated))
		{
		const JXDialogDirector::Deactivated* info =
			dynamic_cast(const JXDialogDirector::Deactivated*, &message);
		assert( info != NULL );
		if (info->Successful())
			{
			itsOpenInNewWindow = itsPathDialog->OpenInNewWindow();
			}
		itsPathDialog = NULL;
		}

	JXChooseSaveFile::Receive(sender, message);
}

/******************************************************************************
 Read/WritePrefs

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

void
SyGChooseSaveFile::ReadPrefs
	(
	istream& input
	)
{
	JFileVersion version;
	input >> version;
	if (version == 1)
		{
		input >> itsOpenInNewWindow;
		ReadSetup(input);
		}
}

void
SyGChooseSaveFile::WritePrefs
	(
	ostream& output
	)
	const
{
	output << kCurrentPrefsVersion << ' ';
	output << itsOpenInNewWindow << ' ';
	WriteSetup(output);
}
