]> git.lyx.org Git - features.git/blob - src/Row.h
New helper method Row::Element::isRTL()
[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
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         };
54
55 /**
56  * One element of a Row. It has a set of attributes that can be used
57  * by other methods that need to parse the Row contents.
58  */
59         struct Element {
60                 Element(Type const t, pos_type p, Font const & f, Change const & ch)
61                         : type(t), pos(p), endpos(p + 1), inset(0),
62                           extra(0), font(f), change(ch), final(false) {}
63
64                 // Return total width of element, including separator overhead
65                 double full_width() const { return dim.wid + extra * countSeparators(); }
66                 // Return the number of separator in the element (only STRING type)
67                 int countSeparators() const;
68
69                 /** Return position in pixels (from the left) of position
70                  * \param i in the row element.
71                  */
72                 double pos2x(pos_type const i) const;
73                 /** Return character position that is the closest to
74                  *  pixel position \param x. The value \param x is
75                  *  adjusted to the actual pixel position.
76                 */
77                 pos_type x2pos(int &x) const;
78                 /** Break the element if possible, so that its width is less
79                  * than \param w. Returns true on success. When \param force
80                  * is true, the string is cut at any place, other wise it
81                  * respects the row breaking rules of characters.
82                  */
83                 bool breakAt(int w, bool force);
84
85                 // Returns the position on left side of the element.
86                 pos_type left_pos() const;
87                 // Returns the position on right side of the element.
88                 pos_type right_pos() const;
89
90                 //
91                 bool isRTL() const { return font.isVisibleRightToLeft(); }
92
93                 // The kind of row element
94                 Type type;
95                 // position of the element in the paragraph
96                 pos_type pos;
97                 // first position after the element in the paragraph
98                 pos_type endpos;
99                 // The dimension of the chunk (does not contains the
100                 // separator correction)
101                 Dimension dim;
102
103                 // Non-zero only if element is an inset
104                 Inset const * inset;
105
106                 // Only non-null for justified rows
107                 double extra;
108
109                 // Non-empty if element is a string or is virtual
110                 docstring str;
111                 //
112                 Font font;
113                 //
114                 Change change;
115                 // is it possible to add contents to this element?
116                 bool final;
117
118                 friend std::ostream & operator<<(std::ostream & os, Element const & row);
119         };
120
121
122         ///
123         Row();
124         ///
125         bool changed() const { return changed_; }
126         ///
127         void setChanged(bool c) { changed_ = c; }
128         ///
129         void setCrc(size_type crc) const;
130         /// Set the selection begin and end.
131         /**
132           * This is const because we update the selection status only at draw()
133           * time.
134           */
135         void setSelection(pos_type sel_beg, pos_type sel_end) const;
136         ///
137         bool selection() const;
138         /// Set the selection begin and end and whether the left and/or right
139         /// margins are selected.
140         void setSelectionAndMargins(DocIterator const & beg,
141                 DocIterator const & end) const;
142
143         ///
144         void pos(pos_type p) { pos_ = p; }
145         ///
146         pos_type pos() const { return pos_; }
147         ///
148         void endpos(pos_type p) { end_ = p; }
149         ///
150         pos_type endpos() const { return end_; }
151         ///
152         void right_boundary(bool b) { right_boundary_ = b; }
153         ///
154         bool right_boundary() const { return right_boundary_; }
155
156         ///
157         Dimension const & dimension() const { return dim_; }
158         ///
159         Dimension & dimension() { return dim_; }
160         ///
161         int height() const { return dim_.height(); }
162         /// The width of the row, including the left margin, but not the right one.
163         int width() const { return dim_.wid; }
164         ///
165         int ascent() const { return dim_.asc; }
166         ///
167         int descent() const { return dim_.des; }
168
169         // Return the number of separators in the row
170         int countSeparators() const;
171         // Set the extra spacing for every separator in STRING elements
172         void setSeparatorExtraWidth(double w);
173
174         ///
175         void add(pos_type pos, Inset const * ins, Dimension const & dim,
176                  Font const & f, Change const & ch);
177         ///
178         void add(pos_type pos, char_type const c,
179                  Font const & f, Change const & ch);
180         ///
181         void addVirtual(pos_type pos, docstring const & s,
182                         Font const & f, Change const & ch);
183         ///
184         void addSpace(pos_type pos, int width, Font const & f, Change const & ch);
185
186         ///
187         typedef std::vector<Element> Elements;
188         ///
189         typedef Elements::iterator iterator;
190         ///
191         typedef Elements::const_iterator const_iterator;
192         ///
193         iterator begin() { return elements_.begin(); }
194         ///
195         iterator end() { return elements_.end(); }
196         ///
197         const_iterator begin() const { return elements_.begin(); }
198         ///
199         const_iterator end() const { return elements_.end(); }
200
201         ///
202         bool empty() const { return elements_.empty(); }
203         ///
204         Element & front() { return elements_.front(); }
205         ///
206         Element const & front() const { return elements_.front(); }
207         ///
208         Element & back() { return elements_.back(); }
209         ///
210         Element const & back() const { return elements_.back(); }
211         /// remove last element
212         void pop_back();
213         /// remove all row elements
214         void clear() { elements_.clear(); }
215         /**
216          * if row width is too large, remove all elements after last
217          * separator and update endpos if necessary. If all that
218          * remains is a large word, cut it to \param width.
219          * \param body_pos minimum amount of text to keep.
220          * \param width maximum width of the row
221          */
222         void shortenIfNeeded(pos_type const body_pos, int const width);
223
224         /**
225          * If last element of the row is a string, compute its width
226          * and mark it final.
227          */
228         void finalizeLast();
229
230         /**
231          * Find sequences of right-to-left elements and reverse them.
232          * This should be called once the row is completely built.
233          */
234         void reverseRTL(bool rtl_par);
235
236         friend std::ostream & operator<<(std::ostream & os, Row const & row);
237
238         /// additional width for separators in justified rows (i.e. space)
239         double separator;
240         /// width of hfills in the label
241         double label_hfill;
242         /// the left margin position of the row
243         int left_margin;
244         /// the right margin of the row
245         int right_margin;
246         ///
247         mutable pos_type sel_beg;
248         ///
249         mutable pos_type sel_end;
250         ///
251         mutable bool begin_margin_sel;
252         ///
253         mutable bool end_margin_sel;
254
255 private:
256         /// Decides whether the margin is selected.
257         /**
258           * \param margin_begin
259           * \param beg
260           * \param end
261           */
262         bool isMarginSelected(bool left_margin, DocIterator const & beg,
263                 DocIterator const & end) const;
264
265         /**
266          * Returns true if a char or string with font \c f and change
267          * type \c ch can be added to the current last element of the
268          * row.
269          */
270         bool sameString(Font const & f, Change const & ch) const;
271
272         ///
273         Elements elements_;
274
275         /// has the Row appearance changed since last drawing?
276         mutable bool changed_;
277         /// CRC of row contents.
278         mutable size_type crc_;
279         /// first pos covered by this row
280         pos_type pos_;
281         /// one behind last pos covered by this row
282         pos_type end_;
283         // Is there is a boundary at the end of the row (display inset...)
284         bool right_boundary_;
285         /// Row dimension.
286         Dimension dim_;
287 };
288
289
290 } // namespace lyx
291
292 #endif