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 * 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
39 // Possible types of row elements
41 // a string of character
44 * Something (completion, end-of-par marker)
45 * that occupies space one screen but does not
46 * correspond to any paragraph contents
51 // Some spacing described by its width, not a string
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.
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) {}
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;
69 /** Return position in pixels (from the left) of position
70 * \param i in the row element.
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.
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.
83 bool breakAt(int w, bool force);
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;
91 bool isRTL() const { return font.isVisibleRightToLeft(); }
92 // This is true for virtual elements.
93 // Note that we do not use the type here. The two definitions
94 // should be equivalent
95 bool isVirtual() const { return pos == endpos; }
97 // The kind of row element
99 // position of the element in the paragraph
101 // first position after the element in the paragraph
103 // The dimension of the chunk (does not contains the
104 // separator correction)
107 // Non-zero only if element is an inset
110 // Only non-null for justified rows
113 // Non-empty if element is a string or is virtual
119 // is it possible to add contents to this element?
122 friend std::ostream & operator<<(std::ostream & os, Element const & row);
129 bool changed() const { return changed_; }
131 void setChanged(bool c) { changed_ = c; }
133 void setCrc(size_type crc) const;
134 /// Set the selection begin and end.
136 * This is const because we update the selection status only at draw()
139 void setSelection(pos_type sel_beg, pos_type sel_end) const;
141 bool selection() const;
142 /// Set the selection begin and end and whether the left and/or right
143 /// margins are selected.
144 void setSelectionAndMargins(DocIterator const & beg,
145 DocIterator const & end) const;
148 void pit(pit_type p) { pit_ = p; }
150 pit_type pit() const { return pit_; }
152 void pos(pos_type p) { pos_ = p; }
154 pos_type pos() const { return pos_; }
156 void endpos(pos_type p) { end_ = p; }
158 pos_type endpos() const { return end_; }
160 void right_boundary(bool b) { right_boundary_ = b; }
162 bool right_boundary() const { return right_boundary_; }
165 Dimension const & dimension() const { return dim_; }
167 Dimension & dimension() { return dim_; }
169 int height() const { return dim_.height(); }
170 /// The width of the row, including the left margin, but not the right one.
171 int width() const { return dim_.wid; }
173 int ascent() const { return dim_.asc; }
175 int descent() const { return dim_.des; }
177 /// The offset of the left-most cursor position on the row
179 /// The offset of the right-most cursor position on the row
182 // Return the number of separators in the row
183 int countSeparators() const;
184 // Set the extra spacing for every separator in STRING elements
185 void setSeparatorExtraWidth(double w);
188 void add(pos_type pos, Inset const * ins, Dimension const & dim,
189 Font const & f, Change const & ch);
191 void add(pos_type pos, char_type const c,
192 Font const & f, Change const & ch);
194 void addVirtual(pos_type pos, docstring const & s,
195 Font const & f, Change const & ch);
197 void addSpace(pos_type pos, int width, Font const & f, Change const & ch);
200 typedef std::vector<Element> Elements;
202 typedef Elements::iterator iterator;
204 typedef Elements::const_iterator const_iterator;
206 iterator begin() { return elements_.begin(); }
208 iterator end() { return elements_.end(); }
210 const_iterator begin() const { return elements_.begin(); }
212 const_iterator end() const { return elements_.end(); }
215 bool empty() const { return elements_.empty(); }
217 Element & front() { return elements_.front(); }
219 Element const & front() const { return elements_.front(); }
221 Element & back() { return elements_.back(); }
223 Element const & back() const { return elements_.back(); }
224 /// remove last element
226 /// remove all row elements
227 void clear() { elements_.clear(); }
229 * if row width is too large, remove all elements after last
230 * separator and update endpos if necessary. If all that
231 * remains is a large word, cut it to \param width.
232 * \param body_pos minimum amount of text to keep.
233 * \param width maximum width of the row
234 * \return true if the row has been shortened.
236 bool shortenIfNeeded(pos_type const body_pos, int const width);
239 * If last element of the row is a string, compute its width
245 * Find sequences of right-to-left elements and reverse them.
246 * This should be called once the row is completely built.
248 void reverseRTL(bool rtl_par);
250 friend std::ostream & operator<<(std::ostream & os, Row const & row);
252 /// additional width for separators in justified rows (i.e. space)
254 /// width of hfills in the label
256 /// the left margin position of the row
258 /// the right margin of the row
261 mutable pos_type sel_beg;
263 mutable pos_type sel_end;
265 mutable bool begin_margin_sel;
267 mutable bool end_margin_sel;
270 /// Decides whether the margin is selected.
272 * \param margin_begin
276 bool isMarginSelected(bool left_margin, DocIterator const & beg,
277 DocIterator const & end) const;
280 * Returns true if a char or string with font \c f and change
281 * type \c ch can be added to the current last element of the
284 bool sameString(Font const & f, Change const & ch) const;
289 /// has the Row appearance changed since last drawing?
290 mutable bool changed_;
291 /// CRC of row contents.
292 mutable size_type crc_;
293 /// Index of the paragraph that contains this row
295 /// first pos covered by this row
297 /// one behind last pos covered by this row
299 // Is there is a boundary at the end of the row (display inset...)
300 bool right_boundary_;