]> git.lyx.org Git - lyx.git/blob - src/Row.h
Account for old versions of Pygments
[lyx.git] / src / Row.h
1 // -*- C++ -*-
2 /**
3  * \file Row.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  *
10  * Full author contact details are available in file CREDITS.
11  *
12  * Metrics for an on-screen text row.
13  */
14
15 #ifndef ROW_H
16 #define ROW_H
17
18 #include "Changes.h"
19 #include "Dimension.h"
20 #include "Font.h"
21
22 #include "support/docstring.h"
23 #include "support/types.h"
24
25 #include <vector>
26
27 namespace lyx {
28
29 class DocIterator;
30 class Inset;
31
32 /**
33  * An on-screen row of text. A paragraph is broken into a RowList for
34  * display. Each Row contains a tokenized description of the contents
35  * of the line.
36  */
37 class Row {
38 public:
39         // Possible types of row elements
40         enum Type {
41                 // a string of character
42                 STRING,
43                 /**
44                  * Something (completion, end-of-par marker)
45                  * that occupies space one screen but does not
46                  * correspond to any paragraph contents
47                  */
48                 VIRTUAL,
49                 // An inset
50                 INSET,
51                 // Some spacing described by its width, not a string
52                 SPACE
53         };
54
55 /**
56  * One element of a Row. It has a set of attributes that can be used
57  * by other methods that need to parse the Row contents.
58  */
59         struct Element {
60                 Element(Type const t, pos_type p, Font const & f, Change const & ch)
61                         : type(t), pos(p), endpos(p + 1), inset(0),
62                           extra(0), font(f), change(ch), final(false) {}
63
64                 // Return the number of separator in the element (only STRING type)
65                 int countSeparators() const;
66
67                 // Return total width of element, including separator overhead
68                 // FIXME: Cache this value or the number of expanders?
69                 double full_width() const { return dim.wid + extra * countExpanders(); }
70                 // Return the number of expanding characters in the element (only STRING
71                 // type).
72                 int countExpanders() const;
73                 // Return the amount of expansion: the number of expanding characters
74                 // that get stretched during justification, times the em of the font
75                 // (only STRING type).
76                 int expansionAmount() const;
77                 // set extra proportionally to the font em value.
78                 void setExtra(double extra_per_em);
79
80                 /** Return position in pixels (from the left) of position
81                  * \param i in the row element.
82                  */
83                 double pos2x(pos_type const i) const;
84                 /** Return character position that is the closest to
85                  *  pixel position \param x. The value \param x is
86                  *  adjusted to the actual pixel position.
87                 */
88                 pos_type x2pos(int &x) const;
89                 /** Break the element if possible, so that its width is less
90                  * than \param w. Returns true on success. When \param force
91                  * is true, the string is cut at any place, other wise it
92                  * respects the row breaking rules of characters.
93                  */
94                 bool breakAt(int w, bool force);
95
96                 // Returns the position on left side of the element.
97                 pos_type left_pos() const;
98                 // Returns the position on right side of the element.
99                 pos_type right_pos() const;
100
101                 //
102                 bool isRTL() const { return font.isVisibleRightToLeft(); }
103                 // This is true for virtual elements.
104                 // Note that we do not use the type here. The two definitions
105                 // should be equivalent
106                 bool isVirtual() const { return pos == endpos; }
107
108                 // The kind of row element
109                 Type type;
110                 // position of the element in the paragraph
111                 pos_type pos;
112                 // first position after the element in the paragraph
113                 pos_type endpos;
114                 // The dimension of the chunk (does not contains the
115                 // separator correction)
116                 Dimension dim;
117
118                 // Non-zero only if element is an inset
119                 Inset const * inset;
120
121                 // Only non-null for justified rows
122                 double extra;
123
124                 // Non-empty if element is a string or is virtual
125                 docstring str;
126                 //
127                 Font font;
128                 //
129                 Change change;
130                 // is it possible to add contents to this element?
131                 bool final;
132
133                 friend std::ostream & operator<<(std::ostream & os, Element const & row);
134         };
135
136
137         ///
138         Row();
139         ///
140         bool changed() const { return changed_; }
141         ///
142         void setChanged(bool c) { changed_ = c; }
143         ///
144         void setCrc(size_type crc) const;
145         /// Set the selection begin and end.
146         /**
147           * This is const because we update the selection status only at draw()
148           * time.
149           */
150         void setSelection(pos_type sel_beg, pos_type sel_end) const;
151         ///
152         bool selection() const;
153         /// Set the selection begin and end and whether the left and/or right
154         /// margins are selected.
155         void setSelectionAndMargins(DocIterator const & beg,
156                 DocIterator const & end) const;
157
158         ///
159         void pit(pit_type p) { pit_ = p; }
160         ///
161         pit_type pit() const { return pit_; }
162         ///
163         void pos(pos_type p) { pos_ = p; }
164         ///
165         pos_type pos() const { return pos_; }
166         ///
167         void endpos(pos_type p) { end_ = p; }
168         ///
169         pos_type endpos() const { return end_; }
170         ///
171         void right_boundary(bool b) { right_boundary_ = b; }
172         ///
173         bool right_boundary() const { return right_boundary_; }
174         ///
175         void flushed(bool b) { flushed_ = b; }
176         ///
177         bool flushed() const { return flushed_; }
178
179         ///
180         Dimension const & dimension() const { return dim_; }
181         ///
182         Dimension & dimension() { return dim_; }
183         ///
184         int height() const { return dim_.height(); }
185         /// The width of the row, including the left margin, but not the right one.
186         int width() const { return dim_.wid; }
187         ///
188         int ascent() const { return dim_.asc; }
189         ///
190         int descent() const { return dim_.des; }
191
192         /// The offset of the left-most cursor position on the row
193         int left_x() const;
194         /// The offset of the right-most cursor position on the row
195         int right_x() const;
196
197         // Return the number of separators in the row
198         int countSeparators() const;
199         // Set the extra spacing for every expanding character in STRING-type
200         // elements.  \param w is the total amount of extra width for the row to be
201         // distributed among expanders.  \return false if the justification fails.
202         bool setExtraWidth(int w);
203
204         ///
205         void add(pos_type pos, Inset const * ins, Dimension const & dim,
206                  Font const & f, Change const & ch);
207         ///
208         void add(pos_type pos, char_type const c,
209                  Font const & f, Change const & ch);
210         ///
211         void addVirtual(pos_type pos, docstring const & s,
212                         Font const & f, Change const & ch);
213         ///
214         void addSpace(pos_type pos, int width, Font const & f, Change const & ch);
215
216         ///
217         typedef std::vector<Element> Elements;
218         ///
219         typedef Elements::iterator iterator;
220         ///
221         typedef Elements::const_iterator const_iterator;
222         ///
223         iterator begin() { return elements_.begin(); }
224         ///
225         iterator end() { return elements_.end(); }
226         ///
227         const_iterator begin() const { return elements_.begin(); }
228         ///
229         const_iterator end() const { return elements_.end(); }
230
231         ///
232         bool empty() const { return elements_.empty(); }
233         ///
234         Element & front() { return elements_.front(); }
235         ///
236         Element const & front() const { return elements_.front(); }
237         ///
238         Element & back() { return elements_.back(); }
239         ///
240         Element const & back() const { return elements_.back(); }
241         /// remove last element
242         void pop_back();
243         /// remove all row elements
244         void clear() { elements_.clear(); }
245         /**
246          * if row width is too large, remove all elements after last
247          * separator and update endpos if necessary. If all that
248          * remains is a large word, cut it to \param width.
249          * \param body_pos minimum amount of text to keep.
250          * \param width maximum width of the row.
251          * \param available width on next row.
252          * \return true if the row has been shortened.
253          */
254         bool shortenIfNeeded(pos_type const body_pos, int const width, int const next_width);
255
256         /**
257          * If last element of the row is a string, compute its width
258          * and mark it final.
259          */
260         void finalizeLast();
261
262         /**
263          * Find sequences of right-to-left elements and reverse them.
264          * This should be called once the row is completely built.
265          */
266         void reverseRTL(bool rtl_par);
267         ///
268         bool isRTL() const { return rtl_; }
269         /// Find row element that contains \c pos, and compute x offset.
270         const_iterator const findElement(pos_type pos, bool boundary, double & x) const;
271
272         friend std::ostream & operator<<(std::ostream & os, Row const & row);
273
274         /// additional width for separators in justified rows (i.e. space)
275         double separator;
276         /// width of hfills in the label
277         double label_hfill;
278         /// the left margin position of the row
279         int left_margin;
280         /// the right margin of the row
281         int right_margin;
282         ///
283         mutable pos_type sel_beg;
284         ///
285         mutable pos_type sel_end;
286         ///
287         mutable bool begin_margin_sel;
288         ///
289         mutable bool end_margin_sel;
290
291 private:
292         /// Decides whether the margin is selected.
293         /**
294           * \param margin_begin
295           * \param beg
296           * \param end
297           */
298         bool isMarginSelected(bool left_margin, DocIterator const & beg,
299                 DocIterator const & end) const;
300
301         /**
302          * Returns true if a char or string with font \c f and change
303          * type \c ch can be added to the current last element of the
304          * row.
305          */
306         bool sameString(Font const & f, Change const & ch) const;
307
308         ///
309         Elements elements_;
310
311         /// has the Row appearance changed since last drawing?
312         mutable bool changed_;
313         /// CRC of row contents.
314         mutable size_type crc_;
315         /// Index of the paragraph that contains this row
316         pit_type pit_;
317         /// first pos covered by this row
318         pos_type pos_;
319         /// one behind last pos covered by this row
320         pos_type end_;
321         // Is there a boundary at the end of the row (display inset...)
322         bool right_boundary_;
323         // Shall the row be flushed when it is supposed to be justified?
324         bool flushed_;
325         /// Row dimension.
326         Dimension dim_;
327         /// true when this row lives in a right-to-left paragraph
328         bool rtl_;
329 };
330
331
332 } // namespace lyx
333
334 #endif