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