]> git.lyx.org Git - lyx.git/blob - src/Row.h
Handle boundary in getColumnNearX (and more)
[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                 // 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;
71
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
76                  *  default.
77                 */
78                 pos_type x2pos(double &x, bool low = false) const;
79
80                 // The kind of row element
81                 Type type;
82                 // position of the element in the paragraph
83                 pos_type pos;
84                 // first position after the element in the paragraph
85                 pos_type endpos;
86                 // The dimension of the chunk (does not contains the
87                 // separator correction)
88                 Dimension dim;
89
90                 // Non-zero only if element is an inset
91                 Inset const * inset;
92
93                 // Only non-null for separator elements
94                 double extra;
95
96                 // Non-empty if element is a string or separator
97                 docstring str;
98                 //
99                 Font font;
100                 //
101                 Change change;
102                 // is it possible to add contents to this element?
103                 bool final;
104
105                 friend std::ostream & operator<<(std::ostream & os, Element const & row);
106         };
107
108
109         ///
110         Row();
111         ///
112         bool changed() const { return changed_; }
113         ///
114         void setChanged(bool c) { changed_ = c; }
115         ///
116         void setCrc(size_type crc) const;
117         /// Set the selection begin and end.
118         /**
119           * This is const because we update the selection status only at draw()
120           * time.
121           */
122         void setSelection(pos_type sel_beg, pos_type sel_end) const;
123         ///
124         bool selection() const;
125         /// Set the selection begin and end and whether the left and/or right
126         /// margins are selected.
127         void setSelectionAndMargins(DocIterator const & beg,
128                 DocIterator const & end) const;
129
130         ///
131         void pos(pos_type p);
132         ///
133         pos_type pos() const { return pos_; }
134         ///
135         void endpos(pos_type p);
136         ///
137         pos_type endpos() const { return end_; }
138         ///
139         Dimension const & dimension() const { return dim_; }
140         ///
141         Dimension & dimension() { return dim_; }
142         ///
143         int height() const { return dim_.height(); }
144         ///
145         int width() const { return dim_.wid; }
146         ///
147         int ascent() const { return dim_.asc; }
148         ///
149         int descent() const { return dim_.des; }
150
151         ///
152         void add(pos_type pos, Inset const * ins, Dimension const & dim,
153                  Font const & f, Change const & ch);
154         ///
155         void add(pos_type pos, char_type const c,
156                  Font const & f, Change const & ch);
157         ///
158         void addVirtual(pos_type pos, docstring const & s,
159                         Font const & f, Change const & ch);
160         ///
161         void addSeparator(pos_type pos, char_type const c,
162                           Font const & f, Change const & ch);
163         ///
164         void addSpace(pos_type pos, int width, Font const & f, Change const & ch);
165
166         ///
167         typedef std::vector<Element> Elements;
168         ///
169         typedef Elements::iterator iterator;
170         ///
171         typedef Elements::const_iterator const_iterator;
172         ///
173         iterator begin() { return elements_.begin(); }
174         ///
175         iterator end() { return elements_.end(); }
176         ///
177         const_iterator begin() const { return elements_.begin(); }
178         ///
179         const_iterator end() const { return elements_.end(); }
180
181         ///
182         bool empty() const { return elements_.empty(); }
183         ///
184         Element & back() { return elements_.back(); }
185         ///
186         Element const & back() const { return elements_.back(); }
187         /// remove last element
188         void pop_back();
189         /// remove all row elements
190         void clear() { elements_.clear(); }
191         /**
192          * if row width is too large, remove all elements after last
193          * separator and update endpos if necessary. If all that
194          * rename is a large word, cut it to \param width.
195          * \param body_pos minimum amount of text to keep.
196          * \param width maximum width of the row
197          */
198         void shorten_if_needed(pos_type const body_pos, int const width);
199
200         /**
201          * If last element of the row is a string, compute its width
202          * and mark it final.
203          */
204         void finalizeLast();
205
206         /**
207          * Find sequences of right-to-left elements and reverse them.
208          * This should be called once the row is completely built.
209          */
210         void reverseRTL();
211
212         friend std::ostream & operator<<(std::ostream & os, Row const & row);
213
214         /// width of a separator (i.e. space)
215         double separator;
216         /// width of hfills in the label
217         double label_hfill;
218         /// the x position of the row (left margin)
219         double x;
220         /// the right margin of the row
221         int right_margin;
222         ///
223         mutable pos_type sel_beg;
224         ///
225         mutable pos_type sel_end;
226         ///
227         mutable bool begin_margin_sel;
228         ///
229         mutable bool end_margin_sel;
230
231 private:
232         /// Decides whether the margin is selected.
233         /**
234           * \param margin_begin
235           * \param beg
236           * \param end
237           */
238         bool isMarginSelected(bool left_margin, DocIterator const & beg,
239                 DocIterator const & end) const;
240
241         /**
242          * Returns true if a char or string with font \c f and change
243          * type \c ch can be added to the current last element of the
244          * row.
245          */
246         bool sameString(Font const & f, Change const & ch) const;
247
248         ///
249         Elements elements_;
250
251         /// has the Row appearance changed since last drawing?
252         mutable bool changed_;
253         /// CRC of row contents.
254         mutable size_type crc_;
255         /// first pos covered by this row
256         pos_type pos_;
257         /// one behind last pos covered by this row
258         pos_type end_;
259         /// Row dimension.
260         Dimension dim_;
261 };
262
263
264 } // namespace lyx
265
266 #endif