/******************************************************************************
 JXSRFullDialog.cc

	This is used as a global object.  We relay all the requests for action
	to the active text editor.  This way, all the text editors share the
	same dialog window.

	BASE CLASS = JXSRTextDialogBase

	Copyright © 1996 by John Lindal. All rights reserved.

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

#include "JXSRFullDialog.h"
#include "JXRegexHelpText.h"

#include <JXDisplay.h>
#include <JXWindow.h>
#include <JXStaticText.h>
#include <JXTextButton.h>
#include <JXInputField.h>
#include <JXTextCheckbox.h>
#include <JXRadioGroup.h>
#include <JXTextRadioButton.h>
#include <JXStringHistoryMenu.h>

#include <JString.h>

#include <jAssert.h>

const JSize kHistoryLength = 20;

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

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

JXSRFullDialog::JXSRFullDialog
	(
	JXDirector* supervisor
	)
	:
	JXSRTextDialogBase(supervisor)
{
	BuildWindow();

//	CBPrefsManager* prefsMgr = CBGetPrefsManager();
//	prefsMgr->ReadSearchTextPrefs(this);

//	JString fontName;
//	JSize fontSize;
//	prefsMgr->GetDefaultFont(&fontName, &fontSize);
//	SetFont(fontName, fontSize);
}

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

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

JXSRFullDialog::~JXSRFullDialog()
{
//	(CBGetPrefsManager())->WriteSearchTextPrefs(this);
}

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

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

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

    JXWindow* window = new JXWindow(this, 440,300, "");
    assert( window != NULL );
    SetWindow(window);

    itsSearchRG =
        new JXRadioGroup(window,
                    JXWidget::kFixedLeft, JXWidget::kFixedTop, 80,10, 184,44);
    assert( itsSearchRG != NULL );

    itsCloseButton =
        new JXTextButton("Close", window,
                    JXWidget::kFixedRight, JXWidget::kFixedTop, 330,180, 100,20);
    assert( itsCloseButton != NULL );
    itsCloseButton->SetShortcuts("#W^[");

    itsFindButton =
        new JXTextButton("Find", window,
                    JXWidget::kFixedRight, JXWidget::kFixedTop, 329,19, 102,22);
    assert( itsFindButton != NULL );
    itsFindButton->SetShortcuts("#G^M");

    JXStaticText* obj1 =
        new JXStaticText("Search for:", window,
                    JXWidget::kFixedLeft, JXWidget::kFixedTop, 20,61, 80,20);
    assert( obj1 != NULL );

    JXStaticText* obj2 =
        new JXStaticText("Replace with:", window,
                    JXWidget::kFixedLeft, JXWidget::kFixedTop, 20,131, 90,20);
    assert( obj2 != NULL );

    itsIgnoreCaseCB =
        new JXTextCheckbox("Ignore case", window,
                    JXWidget::kFixedLeft, JXWidget::kFixedTop, 20,210, 110,20);
    assert( itsIgnoreCaseCB != NULL );
    itsIgnoreCaseCB->SetShortcuts("#I");

    itsWrapSearchCB =
        new JXTextCheckbox("Wrap search", window,
                    JXWidget::kFixedLeft, JXWidget::kFixedTop, 20,240, 110,20);
    assert( itsWrapSearchCB != NULL );
    itsWrapSearchCB->SetShortcuts("#S");

    itsReplaceButton =
        new JXTextButton("Replace", window,
                    JXWidget::kFixedRight, JXWidget::kFixedTop, 330,50, 100,20);
    assert( itsReplaceButton != NULL );
    itsReplaceButton->SetShortcuts("#R#=");

    itsReplaceAllButton =
        new JXTextButton("Replace all", window,
                    JXWidget::kFixedRight, JXWidget::kFixedTop, 330,110, 100,20);
    assert( itsReplaceAllButton != NULL );

    itsReplaceFindButton =
        new JXTextButton("Replace & Find", window,
                    JXWidget::kFixedRight, JXWidget::kFixedTop, 330,80, 100,20);
    assert( itsReplaceFindButton != NULL );
    itsReplaceFindButton->SetShortcuts("#L");

    JXStaticText* obj3 =
        new JXStaticText("Search:", window,
                    JXWidget::kFixedLeft, JXWidget::kFixedTop, 20,20, 60,20);
    assert( obj3 != NULL );

    JXTextRadioButton* obj4 =
        new JXTextRadioButton(1, "Forward", itsSearchRG,
                    JXWidget::kFixedLeft, JXWidget::kFixedTop, 10,10, 80,20);
    assert( obj4 != NULL );
    obj4->SetShortcuts("#F");

    JXTextRadioButton* obj5 =
        new JXTextRadioButton(2, "Backward", itsSearchRG,
                    JXWidget::kFixedLeft, JXWidget::kFixedTop, 90,10, 80,20);
    assert( obj5 != NULL );
    obj5->SetShortcuts("#B");

    itsEntireWordCB =
        new JXTextCheckbox("Entire words only", window,
                    JXWidget::kFixedLeft, JXWidget::kFixedTop, 20,270, 130,20);
    assert( itsEntireWordCB != NULL );
    itsEntireWordCB->SetShortcuts("#E");

    itsPrevReplaceMenu =
        new JXStringHistoryMenu(kHistoryLength, "", window,
                    JXWidget::kFixedRight, JXWidget::kFixedTop, 290,160, 30,20);
    assert( itsPrevReplaceMenu != NULL );

    itsPrevSearchMenu =
        new JXStringHistoryMenu(kHistoryLength, "", window,
                    JXWidget::kFixedRight, JXWidget::kFixedTop, 290,90, 30,20);
    assert( itsPrevSearchMenu != NULL );

    itsSearchInput =
        new JXInputField(kTrue, kFalse, window,
                    JXWidget::kHElastic, JXWidget::kFixedTop, 20,80, 260,40);
    assert( itsSearchInput != NULL );

    itsReplaceInput =
        new JXInputField(kTrue, kFalse, window,
                    JXWidget::kHElastic, JXWidget::kFixedTop, 20,150, 260,40);
    assert( itsReplaceInput != NULL );

    itsHelpButton =
        new JXTextButton("Help", window,
                    JXWidget::kFixedRight, JXWidget::kFixedTop, 330,150, 50,20);
    assert( itsHelpButton != NULL );

    itsSearchIsRegexCB =
        new JXTextCheckbox("Regex search (egrep)", window,
                    JXWidget::kFixedLeft, JXWidget::kFixedTop, 160,210, 150,20);
    assert( itsSearchIsRegexCB != NULL );

    itsReplaceIsRegexCB =
        new JXTextCheckbox("Regex replace", window,
                    JXWidget::kFixedLeft, JXWidget::kFixedTop, 320,210, 110,20);
    assert( itsReplaceIsRegexCB != NULL );

    itsSingleLineCB =
        new JXTextCheckbox("Treat as single line", window,
                    JXWidget::kFixedLeft, JXWidget::kFixedTop, 160,240, 150,20);
    assert( itsSingleLineCB != NULL );

    itsPreserveCaseCB =
        new JXTextCheckbox("Preserve case", window,
                    JXWidget::kFixedLeft, JXWidget::kFixedTop, 320,240, 110,20);
    assert( itsPreserveCaseCB != NULL );

    itsQRefButton =
        new JXTextButton("Ref", window,
                    JXWidget::kFixedRight, JXWidget::kFixedTop, 380,150, 50,20);
    assert( itsQRefButton != NULL );

// end JXLayout

	window->SetCloseAction(JXWindow::kDeactivateDirector);

	SetSearchObjects(itsSearchRG, itsCloseButton, itsFindButton,
					itsIgnoreCaseCB, itsWrapSearchCB, itsEntireWordCB,
					itsPrevSearchMenu, itsSearchInput, itsHelpButton,
					itsSearchIsRegexCB, itsSingleLineCB, itsPreserveCaseCB,
					itsQRefButton, "", "");
	SetReplaceObjects(itsReplaceButton, itsReplaceAllButton, itsReplaceFindButton,
					 itsPrevReplaceMenu, itsReplaceInput, itsReplaceIsRegexCB);

}
