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