]> git.lyx.org Git - features.git/blob - src/Row.h
b0b17556f4bf1f9ed551cc8182fa85bc4e71cdb7
[features.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 #include "RowFlags.h"
22
23 #include "support/docstring.h"
24 #include "support/types.h"
25
26 #include <vector>
27
28 namespace lyx {
29
30 class DocIterator;
31 class Inset;
32
33 /**
34  * An on-screen row of text. A paragraph is broken into a RowList for
35  * display. Each Row contains a tokenized description of the contents
36  * of the line.
37  */
38 class Row {
39 public:
40         // Possible types of row elements
41         enum Type {
42                 // a string of character
43                 STRING,
44                 /**
45                  * Something (completion, end-of-par marker)
46                  * that occupies space one screen but does not
47                  * correspond to any paragraph contents
48                  */
49                 VIRTUAL,
50                 // An inset
51                 INSET,
52                 // Some spacing described by its width, not a string
53                 SPACE,
54                 // Something that should not happen (for error handling)
55                 INVALID
56         };
57
58 /**
59  * One element of a Row. It has a set of attributes that can be used
60  * by other methods that need to parse the Row contents.
61  */
62         struct Element {
63                 //
64                 Element() = default;
65                 //
66                 Element(Type const t, pos_type p, Font const & f, Change const & ch)
67                         : type(t), pos(p), endpos(p + 1), font(f), change(ch) {}
68
69
70                 // Return the number of separator in the element (only STRING type)
71                 int countSeparators() const;
72
73                 // Return total width of element, including separator overhead
74                 // FIXME: Cache this value or the number of expanders?
75                 double full_width() const { return dim.wid + extra * countExpanders(); }
76                 // Return the number of expanding characters in the element (only STRING
77                 // type).
78                 int countExpanders() const;
79                 // Return the amount of expansion: the number of expanding characters
80                 // that get stretched during justification, times the em of the font
81                 // (only STRING type).
82                 int expansionAmount() const;
83                 // set extra proportionally to the font em value.
84                 void setExtra(double extra_per_em);
85
86                 /** Return position in pixels (from the left) of position
87                  * \param i in the row element.
88                  */
89                 double pos2x(pos_type const i) const;
90                 /** Return character position that is the closest to
91                  *  pixel position \param x. The value \param x is
92                  *  adjusted to the actual pixel position.
93                 */
94                 pos_type x2pos(int &x) const;
95                 /** Break the element in two if possible, so that its width is less
96                  * than \param w.
97                  * \return an element containing the remainder of the text, or
98                  *   an invalid element if nothing happened.
99                  * \param w: the desired maximum width
100                  * \param force: if true, the string is cut at any place, otherwise it
101                  *   respects the row breaking rules of characters.
102                  */
103                 Element splitAt(int w, bool force);
104
105                 //
106                 bool isRTL() const { return font.isVisibleRightToLeft(); }
107                 // This is true for virtual elements.
108                 bool isVirtual() const { return type == VIRTUAL; }
109                 // Invalid element, for error handling
110                 bool isValid() const { return type !=INVALID; }
111
112                 // Returns the position on left side of the element.
113                 pos_type left_pos() const { return isRTL() ? endpos : pos; };
114                 // Returns the position on right side of the element.
115                 pos_type right_pos() const { return isRTL() ? pos : endpos; };
116
117                 // The kind of row element
118                 Type type = INVALID;
119                 // position of the element in the paragraph
120                 pos_type pos = 0;
121                 // first position after the element in the paragraph
122                 pos_type endpos = 0;
123                 // The dimension of the chunk (does not contains the
124                 // separator correction)
125                 Dimension dim;
126
127                 // Non-zero only if element is an inset
128                 Inset const * inset = nullptr;
129
130                 // Only non-null for justified rows
131                 double extra = 0;
132
133                 // Non-empty if element is a string or is virtual
134                 docstring str;
135                 //
136                 Font font;
137                 //
138                 Change change;
139                 // is it possible to add contents to this element?
140                 bool final = false;
141                 // properties with respect to row breaking (made of RowFlag enums)
142                 int row_flags = Inline;
143
144                 friend std::ostream & operator<<(std::ostream & os, Element const & row);
145         };
146
147         ///
148         typedef Element value_type;
149
150         ///
151         Row() {}
152
153         /**
154          * Helper function: set variable \c var to value \c val, and mark
155          * row as changed is the values were different. This is intended
156          * for use when changing members of the row object.
157          */
158         template<class T1, class T2>
159         void change(T1 & var, T2 const val) {
160                 if (var != val)
161                         changed(true);
162                 var = val;
163         }
164         /**
165          * Helper function: set variable \c var to value \c val, and mark
166          * row as changed is the values were different. This is intended
167          * for use when changing members of the row object.
168          * This is the const version, useful for mutable members.
169          */
170         template<class T1, class T2>
171         void change(T1 & var, T2 const val) const {
172                 if (var != val)
173                         changed(true);
174                 var = val;
175         }
176         ///
177         bool changed() const { return changed_; }
178         ///
179         void changed(bool c) const { changed_ = c; }
180         ///
181         bool selection() const;
182         /**
183          * Set the selection begin and end and whether the left and/or
184          * right margins are selected.
185          * This is const because we update the selection status only at
186          * draw() time.
187          */
188         void setSelectionAndMargins(DocIterator const & beg,
189                 DocIterator const & end) const;
190         /// no selection on this row.
191         void clearSelectionAndMargins() const;
192
193         ///
194         void pit(pit_type p) { pit_ = p; }
195         ///
196         pit_type pit() const { return pit_; }
197         ///
198         void pos(pos_type p) { pos_ = p; }
199         ///
200         pos_type pos() const { return pos_; }
201         ///
202         void endpos(pos_type p) { end_ = p; }
203         ///
204         pos_type endpos() const { return end_; }
205         ///
206         void right_boundary(bool b) { right_boundary_ = b; }
207         ///
208         bool right_boundary() const { return right_boundary_; }
209         ///
210         void flushed(bool b) { flushed_ = b; }
211         ///
212         bool flushed() const { return flushed_; }
213
214         ///
215         Dimension const & dim() const { return dim_; }
216         ///
217         Dimension & dim() { return dim_; }
218         ///
219         int height() const { return dim_.height(); }
220         /// The width of the row, including the left margin, but not the right one.
221         int width() const { return dim_.wid; }
222         ///
223         int ascent() const { return dim_.asc; }
224         ///
225         int descent() const { return dim_.des; }
226
227         ///
228         Dimension const & contents_dim() const { return contents_dim_; }
229         ///
230         Dimension & contents_dim() { return contents_dim_; }
231
232         /// The offset of the left-most cursor position on the row
233         int left_x() const;
234         /// The offset of the right-most cursor position on the row
235         int right_x() const;
236
237         // Return the number of separators in the row
238         int countSeparators() const;
239         // Set the extra spacing for every expanding character in STRING-type
240         // elements.  \param w is the total amount of extra width for the row to be
241         // distributed among expanders.  \return false if the justification fails.
242         bool setExtraWidth(int w);
243
244         ///
245         void add(pos_type pos, Inset const * ins, Dimension const & dim,
246                  Font const & f, Change const & ch);
247         ///
248         void add(pos_type pos, char_type const c,
249                  Font const & f, Change const & ch, bool can_break);
250         ///
251         void addVirtual(pos_type pos, docstring const & s,
252                         Font const & f, Change const & ch);
253         ///
254         void addSpace(pos_type pos, int width, Font const & f, Change const & ch);
255
256         ///
257         typedef std::vector<Element> Elements;
258         ///
259         typedef Elements::iterator iterator;
260         ///
261         typedef Elements::const_iterator const_iterator;
262         ///
263         iterator begin() { return elements_.begin(); }
264         ///
265         iterator end() { return elements_.end(); }
266         ///
267         const_iterator begin() const { return elements_.begin(); }
268         ///
269         const_iterator end() const { return elements_.end(); }
270
271         ///
272         bool empty() const { return elements_.empty(); }
273         ///
274         Element & front() { return elements_.front(); }
275         ///
276         Element const & front() const { return elements_.front(); }
277         ///
278         Element & back() { return elements_.back(); }
279         ///
280         Element const & back() const { return elements_.back(); }
281         /// add element at the end and update width
282         void push_back(Element const &);
283         /// remove last element and update width
284         void pop_back();
285         /**
286          * if row width is too large, remove all elements after last
287          * separator and update endpos if necessary. If all that
288          * remains is a large word, cut it to \param width.
289          * \param width maximum width of the row.
290          * \param available width on next row.
291          * \return true if the row has been shortened.
292          */
293         Elements shortenIfNeeded(int const width, int const next_width);
294
295         /**
296          * If last element of the row is a string, compute its width
297          * and mark it final.
298          */
299         void finalizeLast();
300
301         /**
302          * Find sequences of right-to-left elements and reverse them.
303          * This should be called once the row is completely built.
304          */
305         void reverseRTL(bool rtl_par);
306         ///
307         bool isRTL() const { return rtl_; }
308         ///
309         bool needsChangeBar() const { return changebar_; }
310         ///
311         void needsChangeBar(bool ncb) { changebar_ = ncb; }
312
313         /// Find row element that contains \c pos, and compute x offset.
314         const_iterator const findElement(pos_type pos, bool boundary, double & x) const;
315
316         friend std::ostream & operator<<(std::ostream & os, Row const & row);
317
318         /// additional width for separators in justified rows (i.e. space)
319         double separator = 0;
320         /// width of hfills in the label
321         double label_hfill = 0;
322         /// the left margin position of the row
323         int left_margin = 0;
324         /// the right margin of the row
325         int right_margin = 0;
326         ///
327         mutable pos_type sel_beg = -1;
328         ///
329         mutable pos_type sel_end = -1;
330         ///
331         mutable bool begin_margin_sel = false;
332         ///
333         mutable bool end_margin_sel = false;
334
335 private:
336         /// Decides whether the margin is selected.
337         /**
338           * \param margin_begin
339           * \param beg
340           * \param end
341           */
342         bool isMarginSelected(bool left, DocIterator const & beg,
343                 DocIterator const & end) const;
344         /// Set the selection begin and end.
345         void setSelection(pos_type sel_beg, pos_type sel_end) const;
346
347         /**
348          * Returns true if a char or string with font \c f and change
349          * type \c ch can be added to the current last element of the
350          * row.
351          */
352         bool sameString(Font const & f, Change const & ch) const;
353
354         ///
355         Elements elements_;
356
357         /// has the Row appearance changed since last drawing?
358         mutable bool changed_ = true;
359         /// Index of the paragraph that contains this row
360         pit_type pit_ = 0;
361         /// first pos covered by this row
362         pos_type pos_ = 0;
363         /// one behind last pos covered by this row
364         pos_type end_ = 0;
365         // Is there a boundary at the end of the row (display inset...)
366         bool right_boundary_ = false;
367         // Shall the row be flushed when it is supposed to be justified?
368         bool flushed_ = false;
369         /// Row dimension.
370         Dimension dim_;
371         /// Row contents dimension. Does not contain the space above/below row.
372         Dimension contents_dim_;
373         /// true when this row lives in a right-to-left paragraph
374         bool rtl_ = false;
375         /// true when a changebar should be drawn in the margin
376         bool changebar_ = false;
377 };
378
379
380 /**
381  * Each paragraph is broken up into a number of rows on the screen.
382  * This is a list of such on-screen rows, ordered from the top row
383  * downwards.
384  */
385 typedef std::vector<Row> RowList;
386
387 } // namespace lyx
388
389 #endif