/******************************************************************************
 SyGEditPrefsDialog.cc

	BASE CLASS = JXDialogDirector

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

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

#include "SyGEditPrefsDialog.h"

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

#include <JString.h>
#include <jAssert.h>

enum
{
	kMoveAction = 1,
	kCopyAction
};

enum
{
	kTrashAction = 1,
	kDeleteAction
};


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

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

SyGEditPrefsDialog::SyGEditPrefsDialog
	(
	JXDirector*					supervisor,
	const JCharacter*			terminalCmd,
	const JCharacter*			manViewCmd,
	const JBoolean 				defMove,
	const JBoolean 				bsTrash,
	const JBoolean 				warn
	)
	:
	JXDialogDirector(supervisor, kTrue)
{
	BuildWindow(terminalCmd, manViewCmd, defMove, bsTrash, warn);
}

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

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

SyGEditPrefsDialog::~SyGEditPrefsDialog()
{
}

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

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

void
SyGEditPrefsDialog::BuildWindow
	(
	const JCharacter*			terminalCmd,
	const JCharacter*			manViewCmd,
	const JBoolean	 			defMove,
	const JBoolean	 			bsTrash,
	const JBoolean 				warn
	)
{
// begin JXLayout

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

    itsManInput =
        new JXInputField(window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 190,185, 245,20);
    assert( itsManInput != NULL );

    itsTerminalInput =
        new JXInputField(window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 190,225, 245,20);
    assert( itsTerminalInput != NULL );

    JXStaticText* obj1 =
        new JXStaticText("Shell cmd to open terminal:", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 10,225, 165,20);
    assert( obj1 != NULL );

    JXStaticText* obj2 =
        new JXStaticText("Shell cmd to view man page:", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 10,185, 175,20);
    assert( obj2 != NULL );

    JXTextButton* cancelButton =
        new JXTextButton("Cancel", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 95,270, 70,20);
    assert( cancelButton != NULL );
    cancelButton->SetShortcuts("^[");

    JXTextButton* okButton =
        new JXTextButton("OK", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 295,270, 70,20);
    assert( okButton != NULL );
    okButton->SetShortcuts("^M");

    JXStaticText* obj3 =
        new JXStaticText("(use $n in place of  folder name)", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 185,245, 215,20);
    assert( obj3 != NULL );

    itsDefAction =
        new JXRadioGroup(window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 30,30, 180,90);
    assert( itsDefAction != NULL );

    JXStaticText* obj4 =
        new JXStaticText("Default action:", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 40,23, 90,20);
    assert( obj4 != NULL );

    JXTextRadioButton* obj5 =
        new JXTextRadioButton(1, "Move", itsDefAction,
                    JXWidget::kHElastic, JXWidget::kVElastic, 10,15, 150,30);
    assert( obj5 != NULL );

    JXTextRadioButton* obj6 =
        new JXTextRadioButton(2, "Copy", itsDefAction,
                    JXWidget::kHElastic, JXWidget::kVElastic, 10,45, 150,30);
    assert( obj6 != NULL );

    itsBSAction =
        new JXRadioGroup(window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 240,30, 180,90);
    assert( itsBSAction != NULL );

    JXStaticText* obj7 =
        new JXStaticText("Backspace action:", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 250,23, 115,20);
    assert( obj7 != NULL );

    JXTextRadioButton* obj8 =
        new JXTextRadioButton(1, "Move to trash", itsBSAction,
                    JXWidget::kHElastic, JXWidget::kVElastic, 10,15, 150,30);
    assert( obj8 != NULL );

    JXTextRadioButton* obj9 =
        new JXTextRadioButton(2, "Delete from file system", itsBSAction,
                    JXWidget::kHElastic, JXWidget::kVElastic, 10,45, 165,30);
    assert( obj9 != NULL );

    itsWarnBeforeTrashCB =
        new JXTextCheckbox("Warn before moving to trash", window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 35,135, 195,30);
    assert( itsWarnBeforeTrashCB != NULL );

// end JXLayout

	window->SetTitle("Edit preferences");
	SetButtons(okButton, cancelButton);

	itsManInput->SetText(manViewCmd);
	itsManInput->SetIsRequired();

	itsTerminalInput->SetText(terminalCmd);
	itsTerminalInput->SetIsRequired();

	itsWarnBeforeTrashCB->SetState(warn);
	if (defMove)
		{
		itsDefAction->SelectItem(kMoveAction);
		}
	else
		{
		itsDefAction->SelectItem(kCopyAction);
		}

	if (bsTrash)
		{
		itsBSAction->SelectItem(kTrashAction);
		}
	else
		{
		itsBSAction->SelectItem(kDeleteAction);
		}
	
}

/******************************************************************************
 GetPrefs

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

void
SyGEditPrefsDialog::GetPrefs
	(
	JString*			terminalCmd,
	JString*			manViewCmd,
	JBoolean* 			defMove, 
	JBoolean* 			bsTrash,
	JBoolean* 			warn
	)
	const
{
	*terminalCmd    = itsTerminalInput->GetText();
	*manViewCmd 	= itsManInput->GetText();

	if (itsDefAction->GetSelectedItem() == kMoveAction)
		{
		*defMove = kTrue;
		}
	else
		{
		*defMove = kFalse;
		}
	if (itsBSAction->GetSelectedItem() == kTrashAction)
		{
		*bsTrash = kTrue;
		}
	else
		{
		*bsTrash = kFalse;
		}
	*warn = itsWarnBeforeTrashCB->IsChecked();
}
