The attached almost works, but fails sometimes because start_pos and end_pos 
are sometimes calculated as both 0, despite the fact that I have 17 rows!

I know that I shoudl return end_pos as the /end/ of the last row, not the 
front but can you gve me further clues.

Angus
/**
 *  \file PreviewSupport.C
 *  Copyright 2002 the LyX Team
 *  Read the file COPYING
 *
 * \author Angus Leeming <[EMAIL PROTECTED]>
 */

#include <config.h>

#ifdef __GNUG__
#pragma implementation
#endif

#include "PreviewSupport.h"

#include "debug.h"

#include "BufferView.h"
#include "lyxtext.h"
#include "lyxrow.h"
#include "paragraph.h"
#include "frontends/Painter.h"

using std::endl;
using std::list;

typedef list<Row const *> RowList;

RowList const getVisibleRows(BufferView const & bv)
{
	RowList rows;

	// top_y is not const because it's reset by getRowNearY.
	int top_y = bv.text->first_y;
	Row const * row = bv.text->getRowNearY(top_y);

	int const bv_height = bv.painter().paperHeight();
	int height = row->height();
	rows.push_back(row);

	while (height < bv_height) {
		row = row->next();
		height += row->height();
		rows.push_back(row);
	}

	return rows;
}

bool isInsetVisible(Inset const & inset, RowList const & rows)
{
	if (rows.empty())
		return false;

	Row const * first = rows.front();
	Row const * last  = rows.back();
	
	lyx::pos_type start_pos = first->pos();
	lyx::pos_type end_pos   = last->pos();
	if (end_pos < start_pos)
		std::swap(start_pos, end_pos);
	
	lyxerr << "first " << first
	       << " last " << last
	       << " start_pos " << start_pos
	       << " end_pos " << end_pos
	       << std::endl;

	// Loop over all insets in these rows lying between start_pos and
	// end_pos and compare to inset
	RowList::const_iterator rit  = rows.begin();
	RowList::const_iterator rend = rows.end();
	Paragraph * par = 0;

	for (; rit != rend; ++rit) {
		Paragraph * rpar = (*rit)->par();
		if (rpar == par)
			continue;

		par = rpar;
		Paragraph::inset_iterator iit  = par->inset_iterator_begin();
		Paragraph::inset_iterator iend = par->inset_iterator_end();

		lyx::pos_type pos = 0;
		for (; iit != iend; ++iit) {
			pos = iit.getPos();
			lyxerr << "inset, " << pos << std::endl;
			if (pos >= start_pos && pos <= end_pos) {
				if (*iit == &inset)
					return true;
			}
		}
	}

	return false;
}

Reply via email to