/*************************************************************************
 *
 *  $RCSfile: hintlist.cxx,v $
 *
 *  $Revision: 1.1.1.1 $
 *
 *  last change: $Author: hr $ $Date: 2000/09/18 17:14:59 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/

#ifdef PRECOMPILED
#include "filt_pch.hxx"
#endif

#pragma hdrstop

#ifdef XML_CORE_API

#include "hintids.hxx"

#ifndef _SFXPOOLITEM_HXX
#include <svtools/poolitem.hxx>
#endif
#ifndef _SVARRAY_HXX
#include <svtools/svarray.hxx>
#endif

#ifndef _ERRHDL_HXX
#include <errhdl.hxx>
#endif

#ifndef _HINTLIST_HXX
#include "hintlist.hxx"
#endif

SwHintSttEndPos::SwHintSttEndPos( const SfxPoolItem& rItem,
								  xub_StrLen nStt, xub_StrLen nE ) :
	nStart( nStt ),
	nEnd( nE ),
	pItem( rItem.Clone() )
{}

SwHintSttEndPos::~SwHintSttEndPos()
{
	delete pItem;
}

SwHintEndPosList::SwHintEndPosList()
{
	pStartLst = new SwHintSttEndPosList_Impl;
	pEndLst = new SwHintSttEndPosList_Impl;
}

SwHintEndPosList::~SwHintEndPosList()
{
	ASSERT( !pStartLst->Count(),
			"SwHintEndPosLst::~SwHintEndPosLst: start list is not empty" );
	ASSERT( !pEndLst->Count(),
			"SwHintEndPosLst::~SwHintEndPosLst: end list is not empty" );

	delete pStartLst;
	delete pEndLst;
}

sal_uInt16 SwHintEndPosList::_FindStartPos( const SwHintSttEndPos *pPos ) const
{
	for( sal_uInt16 i=0; i<pStartLst->Count() && (*pStartLst)[i]!=pPos;  i++ )
		;

	ASSERT( i != pStartLst->Count(), 
			"SwHintEndPosList::_FindStartPos: Didn't find hint" );

	return i==pStartLst->Count() ? USHRT_MAX : i;
}

void SwHintEndPosList::_InsertHint( SwHintSttEndPos *pPos, sal_uInt16 nEndPos )
{
	// The hint will be inserted into the start list behind all other hints
	// that are started at the same position.
	xub_StrLen nStart = pPos->GetStart();
	for( sal_uInt16 i=0; i < pStartLst->Count() &&
					 	 (*pStartLst)[i]->GetStart() <= nStart; i++ )
		;
	pStartLst->Insert( pPos, i );

	// The position within the end list is already known.
	pEndLst->Insert( pPos, nEndPos );
}

void SwHintEndPosList::_RemoveHint( sal_uInt16 nEndPos )
{
	SwHintSttEndPos *pPos = (*pEndLst)[nEndPos];

	// Get hint position within start list
	sal_uInt16 nStartPos = _FindStartPos( pPos );
	if( nStartPos != USHRT_MAX )
		pStartLst->Remove( nStartPos, 1 );

	pEndLst->Remove( nEndPos, 1 );

	delete pPos;
}

void SwHintEndPosList::InsertHint( const SfxPoolItem& rItem, xub_StrLen nStart,
								   xub_StrLen nEnd )
{
	for( sal_uInt16 i=0; i<pEndLst->Count(); i++ )
	{
		SwHintSttEndPos *pTest = (*pEndLst)[i];
		xub_StrLen nTestEnd = pTest->GetEnd();
		if( nTestEnd <= nStart )
		{
			// The existing hint ends before the new one starts
			// => The existing hint is not of interest, but maybe the ones
			//    that are following.
			continue;
		}
		else if( nTestEnd < nEnd )
		{
			// The existing hind ends before the new one ends
			// => The new hint has to be spplitted into to parts
			_InsertHint( new SwHintSttEndPos( rItem, nStart, nTestEnd ), i );
			nStart = nTestEnd;
		}
		else
		{
			// The existing hint (and all other existing hints contained in
			// the list) end behind the new hint. They are not of interest.
			// => The search can be stopped.
			break;
		}
	}

	// Insert the hint. It may be the last part of a splitted one.
	_InsertHint( new SwHintSttEndPos( rItem, nStart, nEnd ), i );
}

#endif

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

	  Source Code Control System - Header

	  $Header: /cvs/oo/sw/sw/source/filter/xml/hintlist.cxx,v 1.1.1.1 2000/09/18 17:14:59 hr Exp $

	  Source Code Control System - Update

	  $Log: hintlist.cxx,v $
	  Revision 1.1.1.1  2000/09/18 17:14:59  hr
	  initial import
	
	  Revision 1.6  2000/09/18 16:05:04  willem.vandorp
	  OpenOffice header added.
	
	  Revision 1.5  2000/07/31 09:42:35  mib
	  text export continued
	
	  Revision 1.4  2000/05/03 12:08:04  mib
	  unicode
	
	  Revision 1.3  2000/03/13 14:33:43  mib
	  UNO3
	
	  Revision 1.2  1999/11/19 15:26:16  mib
	  Opt: some methods made inline
	
	  Revision 1.1  1999/09/28 08:29:32  mib
	  Hint list
	

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

