]> git.lyx.org Git - features.git/blob - src/Row.h
Do not break row elements at spaces
[features.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  * FIXME: Change Row object to operate only on integers and not doubles.
34  *
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.
41  */
42
43
44 /**
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
47  * of the line.
48  */
49 class Row {
50 public:
51         // Possible types of row elements
52         enum Type {
53                 // a string of character
54                 STRING,
55                 /**
56                  * Something (completion, end-of-par marker)
57                  * that occupies space one screen but does not
58                  * correspond to any paragraph contents
59                  */
60                 VIRTUAL,
61                 // A stretchable space, basically
62                 SEPARATOR,
63                 // An inset
64                 INSET,
65                 // Some spacing described by its width, not a string
66                 SPACE
67         };
68
69 /**
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.
72  */
73         struct Element {
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) {}
77
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.
82                  */
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.
87                 */
88                 pos_type x2pos(int &x) const;
89                 /** Break the element if possible, so that its width is less
90                  * than \param w. Returns true on success. When \param force
91                  * is true, the string is cut at any place, other wise it
92                  * respects the row breaking rules of characters.
93                  */
94                 bool breakAt(int w, bool force);
95
96                 // Returns the position on left side of the element.
97                 pos_type left_pos() const;
98                 // Returns the position on right side of the element.
99                 pos_type right_pos() const;
100
101                 // The kind of row element
102                 Type type;
103                 // position of the element in the paragraph
104                 pos_type pos;
105                 // first position after the element in the paragraph
106                 pos_type endpos;
107                 // The dimension of the chunk (does not contains the
108                 // separator correction)
109                 Dimension dim;
110
111                 // Non-zero only if element is an inset
112                 Inset const * inset;
113
114                 // Only non-null for separator elements
115                 double extra;
116
117                 // Non-empty if element is a string or separator
118                 docstring str;
119                 //
120                 Font font;
121                 //
122                 Change change;
123                 // is it possible to add contents to this element?
124                 bool final;
125
126                 friend std::ostream & operator<<(std::ostream & os, Element const & row);
127         };
128
129
130         ///
131         Row();
132         ///
133         bool changed() const { return changed_; }
134         ///
135         void setChanged(bool c) { changed_ = c; }
136         ///
137         void setCrc(size_type crc) const;
138         /// Set the selection begin and end.
139         /**
140           * This is const because we update the selection status only at draw()
141           * time.
142           */
143         void setSelection(pos_type sel_beg, pos_type sel_end) const;
144         ///
145         bool selection() const;
146         /// Set the selection begin and end and whether the left and/or right
147         /// margins are selected.
148         void setSelectionAndMargins(DocIterator const & beg,
149                 DocIterator const & end) const;
150
151         ///
152         void pos(pos_type p) { pos_ = p; }
153         ///
154         pos_type pos() const { return pos_; }
155         ///
156         void endpos(pos_type p) { end_ = p; }
157         ///
158         pos_type endpos() const { return end_; }
159         ///
160         void right_boundary(bool b) { right_boundary_ = b; }
161         ///
162         bool right_boundary() const { return right_boundary_; }
163
164         ///
165         Dimension const & dimension() const { return dim_; }
166         ///
167         Dimension & dimension() { return dim_; }
168         ///
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; }
172         ///
173         int ascent() const { return dim_.asc; }
174         ///
175         int descent() const { return dim_.des; }
176
177         ///
178         void add(pos_type pos, Inset const * ins, Dimension const & dim,
179                  Font const & f, Change const & ch);
180         ///
181         void add(pos_type pos, char_type const c,
182                  Font const & f, Change const & ch);
183         ///
184         void addVirtual(pos_type pos, docstring const & s,
185                         Font const & f, Change const & ch);
186         ///
187         void addSeparator(pos_type pos, char_type const c,
188                           Font const & f, Change const & ch);
189         ///
190         void addSpace(pos_type pos, int width, Font const & f, Change const & ch);
191
192         ///
193         typedef std::vector<Element> Elements;
194         ///
195         typedef Elements::iterator iterator;
196         ///
197         typedef Elements::const_iterator const_iterator;
198         ///
199         iterator begin() { return elements_.begin(); }
200         ///
201         iterator end() { return elements_.end(); }
202         ///
203         const_iterator begin() const { return elements_.begin(); }
204         ///
205         const_iterator end() const { return elements_.end(); }
206
207         ///
208         bool empty() const { return elements_.empty(); }
209         ///
210         Element & front() { return elements_.front(); }
211         ///
212         Element const & front() const { return elements_.front(); }
213         ///
214         Element & back() { return elements_.back(); }
215         ///
216         Element const & back() const { return elements_.back(); }
217         /// remove last element
218         void pop_back();
219         /// remove all row elements
220         void clear() { elements_.clear(); }
221         /**
222          * if row width is too large, remove all elements after last
223          * separator and update endpos if necessary. If all that
224          * remains is a large word, cut it to \param width.
225          * \param body_pos minimum amount of text to keep.
226          * \param width maximum width of the row
227          */
228         void shortenIfNeeded(pos_type const body_pos, int const width);
229
230         /**
231          * If last element of the row is a string, compute its width
232          * and mark it final.
233          */
234         void finalizeLast();
235
236         /**
237          * Find sequences of right-to-left elements and reverse them.
238          * This should be called once the row is completely built.
239          */
240         void reverseRTL(bool rtl_par);
241
242         friend std::ostream & operator<<(std::ostream & os, Row const & row);
243
244         /// width of a separator (i.e. space)
245         double separator;
246         /// width of hfills in the label
247         double label_hfill;
248         /// the left margin position of the row
249         int left_margin;
250         /// the right margin of the row
251         int right_margin;
252         ///
253         mutable pos_type sel_beg;
254         ///
255         mutable pos_type sel_end;
256         ///
257         mutable bool begin_margin_sel;
258         ///
259         mutable bool end_margin_sel;
260
261 private:
262         /// Decides whether the margin is selected.
263         /**
264           * \param margin_begin
265           * \param beg
266           * \param end
267           */
268         bool isMarginSelected(bool left_margin, DocIterator const & beg,
269                 DocIterator const & end) const;
270
271         /**
272          * Returns true if a char or string with font \c f and change
273          * type \c ch can be added to the current last element of the
274          * row.
275          */
276         bool sameString(Font const & f, Change const & ch) const;
277
278         ///
279         Elements elements_;
280
281         /// has the Row appearance changed since last drawing?
282         mutable bool changed_;
283         /// CRC of row contents.
284         mutable size_type crc_;
285         /// first pos covered by this row
286         pos_type pos_;
287         /// one behind last pos covered by this row
288         pos_type end_;
289         // Is there is a boundary at the end of the row (display inset...)
290         bool right_boundary_;
291         /// Row dimension.
292         Dimension dim_;
293 };
294
295
296 } // namespace lyx
297
298 #endif