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