/******************************************************************************
 JXToolBarEditDir.cc

	<Description>

	BASE CLASS = public JXDocument

	Copyright © 1998 by Glenn W. Bach.  All rights reserved.
	
	Base code generated by Codemill v0.1.0

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

// includes

//Class Header
#include <JXToolBarEditDir.h>
#include <JTree.h>
#include <JXToolBarNode.h>
#include <JXToolBarEditWidget.h>
#include <JNamedTreeList.h>

#include <JXToolBar.h>

#include <JXWindow.h>
#include <JXStaticText.h>
#include <JXMenuBar.h>
#include <JXScrollbarSet.h>
#include <JXApplication.h>
#include <JXTextButton.h>
#include <JXDocumentManager.h>
#include <JXDocumentMenu.h>
#include <jXCSFIcons.h>
#include <JXImage.h>
#include <JXTextCheckbox.h>
#include <JXAtLeastOneCBGroup.h>

#include <JChooseSaveFile.h>
#include <JString.h>
#include <JFileArray.h>

#include <iostream.h>
#include <jAssert.h>

const JCoordinate kCurrentPrefsVersion	= 1;
const JIndex kTypeCount					= 3;

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

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

JXToolBarEditDir::JXToolBarEditDir
	(
	JTree* 						tree,
	const JBoolean 				show, 
	const JBoolean 				useSmall,
	const JXToolBarButton::Type	type,
	JXDirector* 				supervisor
	)
	:
   JXDialogDirector(supervisor, kTrue),
   itsTree(tree),
   itsTreeChanged(kFalse)
{
	UseModalPlacement(kFalse);

	BuildWindow();
	ListenTo(itsTree);

	itsShowToolBarCB->SetState(show);
	itsUseSmallButtonsCB->SetState(useSmall);
	if ((type == JXToolBarButton::kImage) || (type == JXToolBarButton::kBoth))
		{
		itsShowImagesCB->SetState(kTrue);
		}
	if ((type == JXToolBarButton::kText) || (type == JXToolBarButton::kBoth))
		{
		itsShowTextCB->SetState(kTrue);
		}
	
}

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

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

JXToolBarEditDir::~JXToolBarEditDir()
{
}

/******************************************************************************
 BuildWindow 

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

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

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

    JXScrollbarSet* scrollbarSet =
        new JXScrollbarSet(window,
                    JXWidget::kHElastic, JXWidget::kVElastic, 15,100, 290,290);
    assert( scrollbarSet != NULL );

    JXTextButton* cancelButton =
        new JXTextButton("Cancel", window,
                    JXWidget::kFixedLeft, JXWidget::kFixedBottom, 50,400, 70,20);
    assert( cancelButton != NULL );
    cancelButton->SetShortcuts("^[");

    JXTextButton* okButton =
        new JXTextButton("Ok", window,
                    JXWidget::kFixedRight, JXWidget::kFixedBottom, 200,400, 70,20);
    assert( okButton != NULL );
    okButton->SetShortcuts("^M");

    JXStaticText* obj1 =
        new JXStaticText("Check off the menu items below that you\nwant to appear on the toolbar.", window,
                    JXWidget::kHElastic, JXWidget::kFixedTop, 20,10, 270,40);
    assert( obj1 != NULL );

    itsShowToolBarCB =
        new JXTextCheckbox("Show toolbar", window,
                    JXWidget::kFixedLeft, JXWidget::kFixedTop, 20,50, 135,20);
    assert( itsShowToolBarCB != NULL );

    itsUseSmallButtonsCB =
        new JXTextCheckbox("Use small buttons", window,
                    JXWidget::kFixedLeft, JXWidget::kFixedTop, 20,70, 135,20);
    assert( itsUseSmallButtonsCB != NULL );

    itsShowImagesCB =
        new JXTextCheckbox("Show images", window,
                    JXWidget::kFixedLeft, JXWidget::kFixedTop, 160,50, 140,20);
    assert( itsShowImagesCB != NULL );

    itsShowTextCB =
        new JXTextCheckbox("Show text", window,
                    JXWidget::kFixedLeft, JXWidget::kFixedTop, 160,70, 130,20);
    assert( itsShowTextCB != NULL );

// end JXLayout

	window->SetTitle("Configure Toolbar");
	window->PlaceAsDialogWindow();
	window->LockCurrentMinSize();

	SetButtons(okButton, cancelButton);
    
	JXAtLeastOneCBGroup* group =
		new JXAtLeastOneCBGroup(2, itsShowImagesCB, itsShowTextCB);
	assert( group != NULL );

    JNamedTreeList* list = new JNamedTreeList(itsTree);
    assert(list != NULL);
    
    itsWidget = 
    	JXToolBarEditWidget::Create(list,
    		scrollbarSet, scrollbarSet->GetScrollEnclosure(),
    		JXWidget::kHElastic, JXWidget::kVElastic,
    		0,0,10,10);
    assert(itsWidget != NULL);
    itsWidget->FitToEnclosure();
    ListenTo(itsWidget);
}

/******************************************************************************
 ReadPrefs 

	This uses assert() to check the version number because we cannot use
	a marker to skip the data (since it contains strings), so it is the
	client's responsibility to check whether or not the file can be
	read (via a higher level version number).

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

void
JXToolBarEditDir::ReadSetup
	(
	istream& is
	)
{
	JCoordinate version;
	is >> version;
	assert( version <= kCurrentPrefsVersion );
	GetWindow()->ReadGeometry(is);
}

/******************************************************************************
 WritePrefs 

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

void
JXToolBarEditDir::WriteSetup
	(
	ostream& os
	)
{
	os << kCurrentPrefsVersion << ' ';
	GetWindow()->WriteGeometry(os);
}

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

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

void
JXToolBarEditDir::Receive
	(
	JBroadcaster*  sender, 
	const Message& message
	)
{
	if (sender == itsTree && message.Is(JTree::kNodeChanged))
		{
		itsTreeChanged = kTrue;
		}
	else
		{
		JXDialogDirector::Receive(sender, message);
		}
}

/******************************************************************************
 ShowToolBar (public)

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

JBoolean
JXToolBarEditDir::ShowToolBar()
{
	return itsShowToolBarCB->IsChecked();
}

/******************************************************************************
 UseSmallButtons (public)

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

JBoolean
JXToolBarEditDir::UseSmallButtons()
{
	return itsUseSmallButtonsCB->IsChecked();
}

/******************************************************************************
 GetType (public)

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

JXToolBarButton::Type
JXToolBarEditDir::GetType()
{
	JBoolean images = itsShowImagesCB->IsChecked();
	JBoolean text 	= itsShowTextCB->IsChecked();

	if (images && !text)
		{
		return JXToolBarButton::kImage;
		}
	else if (text && !images)
		{
		return JXToolBarButton::kText;
		}
	return JXToolBarButton::kBoth;
}
