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