// ***************************************************************
//
// 	BlugsExtensions.c
//
//	Some useful functions that extend the Blugs API.
//	They are all more or less wrapper functions created
//	around the standard Blugs API. 
//
// 	© Andreas Petterson 2001
//
//	You may incorporate this source code into your program(s) without
//	any restrictions.
//
//	Blugs is copyright by Brian S. Hall and can be found at www.blugs.com.
//
//	Version history
//	---------------
//
//	Version 1.2.1	-	2001/11/21	Uses the version of BLGetSelect found in Blugs 1.1
//
//  Version 1.2		-	2001/10/01	Removed BLSelectAll since it has migrated into the standard Blugs API as of Blugs 1.0
//									Changed BLCountSelectedRows to use the version of BLGetSelect found in Blugs 1.0
//
//	Version 1.1		-	2001/06/16	Added BLCountSelectedRows
//
//	Version 1.0		- 	2001/06/01	First release
//
// ***************************************************************


#include "Blugs.h"


// ************************************************************************************ BLClearList
// Delete all rows except the title bar from a Blugs list.
// Input: 	inList = the list to be cleared
// Output: 	none
// Return: 	an error code or noErr
// -------------------------------

OSErr BLClearList(BlugsRef inList)
{
	UInt16		rows;
	OSErr		theErr;
	
	rows = BLCountRows(inList);
	theErr = BLDeleteRows(rows, 1 , inList);
	
	return theErr;
}



// ***************************************************************************************** BLGrow
// Grow or shrink a Blugs list. The top left corner of the list remains unchanged.
// Input: 	inList = the list to grow or shrink
//			deltaH = the number of pixels the list should grow horizontally
//			deltaV = the number of pixels the list should grow vertically
//			Pass a negative value to shrink the list and a positive to grow it.
//	Output: none
//	Return: an error code or noErr
// -------------------------------

OSErr BLGrow(BlugsRef inList, SInt32 deltaH, SInt32 deltaV)
{
	OSErr		theErr;
	Rect		listRect;
	
	theErr = BLGetRect(&listRect, inList);	
	listRect.bottom += deltaV;
	listRect.right += deltaH;
	if(!theErr)
		theErr = BLSetRect(&listRect, inList);
		
	return theErr;
}


// *************************************************************************** BLCountSelectedRows
// Counts the number of rows with at least one selected cell in it
// Input:	inList = the list to count rows in
// Output:	the number of rows with selected cells
// -----------------------------------------------

UInt16 BLCountSelectedRows(BlugsRef inList)
{
	OSErr		theErr = noErr;
	BLCell		cell = {1,1};
	UInt16		oldRow = 0, rowCount = 0, maxRows;
	
	maxRows = BLCountRows(inList);
	
	while(!theErr && cell.row <= maxRows)
	{
		theErr = BLGetSelect(blCellGetSelectMethod, &cell, inList);	// Let Blugs find the next selected cell
		if(!theErr)
		{
			if(cell.row != oldRow)					// Was it on the same row as the last found cell?
			{
				rowCount++;							// If not increment number of selected rows
				oldRow = cell.row;					// Remember this row
										
													// Since we only count rows, we don't care about
				cell.row++;							// any other selected cells on this row. Start
				cell.col = 1;						// next search from the first cell on the next row.
			}
		}
	}
	
	return rowCount;
}