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"
31 /// types for cells and math insets
32 typedef void const * uid_type;
33 typedef size_t idx_type;
36 /// Represents the correspondence between paragraphs and the generated
41 /// an individual par id/pos <=> row mapping
42 struct TextEntry { int id; int pos; };
44 /// an individual math id/cell <=> row mapping
45 struct MathEntry { uid_type id; idx_type cell; };
47 /// a container for passing entries around
49 bool is_math;// true iff the union is a math
51 struct TextEntry text;
52 struct MathEntry math;
56 // For each row we store a list of one TextEntry and several
57 // MathEntries. (The order is important.) We only want one text entry
58 // because we do not want to store every position in the lyx file. On the
59 // other hand we want to record all math cells positions for enough
60 // precision. Usually the count of math cells is easier to handle.
61 class RowEntryList : public std::vector<RowEntry> {
63 RowEntryList() : std::vector<RowEntry>(), text_entry_(-1) {}
65 // returns true if the row entry will appear in the row entry list
66 bool addEntry(RowEntry const &);
68 // returns the TextEntry or TexRow::text_none if none
69 TextEntry getTextEntry() const;
71 // returns the first entry, or TexRow::row_none if none
72 RowEntry entry() const;
78 /// Returns true if RowEntry is devoid of information
79 static bool isNone(RowEntry const &);
80 static const TextEntry text_none;
81 static const RowEntry row_none;
83 /// Returns true if TextEntry is devoid of information
84 static bool isNone(TextEntry const &);
86 /// Converts a CursorSlice into a RowEntry
87 static RowEntry rowEntryFromCursorSlice(CursorSlice const & slice);
89 /// Encapsulates the paragraph and position for later use
90 static RowEntry textEntry(int id, int pos);
92 /// Encapsulates a cell and position for later use
93 static RowEntry mathEntry(uid_type id, idx_type cell);
95 /// true iff same paragraph or math inset
96 static bool sameParOrInsetMath(RowEntry const &, RowEntry const &);
98 /// computes the distance in pos or cell index
99 /// assumes it is the sameParOrInsetMath
100 static int comparePos(RowEntry const & entry1, RowEntry const & entry2);
102 /// for debugging purposes
103 static docstring asString(RowEntry const &);
106 TexRow(bool enable = true)
107 : current_row_(RowEntryList()), enabled_(enable) {}
109 /// Clears structure. Set enable to false if texrow is not needed, to avoid
110 /// computing TexRow when it is going to be immediately discarded.
111 void reset(bool enable = true);
113 /// Defines the row information for the current line
114 /// returns true if this entry will appear on the current row
115 bool start(RowEntry entry);
117 /// Defines the paragraph and position for the current line
118 /// returns true if this entry will appear on the current row
119 bool start(int id, int pos);
121 /// Defines a cell and position for the current line
122 /// returns true if this entry will appear on the current row
123 bool startMath(uid_type id, idx_type cell);
125 /// Insert node when line is completed
128 /// Insert multiple nodes when zero or more lines are completed
129 void newlines(int num_lines);
131 /// Call when code generation is complete
135 * getIdFromRow - find pid and position for a given row
136 * @param row row number to find
137 * @param id set to id if found
138 * @param pos set to paragraph position if found
139 * @return true if found, false otherwise
141 * If the row could not be found, pos is set to zero and
144 bool getIdFromRow(int row, int & id, int & pos) const;
146 /// Finds the best pair of rows for dit
147 /// returns (-1,-1) if not found.
148 std::pair<int,int> rowFromDocIterator(DocIterator const & dit) const;
150 /// Finds the best pair of rows for cursor, taking the selection into
152 /// returns (-1,-1) if not found.
153 std::pair<int,int> rowFromCursor(Cursor const & dit) const;
155 /// Returns the number of rows contained
156 int rows() const { return rowlist_.size(); }
158 /// for debugging purpose
159 void prepend(docstring_list &) const;
162 typedef std::vector<RowEntryList> RowList;
164 class RowListIterator;
166 RowListIterator begin() const;
168 RowListIterator end() const;
169 /// container of id/pos <=> row mapping
171 /// Entry of current line
172 RowEntryList current_row_;
177 bool operator==(TexRow::RowEntry const &, TexRow::RowEntry const &);
179 LyXErr & operator<<(LyXErr &, TexRow &);