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