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