/***************************************************************************
TMultiple Box:
	written by Michael Ogrinz       70007,6544
	September,1992

Usage:  Simply use this in a dialog exactly as you would a TListBox. When
	you assign the collection (with the newList method) make sure that
	the items in the list start with a blank space so that the
	marked tag will not erase the first character of the list item.

	Items can be marked either by pressing Enter, or clicking on
	them with the left mouse button. The following hot-keys are
	also defined:
	
	kbF7: Mark all items.
	kbF8: Unmark all items.
	kbF9: Toggle Marked/Unmarked items.

	Please inform me of any comments, concerns, etc. This is the first
	piece of code I've ever u/l'd to CIS!
****************************************************************************/


class TMultipleBox:public TListBox
{
   private:
	void ToggleItem(int item_no);
	void MarkAll();
	void UnMarkAll();
	void ToggleAll();
   public:
	//simply calls base class constructor for now:
	TMultipleBox(const TRect& bounds, ushort aNumCols,
		     TScrollBar *aScrollBar);
	char tag;
	virtual void handleEvent(TEvent& event);
};

TMultipleBox::TMultipleBox(const TRect& bounds, ushort aNumCols,
			   TScrollBar *aScrollBar)
	     :TListBox(bounds,aNumCols,aScrollBar)
{
	// calls base class constructor; sets tag character.
	tag='';
}

void TMultipleBox::handleEvent(TEvent& event)
{
	// we dont want to lose mouse button events to the main handler....
	if ((event.what==evMouseDown) && (event.mouse.buttons==mbLeftButton))
	{
		TListBox::handleEvent(event);
		ToggleItem(focused);
		TListBox::draw();
	}
	else
		TListBox::handleEvent(event);

	switch (event.what)
	{
		case evKeyDown:
			switch(event.keyDown.keyCode)
			{
				case kbEnter:
					ToggleItem(focused);
					break;
				case kbF7:
					MarkAll();
					break;
				case kbF8:
					UnMarkAll();
					break;
				case kbF9:
					ToggleAll();
					break;
			}
			TListBox::draw();
			break;
	}
}

void TMultipleBox::ToggleItem(int item_no)
{
	if (range<=0)
		return;

	char *itemaddr;
	itemaddr=(char *)(list()->at(item_no));
	if (itemaddr[0]==tag)
		itemaddr[0]=' ';
	else
		itemaddr[0]=tag;
}

void TMultipleBox::MarkAll()
{
	if (range<=0)
		return;
	char *itemaddr;
	for (int i=0;i<range;i++)
	{
		itemaddr=(char *)(list()->at(i));
		itemaddr[0]=tag;
	}
}

void TMultipleBox::UnMarkAll()
{
	if (range<=0)
		return;
	char *itemaddr;
	for (int i=0;i<range;i++)
	{
		itemaddr=(char *)(list()->at(i));
		itemaddr[0]=' ';
	}
}

void TMultipleBox::ToggleAll()
{
	if (range<=0)
		return;
	char *itemaddr;
	for (int i=0;i<range;i++)
	{
		itemaddr=(char *)(list()->at(i));
		if (itemaddr[0]==tag)
			itemaddr[0]=' ';
		else
			itemaddr[0]=tag;
	}
}
/***************************************************************************/
