2 * \file MetricsInfo.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "BufferView.h"
15 #include "MetricsInfo.h"
17 #include "insets/Inset.h"
19 #include "mathed/MathSupport.h"
21 #include "frontends/Painter.h"
23 #include "support/docstring.h"
24 #include "support/lassert.h"
31 /////////////////////////////////////////////////////////////////////////
35 /////////////////////////////////////////////////////////////////////////
37 MetricsBase::MetricsBase()
38 : bv(0), font(), style(LM_ST_TEXT), fontname("mathnormal"),
43 MetricsBase::MetricsBase(BufferView * b, FontInfo const & f, int w)
44 : bv(b), font(f), style(LM_ST_TEXT), fontname("mathnormal"),
49 /////////////////////////////////////////////////////////////////////////
53 /////////////////////////////////////////////////////////////////////////
55 MetricsInfo::MetricsInfo(BufferView * bv, FontInfo const & font, int textwidth,
56 MacroContext const & mc)
57 : base(bv, font, textwidth), macrocontext(mc)
61 /////////////////////////////////////////////////////////////////////////
65 /////////////////////////////////////////////////////////////////////////
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)
75 void PainterInfo::draw(int x, int y, char_type c)
77 pain.text(x, y, c, base.font);
81 void PainterInfo::draw(int x, int y, docstring const & str)
83 pain.text(x, y, str, base.font);
87 ColorCode PainterInfo::backgroundColor(Inset const * inset, bool sel) const
89 ColorCode const color_bg = inset->backgroundColor(*this);
92 // This inset is in a selection
93 return Color_selection;
95 if (color_bg != Color_none)
96 // This inset has its own color
99 if (background_color == Color_none)
100 // This inset has no own color and does not inherit a color
101 return Color_background;
103 // This inset has no own color, but inherits a color
104 return background_color;
110 Color PainterInfo::textColor(Color const & color) const
112 if (change_.changed())
113 return change_.color();
115 return Color_selectiontext;
120 /////////////////////////////////////////////////////////////////////////
124 /////////////////////////////////////////////////////////////////////////
126 Styles smallerScriptStyle(Styles st)
133 case LM_ST_SCRIPTSCRIPT:
134 default: // shut up compiler
135 return LM_ST_SCRIPTSCRIPT;
140 ScriptChanger::ScriptChanger(MetricsBase & mb)
141 : StyleChanger(mb, smallerScriptStyle(mb.style))
145 /////////////////////////////////////////////////////////////////////////
149 /////////////////////////////////////////////////////////////////////////
151 Styles smallerFracStyle(Styles st)
159 case LM_ST_SCRIPTSCRIPT:
160 default: // shut up compiler
161 return LM_ST_SCRIPTSCRIPT;
166 FracChanger::FracChanger(MetricsBase & mb)
167 : StyleChanger(mb, smallerFracStyle(mb.style))
171 /////////////////////////////////////////////////////////////////////////
175 /////////////////////////////////////////////////////////////////////////
177 ArrayChanger::ArrayChanger(MetricsBase & mb)
178 : StyleChanger(mb, mb.style == LM_ST_DISPLAY ? LM_ST_TEXT : mb.style)
182 /////////////////////////////////////////////////////////////////////////
186 /////////////////////////////////////////////////////////////////////////
188 ShapeChanger::ShapeChanger(FontInfo & font, FontShape shape)
189 : Changer<FontInfo, FontShape>(font, font.shape())
191 orig_.setShape(shape);
195 ShapeChanger::~ShapeChanger()
197 orig_.setShape(save_);
201 /////////////////////////////////////////////////////////////////////////
205 /////////////////////////////////////////////////////////////////////////
207 StyleChanger::StyleChanger(MetricsBase & mb, Styles style)
208 : Changer<MetricsBase>(mb)
210 static const int diff[4][4] =
215 int t = diff[mb.style][style];
226 StyleChanger::~StyleChanger()
232 /////////////////////////////////////////////////////////////////////////
236 /////////////////////////////////////////////////////////////////////////
238 FontSetChanger::FontSetChanger(MetricsBase & mb, char const * name,
239 bool really_change_font)
240 : Changer<MetricsBase>(mb), change_(really_change_font)
243 FontSize oldsize = save_.font.size();
244 ColorCode oldcolor = save_.font.color();
245 docstring const oldname = from_ascii(save_.fontname);
247 if (isMathFont(from_ascii(name)) || isMathFont(oldname))
249 augmentFont(mb.font, from_ascii(name));
250 mb.font.setSize(oldsize);
251 if (string(name) != "lyxtex"
252 && ((isTextFont(oldname) && oldcolor != Color_foreground)
253 || (isMathFont(oldname) && oldcolor != Color_math)))
254 mb.font.setColor(oldcolor);
259 FontSetChanger::FontSetChanger(MetricsBase & mb, docstring const & name,
260 bool really_change_font)
261 : Changer<MetricsBase>(mb), change_(really_change_font)
264 FontSize oldsize = save_.font.size();
265 ColorCode oldcolor = save_.font.color();
266 docstring const oldname = from_ascii(save_.fontname);
267 mb.fontname = to_utf8(name);
268 if (isMathFont(name) || isMathFont(oldname))
270 augmentFont(mb.font, name);
271 mb.font.setSize(oldsize);
273 && ((isTextFont(oldname) && oldcolor != Color_foreground)
274 || (isMathFont(oldname) && oldcolor != Color_math)))
275 mb.font.setColor(oldcolor);
280 FontSetChanger::~FontSetChanger()
287 /////////////////////////////////////////////////////////////////////////
291 /////////////////////////////////////////////////////////////////////////
293 WidthChanger::WidthChanger(MetricsBase & mb, int w)
294 : Changer<MetricsBase>(mb)
300 WidthChanger::~WidthChanger()
306 /////////////////////////////////////////////////////////////////////////
310 /////////////////////////////////////////////////////////////////////////
312 ColorChanger::ColorChanger(FontInfo & font, ColorCode color,
313 bool really_change_color)
314 : Changer<FontInfo, ColorCode>(font, font.color()), change_(really_change_color)
317 font.setColor(color);
322 ColorChanger::~ColorChanger()
325 orig_.setColor(save_);