/******************************************************************************
 JXToolBarButton.cc

	<Description>

	BASE CLASS = public JXButton

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

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

#include "JXHintDirector.h"

#include <JXToolBarButton.h>
#include <JXWindow.h>
#include <JXDirector.h>
#include <JXApplication.h>
#include <JXTextMenu.h>
#include <JXImage.h>
#include <JXColormap.h>
#include <JXWindowPainter.h>

#include <JFontManager.h>

#include <jXGlobals.h>
#include <jXPainterUtil.h>

#include <jAssert.h>

const JCoordinate kLabelBuffer	= 10;
const JCoordinate kDualBuffer	= 5;
const Time kHintWaitDelay 		= 500;

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

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

JXToolBarButton::JXToolBarButton
	(
	JXTextMenu*			menu, 
	const JIndex 		index,
	const Type 			type,
	JXContainer* 		enclosure,
	const HSizingOption hSizing, 
	const VSizingOption vSizing,
	const JCoordinate 	x, 
	const JCoordinate 	y,
	const JCoordinate 	h
	)
	:
	JXButton(enclosure, hSizing, vSizing, x, y, h, h),
	itsMenu(menu),
	itsMenuItem(index),
	itsType(type),
	itsImage(NULL)
{
	itsLabel = menu->GetItemText(index);

	if (itsType != kText)
		{
		const JXImage* image;
		if (menu->GetItemImage(index, &image))
			{
			itsImage = const_cast<JXImage*>(image);
			}
		}
	if ((itsType == kImage) && (itsImage != NULL))
		{
		JString hint = itsLabel;
		JString shortCut;
		if (menu->GetItemNMShortcut(index, &shortCut))
			{
			hint += "   " + shortCut;
			}
		SetHint(hint);
		}
	else
		{
		menu->GetItemFont(index, &itsFontName, &itsFontSize, &itsFontStyle);
		const JFontManager* fm 	= GetFontManager();
		JSize width				= fm->GetStringWidth(itsFontName,
													 itsFontSize,
													 itsFontStyle, itsLabel);
		if ((itsType == kText) || (itsImage == NULL))
			{
			AdjustSize(width + 2*kLabelBuffer - h, 0);
			}
		else
			{
			AdjustSize(width + 2*kDualBuffer, 0);
			}

		itsTrueLabelColor = itsFontStyle.color;

		if (!IsActive())
			{
			itsFontStyle.color = (GetColormap())->GetInactiveLabelColor();
			}
		}
}

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

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

JXToolBarButton::~JXToolBarButton()
{
}

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


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

void
JXToolBarButton::Activate()
{
	const JBoolean wasActive = IsActive();
	JXButton::Activate();
	if (!wasActive && IsActive())
		{
		itsFontStyle.color = itsTrueLabelColor;
		Refresh();
		}
}

/******************************************************************************
 Deactivate (virtual)

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

void
JXToolBarButton::Deactivate()
{
	const JBoolean wasActive = IsActive();
	JXButton::Deactivate();
	if (wasActive && !IsActive())
		{
		itsFontStyle.color = (GetColormap())->GetInactiveLabelColor();
		Refresh();
		}
}

/******************************************************************************
 Draw (virtual protected)

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

void
JXToolBarButton::Draw
	(
	JXWindowPainter&	p,
	const JRect&		rect
	)
{
	const JRect bounds = GetBounds();

	if ((itsType == kImage) && (itsImage != NULL))
		{
		p.Image(*itsImage, itsImage->GetBounds(), bounds);
		}
	else if ((itsType == kText) || (itsImage == NULL))
		{
		p.SetFont(itsFontName, itsFontSize, itsFontStyle);
		p.String(bounds.left, bounds.top, itsLabel,
				 bounds.width(), JPainter::kHAlignCenter,
				 bounds.height(), JPainter::kVAlignCenter);
		}
	else
		{
		assert(itsImage != NULL);
		JRect ri = bounds;
		ri.right = bounds.left + bounds.height();
		p.Image(*itsImage, itsImage->GetBounds(), ri);
		JRect rt = bounds;
		rt.left = bounds.left + bounds.height();
		rt.right = bounds.right - kDualBuffer;
		p.SetFont(itsFontName, itsFontSize, itsFontStyle);
		p.String(rt.left, rt.top, itsLabel,
				 rt.width(), JPainter::kHAlignCenter,
				 rt.height(), JPainter::kVAlignCenter);
		}
}

/******************************************************************************
 DrawBorder (virtual protected)

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

void
JXToolBarButton::DrawBorder
	(
	JXWindowPainter&	p,
	const JRect&		frame
	)
{
	JSize borderWidth = GetBorderWidth();
	if (borderWidth > 0 && IsActive())
		{
		if ((IsPushed() && !(itsMenu->IsChecked(itsMenuItem))) ||
			(!IsPushed() && itsMenu->IsChecked(itsMenuItem)))
			{
			JXDrawDownFrame(p, frame, borderWidth);
			}
		else
			{
			JXDrawUpFrame(p, frame, borderWidth);
			}
		}
	else if (borderWidth > 0)
		{
		p.SetLineWidth(borderWidth);
		if (itsMenu->IsChecked(itsMenuItem))
			{
			p.SetPenColor((GetColormap())->GetWhiteColor());
			}
		else
			{
			p.SetPenColor((GetColormap())->GetInactiveLabelColor());
			}
		p.RectInside(frame);
		}

}

/******************************************************************************
 NeedsGeometryAdjustment (public)

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

JBoolean
JXToolBarButton::NeedsGeometryAdjustment()
{
	itsLabel 			= itsMenu->GetItemText(itsMenuItem);

	const JXImage* image;
	if (itsMenu->GetItemImage(itsMenuItem, &image))
		{
		itsImage = const_cast<JXImage*>(image);
		}

	if ((itsType == kImage) && (itsImage != NULL))
		{
		JString hint = itsLabel;
		JString shortCut;
		if (itsMenu->GetItemNMShortcut(itsMenuItem, &shortCut))
			{
			hint += "   " + shortCut;
			}
		SetHint(hint);
		}
	else
		{
		itsMenu->GetItemFont(itsMenuItem, &itsFontName, &itsFontSize, &itsFontStyle);
		const JFontManager* fm 	= GetFontManager();
		JSize width				= fm->GetStringWidth(itsFontName,
													 itsFontSize,
													 itsFontStyle, itsLabel);
		JSize newSize;
		if ((itsType == kText) || (itsImage == NULL))
			{
			newSize = width + 2*kLabelBuffer, 0;
			}
		else
			{
			newSize = width + 2*kDualBuffer + GetFrameHeight();
			}

		itsTrueLabelColor = itsFontStyle.color;

		if (!IsActive())
			{
			itsFontStyle.color = (GetColormap())->GetInactiveLabelColor();
			}

		JSize delta = newSize - GetFrameWidth();
		if (delta != 0)
			{
			AdjustSize(delta,0);
			return kTrue;
			}
		}
	return kFalse;
}
