4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Matthias Ettrich
8 * \author Lars Gullik Bjønnes
10 * \author Guillaume Munch
12 * Full author contact details are available in file CREDITS.
18 #include "support/types.h"
19 #include "support/debug.h"
30 /// types for cells and math insets
31 typedef void const * uid_type;
32 typedef size_t idx_type;
35 /// Represents the correspondence between paragraphs and the generated
40 /// an individual par id/pos <=> row mapping
41 struct TextEntry { int id; int pos; };
43 /// an individual math id/cell <=> row mapping
44 struct MathEntry { uid_type id; idx_type cell; };
46 /// a container for passing entries around
48 bool is_math;// true iff the union is a math
50 struct TextEntry text;
51 struct MathEntry math;
55 // For each row we store a list of one TextEntry and several
56 // MathEntries. (The order is important.) We only want one text entry
57 // because we do not want to store every position in the lyx file. On the
58 // other hand we want to record all math cells positions for enough
59 // precision. Usually the count of math cells is easier to handle.
60 class RowEntryList : public std::vector<RowEntry> {
62 RowEntryList() : std::vector<RowEntry>(), text_entry_(-1) {}
64 // returns true if the row entry will appear in the row entry list
65 bool addEntry(RowEntry const &);
67 // returns the TextEntry or TexRow::text_none if none
68 TextEntry getTextEntry() const;
70 // returns the first entry, or TexRow::row_none if none
71 RowEntry entry() const;
77 /// Returns true if RowEntry is devoid of information
78 static bool isNone(RowEntry const &);
79 static const TextEntry text_none;
80 static const RowEntry row_none;
82 /// Returns true if TextEntry is devoid of information
83 static bool isNone(TextEntry const &);
85 /// Converts a CursorSlice into a RowEntry
86 static RowEntry rowEntryFromCursorSlice(CursorSlice const & slice);
88 /// Encapsulates the paragraph and position for later use
89 static RowEntry textEntry(int id, int pos);
91 /// Encapsulates a cell and position for later use
92 static RowEntry mathEntry(uid_type id, idx_type cell);
94 /// true iff same paragraph or math inset
95 static bool sameParOrInsetMath(RowEntry const &, RowEntry const &);
97 /// computes the distance in pos or cell index
98 /// assumes it is the sameParOrInsetMath
99 static int comparePos(RowEntry const & entry1, RowEntry const & entry2);
101 /// for debugging purposes
102 static docstring asString(RowEntry const &);
105 TexRow(bool enable = true)
106 : current_row_(RowEntryList()), enabled_(enable) {}
108 /// Clears structure. Set enable to false if texrow is not needed, to avoid
109 /// computing TexRow when it is going to be immediately discarded.
110 void reset(bool enable = true);
112 /// Defines the row information for the current line
113 /// returns true if this entry will appear on the current row
114 bool start(RowEntry entry);
116 /// Defines the paragraph and position for the current line
117 /// returns true if this entry will appear on the current row
118 bool start(int id, int pos);
120 /// Defines a cell and position for the current line
121 /// returns true if this entry will appear on the current row
122 bool startMath(uid_type id, idx_type cell);
124 /// Insert node when line is completed
127 /// Insert multiple nodes when zero or more lines are completed
128 void newlines(int num_lines);
130 /// Call when code generation is complete
134 * getIdFromRow - find pid and position for a given row
135 * @param row row number to find
136 * @param id set to id if found
137 * @param pos set to paragraph position if found
138 * @return true if found, false otherwise
140 * If the row could not be found, pos is set to zero and
143 bool getIdFromRow(int row, int & id, int & pos) const;
145 /// Finds the best pair of rows for dit
146 /// returns (-1,-1) if not found.
147 std::pair<int,int> rowFromDocIterator(DocIterator const & dit) const;
149 /// Returns the number of rows contained
150 int rows() const { return rowlist_.size(); }
152 /// for debugging purpose
153 void prepend(docstring_list &) const;
156 typedef std::vector<RowEntryList> RowList;
158 class RowListIterator;
160 RowListIterator begin() const;
162 RowListIterator end() const;
163 /// container of id/pos <=> row mapping
165 /// Entry of current line
166 RowEntryList current_row_;
171 bool operator==(TexRow::RowEntry const &, TexRow::RowEntry const &);
173 LyXErr & operator<<(LyXErr &, TexRow &);