]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathChar.C
BUG 686: delete empty math box with delete/backspace key
[lyx.git] / src / mathed / InsetMathChar.C
1 /**
2  * \file InsetMathChar.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author André Pönitz
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetMathChar.h"
15 #include "MathSupport.h"
16 #include "MathStream.h"
17
18 #include "debug.h"
19 #include "dimension.h"
20 #include "support/lstrings.h"
21 #include "TextPainter.h"
22
23 #include "frontends/FontMetrics.h"
24
25
26 namespace lyx {
27
28 using std::auto_ptr;
29
30 extern bool has_math_fonts;
31
32 namespace {
33
34         bool isBinaryOp(char_type c)
35         {
36                 return support::contains("+-<>=/*", static_cast<char>(c));
37         }
38
39
40         bool slanted(char_type c)
41         {
42                 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
43         }
44
45 }
46
47
48 InsetMathChar::InsetMathChar(char_type c)
49         : char_(c)
50 {}
51
52
53
54 auto_ptr<InsetBase> InsetMathChar::doClone() const
55 {
56         return auto_ptr<InsetBase>(new InsetMathChar(*this));
57 }
58
59
60 void InsetMathChar::metrics(MetricsInfo & mi, Dimension & dim) const
61 {
62 #if 1
63         if (char_ == '=' && has_math_fonts) {
64                 FontSetChanger dummy(mi.base, "cmr");
65                 mathed_char_dim(mi.base.font, char_, dim);
66         } else if ((char_ == '>' || char_ == '<') && has_math_fonts) {
67                 FontSetChanger dummy(mi.base, "cmm");
68                 mathed_char_dim(mi.base.font, char_, dim);
69         } else if (!slanted(char_) && mi.base.fontname == "mathnormal") {
70                 ShapeChanger dummy(mi.base.font, LyXFont::UP_SHAPE);
71                 mathed_char_dim(mi.base.font, char_, dim);
72         } else {
73                 mathed_char_dim(mi.base.font, char_, dim);
74         }
75         int const em = mathed_char_width(mi.base.font, 'M');
76         if (isBinaryOp(char_))
77                 dim.wid += static_cast<int>(0.5*em+0.5);
78         else if (char_ == '\'')
79                 dim.wid += static_cast<int>(0.1667*em+0.5);
80 #else
81         whichFont(font_, code_, mi);
82         mathed_char_dim(font_, char_, dim_);
83         if (isBinaryOp(char_, code_))
84                 width_ += 2 * theFontMetrics(font_).width(' ');
85         lyxerr << "InsetMathChar::metrics: " << dim << endl;
86 #endif
87         width_ = dim.wid;
88 }
89
90
91 void InsetMathChar::draw(PainterInfo & pi, int x, int y) const
92 {
93         //lyxerr << "drawing '" << char_ << "' font: " << pi.base.fontname << endl;
94         int const em = mathed_char_width(pi.base.font, 'M');
95         if (isBinaryOp(char_))
96                 x += static_cast<int>(0.25*em+0.5);
97         else if (char_ == '\'')
98                 x += static_cast<int>(0.0833*em+0.5);
99 #if 1
100         if (char_ == '=' && has_math_fonts) {
101                 FontSetChanger dummy(pi.base, "cmr");
102                 pi.draw(x, y, char_);
103         } else if ((char_ == '>' || char_ == '<') && has_math_fonts) {
104                 FontSetChanger dummy(pi.base, "cmm");
105                 pi.draw(x, y, char_);
106         } else if (!slanted(char_) && pi.base.fontname == "mathnormal") {
107                 ShapeChanger dummy(pi.base.font, LyXFont::UP_SHAPE);
108                 pi.draw(x, y, char_);
109         } else {
110                 pi.draw(x, y, char_);
111         }
112 #else
113         drawChar(pain, font_, x, y, char_);
114 #endif
115 }
116
117
118 void InsetMathChar::metricsT(TextMetricsInfo const &, Dimension & dim) const
119 {
120         dim.wid = 1;
121         dim.asc = 1;
122         dim.des = 0;
123 }
124
125
126 void InsetMathChar::drawT(TextPainter & pain, int x, int y) const
127 {
128         //lyxerr << "drawing text '" << char_ << "' code: " << code_ << endl;
129         pain.draw(x, y, char_);
130 }
131
132
133 void InsetMathChar::write(WriteStream & os) const
134 {
135         os.os().put(char_);
136 }
137
138
139 void InsetMathChar::normalize(NormalStream & os) const
140 {
141         os << "[char ";
142         os.os().put(char_);
143         os << " mathalpha]";
144 }
145
146
147 void InsetMathChar::octave(OctaveStream & os) const
148 {
149         os.os().put(char_);
150 }
151
152
153 void InsetMathChar::mathmlize(MathStream & ms) const
154 {
155         switch (char_) {
156                 case '<': ms << "&lt;"; break;
157                 case '>': ms << "&gt;"; break;
158                 case '&': ms << "&amp;"; break;
159                 default: ms.os().put(char_); break;
160         }
161 }
162
163
164 bool InsetMathChar::isRelOp() const
165 {
166         return char_ == '=' || char_ == '<' || char_ == '>';
167 }
168
169
170 } // namespace lyx