]> git.lyx.org Git - features.git/blob - src/TexRow.h
47a7d74beeccc18d065ff1ac7f1bc6271c692875
[features.git] / src / TexRow.h
1 // -*- C++ -*-
2 /**
3  * \file TexRow.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Matthias Ettrich
8  * \author Lars Gullik Bjønnes
9  * \author John Levon
10  * \author Guillaume Munch
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #ifndef TEXROW_H
16 #define TEXROW_H
17
18 #include "support/types.h"
19 #include "support/debug.h"
20
21 #include <vector>
22
23 namespace lyx {
24
25 class LyXErr;
26 class CursorSlice;
27 class DocIterator;
28 class docstring_list;
29
30 /// types for cells and math insets
31 typedef void const * uid_type;
32 typedef size_t idx_type;
33
34
35 /// Represents the correspondence between paragraphs and the generated
36 /// LaTeX file
37
38 class TexRow {
39 public:
40         /// an individual par id/pos <=> row mapping
41         struct TextEntry { int id; int pos; };
42
43         /// an individual math id/cell <=> row mapping
44         struct MathEntry { uid_type id; idx_type cell; };
45
46         /// a container for passing entries around
47         struct RowEntry {
48                 bool is_math;// true iff the union is a math
49                 union {
50                         struct TextEntry text;
51                         struct MathEntry math;
52                 };
53         };
54
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> {
61         public:
62                 RowEntryList() : std::vector<RowEntry>(), text_entry_(-1) {}
63
64                 // returns true if the row entry will appear in the row entry list
65                 bool addEntry(RowEntry const &);
66
67                 // returns the TextEntry or TexRow::text_none if none
68                 TextEntry getTextEntry() const;
69
70                 // returns the first entry, or TexRow::row_none if none
71                 RowEntry entry() const;
72
73         private:
74                 size_t text_entry_;
75         };
76
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;
81         
82         /// Returns true if TextEntry is devoid of information
83         static bool isNone(TextEntry const &);
84
85         /// Converts a CursorSlice into a RowEntry
86         static RowEntry rowEntryFromCursorSlice(CursorSlice const & slice);
87
88         /// Encapsulates the paragraph and position for later use
89         static RowEntry textEntry(int id, int pos);
90
91         /// Encapsulates a cell and position for later use
92         static RowEntry mathEntry(uid_type id, idx_type cell);
93
94         /// true iff same paragraph or math inset
95         static bool sameParOrInsetMath(RowEntry const &, RowEntry const &);
96
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);
100
101         /// for debugging purposes
102         static docstring asString(RowEntry const &);
103
104         ///
105         TexRow(bool enable = true)
106                 : current_row_(RowEntryList()), enabled_(enable) {}
107
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);
111
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);
115
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);
119
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);
123
124         /// Insert node when line is completed
125         void newline();
126
127         /// Insert multiple nodes when zero or more lines are completed
128         void newlines(int num_lines);
129
130         /// Call when code generation is complete
131         void finalize();
132
133         /**
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
139          *
140          * If the row could not be found, pos is set to zero and
141          * id is set to -1
142          */
143         bool getIdFromRow(int row, int & id, int & pos) const;
144
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;
148         
149         /// Returns the number of rows contained
150         int rows() const { return rowlist_.size(); }
151
152         /// for debugging purpose
153         void prepend(docstring_list &) const;
154
155 private:
156         typedef std::vector<RowEntryList> RowList;
157         ///
158         class RowListIterator;
159         ///
160         RowListIterator begin() const;
161         ///
162         RowListIterator end() const;
163         /// container of id/pos <=> row mapping
164         RowList rowlist_;
165         /// Entry of current line
166         RowEntryList current_row_;
167         /// 
168         bool enabled_;
169 };
170
171 bool operator==(TexRow::RowEntry const &, TexRow::RowEntry const &);
172
173 LyXErr & operator<<(LyXErr &, TexRow &);
174
175
176 } // namespace lyx
177
178 #endif // TEXROW_H