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