4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
9 * \author Abdelrazak Younes
11 * Full author contact details are available in file CREDITS.
14 #ifndef TEXT_METRICS_H
15 #define TEXT_METRICS_H
17 #include "ParagraphMetrics.h"
19 #include "support/types.h"
21 #include <boost/utility.hpp>
22 #include <boost/tuple/tuple.hpp>
34 /// A map from a Text to the map of paragraphs metrics
38 /// Default constructor (only here for STL containers).
39 TextMetrics(): text_(0) {}
40 /// The only useful constructor.
41 TextMetrics(BufferView *, Text *);
43 ParagraphMetrics const & parMetrics(pit_type) const;
45 Dimension const & dimension() const;
46 Dimension const & dimension();
48 /// compute text metrics.
49 bool metrics(MetricsInfo & mi, Dimension & dim);
51 /// Rebreaks the given paragraph.
52 /// \retval true if a full screen redraw is needed.
53 /// \retval false if a single paragraph redraw is enough.
54 bool redoParagraph(pit_type const pit);
55 /// Clear cache of paragraph metrics
56 void clear() { par_metrics_.clear(); }
59 int ascent() const { return dim_.asc; }
61 int descent() const { return dim_.des; }
62 /// current text width.
63 int width() const { return dim_.wid; }
64 /// current text heigth.
65 int height() const { return dim_.height(); }
68 int maxWidth() const { return max_width_; }
70 int singleWidth(pit_type const pit, pos_type pos) const;
73 int rightMargin(ParagraphMetrics const & pm) const;
74 int rightMargin(pit_type const pit) const;
76 /** this calculates the specified parameters. needed when setting
77 * the cursor and when creating a visible row */
78 void computeRowMetrics(pit_type pit, Row & row) const;
81 void draw(PainterInfo & pi, int x, int y) const;
82 /// draw textselection
83 void drawSelection(PainterInfo & pi, int x, int y) const;
85 void drawParagraph(PainterInfo & pi, pit_type pit, int x, int y) const;
89 ParagraphMetrics & parMetrics(pit_type, bool redo_paragraph);
91 /// the minimum space a manual label needs on the screen in pixels
92 int labelFill(pit_type const pit, Row const & row) const;
95 int labelEnd(pit_type const pit) const;
97 /// sets row.end to the pos value *after* which a row should break.
98 /// for example, the pos after which isNewLine(pos) == true
99 pit_type rowBreakPoint(
105 /// sets row.width to the minimum space a row needs on the screen in pixel
109 pos_type const first,
113 /// Calculate and set the height of the row
114 boost::tuple<int, int> rowHeight(
116 pos_type const first,
120 /// draw selection for a single row
121 void drawRowSelection(PainterInfo & pi, int x, Row const & row,
122 DocIterator const & beg, DocIterator const & end,
123 bool drawOnBegMargin, bool drawOnEndMargin) const;
127 /// returns the column near the specified x-coordinate of the row.
128 /// x is an absolute screen coord, it is set to the real beginning
130 pos_type getColumnNearX(pit_type pit, Row const & row, int & x,
131 bool & boundary) const;
133 /// returns pos in given par at given x coord.
134 pos_type x2pos(pit_type pit, int row, int x) const;
136 // FIXME: is there a need for this?
137 //int pos2x(pit_type pit, pos_type pos) const;
139 /** returns row near the specified
140 * y-coordinate in given paragraph (relative to the screen).
142 Row const & getRowNearY(int y,
145 /// returns the paragraph number closest to screen y-coordinate.
146 /// This method uses the BufferView CoordCache to locate the
147 /// paragraph. The y-coodinate is allowed to be off-screen and
148 /// the CoordCache will be automatically updated if needed. This is
149 /// the reason why we need a non const BufferView.
150 pit_type getPitNearY(int y);
152 /// sets cursor recursively descending into nested editable insets
154 \return the inset pointer if x,y is covering that inset
155 \param x,y are absolute screen coordinates.
156 \retval inset is non-null if the cursor is positionned inside
158 /// FIXME: cleanup to use BufferView::getCoveringInset() and
159 /// setCursorFromCoordinates() instead of checkInsetHit().
160 Inset * editXY(Cursor & cur, int x, int y);
162 /// sets cursor only within this Text.
163 /// x,y are screen coordinates
164 void setCursorFromCoordinates(Cursor & cur, int x, int y);
167 int cursorX(CursorSlice const & cursor,
168 bool boundary) const;
170 int cursorY(CursorSlice const & cursor,
171 bool boundary) const;
174 void cursorPrevious(Cursor & cur);
176 void cursorNext(Cursor & cur);
178 bool cursorHome(Cursor & cur);
180 bool cursorEnd(Cursor & cur);
182 void deleteLineForward(Cursor & cur);
184 /// is this row the last in the text?
185 bool isLastRow(pit_type pit, Row const & row) const;
186 /// is this row the first in the text?
187 bool isFirstRow(pit_type pit, Row const & row) const;
189 /// Returns an inset if inset was hit, or 0 if not.
190 /// \warning This method is not recursive! It will return the
191 /// outermost inset within this Text.
192 /// \sa BufferView::getCoveringInset() to get the innermost inset.
193 Inset * checkInsetHit(int x, int y);
196 * Returns the left beginning of the text.
197 * This information cannot be taken from the layout object, because
198 * in LaTeX the beginning of the text fits in some cases
199 * (for example sections) exactly the label-width.
201 int leftMargin(int max_width, pit_type pit, pos_type pos) const;
202 int leftMargin(int max_width, pit_type pit) const;
206 /// The BufferView owner.
209 /// The text contents (the model).
210 /// \todo FIXME: this should be const.
214 /// A map from paragraph index number to paragraph metrics
215 typedef std::map<pit_type, ParagraphMetrics> ParMetricsCache;
217 mutable ParMetricsCache par_metrics_;
221 /// FIXME: transfer this code in CoordCache here.
223 /// A map from paragraph index number to screen point
224 typedef std::map<pit_type, Point> InnerParPosCache;
225 /// A map from a Text to the map of paragraphs to screen points
226 typedef std::map<Text const *, InnerParPosCache> ParPosCache;
227 /// Paragraph grouped by owning text
232 /// return the default height of a row in pixels, considering font zoom
233 int defaultRowHeight();
237 #endif // TEXT_METRICS_H