]> git.lyx.org Git - lyx.git/blob - src/MetricsInfo.cpp
listerrors.lyx : Update a link.
[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         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)
190 {
191         save_ = orig_.shape();
192         orig_.setShape(shape);
193 }
194
195
196 ShapeChanger::~ShapeChanger()
197 {
198         orig_.setShape(save_);
199 }
200
201
202 /////////////////////////////////////////////////////////////////////////
203 //
204 // StyleChanger
205 //
206 /////////////////////////////////////////////////////////////////////////
207
208 StyleChanger::StyleChanger(MetricsBase & mb, Styles style)
209         : Changer<MetricsBase>(mb)
210 {
211         static const int diff[4][4] =
212                 { { 0, 0, -3, -5 },
213                   { 0, 0, -3, -5 },
214                   { 3, 3,  0, -2 },
215                   { 5, 5,  2,  0 } };
216         save_ = mb;
217         int t = diff[mb.style][style];
218         if (t > 0)
219                 while (t--)
220                         mb.font.incSize();
221         else
222                 while (t++)
223                         mb.font.decSize();
224         mb.style = style;
225 }
226
227
228 StyleChanger::~StyleChanger()
229 {
230         orig_ = save_;
231 }
232
233
234 /////////////////////////////////////////////////////////////////////////
235 //
236 // FontSetChanger
237 //
238 /////////////////////////////////////////////////////////////////////////
239
240 FontSetChanger::FontSetChanger(MetricsBase & mb, char const * name,
241                                 bool really_change_font)
242         : Changer<MetricsBase>(mb), change_(really_change_font)
243 {
244         if (change_) {
245                 save_ = mb;
246                 FontSize oldsize = save_.font.size();
247                 ColorCode oldcolor = save_.font.color();
248                 docstring const oldname = from_ascii(save_.fontname);
249                 mb.fontname = name;
250                 mb.font = sane_font;
251                 augmentFont(mb.font, from_ascii(name));
252                 mb.font.setSize(oldsize);
253                 if (string(name) != "lyxtex"
254                     && ((isTextFont(oldname) && oldcolor != Color_foreground)
255                         || (isMathFont(oldname) && oldcolor != Color_math)))
256                         mb.font.setColor(oldcolor);
257         }
258 }
259
260
261 FontSetChanger::FontSetChanger(MetricsBase & mb, docstring const & name,
262                                 bool really_change_font)
263         : Changer<MetricsBase>(mb), change_(really_change_font)
264 {
265         if (change_) {
266                 save_ = mb;
267                 FontSize oldsize = save_.font.size();
268                 ColorCode oldcolor = save_.font.color();
269                 docstring const oldname = from_ascii(save_.fontname);
270                 mb.fontname = to_utf8(name);
271                 mb.font = sane_font;
272                 augmentFont(mb.font, name);
273                 mb.font.setSize(oldsize);
274                 if (name != "lyxtex"
275                     && ((isTextFont(oldname) && oldcolor != Color_foreground)
276                         || (isMathFont(oldname) && oldcolor != Color_math)))
277                         mb.font.setColor(oldcolor);
278         }
279 }
280
281
282 FontSetChanger::~FontSetChanger()
283 {
284         if (change_)
285                 orig_ = save_;
286 }
287
288
289 /////////////////////////////////////////////////////////////////////////
290 //
291 // WidthChanger
292 //
293 /////////////////////////////////////////////////////////////////////////
294
295 WidthChanger::WidthChanger(MetricsBase & mb, int w)
296         : Changer<MetricsBase>(mb)
297 {
298         save_ = mb;
299         mb.textwidth = w;
300 }
301
302
303 WidthChanger::~WidthChanger()
304 {
305         orig_ = save_;
306 }
307
308
309 /////////////////////////////////////////////////////////////////////////
310 //
311 // ColorChanger
312 //
313 /////////////////////////////////////////////////////////////////////////
314
315 ColorChanger::ColorChanger(FontInfo & font, ColorCode color,
316                            bool really_change_color)
317         : Changer<FontInfo, ColorCode>(font), change_(really_change_color)
318 {
319         if (change_) {
320                 save_ = font.color();
321                 font.setColor(color);
322         }
323 }
324
325
326 ColorChanger::~ColorChanger()
327 {
328         if (change_)
329                 orig_.setColor(save_);
330 }
331
332
333 } // namespace lyx