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