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