]> git.lyx.org Git - lyx.git/blob - src/MetricsInfo.h
05acbbbb045b9f73cbd1608b66686e23498e9a7c
[lyx.git] / src / MetricsInfo.h
1 // -*- C++ -*-
2 /**
3  * \file MetricsInfo.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author André Pönitz
8  * \author Stefan Schimanski
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #ifndef METRICSINFO_H
14 #define METRICSINFO_H
15
16 #include "Changes.h"
17 #include "ColorCode.h"
18 #include "FontInfo.h"
19
20 #include "support/strfwd.h"
21
22 #include <string>
23
24 class BufferView;
25
26 namespace lyx {
27
28 namespace frontend { class Painter; }
29 class Inset;
30 class MacroContext;
31
32
33 /// Standard Sizes (mode styles)
34 enum Styles {
35         ///
36         LM_ST_DISPLAY = 0,
37         ///
38         LM_ST_TEXT,
39         ///
40         LM_ST_SCRIPT,
41         ///
42         LM_ST_SCRIPTSCRIPT
43 };
44
45
46 //
47 // This is the part common to MetricsInfo and PainterInfo
48 //
49 class MetricsBase {
50 public:
51         ///
52         MetricsBase();
53         ///
54         MetricsBase(BufferView * bv, FontInfo const & font, int textwidth);
55
56         /// the current view
57         BufferView * bv;
58         /// current font
59         FontInfo font;
60         /// current math style (display/text/script/..)
61         Styles style;
62         /// name of current font - mathed specific
63         std::string fontname;
64         /// This is the width available in pixels
65         int textwidth;
66 };
67
68
69 //
70 // This contains a MetricsBase and information that's only relevant during
71 // the first phase of the two-phase draw
72 //
73 class MetricsInfo {
74 public:
75         ///
76         MetricsInfo();
77         ///
78         MetricsInfo(BufferView * bv, FontInfo const & font, int textwidth, MacroContext const & mc);
79
80         ///
81         MetricsBase base;
82         /// The context to resolve macros
83         MacroContext const & macrocontext;
84 };
85
86
87 //
88 // This contains a MetricsBase and information that's only relevant during
89 // the second phase of the two-phase draw
90 //
91 class PainterInfo {
92 public:
93         ///
94         PainterInfo(BufferView * bv, frontend::Painter & pain);
95         ///
96         void draw(int x, int y, char_type c);
97         ///
98         void draw(int x, int y, docstring const & str);
99         /// Determines the background color for the specified inset based on the
100         /// selection state, the background color inherited from the parent inset 
101         /// and the inset's own background color.
102         /// \param sel whether to take the selection state into account
103         ColorCode backgroundColor(Inset const * inset, bool sel = true) const;
104
105         /// Determines the text color based on the intended color, the
106         /// change tracking state and the selection state. 
107         /// \param color what the color should be by default
108         Color textColor(Color const & color) const;
109
110         ///
111         MetricsBase base;
112         ///
113         frontend::Painter & pain;
114         /// Whether the text at this point is right-to-left (for InsetNewline)
115         bool ltr_pos;
116         /// The change the parent is part of (change tracking)
117         Change change_;
118         /// Whether the parent is selected as a whole
119         bool selected;
120         ///
121         bool full_repaint;
122         /// Current background color
123         ColorCode background_color;
124 };
125
126 class TextMetricsInfo {};
127
128
129 /// Generic base for temporarily changing things.
130 /// The original state gets restored when the Changer is destructed.
131 template <class Struct, class Temp = Struct>
132 class Changer {
133 public:
134         ///
135         Changer(Struct & orig) : orig_(orig) {}
136 protected:
137         ///
138         Struct & orig_;
139         ///
140         Temp save_;
141 };
142
143
144
145 // temporarily change some aspect of a font
146 class FontChanger : public Changer<FontInfo> {
147 public:
148         ///
149         FontChanger(FontInfo & orig, docstring const & font);
150         FontChanger(MetricsBase & mb, char const * const font);
151         ///
152         ~FontChanger();
153 };
154
155
156 // temporarily change a full font
157 class FontSetChanger : public Changer<MetricsBase> {
158 public:
159         ///
160         FontSetChanger(MetricsBase & mb, docstring const & font,
161                         bool really_change_font = true);
162         FontSetChanger(MetricsBase & mb, char const * const font,
163                         bool really_change_font = true);
164         ///
165         ~FontSetChanger();
166 private:
167         ///
168         bool change_;
169 };
170
171
172 // temporarily change the style
173 class StyleChanger : public Changer<MetricsBase> {
174 public:
175         ///
176         StyleChanger(MetricsBase & mb, Styles style);
177         ///
178         ~StyleChanger();
179 };
180
181
182 // temporarily change the style to script style
183 class ScriptChanger : public StyleChanger {
184 public:
185         ///
186         ScriptChanger(MetricsBase & mb);
187 };
188
189
190 // temporarily change the style suitable for use in fractions
191 class FracChanger : public StyleChanger {
192 public:
193         ///
194         FracChanger(MetricsBase & mb);
195 };
196
197
198 // temporarily change the style suitable for use in tabulars and arrays
199 class ArrayChanger : public StyleChanger {
200 public:
201         ///
202         ArrayChanger(MetricsBase & mb);
203 };
204
205
206
207 // temporarily change the shape of a font
208 class ShapeChanger : public Changer<FontInfo, FontShape> {
209 public:
210         ///
211         ShapeChanger(FontInfo & font, FontShape shape);
212         ///
213         ~ShapeChanger();
214 };
215
216
217 // temporarily change the available text width
218 class WidthChanger : public Changer<MetricsBase>
219 {
220 public:
221         ///
222         WidthChanger(MetricsBase & mb, int width);
223         ///
224         ~WidthChanger();
225 };
226
227
228 // temporarily change the used color
229 class ColorChanger : public Changer<FontInfo, ColorCode> {
230 public:
231         ///
232         ColorChanger(FontInfo & font, ColorCode color,
233                      bool really_change_color = true);
234         ///
235         ~ColorChanger();
236 private:
237         ///
238         bool change_;
239 };
240
241 } // namespace lyx
242
243 #endif