2004-03-01 17:12:09 +00:00
|
|
|
|
// -*- C++ -*-
|
|
|
|
|
/**
|
2007-04-26 04:41:58 +00:00
|
|
|
|
* \file DocIterator.h
|
2004-03-01 17:12:09 +00:00
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
|
*
|
|
|
|
|
* \author Andr<EFBFBD> P<EFBFBD>nitz
|
|
|
|
|
*
|
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef DOCITERATOR_H
|
|
|
|
|
#define DOCITERATOR_H
|
|
|
|
|
|
2007-04-26 04:41:58 +00:00
|
|
|
|
#include "CursorSlice.h"
|
2004-03-01 17:12:09 +00:00
|
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
|
2007-04-29 23:33:02 +00:00
|
|
|
|
class Text;
|
2004-03-18 12:53:43 +00:00
|
|
|
|
class MathAtom;
|
2004-03-01 17:12:09 +00:00
|
|
|
|
class Paragraph;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// The public inheritance should go in favour of a suitable data member
|
|
|
|
|
// (or maybe private inheritance) at some point of time.
|
2005-02-08 13:18:05 +00:00
|
|
|
|
class DocIterator // : public std::vector<CursorSlice>
|
2004-03-01 17:12:09 +00:00
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
/// type for cell number in inset
|
|
|
|
|
typedef CursorSlice::idx_type idx_type;
|
|
|
|
|
/// type for row indices
|
|
|
|
|
typedef CursorSlice::row_type row_type;
|
|
|
|
|
/// type for col indices
|
|
|
|
|
typedef CursorSlice::col_type col_type;
|
|
|
|
|
|
|
|
|
|
public:
|
2004-03-18 16:41:45 +00:00
|
|
|
|
///
|
2004-03-31 19:11:56 +00:00
|
|
|
|
DocIterator();
|
2004-03-18 16:41:45 +00:00
|
|
|
|
///
|
2007-04-29 13:39:47 +00:00
|
|
|
|
explicit DocIterator(Inset & inset);
|
2004-03-18 16:41:45 +00:00
|
|
|
|
|
2005-07-06 07:28:16 +00:00
|
|
|
|
/// access slice at position \p i
|
2005-07-15 15:49:40 +00:00
|
|
|
|
CursorSlice const & operator[](size_t i) const { return slices_[i]; }
|
2005-07-06 07:28:16 +00:00
|
|
|
|
/// access slice at position \p i
|
2005-07-15 15:49:40 +00:00
|
|
|
|
CursorSlice & operator[](size_t i) { return slices_[i]; }
|
|
|
|
|
/// chop a few slices from the iterator
|
2005-02-08 13:18:05 +00:00
|
|
|
|
void resize(size_t i) { slices_.resize(i); }
|
|
|
|
|
|
2004-03-27 12:37:41 +00:00
|
|
|
|
/// is the iterator valid?
|
|
|
|
|
operator const void*() const { return empty() ? 0 : this; }
|
|
|
|
|
/// is this iterator invalid?
|
|
|
|
|
bool operator!() const { return empty(); }
|
|
|
|
|
|
2005-07-06 07:28:16 +00:00
|
|
|
|
/// does this iterator have any content?
|
2005-02-08 13:18:05 +00:00
|
|
|
|
bool empty() const { return slices_.empty(); }
|
|
|
|
|
|
2004-03-01 17:12:09 +00:00
|
|
|
|
//
|
|
|
|
|
// access to slice at tip
|
|
|
|
|
//
|
|
|
|
|
/// access to tip
|
2005-02-08 13:18:05 +00:00
|
|
|
|
CursorSlice & top() { return slices_.back(); }
|
2004-03-01 17:12:09 +00:00
|
|
|
|
/// access to tip
|
2005-02-08 13:18:05 +00:00
|
|
|
|
CursorSlice const & top() const { return slices_.back(); }
|
2004-04-03 08:37:12 +00:00
|
|
|
|
/// access to outermost slice
|
2005-02-08 13:18:05 +00:00
|
|
|
|
CursorSlice & bottom() { return slices_.front(); }
|
2005-07-06 07:28:16 +00:00
|
|
|
|
/// access to outermost slice
|
2005-02-08 13:18:05 +00:00
|
|
|
|
CursorSlice const & bottom() const { return slices_.front(); }
|
2004-03-01 17:12:09 +00:00
|
|
|
|
/// how many nested insets do we have?
|
2005-02-08 13:18:05 +00:00
|
|
|
|
size_t depth() const { return slices_.size(); }
|
2004-03-01 17:12:09 +00:00
|
|
|
|
/// the containing inset
|
2007-04-29 13:39:47 +00:00
|
|
|
|
Inset & inset() const { return top().inset(); }
|
2004-03-01 17:12:09 +00:00
|
|
|
|
/// return the cell of the inset this cursor is in
|
2005-02-08 13:18:05 +00:00
|
|
|
|
idx_type idx() const { return top().idx(); }
|
2004-03-01 17:12:09 +00:00
|
|
|
|
/// return the cell of the inset this cursor is in
|
2005-02-08 13:18:05 +00:00
|
|
|
|
idx_type & idx() { return top().idx(); }
|
2004-03-01 17:12:09 +00:00
|
|
|
|
/// return the last possible cell in this inset
|
|
|
|
|
idx_type lastidx() const;
|
|
|
|
|
/// return the paragraph this cursor is in
|
2005-02-08 13:18:05 +00:00
|
|
|
|
pit_type pit() const { return top().pit(); }
|
2004-03-01 17:12:09 +00:00
|
|
|
|
/// return the paragraph this cursor is in
|
2005-02-08 13:18:05 +00:00
|
|
|
|
pit_type & pit() { return top().pit(); }
|
2004-03-01 17:12:09 +00:00
|
|
|
|
/// return the last possible paragraph in this inset
|
2004-11-24 21:53:46 +00:00
|
|
|
|
pit_type lastpit() const;
|
2004-03-01 17:12:09 +00:00
|
|
|
|
/// return the position within the paragraph
|
2005-02-08 13:18:05 +00:00
|
|
|
|
pos_type pos() const { return top().pos(); }
|
2004-03-01 17:12:09 +00:00
|
|
|
|
/// return the position within the paragraph
|
2005-02-08 13:18:05 +00:00
|
|
|
|
pos_type & pos() { return top().pos(); }
|
2004-03-01 17:12:09 +00:00
|
|
|
|
/// return the last position within the paragraph
|
|
|
|
|
pos_type lastpos() const;
|
|
|
|
|
|
|
|
|
|
/// return the number of embedded cells
|
|
|
|
|
size_t nargs() const;
|
|
|
|
|
/// return the number of embedded cells
|
|
|
|
|
size_t ncols() const;
|
|
|
|
|
/// return the number of embedded cells
|
|
|
|
|
size_t nrows() const;
|
|
|
|
|
/// return the grid row of the top cell
|
|
|
|
|
row_type row() const;
|
|
|
|
|
/// return the last row of the top grid
|
|
|
|
|
row_type lastrow() const { return nrows() - 1; }
|
|
|
|
|
/// return the grid column of the top cell
|
|
|
|
|
col_type col() const;
|
|
|
|
|
/// return the last column of the top grid
|
|
|
|
|
col_type lastcol() const { return ncols() - 1; }
|
|
|
|
|
/// the inset just behind the cursor
|
2007-08-23 21:13:01 +00:00
|
|
|
|
Inset * nextInset() const;
|
2004-03-01 17:12:09 +00:00
|
|
|
|
/// the inset just in front of the cursor
|
2007-08-23 21:13:01 +00:00
|
|
|
|
Inset * prevInset() const;
|
2005-07-15 15:49:40 +00:00
|
|
|
|
///
|
|
|
|
|
bool boundary() const { return boundary_; }
|
|
|
|
|
///
|
|
|
|
|
void boundary(bool b) { boundary_ = b; }
|
2004-03-01 17:12:09 +00:00
|
|
|
|
|
2007-01-08 15:30:17 +00:00
|
|
|
|
// the two methods below have been inlined out because of
|
|
|
|
|
// profiling results under linux when opening a document.
|
2007-01-08 10:50:15 +00:00
|
|
|
|
/// are we in mathed?.
|
2007-01-08 15:30:17 +00:00
|
|
|
|
bool inMathed() const
|
|
|
|
|
{ return !empty() && inset().inMathed(); }
|
2007-01-08 10:50:15 +00:00
|
|
|
|
/// are we in texted?.
|
2007-01-08 15:30:17 +00:00
|
|
|
|
bool inTexted() const
|
|
|
|
|
{ return !empty() && !inset().inMathed(); }
|
2004-03-01 17:12:09 +00:00
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// math-specific part
|
|
|
|
|
//
|
|
|
|
|
/// return the mathed cell this cursor is in
|
2007-08-23 21:13:01 +00:00
|
|
|
|
MathData & cell() const;
|
2004-03-01 17:12:09 +00:00
|
|
|
|
/// the mathatom left of the cursor
|
2007-08-23 21:13:01 +00:00
|
|
|
|
MathAtom & prevAtom() const;
|
2004-03-01 17:12:09 +00:00
|
|
|
|
/// the mathatom right of the cursor
|
2007-08-23 21:13:01 +00:00
|
|
|
|
MathAtom & nextAtom() const;
|
2004-03-01 17:12:09 +00:00
|
|
|
|
|
|
|
|
|
// text-specific part
|
|
|
|
|
//
|
2007-04-30 11:46:02 +00:00
|
|
|
|
/// the paragraph we're in in text mode.
|
|
|
|
|
/// \warning only works within text!
|
2007-08-23 21:13:01 +00:00
|
|
|
|
Paragraph & paragraph() const;
|
2007-04-30 11:46:02 +00:00
|
|
|
|
/// the paragraph we're in in any case.
|
2007-09-14 12:52:39 +00:00
|
|
|
|
/// This method will give the containing paragraph even
|
|
|
|
|
/// if not in text mode (ex: in mathed).
|
2007-08-23 21:13:01 +00:00
|
|
|
|
Paragraph & innerParagraph() const;
|
2007-09-14 12:52:39 +00:00
|
|
|
|
/// return the inner text slice.
|
|
|
|
|
CursorSlice const & innerTextSlice() const;
|
2005-01-31 16:29:48 +00:00
|
|
|
|
///
|
2007-08-23 21:13:01 +00:00
|
|
|
|
Text * text() const;
|
2005-11-24 16:22:39 +00:00
|
|
|
|
/// the containing inset or the cell, respectively
|
2007-04-29 13:39:47 +00:00
|
|
|
|
Inset * realInset() const;
|
2004-03-01 17:12:09 +00:00
|
|
|
|
///
|
2007-04-29 13:39:47 +00:00
|
|
|
|
Inset * innerInsetOfType(int code) const;
|
2004-03-01 17:12:09 +00:00
|
|
|
|
///
|
2007-08-23 21:13:01 +00:00
|
|
|
|
Text * innerText() const;
|
2004-03-01 17:12:09 +00:00
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// elementary moving
|
|
|
|
|
//
|
2006-01-28 09:46:58 +00:00
|
|
|
|
/**
|
|
|
|
|
* move on one logical position, descend into nested insets
|
|
|
|
|
* skip collapsed insets if \p ignorecollapsed is true
|
|
|
|
|
*/
|
|
|
|
|
void forwardPos(bool ignorecollapsed = false);
|
2004-03-25 09:16:36 +00:00
|
|
|
|
/// move on one physical character or inset
|
|
|
|
|
void forwardChar();
|
2004-03-01 17:12:09 +00:00
|
|
|
|
/// move on one paragraph
|
|
|
|
|
void forwardPar();
|
|
|
|
|
/// move on one inset
|
|
|
|
|
void forwardInset();
|
2004-03-25 09:16:36 +00:00
|
|
|
|
/// move backward one logical position
|
|
|
|
|
void backwardPos();
|
|
|
|
|
/// move backward one physical character or inset
|
|
|
|
|
void backwardChar();
|
|
|
|
|
/// move backward one paragraph
|
|
|
|
|
void backwardPar();
|
|
|
|
|
/// move backward one inset
|
2007-02-07 21:49:00 +00:00
|
|
|
|
/// FIXME: This is not implemented!
|
|
|
|
|
//void backwardInset();
|
2004-03-25 09:16:36 +00:00
|
|
|
|
|
2004-08-14 21:03:55 +00:00
|
|
|
|
/// are we some 'extension' (i.e. deeper nested) of the given iterator
|
|
|
|
|
bool hasPart(DocIterator const & it) const;
|
|
|
|
|
|
2004-03-08 21:14:45 +00:00
|
|
|
|
/// output
|
|
|
|
|
friend std::ostream &
|
2004-03-31 19:11:56 +00:00
|
|
|
|
operator<<(std::ostream & os, DocIterator const & cur);
|
2005-07-15 15:49:40 +00:00
|
|
|
|
///
|
2005-02-08 13:18:05 +00:00
|
|
|
|
friend bool operator==(DocIterator const &, DocIterator const &);
|
2007-05-15 17:07:55 +00:00
|
|
|
|
friend bool operator<(DocIterator const &, DocIterator const &);
|
|
|
|
|
friend bool operator>(DocIterator const &, DocIterator const &);
|
|
|
|
|
friend bool operator<=(DocIterator const &, DocIterator const &);
|
2005-07-15 15:49:40 +00:00
|
|
|
|
///
|
2005-02-08 13:18:05 +00:00
|
|
|
|
friend class StableDocIterator;
|
2005-08-03 20:21:11 +00:00
|
|
|
|
//protected:
|
2005-07-15 15:49:40 +00:00
|
|
|
|
///
|
2005-02-08 13:18:05 +00:00
|
|
|
|
void clear() { slices_.clear(); }
|
2005-07-15 15:49:40 +00:00
|
|
|
|
///
|
|
|
|
|
void push_back(CursorSlice const & sl) { slices_.push_back(sl); }
|
|
|
|
|
///
|
|
|
|
|
void pop_back() { slices_.pop_back(); }
|
2005-10-25 09:14:11 +00:00
|
|
|
|
/// recompute the inset parts of the cursor from the document data
|
2007-04-29 13:39:47 +00:00
|
|
|
|
void updateInsets(Inset * inset);
|
2007-05-25 23:17:24 +00:00
|
|
|
|
/// fix DocIterator in circumstances that should never happen.
|
|
|
|
|
/// \return true if the DocIterator was fixed.
|
|
|
|
|
bool fixIfBroken();
|
2005-10-25 09:14:11 +00:00
|
|
|
|
|
2007-11-01 10:56:18 +00:00
|
|
|
|
/// find index of CursorSlice with &cell() == &cell (or -1 if not found)
|
|
|
|
|
idx_type find(MathData const & cell) const;
|
|
|
|
|
/// find index of CursorSlice with inset() == inset (or -1 of not found)
|
|
|
|
|
idx_type find(InsetMath const * inset) const;
|
|
|
|
|
/// cut off CursorSlices with index > above and store cut off slices in cut
|
|
|
|
|
void cutOff(idx_type above, std::vector<CursorSlice> & cut);
|
|
|
|
|
/// cut off CursorSlices with index > above
|
|
|
|
|
void cutOff(idx_type above);
|
|
|
|
|
/// push CursorSlices on top
|
|
|
|
|
void append(std::vector<CursorSlice> const & x);
|
|
|
|
|
/// push one CursorSlice on top and set its index and position
|
|
|
|
|
void append(idx_type idx, pos_type pos);
|
|
|
|
|
|
2004-03-30 08:18:09 +00:00
|
|
|
|
private:
|
2005-07-15 15:49:40 +00:00
|
|
|
|
/**
|
|
|
|
|
* When the cursor position is i, is the cursor after the i-th char
|
|
|
|
|
* or before the i+1-th char ? Normally, these two interpretations are
|
|
|
|
|
* equivalent, except when the fonts of the i-th and i+1-th char
|
|
|
|
|
* differ.
|
|
|
|
|
* We use boundary_ to distinguish between the two options:
|
|
|
|
|
* If boundary_=true, then the cursor is after the i-th char
|
|
|
|
|
* and if boundary_=false, then the cursor is before the i+1-th char.
|
|
|
|
|
*
|
|
|
|
|
* We currently use the boundary only when the language direction of
|
|
|
|
|
* the i-th char is different than the one of the i+1-th char.
|
|
|
|
|
* In this case it is important to distinguish between the two
|
|
|
|
|
* cursor interpretations, in order to give a reasonable behavior to
|
|
|
|
|
* the user.
|
|
|
|
|
*/
|
|
|
|
|
bool boundary_;
|
|
|
|
|
///
|
2005-02-08 13:18:05 +00:00
|
|
|
|
std::vector<CursorSlice> const & internalData() const {
|
|
|
|
|
return slices_;
|
|
|
|
|
}
|
2005-07-15 15:49:40 +00:00
|
|
|
|
///
|
2005-02-08 13:18:05 +00:00
|
|
|
|
std::vector<CursorSlice> slices_;
|
2005-07-15 15:49:40 +00:00
|
|
|
|
///
|
2007-04-29 13:39:47 +00:00
|
|
|
|
Inset * inset_;
|
2004-03-01 17:12:09 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2007-04-29 13:39:47 +00:00
|
|
|
|
DocIterator doc_iterator_begin(Inset & inset);
|
|
|
|
|
DocIterator doc_iterator_end(Inset & inset);
|
2004-03-30 08:18:09 +00:00
|
|
|
|
|
|
|
|
|
|
2005-02-08 13:18:05 +00:00
|
|
|
|
inline
|
|
|
|
|
bool operator==(DocIterator const & di1, DocIterator const & di2)
|
|
|
|
|
{
|
|
|
|
|
return di1.slices_ == di2.slices_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline
|
|
|
|
|
bool operator!=(DocIterator const & di1, DocIterator const & di2)
|
|
|
|
|
{
|
|
|
|
|
return !(di1 == di2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-03-31 19:11:56 +00:00
|
|
|
|
// The difference to a ('non stable') DocIterator is the removed
|
2007-01-04 22:01:43 +00:00
|
|
|
|
// (overwritten by 0...) part of the CursorSlice data items. So this thing
|
2004-03-08 21:14:45 +00:00
|
|
|
|
// is suitable for external storage, but not for iteration as such.
|
|
|
|
|
|
2004-03-31 19:11:56 +00:00
|
|
|
|
class StableDocIterator {
|
2004-03-08 21:14:45 +00:00
|
|
|
|
public:
|
|
|
|
|
///
|
2004-03-31 19:11:56 +00:00
|
|
|
|
StableDocIterator() {}
|
2004-03-08 21:14:45 +00:00
|
|
|
|
/// non-explicit intended
|
2004-03-31 19:11:56 +00:00
|
|
|
|
StableDocIterator(const DocIterator & it);
|
2004-03-08 21:14:45 +00:00
|
|
|
|
///
|
2007-04-29 13:39:47 +00:00
|
|
|
|
DocIterator asDocIterator(Inset * start) const;
|
2004-03-08 21:14:45 +00:00
|
|
|
|
///
|
|
|
|
|
size_t size() const { return data_.size(); }
|
2005-10-13 17:20:30 +00:00
|
|
|
|
/// return the position within the paragraph
|
|
|
|
|
pos_type pos() const { return data_.back().pos(); }
|
|
|
|
|
/// return the position within the paragraph
|
|
|
|
|
pos_type & pos() { return data_.back().pos(); }
|
2004-03-08 21:14:45 +00:00
|
|
|
|
///
|
|
|
|
|
friend std::ostream &
|
2004-03-31 19:11:56 +00:00
|
|
|
|
operator<<(std::ostream & os, StableDocIterator const & cur);
|
2004-03-08 21:14:45 +00:00
|
|
|
|
///
|
|
|
|
|
friend std::istream &
|
2004-03-31 19:11:56 +00:00
|
|
|
|
operator>>(std::istream & is, StableDocIterator & cur);
|
2005-07-01 12:36:28 +00:00
|
|
|
|
///
|
|
|
|
|
friend bool
|
|
|
|
|
operator==(StableDocIterator const &, StableDocIterator const &);
|
2004-03-08 21:14:45 +00:00
|
|
|
|
private:
|
|
|
|
|
std::vector<CursorSlice> data_;
|
|
|
|
|
};
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
} // namespace lyx
|
|
|
|
|
|
2004-03-01 17:12:09 +00:00
|
|
|
|
#endif
|