]> git.lyx.org Git - features.git/blob - src/Row.h
fc542f9bb69c64ef454f34f04a4644ee0571a624
[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  * An on-screen row of text. A paragraph is broken into a
34  * RowList for display. Each Row contains position pointers
35  * into the first and last character positions of that row.
36  */
37 class Row {
38 public:
39 /**
40  * One element of a Row. It has a set of attributes that can be used
41  * by other methods that need to parse the Row contents.
42  */
43         struct Element {
44                 enum Type {
45                         STRING,
46                         SEPARATOR,
47                         INSET,
48                         SPACE
49                 };
50
51                 Element(Type const t, pos_type p, Font const & f, Change const & ch) 
52                         : type(t), pos(p), endpos(p + 1), inset(0), final(false),
53                           font(f), change(ch) {}
54
55                 //
56                 bool isLineSeparator() const { return type == SEPARATOR; }
57
58                 // The kind of row element
59                 Type type;
60                 // position of the element in the paragraph
61                 pos_type pos;
62                 // first position after the element in the paragraph
63                 pos_type endpos;
64                 // The dimension of the chunk (only width for strings)
65                 Dimension dim;
66
67                 // Non-zero if element is an inset
68                 Inset const * inset;
69
70                 // Non-empty if element is a string or separator
71                 docstring str;
72                 // is it possible to add contents to this element?
73                 bool final;
74                 //
75                 Font font;
76                 //
77                 Change change;
78         };
79
80
81         ///
82         Row();
83         ///
84         bool changed() const { return changed_; }
85         ///
86         void setChanged(bool c) { changed_ = c; }
87         ///
88         void setCrc(size_type crc) const;
89         /// Set the selection begin and end.
90         /**
91           * This is const because we update the selection status only at draw()
92           * time.
93           */
94         void setSelection(pos_type sel_beg, pos_type sel_end) const;
95         ///
96         bool selection() const;
97         /// Set the selection begin and end and whether the left and/or right
98         /// margins are selected.
99         void setSelectionAndMargins(DocIterator const & beg,
100                 DocIterator const & end) const;
101
102         ///
103         void pos(pos_type p);
104         ///
105         pos_type pos() const { return pos_; }
106         ///
107         void endpos(pos_type p);
108         ///
109         pos_type endpos() const { return end_; }
110         ///
111         Dimension const & dimension() const { return dim_; }
112         ///
113         Dimension & dimension() { return dim_; }
114         ///
115         int height() const { return dim_.height(); }
116         ///
117         int width() const { return dim_.wid; }
118         ///
119         int ascent() const { return dim_.asc; }
120         ///
121         int descent() const { return dim_.des; }
122
123         ///
124         void add(pos_type pos, Inset const * ins, Dimension const & dim,
125                  Font const & f, Change const & ch);
126         ///
127         void add(pos_type pos, char_type const c,
128                  Font const & f, Change const & ch);
129         ///
130         void add(pos_type pos, docstring const & s,
131                  Font const & f, Change const & ch);
132         ///
133         void addSeparator(pos_type pos, char_type const c,
134                           Font const & f, Change const & ch);
135         ///
136         void addSpace(pos_type pos, int width, Font const & f, Change const & ch);
137         ///
138         bool empty() const { return elements_.empty(); }
139         ///
140         Element & back() { return elements_.back(); }
141         ///
142         Element const & back() const { return elements_.back(); }
143         /// remove last element
144         void pop_back();
145         /// remove all row elements
146         void clear() { elements_.clear(); }
147         /**
148          * remove all elements after last separator and update endpos
149          * if necessary.
150          * \param keep is the minimum amount of text to keep.
151          */
152         void separate_back(pos_type keep);
153
154         /**
155          * If last element of the row is a string, compute its width
156          * and mark it final.
157          */
158         void finalizeLast();
159
160         /**
161          * Find sequences of RtL elements and reverse them.
162          * This should be called once the row is completely built.
163          */
164         void reverseRtL();
165
166         friend std::ostream & operator<<(std::ostream & os, Row const & row);
167
168         /// width of a separator (i.e. space)
169         double separator;
170         /// width of hfills in the label
171         double label_hfill;
172         /// the x position of the row
173         double x;
174         ///
175         mutable pos_type sel_beg;
176         ///
177         mutable pos_type sel_end;
178         ///
179         mutable bool begin_margin_sel;
180         ///
181         mutable bool end_margin_sel;
182
183 private:
184         /// Decides whether the margin is selected.
185         /**
186           * \param margin_begin
187           * \param beg
188           * \param end
189           */
190         bool isMarginSelected(bool left_margin, DocIterator const & beg,
191                 DocIterator const & end) const;
192
193         /**
194          * Returns true if a char or string with font \c f and change
195          * type \c ch can be added to the current last element of the
196          * row.
197          */
198         bool sameString(Font const & f, Change const & ch) const;
199
200         ///
201         typedef std::vector<Element> Elements;
202         ///
203         Elements elements_;
204
205         /// has the Row appearance changed since last drawing?
206         mutable bool changed_;
207         /// CRC of row contents.
208         mutable size_type crc_;
209         /// first pos covered by this row
210         pos_type pos_;
211         /// one behind last pos covered by this row
212         pos_type end_;
213         /// Row dimension.
214         Dimension dim_;
215 };
216
217
218 } // namespace lyx
219
220 #endif