]> git.lyx.org Git - lyx.git/blob - src/insets/InsetSpace.cpp
In multi-par sequences, check for arguments in all pars
[lyx.git] / src / insets / InsetSpace.cpp
1 /**
2  * \file InsetSpace.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup Nielsen
7  * \author Jean-Marc Lasgouttes
8  * \author Lars Gullik Bjønnes
9  * \author Jürgen Spitzmüller
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "InsetSpace.h"
17
18 #include "BufferView.h"
19 #include "Cursor.h"
20 #include "Dimension.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
23 #include "Language.h"
24 #include "LaTeXFeatures.h"
25 #include "Length.h"
26 #include "Lexer.h"
27 #include "MetricsInfo.h"
28 #include "OutputParams.h"
29 #include "output_xhtml.h"
30
31 #include "support/debug.h"
32 #include "support/docstream.h"
33 #include "support/gettext.h"
34 #include "support/lassert.h"
35 #include "support/lstrings.h"
36
37 #include "frontends/Application.h"
38 #include "frontends/FontMetrics.h"
39 #include "frontends/Painter.h"
40
41 using namespace std;
42
43 namespace lyx {
44
45
46 InsetSpace::InsetSpace(InsetSpaceParams const & params)
47         : Inset(0), params_(params)
48 {}
49
50
51 InsetSpaceParams::Kind InsetSpace::kind() const
52 {
53         return params_.kind;
54 }
55
56
57 GlueLength InsetSpace::length() const
58 {
59         return params_.length;
60 }
61
62
63 docstring InsetSpace::toolTip(BufferView const &, int, int) const
64 {
65         docstring message;
66         switch (params_.kind) {
67         case InsetSpaceParams::NORMAL:
68                 message = _("Interword Space");
69                 break;
70         case InsetSpaceParams::PROTECTED:
71                 message = _("Protected Space");
72                 break;
73         case InsetSpaceParams::VISIBLE:
74                 message = _("Visible Space");
75                 break;
76         case InsetSpaceParams::THIN:
77                 message = _("Thin Space");
78                 break;
79         case InsetSpaceParams::MEDIUM:
80                 message = _("Medium Space");
81                 break;
82         case InsetSpaceParams::THICK:
83                 message = _("Thick Space");
84                 break;
85         case InsetSpaceParams::QUAD:
86                 message = _("Quad Space");
87                 break;
88         case InsetSpaceParams::QQUAD:
89                 message = _("Double Quad Space");
90                 break;
91         case InsetSpaceParams::ENSPACE:
92                 message = _("Enspace");
93                 break;
94         case InsetSpaceParams::ENSKIP:
95                 message = _("Enskip");
96                 break;
97         case InsetSpaceParams::NEGTHIN:
98                 message = _("Negative Thin Space");
99                 break;
100         case InsetSpaceParams::NEGMEDIUM:
101                 message = _("Negative Medium Space");
102                 break;
103         case InsetSpaceParams::NEGTHICK:
104                 message = _("Negative Thick Space");
105                 break;
106         case InsetSpaceParams::HFILL:
107                 message = _("Horizontal Fill");
108                 break;
109         case InsetSpaceParams::HFILL_PROTECTED:
110                 message = _("Protected Horizontal Fill");
111                 break;
112         case InsetSpaceParams::DOTFILL:
113                 message = _("Horizontal Fill (Dots)");
114                 break;
115         case InsetSpaceParams::HRULEFILL:
116                 message = _("Horizontal Fill (Rule)");
117                 break;
118         case InsetSpaceParams::LEFTARROWFILL:
119                 message = _("Horizontal Fill (Left Arrow)");
120                 break;
121         case InsetSpaceParams::RIGHTARROWFILL:
122                 message = _("Horizontal Fill (Right Arrow)");
123                 break;
124         case InsetSpaceParams::UPBRACEFILL:
125                 message = _("Horizontal Fill (Up Brace)");
126                 break;
127         case InsetSpaceParams::DOWNBRACEFILL:
128                 message = _("Horizontal Fill (Down Brace)");
129                 break;
130         case InsetSpaceParams::CUSTOM:
131                 // FIXME unicode
132                 message = support::bformat(_("Horizontal Space (%1$s)"),
133                                 from_ascii(params_.length.asString()));
134                 break;
135         case InsetSpaceParams::CUSTOM_PROTECTED:
136                 // FIXME unicode
137                 message = support::bformat(_("Protected Horizontal Space (%1$s)"),
138                                 from_ascii(params_.length.asString()));
139                 break;
140         }
141         return message;
142 }
143
144
145 void InsetSpace::doDispatch(Cursor & cur, FuncRequest & cmd)
146 {
147         switch (cmd.action()) {
148
149         case LFUN_INSET_MODIFY:
150                 cur.recordUndo();
151                 string2params(to_utf8(cmd.argument()), params_);
152                 break;
153
154         case LFUN_INSET_DIALOG_UPDATE:
155                 cur.bv().updateDialog("space", params2string(params()));
156                 break;
157
158         default:
159                 Inset::doDispatch(cur, cmd);
160                 break;
161         }
162 }
163
164
165 bool InsetSpace::getStatus(Cursor & cur, FuncRequest const & cmd,
166         FuncStatus & status) const
167 {
168         switch (cmd.action()) {
169         // we handle these
170         case LFUN_INSET_MODIFY:
171                 if (cmd.getArg(0) == "space") {
172                         InsetSpaceParams params;
173                         string2params(to_utf8(cmd.argument()), params);
174                         status.setOnOff(params_.kind == params.kind);
175                         status.setEnabled(true);        
176                 } else
177                         status.setEnabled(false);
178                 return true;
179
180         case LFUN_INSET_DIALOG_UPDATE:
181                 status.setEnabled(true);
182                 return true;
183         default:
184                 return Inset::getStatus(cur, cmd, status);
185         }
186 }
187
188
189 namespace {
190 int const arrow_size = 8;
191 }
192
193
194 void InsetSpace::metrics(MetricsInfo & mi, Dimension & dim) const
195 {
196         if (isStretchableSpace()) {
197                 // The metrics for this kinds are calculated externally in
198                 // \c TextMetrics::computeRowMetrics. Those are dummy value:
199                 dim = Dimension(10, 10, 10);
200                 return;
201         }
202
203         frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
204         dim.asc = fm.maxAscent();
205         dim.des = fm.maxDescent();
206
207         switch (params_.kind) {
208                 case InsetSpaceParams::THIN:
209                 case InsetSpaceParams::NEGTHIN:
210                         dim.wid = fm.width(char_type('M')) / 6;
211                         break;
212                 case InsetSpaceParams::MEDIUM:
213                 case InsetSpaceParams::NEGMEDIUM:
214                         dim.wid = fm.width(char_type('M')) / 4;
215                         break;
216                 case InsetSpaceParams::THICK:
217                 case InsetSpaceParams::NEGTHICK:
218                         dim.wid = fm.width(char_type('M')) / 2;
219                         break;
220                 case InsetSpaceParams::PROTECTED:
221                 case InsetSpaceParams::VISIBLE:
222                 case InsetSpaceParams::NORMAL:
223                         dim.wid = fm.width(char_type(' '));
224                         break;
225                 case InsetSpaceParams::QUAD:
226                         dim.wid = fm.width(char_type('M'));
227                         break;
228                 case InsetSpaceParams::QQUAD:
229                         dim.wid = 2 * fm.width(char_type('M'));
230                         break;
231                 case InsetSpaceParams::ENSPACE:
232                 case InsetSpaceParams::ENSKIP:
233                         dim.wid = int(0.5 * fm.width(char_type('M')));
234                         break;
235                 case InsetSpaceParams::CUSTOM:
236                 case InsetSpaceParams::CUSTOM_PROTECTED: {
237                         int const w = 
238                                 params_.length.len().inPixels(mi.base.textwidth,
239                                                         fm.width(char_type('M')));
240                         int const minw = (w < 0) ? 3 * arrow_size : 4;
241                         dim.wid = max(minw, abs(w));
242                         break;
243                 }
244                 case InsetSpaceParams::HFILL:
245                 case InsetSpaceParams::HFILL_PROTECTED:
246                 case InsetSpaceParams::DOTFILL:
247                 case InsetSpaceParams::HRULEFILL:
248                 case InsetSpaceParams::LEFTARROWFILL:
249                 case InsetSpaceParams::RIGHTARROWFILL:
250                 case InsetSpaceParams::UPBRACEFILL:
251                 case InsetSpaceParams::DOWNBRACEFILL:
252                         // shut up compiler
253                         break;
254         }
255         // Cache the inset dimension.
256         setDimCache(mi, dim);
257 }
258
259
260 void InsetSpace::draw(PainterInfo & pi, int x, int y) const
261 {
262         Dimension const dim = dimension(*pi.base.bv);
263
264         if (isStretchableSpace() || params_.length.len().value() < 0) {
265                 int const asc = theFontMetrics(pi.base.font).ascent('M');
266                 int const desc = theFontMetrics(pi.base.font).descent('M');
267                 // Pixel height divisible by 2 for prettier fill graphics:
268                 int const oddheight = (asc ^ desc) & 1;
269                 int const x0 = x + 1;
270                 int const x1 = x + dim.wid - 2;
271                 int const y0 = y + desc - 1;
272                 int const y1 = y - asc + oddheight - 1;
273                 int const y2 = (y0 + y1) / 2;
274                 int xoffset = (y0 - y1) / 2;
275
276                 // Two tests for very narrow insets
277                 if (xoffset > x1 - x0
278                      && (params_.kind == InsetSpaceParams::LEFTARROWFILL
279                          || params_.kind == InsetSpaceParams::RIGHTARROWFILL))
280                                 xoffset = x1 - x0;
281                 if (xoffset * 6 > (x1 - x0)
282                      && (params_.kind == InsetSpaceParams::UPBRACEFILL
283                          || params_.kind == InsetSpaceParams::DOWNBRACEFILL))
284                                 xoffset = (x1 - x0) / 6;
285
286                 int const x2 = x0 + xoffset;
287                 int const x3 = x1 - xoffset;
288                 int const xm = (x0 + x1) / 2;
289                 int const xml = xm - xoffset;
290                 int const xmr = xm + xoffset;
291
292                 if (params_.kind == InsetSpaceParams::HFILL) {
293                         pi.pain.line(x0, y1, x0, y0, Color_added_space);
294                         pi.pain.line(x0, y2, x1, y2, Color_added_space,
295                                 frontend::Painter::line_onoffdash);
296                         pi.pain.line(x1, y1, x1, y0, Color_added_space);
297                 } else if (params_.kind == InsetSpaceParams::HFILL_PROTECTED) {
298                         pi.pain.line(x0, y1, x0, y0, Color_latex);
299                         pi.pain.line(x0, y2, x1, y2, Color_latex,
300                                 frontend::Painter::line_onoffdash);
301                         pi.pain.line(x1, y1, x1, y0, Color_latex);
302                 } else if (params_.kind == InsetSpaceParams::DOTFILL) {
303                         pi.pain.line(x0, y1, x0, y0, Color_special);
304                         pi.pain.line(x0, y0, x1, y0, Color_special,
305                                 frontend::Painter::line_onoffdash);
306                         pi.pain.line(x1, y1, x1, y0, Color_special);
307                 } else if (params_.kind == InsetSpaceParams::HRULEFILL) {
308                         pi.pain.line(x0, y1, x0, y0, Color_special);
309                         pi.pain.line(x0, y0, x1, y0, Color_special);
310                         pi.pain.line(x1, y1, x1, y0, Color_special);
311                 } else if (params_.kind == InsetSpaceParams::LEFTARROWFILL) {
312                         pi.pain.line(x2, y1 + 1 , x0 + 1, y2, Color_special);
313                         pi.pain.line(x0 + 1, y2 + 1 , x2, y0, Color_special);
314                         pi.pain.line(x0, y2 , x1, y2, Color_special);
315                 } else if (params_.kind == InsetSpaceParams::RIGHTARROWFILL) {
316                         pi.pain.line(x3 + 1, y1 + 1 , x1, y2, Color_special);
317                         pi.pain.line(x1, y2 + 1 , x3 + 1, y0, Color_special);
318                         pi.pain.line(x0, y2 , x1, y2, Color_special);
319                 } else if (params_.kind == InsetSpaceParams::UPBRACEFILL) {
320                         pi.pain.line(x0 + 1, y1 + 1 , x2, y2, Color_special);
321                         pi.pain.line(x2, y2 , xml, y2, Color_special);
322                         pi.pain.line(xml + 1, y2 + 1 , xm, y0, Color_special);
323                         pi.pain.line(xm + 1, y0 , xmr, y2 + 1, Color_special);
324                         pi.pain.line(xmr, y2 , x3, y2, Color_special);
325                         pi.pain.line(x3 + 1, y2 , x1, y1 + 1, Color_special);
326                 } else if (params_.kind == InsetSpaceParams::DOWNBRACEFILL) {
327                         pi.pain.line(x0 + 1, y0 , x2, y2 + 1, Color_special);
328                         pi.pain.line(x2, y2 , xml, y2, Color_special);
329                         pi.pain.line(xml + 1, y2 , xm, y1 + 1, Color_special);
330                         pi.pain.line(xm + 1, y1 + 1 , xmr, y2, Color_special);
331                         pi.pain.line(xmr, y2 , x3, y2, Color_special);
332                         pi.pain.line(x3 + 1, y2 + 1 , x1, y0, Color_special);
333                 } else if (params_.kind == InsetSpaceParams::CUSTOM) {
334                         pi.pain.line(x0, y1 + 1 , x2 + 1, y2, Color_special);
335                         pi.pain.line(x2 + 1, y2 + 1 , x0, y0, Color_special);
336                         pi.pain.line(x1 + 1, y1 + 1 , x3, y2, Color_special);
337                         pi.pain.line(x3, y2 + 1 , x1 + 1, y0, Color_special);
338                         pi.pain.line(x2, y2 , x3, y2, Color_special);
339                 } else if (params_.kind == InsetSpaceParams::CUSTOM_PROTECTED) {
340                         pi.pain.line(x0, y1 + 1 , x2 + 1, y2, Color_latex);
341                         pi.pain.line(x2 + 1, y2 + 1 , x0, y0, Color_latex);
342                         pi.pain.line(x1 + 1, y1 + 1 , x3, y2, Color_latex);
343                         pi.pain.line(x3, y2 + 1 , x1 + 1, y0, Color_latex);
344                         pi.pain.line(x2, y2 , x3, y2, Color_latex);
345                 }
346                 return;
347         }
348
349         int const w = dim.wid;
350         int const h = theFontMetrics(pi.base.font).ascent('x');
351         int xp[4], yp[4];
352
353         xp[0] = x;
354         yp[0] = y - max(h / 4, 1);
355         if (params_.kind == InsetSpaceParams::NORMAL ||
356             params_.kind == InsetSpaceParams::PROTECTED ||
357             params_.kind == InsetSpaceParams::VISIBLE) {
358                 xp[1] = x;     yp[1] = y;
359                 xp[2] = x + w; yp[2] = y;
360         } else {
361                 xp[1] = x;     yp[1] = y + max(h / 4, 1);
362                 xp[2] = x + w; yp[2] = y + max(h / 4, 1);
363         }
364         xp[3] = x + w;
365         yp[3] = y - max(h / 4, 1);
366
367         Color col = Color_special;
368         if (params_.kind == InsetSpaceParams::PROTECTED ||
369             params_.kind == InsetSpaceParams::ENSPACE ||
370             params_.kind == InsetSpaceParams::NEGTHIN ||
371             params_.kind == InsetSpaceParams::NEGMEDIUM ||
372             params_.kind == InsetSpaceParams::NEGTHICK ||
373             params_.kind == InsetSpaceParams::CUSTOM_PROTECTED)
374                 col = Color_latex;
375         else if (params_.kind == InsetSpaceParams::VISIBLE)
376                 col =  Color_foreground;
377
378         pi.pain.lines(xp, yp, 4, col);
379 }
380
381
382 void InsetSpaceParams::write(ostream & os) const
383 {
384         string command;
385         switch (kind) {
386         case InsetSpaceParams::NORMAL:
387                 os << "\\space{}";
388                 break;
389         case InsetSpaceParams::PROTECTED:
390                 os <<  "~";
391                 break;
392         case InsetSpaceParams::VISIBLE:
393                 os <<  "\\textvisiblespace{}";
394                 break;
395         case InsetSpaceParams::THIN:
396                 os <<  "\\thinspace{}";
397                 break;
398         case InsetSpaceParams::MEDIUM:
399                 os <<  "\\medspace{}";
400                 break;
401         case InsetSpaceParams::THICK:
402                 os <<  "\\thickspace{}";
403                 break;
404         case InsetSpaceParams::QUAD:
405                 os <<  "\\quad{}";
406                 break;
407         case InsetSpaceParams::QQUAD:
408                 os <<  "\\qquad{}";
409                 break;
410         case InsetSpaceParams::ENSPACE:
411                 os <<  "\\enspace{}";
412                 break;
413         case InsetSpaceParams::ENSKIP:
414                 os <<  "\\enskip{}";
415                 break;
416         case InsetSpaceParams::NEGTHIN:
417                 os <<  "\\negthinspace{}";
418                 break;
419         case InsetSpaceParams::NEGMEDIUM:
420                 os <<  "\\negmedspace{}";
421                 break;
422         case InsetSpaceParams::NEGTHICK:
423                 os <<  "\\negthickspace{}";
424                 break;
425         case InsetSpaceParams::HFILL:
426                 os <<  "\\hfill{}";
427                 break;
428         case InsetSpaceParams::HFILL_PROTECTED:
429                 os <<  "\\hspace*{\\fill}";
430                 break;
431         case InsetSpaceParams::DOTFILL:
432                 os <<  "\\dotfill{}";
433                 break;
434         case InsetSpaceParams::HRULEFILL:
435                 os <<  "\\hrulefill{}";
436                 break;
437         case InsetSpaceParams::LEFTARROWFILL:
438                 os <<  "\\leftarrowfill{}";
439                 break;
440         case InsetSpaceParams::RIGHTARROWFILL:
441                 os <<  "\\rightarrowfill{}";
442                 break;
443         case InsetSpaceParams::UPBRACEFILL:
444                 os <<  "\\upbracefill{}";
445                 break;
446         case InsetSpaceParams::DOWNBRACEFILL:
447                 os <<  "\\downbracefill{}";
448                 break;
449         case InsetSpaceParams::CUSTOM:
450                 os <<  "\\hspace{}";
451                 break;
452         case InsetSpaceParams::CUSTOM_PROTECTED:
453                 os <<  "\\hspace*{}";
454                 break;
455         }
456         
457         if (!length.len().empty())
458                 os << "\n\\length " << length.asString();
459 }
460
461
462 void InsetSpaceParams::read(Lexer & lex)
463 {
464         lex.setContext("InsetSpaceParams::read");
465         string command;
466         lex >> command;
467
468         // The tests for math might be disabled after a file format change
469         if (command == "\\space{}")
470                 kind = InsetSpaceParams::NORMAL;
471         else if (command == "~")
472                 kind = InsetSpaceParams::PROTECTED;
473         else if (command == "\\textvisiblespace{}")
474                 kind = InsetSpaceParams::VISIBLE;
475         else if (command == "\\thinspace{}")
476                 kind = InsetSpaceParams::THIN;
477         else if (math && command == "\\medspace{}")
478                 kind = InsetSpaceParams::MEDIUM;
479         else if (math && command == "\\thickspace{}")
480                 kind = InsetSpaceParams::THICK;
481         else if (command == "\\quad{}")
482                 kind = InsetSpaceParams::QUAD;
483         else if (command == "\\qquad{}")
484                 kind = InsetSpaceParams::QQUAD;
485         else if (command == "\\enspace{}")
486                 kind = InsetSpaceParams::ENSPACE;
487         else if (command == "\\enskip{}")
488                 kind = InsetSpaceParams::ENSKIP;
489         else if (command == "\\negthinspace{}")
490                 kind = InsetSpaceParams::NEGTHIN;
491         else if (command == "\\negmedspace{}")
492                 kind = InsetSpaceParams::NEGMEDIUM;
493         else if (command == "\\negthickspace{}")
494                 kind = InsetSpaceParams::NEGTHICK;
495         else if (command == "\\hfill{}")
496                 kind = InsetSpaceParams::HFILL;
497         else if (command == "\\hspace*{\\fill}")
498                 kind = InsetSpaceParams::HFILL_PROTECTED;
499         else if (command == "\\dotfill{}")
500                 kind = InsetSpaceParams::DOTFILL;
501         else if (command == "\\hrulefill{}")
502                 kind = InsetSpaceParams::HRULEFILL;
503         else if (command == "\\hspace{}")
504                 kind = InsetSpaceParams::CUSTOM;
505         else if (command == "\\leftarrowfill{}")
506                 kind = InsetSpaceParams::LEFTARROWFILL;
507         else if (command == "\\rightarrowfill{}")
508                 kind = InsetSpaceParams::RIGHTARROWFILL;
509         else if (command == "\\upbracefill{}")
510                 kind = InsetSpaceParams::UPBRACEFILL;
511         else if (command == "\\downbracefill{}")
512                 kind = InsetSpaceParams::DOWNBRACEFILL;
513         else if (command == "\\hspace*{}")
514                 kind = InsetSpaceParams::CUSTOM_PROTECTED;
515         else
516                 lex.printError("InsetSpace: Unknown kind: `$$Token'");
517
518         if (lex.checkFor("\\length"))
519                 lex >> length;
520 }
521
522
523 void InsetSpace::write(ostream & os) const
524 {
525         os << "space ";
526         params_.write(os);
527 }
528
529
530 void InsetSpace::read(Lexer & lex)
531 {
532         params_.read(lex);
533         lex >> "\\end_inset";
534 }
535
536
537 void InsetSpace::latex(otexstream & os, OutputParams const & runparams) const
538 {
539         switch (params_.kind) {
540         case InsetSpaceParams::NORMAL:
541                 os << (runparams.free_spacing ? " " : "\\ ");
542                 break;
543         case InsetSpaceParams::PROTECTED:
544                 if (runparams.local_font &&
545                     runparams.local_font->language()->lang() == "polutonikogreek")
546                         // in babel's polutonikogreek, ~ is active
547                         os << (runparams.free_spacing ? " " : "\\nobreakspace{}");
548                 else
549                         os << (runparams.free_spacing ? ' ' : '~');
550                 break;
551         case InsetSpaceParams::VISIBLE:
552                 os << (runparams.free_spacing ? " " : "\\textvisiblespace{}");
553                 break;
554         case InsetSpaceParams::THIN:
555                 os << (runparams.free_spacing ? " " : "\\,");
556                 break;
557         case InsetSpaceParams::MEDIUM:
558                 os << (runparams.free_spacing ? " " : "\\:");
559                 break;
560         case InsetSpaceParams::THICK:
561                 os << (runparams.free_spacing ? " " : "\\;");
562                 break;
563         case InsetSpaceParams::QUAD:
564                 os << (runparams.free_spacing ? " " : "\\quad{}");
565                 break;
566         case InsetSpaceParams::QQUAD:
567                 os << (runparams.free_spacing ? " " : "\\qquad{}");
568                 break;
569         case InsetSpaceParams::ENSPACE:
570                 os << (runparams.free_spacing ? " " : "\\enspace{}");
571                 break;
572         case InsetSpaceParams::ENSKIP:
573                 os << (runparams.free_spacing ? " " : "\\enskip{}");
574                 break;
575         case InsetSpaceParams::NEGTHIN:
576                 os << (runparams.free_spacing ? " " : "\\negthinspace{}");
577                 break;
578         case InsetSpaceParams::NEGMEDIUM:
579                 os << (runparams.free_spacing ? " " : "\\negmedspace{}");
580                 break;
581         case InsetSpaceParams::NEGTHICK:
582                 os << (runparams.free_spacing ? " " : "\\negthickspace{}");
583                 break;
584         case InsetSpaceParams::HFILL:
585                 os << (runparams.free_spacing ? " " : "\\hfill{}");
586                 break;
587         case InsetSpaceParams::HFILL_PROTECTED:
588                 os << (runparams.free_spacing ? " " : "\\hspace*{\\fill}");
589                 break;
590         case InsetSpaceParams::DOTFILL:
591                 os << (runparams.free_spacing ? " " : "\\dotfill{}");
592                 break;
593         case InsetSpaceParams::HRULEFILL:
594                 os << (runparams.free_spacing ? " " : "\\hrulefill{}");
595                 break;
596         case InsetSpaceParams::LEFTARROWFILL:
597                 os << (runparams.free_spacing ? " " : "\\leftarrowfill{}");
598                 break;
599         case InsetSpaceParams::RIGHTARROWFILL:
600                 os << (runparams.free_spacing ? " " : "\\rightarrowfill{}");
601                 break;
602         case InsetSpaceParams::UPBRACEFILL:
603                 os << (runparams.free_spacing ? " " : "\\upbracefill{}");
604                 break;
605         case InsetSpaceParams::DOWNBRACEFILL:
606                 os << (runparams.free_spacing ? " " : "\\downbracefill{}");
607                 break;
608         case InsetSpaceParams::CUSTOM:
609                 if (runparams.free_spacing)
610                         os << " ";
611                 else
612                         os << "\\hspace{" << from_ascii(params_.length.asLatexString()) << "}";
613                 break;
614         case InsetSpaceParams::CUSTOM_PROTECTED:
615                 if (runparams.free_spacing)
616                         os << " ";
617                 else
618                         os << "\\hspace*{" << from_ascii(params_.length.asLatexString()) << "}";
619                 break;
620         }
621 }
622
623
624 int InsetSpace::plaintext(odocstream & os, OutputParams const &) const
625 {
626         switch (params_.kind) {
627         case InsetSpaceParams::HFILL:
628         case InsetSpaceParams::HFILL_PROTECTED:
629                 os << "     ";
630                 return 5;
631         case InsetSpaceParams::DOTFILL:
632                 os << ".....";
633                 return 5;
634         case InsetSpaceParams::HRULEFILL:
635                 os << "_____";
636                 return 5;
637         case InsetSpaceParams::LEFTARROWFILL:
638                 os << "<----";
639                 return 5;
640         case InsetSpaceParams::RIGHTARROWFILL:
641                 os << "---->";
642                 return 5;
643         case InsetSpaceParams::UPBRACEFILL:
644                 os << "\\-v-/";
645                 return 5;
646         case InsetSpaceParams::DOWNBRACEFILL:
647                 os << "/-^-\\";
648                 return 5;
649         case InsetSpaceParams::VISIBLE:
650                 os.put(0x2423);
651                 return 1;
652         case InsetSpaceParams::ENSKIP:
653                 os.put(0x2002);
654                 return 1;
655         case InsetSpaceParams::ENSPACE:
656                 os.put(0x2060); // WORD JOINER, makes the breakable en space unbreakable
657                 os.put(0x2002);
658                 os.put(0x2060); // WORD JOINER, makes the breakable en space unbreakable
659                 return 3;
660         case InsetSpaceParams::QUAD:
661                 os.put(0x2003);
662                 return 1;
663         case InsetSpaceParams::QQUAD:
664                 os.put(0x2003);
665                 os.put(0x2003);
666                 return 2;
667         case InsetSpaceParams::THIN:
668                 os.put(0x2009);
669                 return 1;
670         case InsetSpaceParams::MEDIUM:
671                 os.put(0x2005);
672                 return 1;
673         case InsetSpaceParams::THICK:
674                 os.put(0x2004);
675                 return 1;
676         case InsetSpaceParams::PROTECTED:
677         case InsetSpaceParams::CUSTOM_PROTECTED:
678                 os.put(0x00a0);
679                 return 1;
680         case InsetSpaceParams::NEGTHIN:
681         case InsetSpaceParams::NEGMEDIUM:
682         case InsetSpaceParams::NEGTHICK:
683                 return 0;
684         default:
685                 os << ' ';
686                 return 1;
687         }
688 }
689
690
691 int InsetSpace::docbook(odocstream & os, OutputParams const &) const
692 {
693         switch (params_.kind) {
694         case InsetSpaceParams::NORMAL:
695         case InsetSpaceParams::QUAD:
696         case InsetSpaceParams::QQUAD:
697         case InsetSpaceParams::ENSKIP:
698                 os << " ";
699                 break;
700         // FIXME For spaces and dashes look here:
701         // http://oreilly.com/catalog/docbook/book2/iso-pub.html
702         case InsetSpaceParams::PROTECTED:
703         // FIXME &blank; ?
704         case InsetSpaceParams::VISIBLE:
705         case InsetSpaceParams::ENSPACE:
706         // FIXME &thinsp; ?
707         case InsetSpaceParams::THIN:
708         case InsetSpaceParams::MEDIUM:
709         case InsetSpaceParams::THICK:
710         case InsetSpaceParams::NEGTHIN:
711         case InsetSpaceParams::NEGMEDIUM:
712         case InsetSpaceParams::NEGTHICK:
713                 os << "&nbsp;";
714                 break;
715         case InsetSpaceParams::HFILL:
716         case InsetSpaceParams::HFILL_PROTECTED:
717                 os << '\n';
718                 break;
719         case InsetSpaceParams::DOTFILL:
720                 // FIXME
721                 os << '\n';
722                 break;
723         case InsetSpaceParams::HRULEFILL:
724                 // FIXME
725                 os << '\n';
726                 break;
727         case InsetSpaceParams::LEFTARROWFILL:
728         case InsetSpaceParams::RIGHTARROWFILL:
729         case InsetSpaceParams::UPBRACEFILL:
730         case InsetSpaceParams::DOWNBRACEFILL:
731         case InsetSpaceParams::CUSTOM:
732         case InsetSpaceParams::CUSTOM_PROTECTED:
733                 // FIXME
734                 os << '\n';
735                 break;
736         }
737         return 0;
738 }
739
740
741 docstring InsetSpace::xhtml(XHTMLStream & xs, OutputParams const &) const
742 {
743         string output;
744         switch (params_.kind) {
745         case InsetSpaceParams::NORMAL:
746                 output = " ";
747                 break;
748         case InsetSpaceParams::ENSKIP:
749                 output ="&ensp;";
750                 break;
751         case InsetSpaceParams::ENSPACE:
752                 output ="&#x2060;&ensp;&#x2060;";
753                 break;
754         case InsetSpaceParams::QQUAD:
755                 output ="&emsp;&emsp;";
756                 break;
757         case InsetSpaceParams::THICK:
758                 output ="&#x2004;";
759                 break;
760         case InsetSpaceParams::QUAD:
761                 output ="&emsp;";
762                 break;
763         case InsetSpaceParams::MEDIUM:
764                 output ="&#x2005;";
765                 break;
766         case InsetSpaceParams::THIN:
767                 output ="&thinsp;";
768                 break;
769         case InsetSpaceParams::PROTECTED:
770         case InsetSpaceParams::NEGTHIN:
771         case InsetSpaceParams::NEGMEDIUM:
772         case InsetSpaceParams::NEGTHICK:
773                 output ="&nbsp;";
774                 break;
775         // no XHTML entity, only unicode code for space character exists
776         case InsetSpaceParams::VISIBLE:
777                 output ="&#x2423;";
778                 break;
779         case InsetSpaceParams::HFILL:
780         case InsetSpaceParams::HFILL_PROTECTED:
781         case InsetSpaceParams::DOTFILL:
782         case InsetSpaceParams::HRULEFILL:
783         case InsetSpaceParams::LEFTARROWFILL:
784         case InsetSpaceParams::RIGHTARROWFILL:
785         case InsetSpaceParams::UPBRACEFILL:
786         case InsetSpaceParams::DOWNBRACEFILL:
787                 // FIXME XHTML
788                 // Can we do anything with those in HTML?
789                 break;
790         case InsetSpaceParams::CUSTOM:
791                 // FIXME XHTML
792                 // Probably we could do some sort of blank span?
793                 break;
794         case InsetSpaceParams::CUSTOM_PROTECTED:
795                 // FIXME XHTML
796                 // Probably we could do some sort of blank span?
797                 output ="&nbsp;";
798                 break;
799         }
800         // don't escape the entities!
801         xs << XHTMLStream::ESCAPE_NONE << from_ascii(output);
802         return docstring();
803 }
804
805
806 void InsetSpace::validate(LaTeXFeatures & features) const
807 {
808         if (params_.kind == InsetSpaceParams::NEGMEDIUM ||
809             params_.kind == InsetSpaceParams::NEGTHICK) 
810                 features.require("amsmath");
811 }
812
813
814 void InsetSpace::toString(odocstream & os) const
815 {
816         plaintext(os, OutputParams(0));
817 }
818
819
820 void InsetSpace::forToc(docstring & os, size_t) const
821 {
822         // There's no need to be cute here.
823         os += " ";
824 }
825
826
827 bool InsetSpace::isStretchableSpace() const
828 {
829         return params_.kind == InsetSpaceParams::HFILL
830                 || params_.kind == InsetSpaceParams::HFILL_PROTECTED
831                 || params_.kind == InsetSpaceParams::DOTFILL
832                 || params_.kind == InsetSpaceParams::HRULEFILL
833                 || params_.kind == InsetSpaceParams::LEFTARROWFILL
834                 || params_.kind == InsetSpaceParams::RIGHTARROWFILL
835                 || params_.kind == InsetSpaceParams::UPBRACEFILL
836                 || params_.kind == InsetSpaceParams::DOWNBRACEFILL;
837 }
838
839
840 string InsetSpace::contextMenuName() const
841 {
842         return "context-space";
843 }
844
845
846 void InsetSpace::string2params(string const & in, InsetSpaceParams & params)
847 {
848         params = InsetSpaceParams();
849         if (in.empty())
850                 return;
851
852         istringstream data(in);
853         Lexer lex;
854         lex.setStream(data);
855         lex.setContext("InsetSpace::string2params");
856         lex.next();
857         string const name = lex.getString();
858         if (name == "mathspace")
859                 params.math = true;
860         else {
861                 params.math = false;
862                 LASSERT(name == "space", /**/);
863         }
864
865         // There are cases, such as when we are called via getStatus() from
866         // Dialog::canApply(), where we are just called with "space" rather
867         // than a full "space \type{}\n\\end_inset".
868         if (lex.isOK())
869                 params.read(lex);
870 }
871
872
873 string InsetSpace::params2string(InsetSpaceParams const & params)
874 {
875         ostringstream data;
876         if (params.math)
877                 data << "math";
878         data << "space" << ' ';
879         params.write(data);
880         return data.str();
881 }
882
883
884 } // namespace lyx