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
49 // A stretchable space, basically
53 // Some spacing described by its width, not a string
58 * One element of a Row. It has a set of attributes that can be used
59 * by other methods that need to parse the Row contents.
62 Element(Type const t, pos_type p, Font const & f, Change const & ch)
63 : type(t), pos(p), endpos(p + 1), inset(0),
64 extra(0), font(f), change(ch), final(false) {}
66 // returns total width of element, including separator overhead
67 double width() const { return dim.wid + extra; };
68 // returns position in pixels (from the left) of position
69 // \param i in the row element.
70 double pos2x(pos_type const i) const;
72 /** Return character position that is the closest to
73 * pixel position \param x. The value \param x is
74 * rounded to the actual pixel position. If \param
75 * short is true, the pixel value is rounded by
78 pos_type x2pos(double &x, bool low = false) const;
80 // Returns the position on left side of the element.
81 pos_type left_pos() const;
82 // Returns the position on right side of the element.
83 pos_type right_pos() const;
85 // The kind of row element
87 // position of the element in the paragraph
89 // first position after the element in the paragraph
91 // The dimension of the chunk (does not contains the
92 // separator correction)
95 // Non-zero only if element is an inset
98 // Only non-null for separator elements
101 // Non-empty if element is a string or separator
107 // is it possible to add contents to this element?
110 friend std::ostream & operator<<(std::ostream & os, Element const & row);
117 bool changed() const { return changed_; }
119 void setChanged(bool c) { changed_ = c; }
121 void setCrc(size_type crc) const;
122 /// Set the selection begin and end.
124 * This is const because we update the selection status only at draw()
127 void setSelection(pos_type sel_beg, pos_type sel_end) const;
129 bool selection() const;
130 /// Set the selection begin and end and whether the left and/or right
131 /// margins are selected.
132 void setSelectionAndMargins(DocIterator const & beg,
133 DocIterator const & end) const;
136 void pos(pos_type p) { pos_ = p; }
138 pos_type pos() const { return pos_; }
140 void endpos(pos_type p) { end_ = p; }
142 pos_type endpos() const { return end_; }
144 void right_boundary(bool b) { right_boundary_ = b; }
146 bool right_boundary() const { return right_boundary_; }
149 Dimension const & dimension() const { return dim_; }
151 Dimension & dimension() { return dim_; }
153 int height() const { return dim_.height(); }
155 int width() const { return dim_.wid; }
157 int ascent() const { return dim_.asc; }
159 int descent() const { return dim_.des; }
162 void add(pos_type pos, Inset const * ins, Dimension const & dim,
163 Font const & f, Change const & ch);
165 void add(pos_type pos, char_type const c,
166 Font const & f, Change const & ch);
168 void addVirtual(pos_type pos, docstring const & s,
169 Font const & f, Change const & ch);
171 void addSeparator(pos_type pos, char_type const c,
172 Font const & f, Change const & ch);
174 void addSpace(pos_type pos, int width, Font const & f, Change const & ch);
177 typedef std::vector<Element> Elements;
179 typedef Elements::iterator iterator;
181 typedef Elements::const_iterator const_iterator;
183 iterator begin() { return elements_.begin(); }
185 iterator end() { return elements_.end(); }
187 const_iterator begin() const { return elements_.begin(); }
189 const_iterator end() const { return elements_.end(); }
192 bool empty() const { return elements_.empty(); }
194 Element & front() { return elements_.front(); }
196 Element const & front() const { return elements_.front(); }
198 Element & back() { return elements_.back(); }
200 Element const & back() const { return elements_.back(); }
201 /// remove last element
203 /// remove all row elements
204 void clear() { elements_.clear(); }
206 * if row width is too large, remove all elements after last
207 * separator and update endpos if necessary. If all that
208 * remains is a large word, cut it to \param width.
209 * \param body_pos minimum amount of text to keep.
210 * \param width maximum width of the row
212 void shorten_if_needed(pos_type const body_pos, int const width);
215 * If last element of the row is a string, compute its width
221 * Find sequences of right-to-left elements and reverse them.
222 * This should be called once the row is completely built.
224 void reverseRTL(bool rtl_par);
226 friend std::ostream & operator<<(std::ostream & os, Row const & row);
228 /// width of a separator (i.e. space)
230 /// width of hfills in the label
232 /// the x position of the row (left margin)
234 /// the right margin of the row
237 mutable pos_type sel_beg;
239 mutable pos_type sel_end;
241 mutable bool begin_margin_sel;
243 mutable bool end_margin_sel;
246 /// Decides whether the margin is selected.
248 * \param margin_begin
252 bool isMarginSelected(bool left_margin, DocIterator const & beg,
253 DocIterator const & end) const;
256 * Returns true if a char or string with font \c f and change
257 * type \c ch can be added to the current last element of the
260 bool sameString(Font const & f, Change const & ch) const;
265 /// has the Row appearance changed since last drawing?
266 mutable bool changed_;
267 /// CRC of row contents.
268 mutable size_type crc_;
269 /// first pos covered by this row
271 /// one behind last pos covered by this row
273 // Is there is a boundary at the end of the row (display inset...)
274 bool right_boundary_;