]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathSpace.cpp
Use convertDelimToXMLEscape with matrix delimiters, too.
[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 "MathFactory.h"
16 #include "MathStream.h"
17 #include "MathSupport.h"
18
19 #include "BufferView.h"
20 #include "Cursor.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
23 #include "LaTeXFeatures.h"
24 #include "MetricsInfo.h"
25
26 #include "insets/InsetSpace.h"
27
28 #include "frontends/Application.h"
29 #include "frontends/Painter.h"
30
31 #include "support/lassert.h"
32
33 using namespace std;
34
35 namespace lyx {
36
37 namespace {
38
39 struct SpaceInfo {
40         string name;
41         int width;
42         InsetSpaceParams::Kind kind;
43         bool negative;
44         bool visible;
45         bool custom;
46         bool escape; ///< whether a backslash needs to be added for writing
47 };
48
49 SpaceInfo space_info[] = {
50         // name           width kind                            negative visible custom escape
51         {"!",                6, InsetSpaceParams::NEGTHIN,         true,  true,  false, true},
52         {"negthinspace",     6, InsetSpaceParams::NEGTHIN,         true,  true,  false, true},
53         {"negmedspace",      8, InsetSpaceParams::NEGMEDIUM,       true,  true,  false, true},
54         {"negthickspace",   10, InsetSpaceParams::NEGTHICK,        true,  true,  false, true},
55         {",",                6, InsetSpaceParams::THIN,            false, true,  false, true},
56         {"thinspace",        6, InsetSpaceParams::THIN,            false, true,  false, true},
57         {":",                8, InsetSpaceParams::MEDIUM,          false, true,  false, true},
58         {"medspace",         8, InsetSpaceParams::MEDIUM,          false, true,  false, true},
59         {";",               10, InsetSpaceParams::THICK,           false, true,  false, true},
60         {"thickspace",      10, InsetSpaceParams::THICK,           false, true,  false, true},
61         {"enskip",          10, InsetSpaceParams::ENSKIP,          false, true,  false, true},
62         {"enspace",         10, InsetSpaceParams::ENSPACE,         false, true,  false, true},
63         {"quad",            20, InsetSpaceParams::QUAD,            false, true,  false, true},
64         {"qquad",           40, InsetSpaceParams::QQUAD,           false, true,  false, true},
65         {"lyxnegspace",     -2, InsetSpaceParams::NEGTHIN,         true,  false, false, true},
66         {"lyxposspace",      2, InsetSpaceParams::THIN,            false, false, false, true},
67         {"hfill",           80, InsetSpaceParams::HFILL,           false, true,  false, true},
68         {"hspace*{\\fill}", 80, InsetSpaceParams::HFILL_PROTECTED, false, true,  false, true},
69         {"hspace*",          0, InsetSpaceParams::CUSTOM_PROTECTED,false, true,  true,  true},
70         {"hspace",           0, InsetSpaceParams::CUSTOM,          false, true,  true,  true},
71         {" ",               10, InsetSpaceParams::NORMAL,          false, true,  false, true},
72         {"~",               10, InsetSpaceParams::PROTECTED,       false, true,  false, false},
73 };
74
75 int const nSpace = sizeof(space_info)/sizeof(SpaceInfo);
76 int const defaultSpace = 4;
77
78 } // anon namespace
79
80 InsetMathSpace::InsetMathSpace()
81         : space_(defaultSpace)
82 {
83 }
84
85
86 InsetMathSpace::InsetMathSpace(string const & name, string const & length)
87         : space_(defaultSpace)
88 {
89         for (int i = 0; i < nSpace; ++i)
90                 if (space_info[i].name == name) {
91                         space_ = i;
92                         break;
93                 }
94         if (space_info[space_].custom) {
95                 length_ = Length(length);
96                 if (length_.zero() || length_.empty()) {
97                         length_.value(1.0);
98                         length_.unit(Length::EM);
99                 }
100         }
101 }
102
103
104 InsetMathSpace::InsetMathSpace(Length const & length, bool const prot)
105         : space_(defaultSpace), length_(length)
106 {
107         for (int i = 0; i < nSpace; ++i)
108                 if ((prot && space_info[i].name == "hspace*")
109                         || (!prot && space_info[i].name == "hspace")) {
110                         space_ = i;
111                         break;
112                 }
113 }
114
115
116 Inset * InsetMathSpace::clone() const
117 {
118         return new InsetMathSpace(*this);
119 }
120
121
122 void InsetMathSpace::metrics(MetricsInfo & mi, Dimension & dim) const
123 {
124         dim.asc = 4;
125         dim.des = 0;
126         if (space_info[space_].custom)
127                 dim.wid = abs(length_.inPixels(mi.base));
128         else
129                 dim.wid = space_info[space_].width;
130 }
131
132
133 void InsetMathSpace::draw(PainterInfo & pi, int x, int y) const
134 {
135         // Sadly, HP-UX CC can't handle that kind of initialization.
136         // XPoint p[4] = {{++x, y-3}, {x, y}, {x+width-2, y}, {x+width-2, y-3}};
137         if (!space_info[space_].visible)
138                 return;
139
140         Dimension const dim = dimension(*pi.base.bv);
141         int xp[4];
142         int yp[4];
143         int w = dim.wid;
144
145         xp[0] = ++x;        yp[0] = y - 3;
146         xp[1] = x;          yp[1] = y;
147         xp[2] = x + w - 2;  yp[2] = y;
148         xp[3] = x + w - 2;  yp[3] = y - 3;
149
150         pi.pain.lines(xp, yp, 4,
151                         space_info[space_].custom ?
152                         Color_special :
153                         (isNegative() ? Color_latex : Color_math));
154 }
155
156
157 void InsetMathSpace::incSpace()
158 {
159         int const oldwidth = space_info[space_].width;
160         do
161                 space_ = (space_ + 1) % nSpace;
162         while ((space_info[space_].width == oldwidth && !space_info[space_].custom) ||
163                !space_info[space_].visible);
164         if (space_info[space_].custom && (length_.zero() || length_.empty())) {
165                 length_.value(1.0);
166                 length_.unit(Length::EM);
167         }
168 }
169
170
171 void InsetMathSpace::validate(LaTeXFeatures & features) const
172 {
173         if (space_info[space_].name == "negmedspace" ||
174             space_info[space_].name == "negthickspace")
175                 features.require("amsmath");
176 }
177
178
179 void InsetMathSpace::maple(MapleStream & os) const
180 {
181         os << ' ';
182 }
183
184 void InsetMathSpace::mathematica(MathematicaStream & os) const
185 {
186         os << ' ';
187 }
188
189
190 void InsetMathSpace::octave(OctaveStream & os) const
191 {
192         os << ' ';
193 }
194
195
196 void InsetMathSpace::mathmlize(MathStream & ms) const
197 {
198         SpaceInfo const & si = space_info[space_];
199         if (si.negative || !si.visible)
200                 return;
201         string l;
202         if (si.custom)
203                 l = length_.asHTMLString();
204         else if (si.kind != InsetSpaceParams::MEDIUM) {
205                 stringstream ss;
206                 ss << si.width;
207                 l = ss.str() + "px";
208         }
209         
210         ms << "<mspace";
211         if (!l.empty())
212                 ms << " width=\"" << from_ascii(l) << "\"";
213         ms << " />";
214 }
215
216         
217 void InsetMathSpace::htmlize(HtmlStream & ms) const
218 {
219         SpaceInfo const & si = space_info[space_];
220         switch (si.kind) {
221         case InsetSpaceParams::THIN:
222                 ms << from_ascii("&thinsp;");
223                 break;
224         case InsetSpaceParams::MEDIUM:
225                 ms << from_ascii("&nbsp;");
226                 break;
227         case InsetSpaceParams::THICK:
228                 ms << from_ascii("&emsp;");
229                 break;
230         case InsetSpaceParams::ENSKIP:
231         case InsetSpaceParams::ENSPACE:
232                 ms << from_ascii("&ensp;");
233                 break;
234         case InsetSpaceParams::QUAD:
235                 ms << from_ascii("&emsp;");
236                 break;
237         case InsetSpaceParams::QQUAD:
238                 ms << from_ascii("&emsp;&emsp;");
239                 break;
240         case InsetSpaceParams::HFILL:
241         case InsetSpaceParams::HFILL_PROTECTED:
242                 // FIXME: is there a useful HTML entity?
243                 break;
244         case InsetSpaceParams::CUSTOM:
245         case InsetSpaceParams::CUSTOM_PROTECTED: {
246                 string l = length_.asHTMLString();
247                 ms << MTag("span", "width='" + l + "'") 
248                    << from_ascii("&nbsp;") << ETag("span");
249                 break;
250         }
251         case InsetSpaceParams::NORMAL:
252         case InsetSpaceParams::PROTECTED:
253                 ms << from_ascii("&nbsp;");
254                 break;
255         default:
256                 break;
257         }
258 }
259
260         
261 void InsetMathSpace::normalize(NormalStream & os) const
262 {
263         os << "[space " << int(space_) << "] ";
264 }
265
266
267 void InsetMathSpace::write(WriteStream & os) const
268 {
269         // no MathEnsurer - all kinds work in text and math mode
270         if (space_info[space_].escape)
271                 os << '\\';
272         os << space_info[space_].name.c_str();
273         if (space_info[space_].custom)
274                 os << '{' << length_.asLatexString().c_str() << '}';
275         else if (space_info[space_].escape && space_info[space_].name.length() > 1)
276                 os.pendingSpace(true);
277 }
278
279
280 InsetSpaceParams InsetMathSpace::params() const
281 {
282         InsetSpaceParams isp(true);
283         LASSERT(space_info[space_].visible, return isp);
284         isp.kind = space_info[space_].kind;
285         isp.length = GlueLength(length_);
286         return isp;
287 }
288
289
290 string InsetMathSpace::contextMenuName() const
291 {
292         return "context-mathspace";
293 }
294
295
296 bool InsetMathSpace::getStatus(Cursor & cur, FuncRequest const & cmd,
297                                FuncStatus & status) const
298 {
299         switch (cmd.action()) {
300         // we handle these
301         case LFUN_INSET_MODIFY:
302         case LFUN_INSET_DIALOG_UPDATE:
303         case LFUN_MOUSE_RELEASE:
304         case LFUN_MOUSE_PRESS:
305         case LFUN_MOUSE_MOTION:
306                 status.setEnabled(true);
307                 return true;
308         default:
309                 bool retval = InsetMath::getStatus(cur, cmd, status);
310                 return retval;
311         }
312 }
313
314
315 void InsetMathSpace::doDispatch(Cursor & cur, FuncRequest & cmd)
316 {
317         switch (cmd.action()) {
318         case LFUN_INSET_MODIFY:
319                 if (cmd.getArg(0) == "mathspace") {
320                         MathData ar;
321                         if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
322                                 cur.recordUndo();
323                                 *this = *ar[0].nucleus()->asSpaceInset();
324                                 break;
325                         }
326                 }
327                 cur.undispatched();
328                 break;
329
330         case LFUN_MOUSE_RELEASE:
331                 if (cmd.button() == mouse_button::button1) {
332                         showInsetDialog(&cur.bv());
333                         break;
334                 }
335                 cur.undispatched();
336                 break;
337
338         case LFUN_MOUSE_PRESS:
339         case LFUN_MOUSE_MOTION:
340                 // eat other mouse commands
341                 break;
342
343         default:
344                 InsetMath::doDispatch(cur, cmd);
345                 break;
346         }
347 }
348
349
350 bool InsetMathSpace::isNegative() const
351 {
352         if (space_info[space_].custom)
353                 return length_.value() < 0;
354         return space_info[space_].negative;
355 }
356
357 } // namespace lyx