Not so soon, I guess, since that FIXME was from r6305.
[lyx.git] / src / MetricsInfo.cpp
blob788709cd49cccc413d88582dc21c44256dbc63dd
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.
6 * \author André Pönitz
8 * Full author contact details are available in file CREDITS.
9 */
11 #include <config.h>
13 #include "BufferView.h"
14 #include "ColorSet.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"
26 using namespace std;
29 namespace lyx {
31 /////////////////////////////////////////////////////////////////////////
33 // MetricsBase
35 /////////////////////////////////////////////////////////////////////////
37 MetricsBase::MetricsBase()
38 : bv(0), font(), style(LM_ST_TEXT), fontname("mathnormal"),
39 textwidth(0)
43 MetricsBase::MetricsBase(BufferView * b, FontInfo const & f, int w)
44 : bv(b), font(f), style(LM_ST_TEXT), fontname("mathnormal"),
45 textwidth(w)
49 /////////////////////////////////////////////////////////////////////////
51 // MetricsInfo
53 /////////////////////////////////////////////////////////////////////////
55 MetricsInfo::MetricsInfo(BufferView * bv, FontInfo const & font, int textwidth,
56 MacroContext const & mc)
57 : base(bv, font, textwidth), macrocontext(mc)
61 /////////////////////////////////////////////////////////////////////////
63 // PainterInfo
65 /////////////////////////////////////////////////////////////////////////
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)
71 base.bv = bv;
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();
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;
110 /////////////////////////////////////////////////////////////////////////
112 // ScriptChanger
114 /////////////////////////////////////////////////////////////////////////
116 Styles smallerScriptStyle(Styles st)
118 switch (st) {
119 case LM_ST_DISPLAY:
120 case LM_ST_TEXT:
121 return LM_ST_SCRIPT;
122 case LM_ST_SCRIPT:
123 case LM_ST_SCRIPTSCRIPT:
124 default: // shut up compiler
125 return LM_ST_SCRIPTSCRIPT;
130 ScriptChanger::ScriptChanger(MetricsBase & mb)
131 : StyleChanger(mb, smallerScriptStyle(mb.style))
135 /////////////////////////////////////////////////////////////////////////
137 // FracChanger
139 /////////////////////////////////////////////////////////////////////////
141 Styles smallerFracStyle(Styles st)
143 switch (st) {
144 case LM_ST_DISPLAY:
145 return LM_ST_TEXT;
146 case LM_ST_TEXT:
147 return LM_ST_SCRIPT;
148 case LM_ST_SCRIPT:
149 case LM_ST_SCRIPTSCRIPT:
150 default: // shut up compiler
151 return LM_ST_SCRIPTSCRIPT;
156 FracChanger::FracChanger(MetricsBase & mb)
157 : StyleChanger(mb, smallerFracStyle(mb.style))
161 /////////////////////////////////////////////////////////////////////////
163 // ArrayChanger
165 /////////////////////////////////////////////////////////////////////////
167 ArrayChanger::ArrayChanger(MetricsBase & mb)
168 : StyleChanger(mb, mb.style == LM_ST_DISPLAY ? LM_ST_TEXT : mb.style)
172 /////////////////////////////////////////////////////////////////////////
174 // ShapeChanger
176 /////////////////////////////////////////////////////////////////////////
178 ShapeChanger::ShapeChanger(FontInfo & font, FontShape shape)
179 : Changer<FontInfo, FontShape>(font)
181 save_ = orig_.shape();
182 orig_.setShape(shape);
186 ShapeChanger::~ShapeChanger()
188 orig_.setShape(save_);
192 /////////////////////////////////////////////////////////////////////////
194 // StyleChanger
196 /////////////////////////////////////////////////////////////////////////
198 StyleChanger::StyleChanger(MetricsBase & mb, Styles style)
199 : Changer<MetricsBase>(mb)
201 static const int diff[4][4] =
202 { { 0, 0, -3, -5 },
203 { 0, 0, -3, -5 },
204 { 3, 3, 0, -2 },
205 { 5, 5, 2, 0 } };
206 save_ = mb;
207 int t = diff[mb.style][style];
208 if (t > 0)
209 while (t--)
210 mb.font.incSize();
211 else
212 while (t++)
213 mb.font.decSize();
214 mb.style = style;
218 StyleChanger::~StyleChanger()
220 orig_ = save_;
224 /////////////////////////////////////////////////////////////////////////
226 // FontSetChanger
228 /////////////////////////////////////////////////////////////////////////
230 FontSetChanger::FontSetChanger(MetricsBase & mb, char const * name)
231 : Changer<MetricsBase>(mb)
233 save_ = mb;
234 FontSize oldsize = save_.font.size();
235 mb.fontname = name;
236 mb.font = sane_font;
237 augmentFont(mb.font, from_ascii(name));
238 mb.font.setSize(oldsize);
242 FontSetChanger::FontSetChanger(MetricsBase & mb, docstring const & name)
243 : Changer<MetricsBase>(mb)
245 save_ = mb;
246 FontSize oldsize = save_.font.size();
247 mb.fontname = to_utf8(name);
248 mb.font = sane_font;
249 augmentFont(mb.font, name);
250 mb.font.setSize(oldsize);
254 FontSetChanger::~FontSetChanger()
256 orig_ = save_;
260 /////////////////////////////////////////////////////////////////////////
262 // WidthChanger
264 /////////////////////////////////////////////////////////////////////////
266 WidthChanger::WidthChanger(MetricsBase & mb, int w)
267 : Changer<MetricsBase>(mb)
269 save_ = mb;
270 mb.textwidth = w;
274 WidthChanger::~WidthChanger()
276 orig_ = save_;
280 /////////////////////////////////////////////////////////////////////////
282 // ColorChanger
284 /////////////////////////////////////////////////////////////////////////
286 ColorChanger::ColorChanger(FontInfo & font, string const & color)
287 : Changer<FontInfo, string>(font)
289 save_ = lcolor.getFromLyXName(color);
290 font.setColor(lcolor.getFromLyXName(color));
294 ColorChanger::~ColorChanger()
296 orig_.setColor(lcolor.getFromLyXName(save_));
300 } // namespace lyx