/******************************************************************************
 JXSpellCheckerDialog.cc

	BASE CLASS = JXWindowDirector

	Copyright @ 1997 by Glenn W. Bach.

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

#include "JXSpellCheckerDialog.h"

#include <JXWindow.h>
#include <JXTextButton.h>
#include <JXInputField.h>
#include <JXStaticText.h>
#include <JXSpellList.h>
#include <JXScrollbarSet.h>
#include <JXColormap.h>

#include <JString.h>

#include <jXGlobals.h>

#include <jAssert.h>

const JCharacter* JXSpellCheckerDialog::kSpellButtonPressed = "kSpellButtonPressed::JXSpellCheckerDialog";

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

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

JXSpellCheckerDialog::JXSpellCheckerDialog
	(
	JXDirector* supervisor
	)
	:
	JXWindowDirector(supervisor)
{
	BuildWindow();
}

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

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

JXSpellCheckerDialog::~JXSpellCheckerDialog()
{
}

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

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

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

    JXWindow* window = new JXWindow(this, 535,270, "");
    assert( window != NULL );
    SetWindow(window);

    JXStaticText* obj1 =
        new JXStaticText("Not in Dictionary:", window,
                    JXWidget::kFixedLeft, JXWidget::kFixedTop, 15,15, 110,20);
    assert( obj1 != NULL );

    itsCheckText =
        new JXStaticText("", window,
                    JXWidget::kHElastic, JXWidget::kFixedTop, 135,15, 280,20);
    assert( itsCheckText != NULL );

    JXStaticText* obj2 =
        new JXStaticText("Change To:", window,
                    JXWidget::kFixedLeft, JXWidget::kFixedTop, 15,48, 90,20);
    assert( obj2 != NULL );

    JXStaticText* obj3 =
        new JXStaticText("Suggestions:", window,
                    JXWidget::kFixedLeft, JXWidget::kFixedTop, 15,78, 90,20);
    assert( obj3 != NULL );

    itsFirstGuess =
        new JXInputField(window,
                    JXWidget::kHElastic, JXWidget::kFixedTop, 105,45, 230,20);
    assert( itsFirstGuess != NULL );

    JXScrollbarSet* set =
        new JXScrollbarSet(window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 105,75, 230,180);
    assert( set != NULL );

    itsIgnoreButton =
        new JXTextButton("Ignore", window,
                    JXWidget::kFixedRight, JXWidget::kFixedTop, 350,60, 75,20);
    assert( itsIgnoreButton != NULL );
    itsIgnoreButton->SetShortcuts("^M");

    itsChangeButton =
        new JXTextButton("Change", window,
                    JXWidget::kFixedRight, JXWidget::kFixedTop, 350,90, 75,20);
    assert( itsChangeButton != NULL );

    itsLearnButton =
        new JXTextButton("Learn word", window,
                    JXWidget::kFixedRight, JXWidget::kFixedTop, 350,130, 165,20);
    assert( itsLearnButton != NULL );

    itsIgnoreAllButton =
        new JXTextButton("Ignore All", window,
                    JXWidget::kFixedRight, JXWidget::kFixedTop, 440,60, 75,20);
    assert( itsIgnoreAllButton != NULL );

    itsChangeAllButton =
        new JXTextButton("Change All", window,
                    JXWidget::kFixedRight, JXWidget::kFixedTop, 440,90, 75,20);
    assert( itsChangeAllButton != NULL );

    itsCloseButton =
        new JXTextButton("Close", window,
                    JXWidget::kFixedRight, JXWidget::kFixedBottom, 440,235, 75,20);
    assert( itsCloseButton != NULL );

    itsLearnCapsButton =
        new JXTextButton("Learn word with caps", window,
                    JXWidget::kFixedRight, JXWidget::kFixedTop, 350,160, 165,20);
    assert( itsLearnCapsButton != NULL );

// end JXLayout

	window->SetTitle("Spelling");

	itsSuggestions =
		JXSpellList::Create(set, set->GetScrollEnclosure(), 
			JXWidget::kHElastic, JXWidget::kVElastic, 0,0, 230,180);
	assert( itsSuggestions != NULL );
	ListenTo(itsSuggestions);

	ListenTo(itsIgnoreButton);
	ListenTo(itsChangeButton);
	ListenTo(itsLearnButton);
	ListenTo(itsLearnCapsButton);
	ListenTo(itsIgnoreAllButton);
	ListenTo(itsChangeAllButton);
	ListenTo(itsCloseButton);

	itsFirstGuess->SetBackColor(JGetCurrColormap()->GetWhiteColor());
	ListenTo(itsFirstGuess);
	itsCheckText->SetCurrentFontBold(kTrue);
	itsCheckText->SetDefaultFontStyle(JFontStyle(kTrue, kFalse, 0, kFalse));
}

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

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

void
JXSpellCheckerDialog::Receive
	(
	JBroadcaster* 	sender, 
	const Message&	message
	)
{
	if (sender == itsSuggestions && message.Is(JXSpellList::kChoiceSelected))
		{
		JXSpellList::ChoiceSelected* choice = 
			dynamic_cast(JXSpellList::ChoiceSelected*, &message);
		assert(choice != NULL);
		JIndex index = choice->GetIndex();
		const JPtrArray<JString>& words = itsSuggestions->GetStringList();
		JString guess = *(words.NthElement(index));
		itsFirstGuess->SetText(guess);
		itsFirstGuess->Focus();
		itsIgnoreButton->SetShortcuts("");
		itsChangeButton->SetShortcuts("^M");
		}
	else if (sender == itsFirstGuess && message.Is(JTextEditor::kTextChanged))
		{
		itsIgnoreButton->SetShortcuts("");
		itsChangeButton->SetShortcuts("^M");
		}
	else if (message.Is(JXButton::kPushed))
		{
		if (sender == itsIgnoreButton)
			{
			itsCurrentAction = kIgnore;
			}
		else if (sender == itsChangeButton)
			{
			itsCurrentAction = kChange;
			}
		else if (sender == itsLearnButton)
			{
			itsCurrentAction = kLearn;
			}
		else if (sender == itsLearnCapsButton)
			{
			itsCurrentAction = kLearnCaps;
			}
		else if (sender == itsIgnoreAllButton)
			{
			itsCurrentAction = kIgnoreAll;
			}
		else if (sender == itsChangeAllButton)
			{
			itsCurrentAction = kChangeAll;
			}
		else if (sender == itsCloseButton)
			{
			itsCurrentAction = kClose;
			}
		Broadcast(SpellButtonPressed());
		}
	else
		{
		JXWindowDirector::Receive(sender, message);
		}
}

/******************************************************************************
 GetCorrection 

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

const JString& 
JXSpellCheckerDialog::GetCorrection()
{
	return itsFirstGuess->GetText();
}

/******************************************************************************
 GetCheckWord 

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

const JString& 
JXSpellCheckerDialog::GetCheckWord()
{
	return itsCheckText->GetText();
}

/******************************************************************************
 GetSpellAction 

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

JXSpellCheckerDialog::SpellAction
JXSpellCheckerDialog::GetSpellAction()
{
	return itsCurrentAction;
}

/******************************************************************************
 SetCheckWord 

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

void
JXSpellCheckerDialog::SetCheckWord
	(
	const JCharacter* word
	)
{
	itsCheckText->SetText(word);
}

/******************************************************************************
 SetFirstGuess 

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

void
JXSpellCheckerDialog::SetFirstGuess
	(
	const JCharacter* word
	)
{
	itsFirstGuess->SetText(word);
}

/******************************************************************************
 SetSuggestions 

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

void
JXSpellCheckerDialog::SetSuggestions
	(
	const JPtrArray<JString>* words
	)
{
	itsSuggestions->SetStringList(words);
}
