/******************************************************************************
 SyGViewManPageDialog.cc

	BASE CLASS = JXWindowDirector, JPrefObject

	Copyright © 1996-98 by John Lindal. All rights reserved.

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

#include "SyGViewManPageDialog.h"
#include "SyGGlobals.h"
#include "SyGHelpText.h"
#include "SyGPrefsMgr.h"
#include "SyGApplication.h"

#include <JXWindow.h>
#include <JXHelpManager.h>
#include <JXTextButton.h>
#include <JXCharInput.h>
#include <JXStaticText.h>
#include <JXTextCheckbox.h>
#include <JXStringHistoryMenu.h>

#include <JString.h>

#include <jAssert.h>

const JSize kHistoryLength = 20;

// setup information

const JFileVersion kCurrentSetupVersion = 0;

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

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

SyGViewManPageDialog::SyGViewManPageDialog
	(
	JXDirector* supervisor
	)
	:
	JXWindowDirector(supervisor),
	JPrefObject(SyGGetPrefsMgr(), kSViewManPagePrefID)
{
	BuildWindow();
	JPrefObject::ReadPrefs();
}

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

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

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

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

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

void
SyGViewManPageDialog::Activate()
{
	JXWindowDirector::Activate();

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

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

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

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

    JXWindow* window = new JXWindow(this, 320,160, "");
    assert( window != NULL );
    SetWindow(window);

    itsCloseButton =
        new JXTextButton("Close", window,
                    JXWidget::kFixedLeft, JXWidget::kFixedTop, 30,130, 60,20);
    assert( itsCloseButton != NULL );
    itsCloseButton->SetShortcuts("#W^[");

    itsViewButton =
        new JXTextButton("View", window,
                    JXWidget::kFixedLeft, JXWidget::kFixedTop, 229,129, 62,22);
    assert( itsViewButton != NULL );
    itsViewButton->SetShortcuts("^M");

    itsFnName =
        new JXInputField(window,
                    JXWidget::kHElastic, JXWidget::kFixedTop, 20,40, 250,20);
    assert( itsFnName != NULL );

    JXStaticText* obj1 =
        new JXStaticText("Function name:", window,
                    JXWidget::kFixedLeft, JXWidget::kFixedTop, 20,20, 100,20);
    assert( obj1 != NULL );

    JXStaticText* obj2 =
        new JXStaticText("Section index:", window,
                    JXWidget::kFixedLeft, JXWidget::kFixedTop, 20,80, 90,20);
    assert( obj2 != NULL );

    itsAproposCheckbox =
        new JXTextCheckbox("Apropos", window,
                    JXWidget::kFixedLeft, JXWidget::kFixedTop, 190,80, 80,20);
    assert( itsAproposCheckbox != NULL );
    itsAproposCheckbox->SetShortcuts("#p");

    itsHelpButton =
        new JXTextButton("Help", window,
                    JXWidget::kFixedLeft, JXWidget::kFixedTop, 130,130, 60,20);
    assert( itsHelpButton != NULL );

    itsFnHistoryMenu =
        new JXStringHistoryMenu(kHistoryLength, "", window,
                    JXWidget::kFixedRight, JXWidget::kFixedTop, 270,40, 30,20);
    assert( itsFnHistoryMenu != NULL );

    itsManIndex =
        new JXCharInput(window,
                    JXWidget::kFixedLeft, JXWidget::kFixedTop, 110,80, 30,20);
    assert( itsManIndex != NULL );

// end JXLayout

	window->SetTitle("Man pages");
	window->SetCloseAction(JXWindow::kDeactivateDirector);
	window->PlaceAsDialogWindow();
	window->LockCurrentMinSize();
	window->SetFocusWhenShow(kTrue);

	ListenTo(itsViewButton);
	ListenTo(itsCloseButton);
	ListenTo(itsHelpButton);
	ListenTo(itsFnHistoryMenu);

	itsFnName->SetIsRequired();
	itsManIndex->SetIsRequired(kFalse);
}

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

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

void
SyGViewManPageDialog::Receive
	(
	JBroadcaster*	sender,
	const Message&	message
	)
{
	if (sender == itsViewButton && message.Is(JXButton::kPushed))
		{
		ViewManPage();
		}
	else if (sender == itsCloseButton && message.Is(JXButton::kPushed))
		{
		(GetWindow())->KillFocus();
		Deactivate();
		}
	else if (sender == itsHelpButton && message.Is(JXButton::kPushed))
		{
		(JXGetHelpManager())->ShowSection(kMiscManPageHelpName);
		}

	else if (sender == itsFnHistoryMenu && message.Is(JXMenu::kItemSelected))
		{
		const JXMenu::ItemSelected* selection =
			dynamic_cast(const JXMenu::ItemSelected*, &message);
		assert( selection != NULL );
		SetFunction(itsFnHistoryMenu->GetItemText(selection->GetIndex()));
		itsFnName->Focus();
		}

	else
		{
		JXWindowDirector::Receive(sender, message);
		}
}

/******************************************************************************
 ViewManPage (private)

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

void
SyGViewManPageDialog::ViewManPage()
{
	if (itsFnName->InputValid() && itsManIndex->InputValid())
		{
		JCharacter manIndex = ' ';
		if (!itsManIndex->IsEmpty())
			{
			manIndex = (itsManIndex->GetText()).GetFirstCharacter();
			}

		SyGGetApplication()->ViewManPage(itsFnName->GetText(), manIndex, 
										 itsAproposCheckbox->IsChecked());
		AddToHistory(itsFnName->GetText(), manIndex, 
					 itsAproposCheckbox->IsChecked());
		}
}

/******************************************************************************
 SetFunction (private)

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

void
SyGViewManPageDialog::SetFunction
	(
	const JCharacter* historyStr
	)
{
	JString fnName = historyStr;

	JCharacter manIndex[] = { '\0', '\0' };
	if (fnName.GetLastCharacter() == ')')
		{
		manIndex[0] = fnName.GetCharacter(fnName.GetLength()-1);
		fnName.RemoveSubstring(fnName.GetLength()-3, fnName.GetLength());
		}

	if (manIndex[0] == '*')
		{
		manIndex[0] = '\0';
		itsAproposCheckbox->SetState(kTrue);
		}
	else
		{
		itsAproposCheckbox->SetState(kFalse);
		}

	itsFnName->SetText(fnName);
	itsManIndex->SetText(manIndex);
}

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

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

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

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

	itsFnHistoryMenu->ReadSetup(input);
}

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

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

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

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

	output << ' ';
	itsFnHistoryMenu->WriteSetup(output);
}

/******************************************************************************
 AddToHistory

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

void
SyGViewManPageDialog::AddToHistory
	(
	const JCharacter*	pageName,
	const JCharacter	pageIndex,
	const JBoolean		apropos
	)
{
	JString historyStr = pageName;
	if (apropos)
		{
		historyStr += " (*)";
		}
	else if (pageIndex != ' ')
		{
		historyStr.AppendCharacter(' ');
		historyStr.AppendCharacter('(');
		historyStr.AppendCharacter(pageIndex);
		historyStr.AppendCharacter(')');
		}

	itsFnHistoryMenu->AddString(historyStr);
}
