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