]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathSpace.cpp
Fulfill promise to Andre: TextClass_ptr --> TextClassPtr.
[lyx.git] / src / mathed / InsetMathSpace.cpp
1 /**
2  * \file InsetMathSpace.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 "InsetMathSpace.h"
14 #include "MathData.h"
15 #include "MathStream.h"
16
17 #include "LaTeXFeatures.h"
18 #include "Color.h"
19
20 #include "frontends/Painter.h"
21
22 using std::string;
23
24 namespace lyx {
25
26 char const * latex_mathspace[] = {
27         "!", "negmedspace", "negthickspace",  // negative space
28         ",", ":", ";", "quad", "qquad",       // positive space
29         "lyxnegspace", "lyxposspace"          // LyX special ("unvisible space")
30 };
31
32 int const nSpace = sizeof(latex_mathspace)/sizeof(char *);
33
34 namespace {
35
36 int spaceToWidth(int space)
37 {
38         switch (space) {
39                 case 0: return 6;
40                 case 1: return 8;
41                 case 2: return 10;
42                 case 3: return 6;
43                 case 4: return 8;
44                 case 5: return 10;
45                 case 6: return 20;
46                 case 7: return 40;
47                 case 8: return -2;
48                 case 9: return  2;
49                 default: return 6;
50         }
51 }
52
53 } // anon namespace
54
55 InsetMathSpace::InsetMathSpace(int sp)
56         : space_(sp)
57 {
58         dim_.asc = 4;
59         dim_.des = 0;
60         dim_.wid = spaceToWidth(space_);
61 }
62
63
64 InsetMathSpace::InsetMathSpace(docstring const & name)
65         : space_(1)
66 {
67         dim_.asc = 4;
68         dim_.des = 0;
69         for (int i = 0; i < nSpace; ++i)
70                 if (latex_mathspace[i] == name)
71                         space_ = i;
72         dim_.wid = spaceToWidth(space_);
73 }
74
75
76 Inset * InsetMathSpace::clone() const
77 {
78         return new InsetMathSpace(*this);
79 }
80
81
82 bool InsetMathSpace::metrics(MetricsInfo &, Dimension & dim) const
83 {
84         dim.wid = width();
85         dim.asc = ascent();
86         dim.des = descent();
87         if (dim_ == dim)
88                 return false;
89         dim_ = dim;
90         return true;
91 }
92
93
94 void InsetMathSpace::draw(PainterInfo & pi, int x, int y) const
95 {
96         // Sadly, HP-UX CC can't handle that kind of initialization.
97         // XPoint p[4] = {{++x, y-3}, {x, y}, {x+width-2, y}, {x+width-2, y-3}};
98         if (space_ >= nSpace - 2)
99                 return;
100
101         int xp[4];
102         int yp[4];
103         int w = width();
104
105         xp[0] = ++x;        yp[0] = y - 3;
106         xp[1] = x;          yp[1] = y;
107         xp[2] = x + w - 2;  yp[2] = y;
108         xp[3] = x + w - 2;  yp[3] = y - 3;
109
110         pi.pain.lines(xp, yp, 4, (space_ < 3) ? Color::latex : Color::math);
111 }
112
113
114 void InsetMathSpace::incSpace()
115 {
116         space_ = (space_ + 1) % (nSpace - 2);
117         dim_.wid = spaceToWidth(space_);
118 }
119
120
121 void InsetMathSpace::validate(LaTeXFeatures & features) const
122 {
123         if (space_ >= 0 && space_< nSpace) {
124                 if ((latex_mathspace[space_] == string("negmedspace"))
125                  || (latex_mathspace[space_] == string("negthickspace")))
126                         features.require("amsmath");
127         }
128 }
129
130
131 void InsetMathSpace::maple(MapleStream & os) const
132 {
133         os << ' ';
134 }
135
136 void InsetMathSpace::mathematica(MathematicaStream & os) const
137 {
138         os << ' ';
139 }
140
141
142 void InsetMathSpace::octave(OctaveStream & os) const
143 {
144         os << ' ';
145 }
146
147
148 void InsetMathSpace::normalize(NormalStream & os) const
149 {
150         os << "[space " << int(space_) << "] ";
151 }
152
153
154 void InsetMathSpace::write(WriteStream & os) const
155 {
156         if (space_ >= 0 && space_ < nSpace) {
157                 os << '\\' << latex_mathspace[space_];
158                 os.pendingSpace(true);
159         }
160 }
161
162
163 } // namespace lyx