]> git.lyx.org Git - lyx.git/blob - src/MetricsInfo.cpp
This optional argument to the InsetCollapsable constructor
[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 "Color.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
25 #include "support/lassert.h"
26
27 using namespace std;
28
29 namespace lyx {
30
31
32 MetricsBase::MetricsBase()
33         : bv(0), font(), style(LM_ST_TEXT), fontname("mathnormal"),
34           textwidth(0)
35 {}
36
37
38 MetricsBase::MetricsBase(BufferView * b, FontInfo const & f, int w)
39         : bv(b), font(f), style(LM_ST_TEXT), fontname("mathnormal"),
40           textwidth(w)
41 {}
42
43
44 MetricsInfo::MetricsInfo(BufferView * bv, FontInfo const & font, int textwidth, 
45         MacroContext const & mc)
46         : base(bv, font, textwidth), macrocontext(mc)
47 {}
48
49
50 PainterInfo::PainterInfo(BufferView * bv, lyx::frontend::Painter & painter)
51         : pain(painter), ltr_pos(false), erased_(false), selected(false),
52         full_repaint(true), background_color(Color_background)
53 {
54         base.bv = bv;
55 }
56
57
58 void PainterInfo::draw(int x, int y, char_type c)
59 {
60         pain.text(x, y, c, base.font);
61 }
62
63
64 void PainterInfo::draw(int x, int y, docstring const & str)
65 {
66         pain.text(x, y, str, base.font);
67 }
68
69
70 ColorCode PainterInfo::backgroundColor(Inset const * inset, bool sel) const
71 {
72         ColorCode const color_bg = inset->backgroundColor();
73
74         if (selected && sel)
75                 // This inset is in a selection
76                 return Color_selection;
77         else {
78                 if (color_bg != Color_none)
79                         // This inset has its own color
80                         return color_bg;
81                 else {
82                         if (background_color == Color_none)
83                                 // This inset has no own color and does not inherit a color
84                                 return Color_background;
85                         else
86                                 // This inset has no own color, but inherits a color
87                                 return background_color;
88                 }
89         }
90 }
91
92
93 Styles smallerScriptStyle(Styles st)
94 {
95         switch (st) {
96                 case LM_ST_DISPLAY:
97                 case LM_ST_TEXT:
98                         return LM_ST_SCRIPT;
99                 case LM_ST_SCRIPT:
100                 case LM_ST_SCRIPTSCRIPT:
101                 default: // shut up compiler
102                         return LM_ST_SCRIPTSCRIPT;
103         }
104 }
105
106 ScriptChanger::ScriptChanger(MetricsBase & mb)
107         : StyleChanger(mb, smallerScriptStyle(mb.style))
108 {}
109
110
111
112 Styles smallerFracStyle(Styles st)
113 {
114         switch (st) {
115                 case LM_ST_DISPLAY:
116                         return LM_ST_TEXT;
117                 case LM_ST_TEXT:
118                         return LM_ST_SCRIPT;
119                 case LM_ST_SCRIPT:
120                 case LM_ST_SCRIPTSCRIPT:
121                 default: // shut up compiler
122                         return LM_ST_SCRIPTSCRIPT;
123         }
124 }
125
126
127 FracChanger::FracChanger(MetricsBase & mb)
128         : StyleChanger(mb, smallerFracStyle(mb.style))
129 {}
130
131
132
133 ArrayChanger::ArrayChanger(MetricsBase & mb)
134         : StyleChanger(mb, mb.style == LM_ST_DISPLAY ? LM_ST_TEXT : mb.style)
135 {}
136
137
138 ShapeChanger::ShapeChanger(FontInfo & font, FontShape shape)
139         : Changer<FontInfo, FontShape>(font)
140 {
141         save_ = orig_.shape();
142         orig_.setShape(shape);
143 }
144
145
146 ShapeChanger::~ShapeChanger()
147 {
148         orig_.setShape(save_);
149 }
150
151
152
153 StyleChanger::StyleChanger(MetricsBase & mb, Styles style)
154         :       Changer<MetricsBase>(mb)
155 {
156         static const int diff[4][4] =
157                 { { 0, 0, -3, -5 },
158                   { 0, 0, -3, -5 },
159                   { 3, 3,  0, -2 },
160                   { 5, 5,  2,  0 } };
161         save_ = mb;
162         int t = diff[mb.style][style];
163         if (t > 0)
164                 while (t--)
165                         mb.font.incSize();
166         else
167                 while (t++)
168                         mb.font.decSize();
169         mb.style = style;
170 }
171
172
173 StyleChanger::~StyleChanger()
174 {
175         orig_ = save_;
176 }
177
178
179
180 FontSetChanger::FontSetChanger(MetricsBase & mb, char const * name)
181         :       Changer<MetricsBase>(mb)
182 {
183         save_ = mb;
184         FontSize oldsize = save_.font.size();
185         mb.fontname = name;
186         mb.font = sane_font;
187         augmentFont(mb.font, from_ascii(name));
188         mb.font.setSize(oldsize);
189 }
190
191
192 FontSetChanger::FontSetChanger(MetricsBase & mb, docstring const & name)
193         :       Changer<MetricsBase>(mb)
194 {
195         save_ = mb;
196         FontSize oldsize = save_.font.size();
197         mb.fontname = to_utf8(name);
198         mb.font = sane_font;
199         augmentFont(mb.font, name);
200         mb.font.setSize(oldsize);
201 }
202
203
204 FontSetChanger::~FontSetChanger()
205 {
206         orig_ = save_;
207 }
208
209
210 WidthChanger::WidthChanger(MetricsBase & mb, int w)
211         :       Changer<MetricsBase>(mb)
212 {
213         save_ = mb;
214         mb.textwidth = w;
215 }
216
217
218 WidthChanger::~WidthChanger()
219 {
220         orig_ = save_;
221 }
222
223
224
225
226 ColorChanger::ColorChanger(FontInfo & font, string const & color)
227         : Changer<FontInfo, string>(font)
228 {
229         save_ = lcolor.getFromLyXName(color);
230         font.setColor(lcolor.getFromLyXName(color));
231 }
232
233
234 ColorChanger::~ColorChanger()
235 {
236         orig_.setColor(lcolor.getFromLyXName(save_));
237 }
238
239
240 } // namespace lyx