]> git.lyx.org Git - lyx.git/blob - src/MetricsInfo.cpp
prepare Qt 5.6 builds
[lyx.git] / src / MetricsInfo.cpp
1 /**
2  * \file MetricsInfo.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "BufferView.h"
14 #include "ColorSet.h"
15 #include "MetricsInfo.h"
16
17 #include "insets/Inset.h"
18
19 #include "mathed/MathSupport.h"
20
21 #include "frontends/Painter.h"
22
23 #include "support/docstring.h"
24 #include "support/lassert.h"
25
26 using namespace std;
27
28
29 namespace lyx {
30
31 /////////////////////////////////////////////////////////////////////////
32 //
33 // MetricsBase
34 //
35 /////////////////////////////////////////////////////////////////////////
36
37 MetricsBase::MetricsBase()
38         : bv(0), font(), style(LM_ST_TEXT), fontname("mathnormal"),
39           textwidth(0)
40 {}
41
42
43 MetricsBase::MetricsBase(BufferView * b, FontInfo const & f, int w)
44         : bv(b), font(f), style(LM_ST_TEXT), fontname("mathnormal"),
45           textwidth(w)
46 {}
47
48
49 /////////////////////////////////////////////////////////////////////////
50 //
51 // MetricsInfo
52 //
53 /////////////////////////////////////////////////////////////////////////
54
55 MetricsInfo::MetricsInfo(BufferView * bv, FontInfo const & font, int textwidth, 
56         MacroContext const & mc)
57         : base(bv, font, textwidth), macrocontext(mc)
58 {}
59
60
61 /////////////////////////////////////////////////////////////////////////
62 //
63 // PainterInfo
64 //
65 /////////////////////////////////////////////////////////////////////////
66
67 PainterInfo::PainterInfo(BufferView * bv, lyx::frontend::Painter & painter)
68         : pain(painter), ltr_pos(false), change_(), selected(false),
69         do_spellcheck(true), full_repaint(true), background_color(Color_background)
70 {
71         base.bv = bv;
72 }
73
74
75 void PainterInfo::draw(int x, int y, char_type c)
76 {
77         pain.text(x, y, c, base.font);
78 }
79
80
81 void PainterInfo::draw(int x, int y, docstring const & str)
82 {
83         pain.text(x, y, str, base.font);
84 }
85
86
87 ColorCode PainterInfo::backgroundColor(Inset const * inset, bool sel) const
88 {
89         ColorCode const color_bg = inset->backgroundColor(*this);
90
91         if (selected && sel)
92                 // This inset is in a selection
93                 return Color_selection;
94         else {
95                 if (color_bg != Color_none)
96                         // This inset has its own color
97                         return color_bg;
98                 else {
99                         if (background_color == Color_none)
100                                 // This inset has no own color and does not inherit a color
101                                 return Color_background;
102                         else
103                                 // This inset has no own color, but inherits a color
104                                 return background_color;
105                 }
106         }
107 }
108
109
110 Color PainterInfo::textColor(Color const & color) const
111 {
112         if (change_.changed()) 
113                 return change_.color();
114         if (selected)
115                 return Color_selectiontext;
116         return color;
117 }
118
119
120 /////////////////////////////////////////////////////////////////////////
121 //
122 // ScriptChanger
123 //
124 /////////////////////////////////////////////////////////////////////////
125
126 Styles smallerScriptStyle(Styles st)
127 {
128         switch (st) {
129                 case LM_ST_DISPLAY:
130                 case LM_ST_TEXT:
131                         return LM_ST_SCRIPT;
132                 case LM_ST_SCRIPT:
133                 case LM_ST_SCRIPTSCRIPT:
134                 default: // shut up compiler
135                         return LM_ST_SCRIPTSCRIPT;
136         }
137 }
138
139
140 ScriptChanger::ScriptChanger(MetricsBase & mb)
141         : StyleChanger(mb, smallerScriptStyle(mb.style))
142 {}
143
144
145 /////////////////////////////////////////////////////////////////////////
146 //
147 // FracChanger
148 //
149 /////////////////////////////////////////////////////////////////////////
150
151 Styles smallerFracStyle(Styles st)
152 {
153         switch (st) {
154                 case LM_ST_DISPLAY:
155                         return LM_ST_TEXT;
156                 case LM_ST_TEXT:
157                         return LM_ST_SCRIPT;
158                 case LM_ST_SCRIPT:
159                 case LM_ST_SCRIPTSCRIPT:
160                 default: // shut up compiler
161                         return LM_ST_SCRIPTSCRIPT;
162         }
163 }
164
165
166 FracChanger::FracChanger(MetricsBase & mb)
167         : StyleChanger(mb, smallerFracStyle(mb.style))
168 {}
169
170
171 /////////////////////////////////////////////////////////////////////////
172 //
173 // ArrayChanger
174 //
175 /////////////////////////////////////////////////////////////////////////
176
177 ArrayChanger::ArrayChanger(MetricsBase & mb)
178         : StyleChanger(mb, mb.style == LM_ST_DISPLAY ? LM_ST_TEXT : mb.style)
179 {}
180
181
182 /////////////////////////////////////////////////////////////////////////
183 //
184 // ShapeChanger
185 //
186 /////////////////////////////////////////////////////////////////////////
187
188 ShapeChanger::ShapeChanger(FontInfo & font, FontShape shape)
189         : Changer<FontInfo, FontShape>(font, font.shape())
190 {
191         orig_.setShape(shape);
192 }
193
194
195 ShapeChanger::~ShapeChanger()
196 {
197         orig_.setShape(save_);
198 }
199
200
201 /////////////////////////////////////////////////////////////////////////
202 //
203 // StyleChanger
204 //
205 /////////////////////////////////////////////////////////////////////////
206
207 StyleChanger::StyleChanger(MetricsBase & mb, Styles style)
208         : Changer<MetricsBase>(mb)
209 {
210         static const int diff[4][4] =
211                 { { 0, 0, -3, -5 },
212                   { 0, 0, -3, -5 },
213                   { 3, 3,  0, -2 },
214                   { 5, 5,  2,  0 } };
215         int t = diff[mb.style][style];
216         if (t > 0)
217                 while (t--)
218                         mb.font.incSize();
219         else
220                 while (t++)
221                         mb.font.decSize();
222         mb.style = style;
223 }
224
225
226 StyleChanger::~StyleChanger()
227 {
228         orig_ = save_;
229 }
230
231
232 /////////////////////////////////////////////////////////////////////////
233 //
234 // FontSetChanger
235 //
236 /////////////////////////////////////////////////////////////////////////
237
238 FontSetChanger::FontSetChanger(MetricsBase & mb, char const * name,
239                                 bool really_change_font)
240         : Changer<MetricsBase>(mb), change_(really_change_font)
241 {
242         if (change_) {
243                 FontSize oldsize = save_.font.size();
244                 ColorCode oldcolor = save_.font.color();
245                 docstring const oldname = from_ascii(save_.fontname);
246                 mb.fontname = name;
247                 mb.font = sane_font;
248                 augmentFont(mb.font, from_ascii(name));
249                 mb.font.setSize(oldsize);
250                 if (string(name) != "lyxtex"
251                     && ((isTextFont(oldname) && oldcolor != Color_foreground)
252                         || (isMathFont(oldname) && oldcolor != Color_math)))
253                         mb.font.setColor(oldcolor);
254         }
255 }
256
257
258 FontSetChanger::FontSetChanger(MetricsBase & mb, docstring const & name,
259                                 bool really_change_font)
260         : Changer<MetricsBase>(mb), change_(really_change_font)
261 {
262         if (change_) {
263                 FontSize oldsize = save_.font.size();
264                 ColorCode oldcolor = save_.font.color();
265                 docstring const oldname = from_ascii(save_.fontname);
266                 mb.fontname = to_utf8(name);
267                 mb.font = sane_font;
268                 augmentFont(mb.font, name);
269                 mb.font.setSize(oldsize);
270                 if (name != "lyxtex"
271                     && ((isTextFont(oldname) && oldcolor != Color_foreground)
272                         || (isMathFont(oldname) && oldcolor != Color_math)))
273                         mb.font.setColor(oldcolor);
274         }
275 }
276
277
278 FontSetChanger::~FontSetChanger()
279 {
280         if (change_)
281                 orig_ = save_;
282 }
283
284
285 /////////////////////////////////////////////////////////////////////////
286 //
287 // WidthChanger
288 //
289 /////////////////////////////////////////////////////////////////////////
290
291 WidthChanger::WidthChanger(MetricsBase & mb, int w)
292         : Changer<MetricsBase>(mb)
293 {
294         mb.textwidth = w;
295 }
296
297
298 WidthChanger::~WidthChanger()
299 {
300         orig_ = save_;
301 }
302
303
304 /////////////////////////////////////////////////////////////////////////
305 //
306 // ColorChanger
307 //
308 /////////////////////////////////////////////////////////////////////////
309
310 ColorChanger::ColorChanger(FontInfo & font, ColorCode color,
311                            bool really_change_color)
312         : Changer<FontInfo, ColorCode>(font, font.color()), change_(really_change_color)
313 {
314         if (change_) {
315                 font.setColor(color);
316         }
317 }
318
319
320 ColorChanger::~ColorChanger()
321 {
322         if (change_)
323                 orig_.setColor(save_);
324 }
325
326
327 } // namespace lyx