/******************************************************************************
 SyGRunCmdDialog.cc.cc

	BASE CLASS = JXDialogDirector, JPrefObject

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

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

#include "SyGRunCmdDialog.h"
#include "SyGPrefsMgr.h"

#include "SyGGlobals.h"
#include "SyGHelpText.h"

#include <JXWindow.h>
#include <JXTextButton.h>
#include <JXInputField.h>
#include <JXStringHistoryMenu.h>
#include <JXStaticText.h>
#include <JXDocumentMenu.h>
#include <JXHelpManager.h>
#include <JXChooseSaveFile.h>

#include <JInPipeStream.h>
#include <JOutPipeStream.h>
#include <JString.h>
#include <JSubstitute.h>

#include <jProcessUtil.h>
#include <jStreamUtil.h>
#include <jDirUtil.h>

#include <jAssert.h>

const JSize kHistoryLength = 20;

// setup information

const JFileVersion kCurrentSetupVersion = 1;

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

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

SyGRunCmdDialog::SyGRunCmdDialog
	(
	JXDirector* supervisor
	)
	:
	JXDialogDirector(supervisor, kTrue),
	JPrefObject(SyGGetPrefsMgr(), kSyGRunCmdDialogID)
{
	BuildWindow();
	JPrefObject::ReadPrefs();
}

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

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

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

/******************************************************************************
 Activate (virtual)

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

void
SyGRunCmdDialog::Activate()
{
	JXDialogDirector::Activate();

	if (IsActive())
		{
		itsCmdInput->Focus();
		itsCmdInput->SelectAll();
		}
}

/******************************************************************************
 BuildWindow (protected)

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

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

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

    itsCmdInput =
        new JXInputField(window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 20,40, 320,20);
    assert( itsCmdInput != NULL );

    JXStaticText* obj1 =
        new JXStaticText("Enter the command to run (use $f for file names):", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 20,20, 330,20);
    assert( obj1 != NULL );

    JXTextButton* closeButton =
        new JXTextButton("Close", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 60,80, 60,20);
    assert( closeButton != NULL );
    closeButton->SetShortcuts("#W^[");

    itsHelpButton =
        new JXTextButton("Help", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 205,80, 60,20);
    assert( itsHelpButton != NULL );

    JXTextButton* okButton =
        new JXTextButton("Run", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 349,79, 62,22);
    assert( okButton != NULL );
    okButton->SetShortcuts("^M");

    itsHistoryMenu =
        new JXStringHistoryMenu(kHistoryLength, "", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 340,40, 30,20);
    assert( itsHistoryMenu != NULL );

    itsBrowseButton =
        new JXTextButton("Browse", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 390,40, 60,20);
    assert( itsBrowseButton != NULL );

// end JXLayout

	SetButtons(okButton, closeButton);

	window->SetTitle("Run command");
	window->LockCurrentMinSize();

	ListenTo(itsBrowseButton);
	ListenTo(itsHelpButton);
	ListenTo(itsHistoryMenu);

	itsCmdInput->SetIsRequired();
	itsCmdInput->SetDefaultFontName("6x13");

}

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

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

void
SyGRunCmdDialog::Receive
	(
	JBroadcaster*	sender,
	const Message&	message
	)
{
	if (sender == itsHelpButton && message.Is(JXButton::kPushed))
		{
		(JXGetHelpManager())->ShowSection(kMiscRunHelpName);
		}

	else if (sender == itsHistoryMenu && message.Is(JXMenu::kItemSelected))
		{
		const JXMenu::ItemSelected* selection =
			dynamic_cast(const JXMenu::ItemSelected*, &message);
		assert( selection != NULL );
		itsCmdInput->SetText(itsHistoryMenu->GetItemText(selection->GetIndex()));
		itsCmdInput->Focus();
		}
	else if (sender == itsBrowseButton && message.Is(JXButton::kPushed))
		{
		JString file;
		if (JXGetChooseSaveFile()->ChooseFile("Select program", "", &file))
			{
			JString path, name;
			JSplitPathAndName(file, &path, &name);
			itsCmdInput->SetText(name);
			itsCmdInput->SelectAll();
			Activate();
			}
		}
	else
		{
		JXDialogDirector::Receive(sender, message);
		}
}

/******************************************************************************
 ReadPrefs (virtual)

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

void
SyGRunCmdDialog::ReadPrefs
	(
	istream& input
	)
{
	JFileVersion vers;
	input >> vers;
	if (vers > kCurrentSetupVersion)
		{
		return;
		}

	JXWindow* window = GetWindow();
	window->ReadGeometry(input);
	window->Deiconify();

	itsHistoryMenu->ReadSetup(input);

	if (vers > 0)
		{
		JString str;
		input >> str;
		itsCmdInput->SetText(str);
		itsCmdInput->SelectAll();
		}
}

/******************************************************************************
 WritePrefs (virtual)

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

void
SyGRunCmdDialog::WritePrefs
	(
	ostream& output
	)
	const
{
	output << kCurrentSetupVersion;

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

	output << ' ';
	itsHistoryMenu->WriteSetup(output);

	output << ' ';
	output << itsCmdInput->GetText();
}

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

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

JBoolean
SyGRunCmdDialog::OKToDeactivate()
{
	if (!JXDialogDirector::OKToDeactivate())
		{
		return kFalse;
		}
	else if (Cancelled())
		{
		return kTrue;
		}
	itsHistoryMenu->AddString(itsCmdInput->GetText());
//	if (JProgramOnPath(itsCmdInput->GetText()))
//		{
//		return kTrue;
//		}
//	return kFalse;
	return kTrue;
}

/******************************************************************************
 GetCommand (public)

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

const JString&
SyGRunCmdDialog::GetCommand()
	const
{
	return itsCmdInput->GetText();
}
