]> git.lyx.org Git - features.git/blob - src/TexRow.h
Improve the TexRow Cursor->Row algorithm for selections.
[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 Cursor;
27 class CursorSlice;
28 class DocIterator;
29 class docstring_list;
30
31 /// types for cells and math insets
32 typedef void const * uid_type;
33 typedef size_t idx_type;
34
35
36 /// Represents the correspondence between paragraphs and the generated
37 /// LaTeX file
38
39 class TexRow {
40 public:
41         /// an individual par id/pos <=> row mapping
42         struct TextEntry { int id; int pos; };
43
44         /// an individual math id/cell <=> row mapping
45         struct MathEntry { uid_type id; idx_type cell; };
46
47         /// a container for passing entries around
48         struct RowEntry {
49                 bool is_math;// true iff the union is a math
50                 union {
51                         struct TextEntry text;
52                         struct MathEntry math;
53                 };
54         };
55
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> {
62         public:
63                 RowEntryList() : std::vector<RowEntry>(), text_entry_(-1) {}
64
65                 // returns true if the row entry will appear in the row entry list
66                 bool addEntry(RowEntry const &);
67
68                 // returns the TextEntry or TexRow::text_none if none
69                 TextEntry getTextEntry() const;
70
71                 // returns the first entry, or TexRow::row_none if none
72                 RowEntry entry() const;
73
74         private:
75                 size_t text_entry_;
76         };
77
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;
82         
83         /// Returns true if TextEntry is devoid of information
84         static bool isNone(TextEntry const &);
85
86         /// Converts a CursorSlice into a RowEntry
87         static RowEntry rowEntryFromCursorSlice(CursorSlice const & slice);
88
89         /// Encapsulates the paragraph and position for later use
90         static RowEntry textEntry(int id, int pos);
91
92         /// Encapsulates a cell and position for later use
93         static RowEntry mathEntry(uid_type id, idx_type cell);
94
95         /// true iff same paragraph or math inset
96         static bool sameParOrInsetMath(RowEntry const &, RowEntry const &);
97
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);
101
102         /// for debugging purposes
103         static docstring asString(RowEntry const &);
104
105         ///
106         TexRow(bool enable = true)
107                 : current_row_(RowEntryList()), enabled_(enable) {}
108
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);
112
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);
116
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);
120
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);
124
125         /// Insert node when line is completed
126         void newline();
127
128         /// Insert multiple nodes when zero or more lines are completed
129         void newlines(int num_lines);
130
131         /// Call when code generation is complete
132         void finalize();
133
134         /**
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
140          *
141          * If the row could not be found, pos is set to zero and
142          * id is set to -1
143          */
144         bool getIdFromRow(int row, int & id, int & pos) const;
145
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;
149
150         /// Finds the best pair of rows for cursor, taking the selection into
151         /// account
152         /// returns (-1,-1) if not found.
153         std::pair<int,int> rowFromCursor(Cursor const & dit) const;
154         
155         /// Returns the number of rows contained
156         int rows() const { return rowlist_.size(); }
157
158         /// for debugging purpose
159         void prepend(docstring_list &) const;
160
161 private:
162         typedef std::vector<RowEntryList> RowList;
163         ///
164         class RowListIterator;
165         ///
166         RowListIterator begin() const;
167         ///
168         RowListIterator end() const;
169         /// container of id/pos <=> row mapping
170         RowList rowlist_;
171         /// Entry of current line
172         RowEntryList current_row_;
173         /// 
174         bool enabled_;
175 };
176
177 bool operator==(TexRow::RowEntry const &, TexRow::RowEntry const &);
178
179 LyXErr & operator<<(LyXErr &, TexRow &);
180
181
182 } // namespace lyx
183
184 #endif // TEXROW_H