/******************************************************************************
 JXToolBarEditWidget.cc

	<Description>

	BASE CLASS = public JXNamedTreeListWidget

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

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

#include <JXToolBarEditWidget.h>
#include <JXToolBarNode.h>

#include <JTreeList.h>

#include <itemchecked.xpm>
#include <itemnotchecked.xpm>
#include <menunode.xpm>

#include <JXMenuBar.h>
#include <JXTextMenu.h>
#include <JString.h>
#include <JXWindow.h>
#include <JXWindowPainter.h>
#include <JXDirector.h>
#include <JXSelectionManager.h>
#include <JXUserNotification.h>
#include <JXApplication.h>
#include <JXDNDManager.h>
#include <JXChooseSaveFile.h>
#include <JXDisplay.h>
#include <JXColormap.h>
#include <JXImage.h>
#include <jXCSFIcons.h>

#include <JFontManager.h>
#include <JTableSelection.h>
#include <JTableSelectionIterator.h>
#include <JOrderedSetIterator.h>
#include <JUNIXDirInfo.h>
#include <JUNIXDirEntry.h>
#include <JError.h>
#include <JStaticBuffer.h>

#include <jAssert.h>

const JSize kIdentWidth 	= 20;

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

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

JXToolBarEditWidget*
JXToolBarEditWidget::Create
	(
	JNamedTreeList* 	treeList, 
	JXScrollbarSet* 	scrollbarSet, 
	JXContainer* 		enclosure,
	const HSizingOption	hSizing, 
	const VSizingOption vSizing,
	const JCoordinate 	x, 
	const JCoordinate 	y,
	const JCoordinate 	w, 
	const JCoordinate 	h
	)
{
	JXToolBarEditWidget* widget =
		new JXToolBarEditWidget(treeList, 
			scrollbarSet,	enclosure, hSizing, vSizing, x, y, w, h);
	assert(widget != NULL);

	widget->JXTreeListWidgetX();
	return widget;
}

JXToolBarEditWidget::JXToolBarEditWidget
	(
	JNamedTreeList* 	treeList, 
	JXScrollbarSet* 	scrollbarSet, 
	JXContainer* 		enclosure,
	const HSizingOption	hSizing, 
	const VSizingOption vSizing,
	const JCoordinate 	x, 
	const JCoordinate 	y,
	const JCoordinate 	w, 
	const JCoordinate 	h
	)
	:
   JXNamedTreeListWidget(treeList,
   					  scrollbarSet,	enclosure, hSizing, vSizing, x, y, w, h)
{
	itsMenuImage = new JXImage(GetDisplay(), GetColormap(), JXPM(menunode));
	assert(itsMenuImage != NULL);

	itsCheckedItemImage = new JXImage(GetDisplay(), GetColormap(), JXPM(itemchecked));
	assert(itsCheckedItemImage != NULL);

	itsUncheckedItemImage = new JXImage(GetDisplay(), GetColormap(), JXPM(itemnotchecked));
	assert(itsUncheckedItemImage != NULL);
}

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

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

JXToolBarEditWidget::~JXToolBarEditWidget()
{
	delete itsMenuImage;
	delete itsCheckedItemImage;
	delete itsUncheckedItemImage;
}

/******************************************************************************
 GetIcon (protected)

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

JBoolean
JXToolBarEditWidget::GetImage
	(
	const JIndex 	index,
	const JXImage**	image
	)
	const
{
	const JTreeNode* node = GetTreeList()->GetNode(index);
	if (node->IsOpenable())
		{
		*image = itsMenuImage;
		return kTrue;
		}
	JXToolBarNode* tbnode = dynamic_cast(JXToolBarNode*, node);
	assert(tbnode != NULL);

	if (tbnode->IsChecked())
		{
		*image = itsCheckedItemImage;
		return kTrue;
		}
	*image = itsUncheckedItemImage;
	return kTrue;
}

/******************************************************************************
 HandleMouseDown (protected)

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

void
JXToolBarEditWidget::HandleMouseDown
	(
	const JPoint& 			pt, 
	const JXMouseButton 	button,
	const JSize 			clickCount,
 	const JXButtonStates& 	buttonStates,
	const JXKeyModifiers& 	modifiers
	)
{
	JPoint cell;	
	if (!GetCell(pt, &cell))
		{
		return;
		}

	JTreeNode* node = GetTreeList()->GetNode(cell.y);

	if ((cell.x == 2) && (!node->IsOpenable()))
		{
		JXToolBarNode* tbnode = dynamic_cast(JXToolBarNode*, node);
		assert(tbnode != NULL);
		tbnode->ToggleChecked();
		}
	JXNamedTreeListWidget::HandleMouseDown(pt, button, clickCount, buttonStates, modifiers);
}
