2004-03-01 17:12:09 +00:00
|
|
|
|
// -*- C++ -*-
|
|
|
|
|
/**
|
|
|
|
|
* \file dociterator.h
|
|
|
|
|
* 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
|
|
|
|
|
|
|
|
|
|
#include "cursor_slice.h"
|
|
|
|
|
|
|
|
|
|
#include <vector>
|
2004-03-08 21:14:45 +00:00
|
|
|
|
#include <iosfwd>
|
2004-03-01 17:12:09 +00:00
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
|
2004-03-11 17:16:38 +00:00
|
|
|
|
class LyXText;
|
2004-03-18 12:53:43 +00:00
|
|
|
|
class MathAtom;
|
2004-03-01 17:12:09 +00:00
|
|
|
|
class Paragraph;
|
|
|
|
|
class Row;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// only needed for gcc 2.95, remove when support terminated
|
|
|
|
|
template <typename A, typename B>
|
|
|
|
|
bool ptr_cmp(A const * a, B const * b)
|
|
|
|
|
{
|
|
|
|
|
return a == b;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
///
|
2004-03-31 19:11:56 +00:00
|
|
|
|
explicit DocIterator(InsetBase & 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
|
2005-02-08 13:18:05 +00:00
|
|
|
|
InsetBase & 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
|
|
|
|
|
InsetBase * nextInset();
|
|
|
|
|
/// the inset just in front of the cursor
|
|
|
|
|
InsetBase * prevInset();
|
|
|
|
|
/// the inset just in front of the cursor
|
|
|
|
|
InsetBase const * 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
|
|
|
|
|
|
|
|
|
/// are we in mathed?
|
|
|
|
|
bool inMathed() const;
|
|
|
|
|
/// are we in texted?
|
|
|
|
|
bool inTexted() const;
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// math-specific part
|
|
|
|
|
//
|
|
|
|
|
/// return the mathed cell this cursor is in
|
|
|
|
|
MathArray const & cell() const;
|
|
|
|
|
/// return the mathed cell this cursor is in
|
|
|
|
|
MathArray & cell();
|
|
|
|
|
/// the mathatom left of the cursor
|
|
|
|
|
MathAtom const & prevAtom() const;
|
|
|
|
|
/// the mathatom left of the cursor
|
|
|
|
|
MathAtom & prevAtom();
|
|
|
|
|
/// the mathatom right of the cursor
|
|
|
|
|
MathAtom const & nextAtom() const;
|
|
|
|
|
/// the mathatom right of the cursor
|
|
|
|
|
MathAtom & nextAtom();
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// text-specific part
|
|
|
|
|
//
|
|
|
|
|
/// the paragraph we're in
|
|
|
|
|
Paragraph & paragraph();
|
|
|
|
|
/// the paragraph we're in
|
|
|
|
|
Paragraph const & paragraph() const;
|
|
|
|
|
/// the row in the paragraph we're in
|
|
|
|
|
Row & textRow();
|
|
|
|
|
/// the row in the paragraph we're in
|
|
|
|
|
Row const & textRow() const;
|
|
|
|
|
///
|
2005-01-31 16:29:48 +00:00
|
|
|
|
LyXText * text();
|
|
|
|
|
///
|
|
|
|
|
LyXText const * text() const;
|
2005-11-24 16:22:39 +00:00
|
|
|
|
/// the containing inset or the cell, respectively
|
|
|
|
|
InsetBase * realInset() const;
|
2004-03-01 17:12:09 +00:00
|
|
|
|
///
|
|
|
|
|
InsetBase * innerInsetOfType(int code) const;
|
|
|
|
|
///
|
2005-01-31 16:29:48 +00:00
|
|
|
|
LyXText * innerText();
|
|
|
|
|
///
|
|
|
|
|
LyXText const * innerText() const;
|
2004-03-01 17:12:09 +00:00
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// elementary moving
|
|
|
|
|
//
|
2005-02-14 08:17:23 +00:00
|
|
|
|
/// move on one logical position, do not descend into nested insets
|
|
|
|
|
void forwardPosNoDescend();
|
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 cell
|
|
|
|
|
void forwardIdx();
|
|
|
|
|
/// 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 cell
|
|
|
|
|
void backwardIdx();
|
|
|
|
|
/// move backward one inset
|
|
|
|
|
void backwardInset();
|
|
|
|
|
|
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 &);
|
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
|
|
|
|
|
void updateInsets(InsetBase * inset);
|
|
|
|
|
|
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
|
|
|
|
///
|
2004-03-30 08:18:09 +00:00
|
|
|
|
InsetBase * inset_;
|
2004-03-01 17:12:09 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2004-03-31 19:11:56 +00:00
|
|
|
|
DocIterator doc_iterator_begin(InsetBase & inset);
|
|
|
|
|
DocIterator doc_iterator_end(InsetBase & 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
|
2004-03-08 21:14:45 +00:00
|
|
|
|
// (overwritte by 0...) part of the CursorSlice data items. So this thing
|
|
|
|
|
// 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
|
|
|
|
///
|
2004-03-31 19:11:56 +00:00
|
|
|
|
DocIterator asDocIterator(InsetBase * 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
|