4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Matthias Ettrich
8 * \author Lars Gullik Bjønnes
10 * Full author contact details are available in file CREDITS.
12 * Metrics for an on-screen text row.
19 #include "Dimension.h"
22 #include "support/docstring.h"
23 #include "support/types.h"
33 * FIXME: Change Row object to operate only on integers and not doubles.
35 * This use of double is only useful to distribute the extra
36 * horizontal space between separators in justified text. If we do
37 * integer arithmetic, then it is possible to have two groups of
38 * separators, with size s or s+1. Then strings can be drawn without
39 * cutting at separators in justfied text, as it is done in
40 * non-justified text. This will improve performance.
45 * An on-screen row of text. A paragraph is broken into a RowList for
46 * display. Each Row contains a tokenized description of the contents
51 // Possible types of row elements
53 // a string of character
56 * Something (completion, end-of-par marker)
57 * that occupies space one screen but does not
58 * correspond to any paragraph contents
61 // A stretchable space, basically
65 // Some spacing described by its width, not a string
70 * One element of a Row. It has a set of attributes that can be used
71 * by other methods that need to parse the Row contents.
74 Element(Type const t, pos_type p, Font const & f, Change const & ch)
75 : type(t), pos(p), endpos(p + 1), inset(0),
76 extra(0), font(f), change(ch), final(false) {}
78 // Return total width of element, including separator overhead
79 double full_width() const { return dim.wid + extra; };
80 /** Return position in pixels (from the left) of position
81 * \param i in the row element.
83 double pos2x(pos_type const i) const;
84 /** Return character position that is the closest to
85 * pixel position \param x. The value \param x is
86 * adjusted to the actual pixel position.
88 pos_type x2pos(int &x) const;
89 /** Break the element if possible, so that its width is
90 * less then \param w. Returns true on success.
94 // Returns the position on left side of the element.
95 pos_type left_pos() const;
96 // Returns the position on right side of the element.
97 pos_type right_pos() const;
99 // The kind of row element
101 // position of the element in the paragraph
103 // first position after the element in the paragraph
105 // The dimension of the chunk (does not contains the
106 // separator correction)
109 // Non-zero only if element is an inset
112 // Only non-null for separator elements
115 // Non-empty if element is a string or separator
121 // is it possible to add contents to this element?
124 friend std::ostream & operator<<(std::ostream & os, Element const & row);
131 bool changed() const { return changed_; }
133 void setChanged(bool c) { changed_ = c; }
135 void setCrc(size_type crc) const;
136 /// Set the selection begin and end.
138 * This is const because we update the selection status only at draw()
141 void setSelection(pos_type sel_beg, pos_type sel_end) const;
143 bool selection() const;
144 /// Set the selection begin and end and whether the left and/or right
145 /// margins are selected.
146 void setSelectionAndMargins(DocIterator const & beg,
147 DocIterator const & end) const;
150 void pos(pos_type p) { pos_ = p; }
152 pos_type pos() const { return pos_; }
154 void endpos(pos_type p) { end_ = p; }
156 pos_type endpos() const { return end_; }
158 void right_boundary(bool b) { right_boundary_ = b; }
160 bool right_boundary() const { return right_boundary_; }
163 Dimension const & dimension() const { return dim_; }
165 Dimension & dimension() { return dim_; }
167 int height() const { return dim_.height(); }
168 /// The width of the row, including the left margin, but not the right one.
169 int width() const { return dim_.wid; }
171 int ascent() const { return dim_.asc; }
173 int descent() const { return dim_.des; }
176 void add(pos_type pos, Inset const * ins, Dimension const & dim,
177 Font const & f, Change const & ch);
179 void add(pos_type pos, char_type const c,
180 Font const & f, Change const & ch);
182 void addVirtual(pos_type pos, docstring const & s,
183 Font const & f, Change const & ch);
185 void addSeparator(pos_type pos, char_type const c,
186 Font const & f, Change const & ch);
188 void addSpace(pos_type pos, int width, Font const & f, Change const & ch);
191 typedef std::vector<Element> Elements;
193 typedef Elements::iterator iterator;
195 typedef Elements::const_iterator const_iterator;
197 iterator begin() { return elements_.begin(); }
199 iterator end() { return elements_.end(); }
201 const_iterator begin() const { return elements_.begin(); }
203 const_iterator end() const { return elements_.end(); }
206 bool empty() const { return elements_.empty(); }
208 Element & front() { return elements_.front(); }
210 Element const & front() const { return elements_.front(); }
212 Element & back() { return elements_.back(); }
214 Element const & back() const { return elements_.back(); }
215 /// remove last element
217 /// remove all row elements
218 void clear() { elements_.clear(); }
220 * if row width is too large, remove all elements after last
221 * separator and update endpos if necessary. If all that
222 * remains is a large word, cut it to \param width.
223 * \param body_pos minimum amount of text to keep.
224 * \param width maximum width of the row
226 void shortenIfNeeded(pos_type const body_pos, int const width);
229 * If last element of the row is a string, compute its width
235 * Find sequences of right-to-left elements and reverse them.
236 * This should be called once the row is completely built.
238 void reverseRTL(bool rtl_par);
240 friend std::ostream & operator<<(std::ostream & os, Row const & row);
242 /// width of a separator (i.e. space)
244 /// width of hfills in the label
246 /// the left margin position of the row
248 /// the right margin of the row
251 mutable pos_type sel_beg;
253 mutable pos_type sel_end;
255 mutable bool begin_margin_sel;
257 mutable bool end_margin_sel;
260 /// Decides whether the margin is selected.
262 * \param margin_begin
266 bool isMarginSelected(bool left_margin, DocIterator const & beg,
267 DocIterator const & end) const;
270 * Returns true if a char or string with font \c f and change
271 * type \c ch can be added to the current last element of the
274 bool sameString(Font const & f, Change const & ch) const;
279 /// has the Row appearance changed since last drawing?
280 mutable bool changed_;
281 /// CRC of row contents.
282 mutable size_type crc_;
283 /// first pos covered by this row
285 /// one behind last pos covered by this row
287 // Is there is a boundary at the end of the row (display inset...)
288 bool right_boundary_;