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.
75 pos_type x2pos(double &x) const;
77 // The kind of row element
79 // position of the element in the paragraph
81 // first position after the element in the paragraph
83 // The dimension of the chunk (does not contains the
84 // separator correction)
87 // Non-zero only if element is an inset
90 // Only non-null for separator elements
93 // Non-empty if element is a string or separator
99 // is it possible to add contents to this element?
102 friend std::ostream & operator<<(std::ostream & os, Element const & row);
109 bool changed() const { return changed_; }
111 void setChanged(bool c) { changed_ = c; }
113 void setCrc(size_type crc) const;
114 /// Set the selection begin and end.
116 * This is const because we update the selection status only at draw()
119 void setSelection(pos_type sel_beg, pos_type sel_end) const;
121 bool selection() const;
122 /// Set the selection begin and end and whether the left and/or right
123 /// margins are selected.
124 void setSelectionAndMargins(DocIterator const & beg,
125 DocIterator const & end) const;
128 void pos(pos_type p);
130 pos_type pos() const { return pos_; }
132 void endpos(pos_type p);
134 pos_type endpos() const { return end_; }
136 Dimension const & dimension() const { return dim_; }
138 Dimension & dimension() { return dim_; }
140 int height() const { return dim_.height(); }
142 int width() const { return dim_.wid; }
144 int ascent() const { return dim_.asc; }
146 int descent() const { return dim_.des; }
149 void add(pos_type pos, Inset const * ins, Dimension const & dim,
150 Font const & f, Change const & ch);
152 void add(pos_type pos, char_type const c,
153 Font const & f, Change const & ch);
155 void addVirtual(pos_type pos, docstring const & s,
156 Font const & f, Change const & ch);
158 void addSeparator(pos_type pos, char_type const c,
159 Font const & f, Change const & ch);
161 void addSpace(pos_type pos, int width, Font const & f, Change const & ch);
164 typedef std::vector<Element> Elements;
166 typedef Elements::iterator iterator;
168 typedef Elements::const_iterator const_iterator;
170 iterator begin() { return elements_.begin(); }
172 iterator end() { return elements_.end(); }
174 const_iterator begin() const { return elements_.begin(); }
176 const_iterator end() const { return elements_.end(); }
179 bool empty() const { return elements_.empty(); }
181 Element & back() { return elements_.back(); }
183 Element const & back() const { return elements_.back(); }
184 /// remove last element
186 /// remove all row elements
187 void clear() { elements_.clear(); }
189 * remove all elements after last separator and update endpos
191 * \param keep is the minimum amount of text to keep.
193 void separate_back(pos_type keep);
196 * If last element of the row is a string, compute its width
202 * Find sequences of RtL elements and reverse them.
203 * This should be called once the row is completely built.
207 friend std::ostream & operator<<(std::ostream & os, Row const & row);
209 /// width of a separator (i.e. space)
211 /// width of hfills in the label
213 /// the x position of the row (left margin)
215 /// the right margin of the row
218 mutable pos_type sel_beg;
220 mutable pos_type sel_end;
222 mutable bool begin_margin_sel;
224 mutable bool end_margin_sel;
227 /// Decides whether the margin is selected.
229 * \param margin_begin
233 bool isMarginSelected(bool left_margin, DocIterator const & beg,
234 DocIterator const & end) const;
237 * Returns true if a char or string with font \c f and change
238 * type \c ch can be added to the current last element of the
241 bool sameString(Font const & f, Change const & ch) const;
246 /// has the Row appearance changed since last drawing?
247 mutable bool changed_;
248 /// CRC of row contents.
249 mutable size_type crc_;
250 /// first pos covered by this row
252 /// one behind last pos covered by this row