]> git.lyx.org Git - lyx.git/blob - src/TextMetrics.h
Further cleanup of InsetFlex, InsetCollapsable and InsetLayout:
[lyx.git] / src / TextMetrics.h
1 // -*- C++ -*-
2 /**
3  * \file TextMetrics.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  * \author John Levon
9  * \author Abdelrazak Younes
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #ifndef TEXT_METRICS_H
15 #define TEXT_METRICS_H
16
17 #include "Font.h"
18 #include "ParagraphMetrics.h"
19
20 #include "support/types.h"
21
22 namespace lyx {
23
24 class BufferView;
25 class Cursor;
26 class CursorSlice;
27 class DocIterator;
28 class MetricsInfo;
29 class Text;
30
31 /// A map from a Text to the map of paragraphs metrics
32 class TextMetrics
33 {
34 public:
35         /// Default constructor (only here for STL containers).
36         TextMetrics() : text_(0) {}
37         /// The only useful constructor.
38         TextMetrics(BufferView *, Text *);
39         
40         ///
41         bool has(pit_type pit) const;
42         ///
43         ParagraphMetrics const & parMetrics(pit_type) const;
44         ///
45         std::pair<pit_type, ParagraphMetrics const *> first() const;
46         ///
47         std::pair<pit_type, ParagraphMetrics const *> last() const;
48
49         ///
50         int parPosition(pit_type pit) const;
51
52         ///
53         Dimension const & dimension() const { return dim_; }
54         ///
55         Point const & origin() const { return origin_; }
56
57
58         /// compute text metrics.
59         bool metrics(MetricsInfo & mi, Dimension & dim, int min_width = 0);
60
61         ///
62         void newParMetricsDown();
63         ///
64         void newParMetricsUp();
65
66         /// Gets the fully instantiated font at a given position in a paragraph
67         /// Basically the same routine as Paragraph::getFont() in Paragraph.cpp.
68         /// The difference is that this one is used for displaying, and thus we
69         /// are allowed to make cosmetic improvements. For instance make footnotes
70         /// smaller. (Asger)
71         Font getDisplayFont(pit_type pit,
72                 pos_type pos) const;
73
74         /// There are currently two font mechanisms in LyX:
75         /// 1. The font attributes in a lyxtext, and
76         /// 2. The inset-specific font properties, defined in an inset's
77         /// metrics() and draw() methods and handed down the inset chain through
78         /// the pi/mi parameters, and stored locally in a lyxtext in font_.
79         /// This is where the two are integrated in the final fully realized
80         /// font.
81         void applyOuterFont(Font &) const;
82
83         /// is this position in the paragraph right-to-left?
84         bool isRTL(CursorSlice const & sl, bool boundary) const;
85         /// is between pos-1 and pos an RTL<->LTR boundary?
86         bool isRTLBoundary(pit_type pit,
87           pos_type pos) const;
88         /// would be a RTL<->LTR boundary between pos and the given font?
89         bool isRTLBoundary(pit_type pit,
90           pos_type pos, Font const & font) const;
91
92
93         /// Rebreaks the given paragraph.
94         /// \retval true if a full screen redraw is needed.
95         /// \retval false if a single paragraph redraw is enough.
96         bool redoParagraph(pit_type const pit);
97         /// Clear cache of paragraph metrics
98         void clear() { par_metrics_.clear(); }
99
100         ///
101         int ascent() const { return dim_.asc; }
102         ///
103         int descent() const { return dim_.des; }
104         /// current text width.
105         int width() const { return dim_.wid; }
106         /// current text heigth.
107         int height() const { return dim_.height(); }
108
109         ///
110         int maxWidth() const { return max_width_; }
111         ///
112         int singleWidth(pit_type const pit,     pos_type pos) const;
113
114         ///
115         int rightMargin(ParagraphMetrics const & pm) const;
116         int rightMargin(pit_type const pit) const;
117
118         /** this calculates the specified parameters. needed when setting
119          * the cursor and when creating a visible row */
120         void computeRowMetrics(pit_type pit, Row & row, int width) const;
121
122         ///
123         void draw(PainterInfo & pi, int x, int y) const;
124         
125         void drawParagraph(PainterInfo & pi, pit_type pit, int x, int y) const;
126
127 private:
128         ///
129         ParagraphMetrics & parMetrics(pit_type, bool redo_paragraph);
130
131         /// draw textselection.
132         /// FIXME: simplify to just to single row painting.
133         void drawSelection(PainterInfo & pi,
134                 DocIterator const & beg, ///< selection begin.
135                 DocIterator const & end, ///< selection end.
136                 int x) const;
137
138         /// the minimum space a manual label needs on the screen in pixels
139         int labelFill(pit_type const pit, Row const & row) const;
140
141         /// FIXME??
142         int labelEnd(pit_type const pit) const;
143
144         /// sets row.end to the pos value *after* which a row should break.
145         /// for example, the pos after which isNewLine(pos) == true
146         pit_type rowBreakPoint(
147                 int width,
148                 pit_type const pit,
149                 pit_type first
150                 ) const;
151
152         /// sets row.width to the minimum space a row needs on the screen in pixel
153         int rowWidth(
154                 int right_margin,
155                 pit_type const pit,
156                 pos_type const first,
157                 pos_type const end
158                 ) const;
159
160         /// Calculate and set the height of the row (width member is set to 0)
161         Dimension rowHeight(
162                 pit_type const pit,
163                 pos_type const first,
164                 pos_type const end
165                 ) const;
166
167         /// draw selection for a single row
168         void drawRowSelection(PainterInfo & pi, int x, Row const & row,
169                 DocIterator const & beg, DocIterator const & end, 
170                 bool drawOnBegMargin, bool drawOnEndMargin) const;
171
172 // Temporary public:
173 public:
174         /// returns the column near the specified x-coordinate of the row.
175         /// x is an absolute screen coord, it is set to the real beginning
176         /// of this column.
177         pos_type getColumnNearX(pit_type pit, Row const & row, int & x,
178                 bool & boundary) const;
179
180         /// returns pos in given par at given x coord.
181         pos_type x2pos(pit_type pit, int row, int x) const;
182
183         // FIXME: is there a need for this?
184         //int pos2x(pit_type pit, pos_type pos) const;
185
186         /** returns row near the specified
187           * y-coordinate in given paragraph (relative to the screen).
188           */
189         Row const & getRowNearY(int y,
190                 pit_type pit) const;
191
192         /// returns the paragraph number closest to screen y-coordinate.
193         /// This method uses the BufferView CoordCache to locate the
194         /// paragraph. The y-coodinate is allowed to be off-screen and
195         /// the CoordCache will be automatically updated if needed. This is
196         /// the reason why we need a non const BufferView.
197         pit_type getPitNearY(int y);
198
199         /// sets cursor recursively descending into nested editable insets
200         /**
201         \return the inset pointer if x,y is covering that inset
202         \param x,y are absolute screen coordinates.
203         \retval inset is non-null if the cursor is positionned inside
204         */
205         /// FIXME: cleanup to use BufferView::getCoveringInset() and
206         /// setCursorFromCoordinates() instead of checkInsetHit().
207         Inset * editXY(Cursor & cur, int x, int y);
208
209         /// sets cursor only within this Text.
210         /// x,y are screen coordinates
211         void setCursorFromCoordinates(Cursor & cur, int x, int y);
212
213         ///
214         int cursorX(CursorSlice const & cursor,
215                 bool boundary) const;
216         ///
217         int cursorY(CursorSlice const & cursor,
218                 bool boundary) const;
219
220         ///
221         void cursorPrevious(Cursor & cur);
222         ///
223         void cursorNext(Cursor & cur);
224         ///
225         bool cursorHome(Cursor & cur);
226         ///
227         bool cursorEnd(Cursor & cur);
228         ///
229         void deleteLineForward(Cursor & cur);
230
231         /// is this row the last in the text?
232         bool isLastRow(pit_type pit, Row const & row) const;
233         /// is this row the first in the text?
234         bool isFirstRow(pit_type pit, Row const & row) const;
235
236         /// Returns an inset if inset was hit, or 0 if not.
237         /// \warning This method is not recursive! It will return the
238         /// outermost inset within this Text.
239         /// \sa BufferView::getCoveringInset() to get the innermost inset.
240         Inset * checkInsetHit(int x, int y);
241
242         /**
243          * Returns the left beginning of the text.
244          * This information cannot be taken from the layout object, because
245          * in LaTeX the beginning of the text fits in some cases
246          * (for example sections) exactly the label-width.
247          */
248         int leftMargin(int max_width, pit_type pit, pos_type pos) const;
249         int leftMargin(int max_width, pit_type pit) const;
250
251 private:
252
253         /// The BufferView owner.
254         BufferView * bv_;
255
256         /// The text contents (the model).
257         /// \todo FIXME: this should be const.
258         Text * text_;
259
260         bool main_text_;
261         /// A map from paragraph index number to paragraph metrics
262         typedef std::map<pit_type, ParagraphMetrics> ParMetricsCache;
263         ///
264         mutable ParMetricsCache par_metrics_;
265         Dimension dim_;
266         int max_width_;
267         mutable Point origin_;
268
269 // temporary public:
270 public:
271         /// our 'outermost' font.
272         /// This is handed down from the surrounding
273         /// inset through the pi/mi parameter (pi.base.font)
274         /// It is used in applyOuterFont() and setCharFont() for reasons 
275         /// that are not clear... to hand hand the outermost language and
276         /// also for char style apparently.
277         Font font_;
278 };
279
280 /// return the default height of a row in pixels, considering font zoom
281 int defaultRowHeight();
282
283 } // namespace lyx
284
285 #endif // TEXT_METRICS_H