]> git.lyx.org Git - lyx.git/blob - src/TextMetrics.h
This addresses a bug in the module code so far committed. The problem is that changin...
[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 private:
140
141         /// The BufferView owner.
142         BufferView * bv_;
143
144         /// The text contents (the model).
145         /// \todo FIXME: this should be const.
146         Text * text_;
147
148         bool main_text_;
149         /// A map from paragraph index number to paragraph metrics
150         typedef std::map<pit_type, ParagraphMetrics> ParMetricsCache;
151         ///
152         mutable ParMetricsCache par_metrics_;
153         Dimension dim_;
154         int max_width_;
155
156         /// FIXME: transfer this code in CoordCache here.
157         /*
158         /// A map from paragraph index number to screen point
159         typedef std::map<pit_type, Point> InnerParPosCache;
160         /// A map from a Text to the map of paragraphs to screen points
161         typedef std::map<Text const *, InnerParPosCache> ParPosCache;
162         /// Paragraph grouped by owning text
163         ParPosCache pars_;
164         */
165 };
166
167 /// return the default height of a row in pixels, considering font zoom
168 int defaultRowHeight();
169
170 } // namespace lyx
171
172 #endif // TEXT_METRICS_H