/*	XGListControl.cpp
 *
 *		This is the control. This contains two scroll bars and the
 *	scroll view object
 */

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

  YAAF is Copyright 1997-1999 by William Woody.

  Redistribution and use in source and binary forms, with or without 
  modification, are permitted provided that the following conditions 
  are met:

  Redistributions of source code must retain the above copyright notice, 
  this list of conditions and the following disclaimer.

  Neither the name of the copyright holder or other contributors may
  be used to promote products derived from this software without
  specific written prior permission.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 
  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 
  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
  SOFTWARE.
  
  Contact:   woody@alumni.caltech.edu
  Web page:  http://www.pandawave.com/yaaf/

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

#include <XConfig.h>			// must be first to optimize compile on MSVC++
#include <XList.h>
#include <XDraw.h>
#include <string.h>

/************************************************************************/
/*																		*/
/*	Construction/Destruction											*/
/*																		*/
/************************************************************************/

/*$	XGListControl::XGListControl
 *
 *		Create the list controls
 */

XGListControl::XGListControl(XGView *owner, XGArgStream &s) :
	XGScrollView(owner,s), fList(sizeof(bool))
{
	fUpdate = true;
	fNeedUpdate = false;
	
	fMultiSelect = s.ReadBoolean();
	fFont = XGFont::LoadFont(s.ReadInteger());
	fSwitchSelect = s.ReadBoolean();

	fView = new XGListView(this);
}

/*$	XGListControl::~XGListControl
 *
 *		Delete this.
 */

XGListControl::~XGListControl()
{
}

/************************************************************************/
/*																		*/
/*	Control List Management												*/
/*																		*/
/************************************************************************/

/*$	XGListControl::GetItem
 *
 *		Get the specified item in a zero-based index
 */

void XGListControl::GetItem(long index, char *data) const
{
	fList.GetString(index,data);
}


void XGListControl::SetItem( long index, const char *data )
{
	char old[256];
	fList.GetString( index, old );
	if (strcmp( data, old ) == 0) return;
	
	fList.SetString( index, data );
	if (fUpdate) {
		fView->RedrawLine(index);
	} else {
		fNeedUpdate = true;
	}
}

/*$	XGListControl::Length
 *
 *		# items in me
 */

long XGListControl::Length() const
{
	return fList.GetNumStrings();
}

/*$	XGListControl::Insert
 *
 *		Insert this item
 */

void XGListControl::Insert(long index, const char *buffer)
{
	fList.Insert(index,buffer);
	
	if (fUpdate) {
		fView->InvalView();
		fView->CalcResize();
	} else {
		fNeedUpdate = true;
	}
}

/*$	XGListControl::Delete
 *
 *		Delete this item
 */

void XGListControl::Delete(long index)
{
	fList.Delete(index);
	
	if (fUpdate) {
		fView->InvalView();
		fView->CalcResize();
	} else {
		fNeedUpdate = true;
	}
}

/*$	XGListControl::DeleteAll
 *
 *		Delete all items
 */

void XGListControl::DeleteAll(void)
{
	fList.DeleteAll();
	
	if (fUpdate) {
		fView->InvalView();
		fView->CalcResize();
	} else {
		fNeedUpdate = true;
	}
}

/*$	XGListControl::SetUpdate
 *
 *		Set the update
 */

void XGListControl::SetUpdate(bool update)
{
	if (fUpdate == update) return;

	if (update && !fUpdate) {
		if (fNeedUpdate) {
			fView->InvalView();
			fView->CalcResize();
			fNeedUpdate = false;
		}
	}
	fUpdate = update;
}

/*$	XGListControl::GetSelect
 *
 *		Get the selection state
 */

bool XGListControl::GetSelect(long index)
{
	bool retval;
	
	if (index < 0) return false;
	if (index >= Length()) return false;
	
	fList.GetExtra(index,&retval);
	return retval;
}

/*$	XGListControl::SetSelect
 *
 *		Set the selection state
 */

void XGListControl::SetSelect(long index, bool state)
{
	long i,len;
	bool b;
	bool ob;
	
	len = Length();
	if (index < 0) return;
	if (index >= len) return;
	
	/*
	 *	Change the state, redrawing the approriate lines
	 */
	
	fList.GetExtra(index,&ob);
	if (ob == state) return;
	if (!fMultiSelect && (state == true)) {
		for (i = 0; i < len; i++) {
			b = (index == i) ? true : false;
			fList.GetExtra(i,&ob);
			if (ob != b) {
				fList.SetExtra(i,&b);
				fView->RedrawLine(i);
			}
		}
	} else {
		if (ob != state) {
			fList.SetExtra(index,&state);
			fView->RedrawLine(index);
		}
	}
}

/*$	XGListControl::ClearSelect
 *
 *		Clear the selection state for everything
 */

void XGListControl::ClearSelect(void)
{
	long i,len;
	
	len = Length();
	for (i = 0; i < len; i++) SetSelect(i,false);
}

/*$	XGListControl::GetValue
 *
 *		Useful for single selection stuff; this returns the index
 *	of the first selected item, or -1 if none are selected
 */

long XGListControl::GetValue()
{
	long i,len;
	
	len = Length();
	for (i = 0; i < len; i++) {
		if (GetSelect(i)) return i;
	}
	return -1;
}

/*$	XGListControl::SetValue
 *
 *		Set only one selected item. Even if multiple selection is
 *	enabled
 */

void XGListControl::SetValue(long value)
{
	long i,len;
	
	len = Length();
	for (i = 0; i < len; i++) {
		SetSelect(i,(value == i));
	}
	fView->ScrollInRange(value);
}
