]> git.lyx.org Git - lyx.git/blob - src/TextMetrics.h
Transfer current_font and real_current_font from Text to Cursor.
[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 "ParagraphMetrics.h"
18
19 #include "support/types.h"
20
21 #include <boost/utility.hpp>
22 #include <boost/tuple/tuple.hpp>
23
24 #include <map>
25
26
27 namespace lyx {
28
29 class BufferView;
30 class DocIterator;
31 class MetricsInfo;
32 class Text;
33
34 /// A map from a Text to the map of paragraphs metrics
35 class TextMetrics
36 {
37 public:
38         /// Default constructor (only here for STL containers).
39         TextMetrics(): text_(0) {}
40         /// The only useful constructor.
41         TextMetrics(BufferView *, Text *);
42         ///
43         ParagraphMetrics const & parMetrics(pit_type) const;
44         ///
45         Dimension const & dimension() const;
46         Dimension const & dimension();
47
48         /// compute text metrics.
49         bool metrics(MetricsInfo & mi, Dimension & dim);
50
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(); }
57
58         ///
59         int ascent() const { return dim_.asc; }
60         ///
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(); }
66
67         ///
68         int maxWidth() const { return max_width_; }
69         ///
70         int singleWidth(pit_type const pit,     pos_type pos) const;
71
72         ///
73         int rightMargin(ParagraphMetrics const & pm) const;
74         int rightMargin(pit_type const pit) const;
75
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;
79
80         ///
81         void draw(PainterInfo & pi, int x, int y) const;
82         /// draw textselection
83         void drawSelection(PainterInfo & pi, int x, int y) const;
84         
85         void drawParagraph(PainterInfo & pi, pit_type pit, int x, int y) const;
86
87 private:
88         ///
89         ParagraphMetrics & parMetrics(pit_type, bool redo_paragraph);
90
91         /// the minimum space a manual label needs on the screen in pixels
92         int labelFill(pit_type const pit, Row const & row) const;
93
94         /// FIXME??
95         int labelEnd(pit_type const pit) const;
96
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(
100                 int width,
101                 pit_type const pit,
102                 pit_type first
103                 ) const;
104
105         /// sets row.width to the minimum space a row needs on the screen in pixel
106         int rowWidth(
107                 int right_margin,
108                 pit_type const pit,
109                 pos_type const first,
110                 pos_type const end
111                 ) const;
112
113         /// Calculate and set the height of the row
114         boost::tuple<int, int> rowHeight(
115                 pit_type const pit,
116                 pos_type const first,
117                 pos_type const end
118                 ) const;
119
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;
124
125 // Temporary public:
126 public:
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
129         /// of this column.
130         pos_type getColumnNearX(pit_type pit, Row const & row, int & x,
131                 bool & boundary) const;
132
133         /// returns pos in given par at given x coord.
134         pos_type x2pos(pit_type pit, int row, int x) const;
135
136         // FIXME: is there a need for this?
137         //int pos2x(pit_type pit, pos_type pos) const;
138
139         /** returns row near the specified
140           * y-coordinate in given paragraph (relative to the screen).
141           */
142         Row const & getRowNearY(int y,
143                 pit_type pit) const;
144
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);
151
152         /// sets cursor recursively descending into nested editable insets
153         /**
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
157         */
158         /// FIXME: cleanup to use BufferView::getCoveringInset() and
159         /// setCursorFromCoordinates() instead of checkInsetHit().
160         Inset * editXY(Cursor & cur, int x, int y);
161
162         /// sets cursor only within this Text.
163         /// x,y are screen coordinates
164         void setCursorFromCoordinates(Cursor & cur, int x, int y);
165
166         ///
167         int cursorX(CursorSlice const & cursor,
168                 bool boundary) const;
169         ///
170         int cursorY(CursorSlice const & cursor,
171                 bool boundary) const;
172
173         ///
174         void cursorPrevious(Cursor & cur);
175         ///
176         void cursorNext(Cursor & cur);
177         ///
178         bool cursorHome(Cursor & cur);
179         ///
180         bool cursorEnd(Cursor & cur);
181         ///
182         void deleteLineForward(Cursor & cur);
183
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;
188
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);
194
195         /**
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.
200          */
201         int leftMargin(int max_width, pit_type pit, pos_type pos) const;
202         int leftMargin(int max_width, pit_type pit) const;
203
204 private:
205
206         /// The BufferView owner.
207         BufferView * bv_;
208
209         /// The text contents (the model).
210         /// \todo FIXME: this should be const.
211         Text * text_;
212
213         bool main_text_;
214         /// A map from paragraph index number to paragraph metrics
215         typedef std::map<pit_type, ParagraphMetrics> ParMetricsCache;
216         ///
217         mutable ParMetricsCache par_metrics_;
218         Dimension dim_;
219         int max_width_;
220
221         /// FIXME: transfer this code in CoordCache here.
222         /*
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
228         ParPosCache pars_;
229         */
230 };
231
232 /// return the default height of a row in pixels, considering font zoom
233 int defaultRowHeight();
234
235 } // namespace lyx
236
237 #endif // TEXT_METRICS_H