//		IconPane.cpp
//		http://www-acs.ucsd.edu/~jstrout/macdev/panes/
//		last modified: 11/10/97

#include "IconPane.h"

IconPane::IconPane( const int firstCicnNum, const int qtyIcons ) : 
		Pane(), mQtyIcons(qtyIcons), mIcons(0), mState(0), mPopBack(false)
{
	mIcons = new CIconHandle[qtyIcons];
	if (!mIcons) {
		mQtyIcons = 0;
		return;
	}
	for (int i=0; i<qtyIcons; i++) {
		mIcons[i] = GetCIcon(firstCicnNum+i);
	}
	if (mIcons[0])
		mFrame = (*mIcons[0])->iconPMap.bounds;
}

IconPane::~IconPane()
{
	for (int i=0; i<mQtyIcons; i++) {
		if (mIcons[i]) DisposeCIcon(mIcons[i]);
	}
}

void IconPane::Draw()
{
	if (mHidden) return;
	if (mState >= 0 && mState < mQtyIcons && mIcons[mState]) {
		PlotCIcon( &mFrame, mIcons[mState] );
	}
}

Boolean IconPane::Click(Point where, short modifiers)
{
	if (mHidden) return false;
	short prevState = mState, newState = (mState+1)%mQtyIcons;
	mState = newState;
	Draw();
	// while the mouse is down, track it, and revert to previous state
	// whenever the mouse leaves the frame
	while (StillDown()) {
		Point m;
		GetMouse(&m);
		if (not Contains(m) and mState == newState) {
			mState = prevState;
			Draw();
		} else if (Contains(m) and mState == prevState) {
			mState = newState;
			Draw();
		}
	}
	// now, if we end up outside the frame, return click not handled
	if (mState == prevState) return false;
	
	if (mPopBack) {
		// if we're supposed to pop back, do so now
		mState = prevState;
		Draw();
	}
	
	return true;
}
