]> git.lyx.org Git - lyx.git/blob - src/insets/InsetSpace.cpp
Fix logic of new InsetFlex::updateBuffer() routine from a9614f1.
[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(odocstringstream & os,
625         OutputParams const &, size_t) const
626 {
627         switch (params_.kind) {
628         case InsetSpaceParams::HFILL:
629         case InsetSpaceParams::HFILL_PROTECTED:
630                 os << "     ";
631                 return 5;
632         case InsetSpaceParams::DOTFILL:
633                 os << ".....";
634                 return 5;
635         case InsetSpaceParams::HRULEFILL:
636                 os << "_____";
637                 return 5;
638         case InsetSpaceParams::LEFTARROWFILL:
639                 os << "<----";
640                 return 5;
641         case InsetSpaceParams::RIGHTARROWFILL:
642                 os << "---->";
643                 return 5;
644         case InsetSpaceParams::UPBRACEFILL:
645                 os << "\\-v-/";
646                 return 5;
647         case InsetSpaceParams::DOWNBRACEFILL:
648                 os << "/-^-\\";
649                 return 5;
650         case InsetSpaceParams::VISIBLE:
651                 os.put(0x2423);
652                 return 1;
653         case InsetSpaceParams::ENSKIP:
654                 os.put(0x2002);
655                 return 1;
656         case InsetSpaceParams::ENSPACE:
657                 os.put(0x2060); // WORD JOINER, makes the breakable en space unbreakable
658                 os.put(0x2002);
659                 os.put(0x2060); // WORD JOINER, makes the breakable en space unbreakable
660                 return 3;
661         case InsetSpaceParams::QUAD:
662                 os.put(0x2003);
663                 return 1;
664         case InsetSpaceParams::QQUAD:
665                 os.put(0x2003);
666                 os.put(0x2003);
667                 return 2;
668         case InsetSpaceParams::THIN:
669                 os.put(0x2009);
670                 return 1;
671         case InsetSpaceParams::MEDIUM:
672                 os.put(0x2005);
673                 return 1;
674         case InsetSpaceParams::THICK:
675                 os.put(0x2004);
676                 return 1;
677         case InsetSpaceParams::PROTECTED:
678         case InsetSpaceParams::CUSTOM_PROTECTED:
679                 os.put(0x00a0);
680                 return 1;
681         case InsetSpaceParams::NEGTHIN:
682         case InsetSpaceParams::NEGMEDIUM:
683         case InsetSpaceParams::NEGTHICK:
684                 return 0;
685         default:
686                 os << ' ';
687                 return 1;
688         }
689 }
690
691
692 int InsetSpace::docbook(odocstream & os, OutputParams const &) const
693 {
694         switch (params_.kind) {
695         case InsetSpaceParams::NORMAL:
696         case InsetSpaceParams::QUAD:
697         case InsetSpaceParams::QQUAD:
698         case InsetSpaceParams::ENSKIP:
699                 os << " ";
700                 break;
701         // FIXME For spaces and dashes look here:
702         // http://oreilly.com/catalog/docbook/book2/iso-pub.html
703         case InsetSpaceParams::PROTECTED:
704         // FIXME &blank; ?
705         case InsetSpaceParams::VISIBLE:
706         case InsetSpaceParams::ENSPACE:
707         // FIXME &thinsp; ?
708         case InsetSpaceParams::THIN:
709         case InsetSpaceParams::MEDIUM:
710         case InsetSpaceParams::THICK:
711         case InsetSpaceParams::NEGTHIN:
712         case InsetSpaceParams::NEGMEDIUM:
713         case InsetSpaceParams::NEGTHICK:
714                 os << "&nbsp;";
715                 break;
716         case InsetSpaceParams::HFILL:
717         case InsetSpaceParams::HFILL_PROTECTED:
718                 os << '\n';
719                 break;
720         case InsetSpaceParams::DOTFILL:
721                 // FIXME
722                 os << '\n';
723                 break;
724         case InsetSpaceParams::HRULEFILL:
725                 // FIXME
726                 os << '\n';
727                 break;
728         case InsetSpaceParams::LEFTARROWFILL:
729         case InsetSpaceParams::RIGHTARROWFILL:
730         case InsetSpaceParams::UPBRACEFILL:
731         case InsetSpaceParams::DOWNBRACEFILL:
732         case InsetSpaceParams::CUSTOM:
733         case InsetSpaceParams::CUSTOM_PROTECTED:
734                 // FIXME
735                 os << '\n';
736                 break;
737         }
738         return 0;
739 }
740
741
742 docstring InsetSpace::xhtml(XHTMLStream & xs, OutputParams const &) const
743 {
744         string output;
745         switch (params_.kind) {
746         case InsetSpaceParams::NORMAL:
747                 output = " ";
748                 break;
749         case InsetSpaceParams::ENSKIP:
750                 output ="&ensp;";
751                 break;
752         case InsetSpaceParams::ENSPACE:
753                 output ="&#x2060;&ensp;&#x2060;";
754                 break;
755         case InsetSpaceParams::QQUAD:
756                 output ="&emsp;&emsp;";
757                 break;
758         case InsetSpaceParams::THICK:
759                 output ="&#x2004;";
760                 break;
761         case InsetSpaceParams::QUAD:
762                 output ="&emsp;";
763                 break;
764         case InsetSpaceParams::MEDIUM:
765                 output ="&#x2005;";
766                 break;
767         case InsetSpaceParams::THIN:
768                 output ="&thinsp;";
769                 break;
770         case InsetSpaceParams::PROTECTED:
771         case InsetSpaceParams::NEGTHIN:
772         case InsetSpaceParams::NEGMEDIUM:
773         case InsetSpaceParams::NEGTHICK:
774                 output ="&nbsp;";
775                 break;
776         // no XHTML entity, only unicode code for space character exists
777         case InsetSpaceParams::VISIBLE:
778                 output ="&#x2423;";
779                 break;
780         case InsetSpaceParams::HFILL:
781         case InsetSpaceParams::HFILL_PROTECTED:
782         case InsetSpaceParams::DOTFILL:
783         case InsetSpaceParams::HRULEFILL:
784         case InsetSpaceParams::LEFTARROWFILL:
785         case InsetSpaceParams::RIGHTARROWFILL:
786         case InsetSpaceParams::UPBRACEFILL:
787         case InsetSpaceParams::DOWNBRACEFILL:
788                 // FIXME XHTML
789                 // Can we do anything with those in HTML?
790                 break;
791         case InsetSpaceParams::CUSTOM:
792                 // FIXME XHTML
793                 // Probably we could do some sort of blank span?
794                 break;
795         case InsetSpaceParams::CUSTOM_PROTECTED:
796                 // FIXME XHTML
797                 // Probably we could do some sort of blank span?
798                 output ="&nbsp;";
799                 break;
800         }
801         // don't escape the entities!
802         xs << XHTMLStream::ESCAPE_NONE << from_ascii(output);
803         return docstring();
804 }
805
806
807 void InsetSpace::validate(LaTeXFeatures & features) const
808 {
809         if (params_.kind == InsetSpaceParams::NEGMEDIUM ||
810             params_.kind == InsetSpaceParams::NEGTHICK) 
811                 features.require("amsmath");
812 }
813
814
815 void InsetSpace::toString(odocstream & os) const
816 {
817         odocstringstream ods;
818         plaintext(ods, OutputParams(0));
819         os << ods.str();
820 }
821
822
823 void InsetSpace::forToc(docstring & os, size_t) const
824 {
825         // There's no need to be cute here.
826         os += " ";
827 }
828
829
830 bool InsetSpace::isStretchableSpace() const
831 {
832         return params_.kind == InsetSpaceParams::HFILL
833                 || params_.kind == InsetSpaceParams::HFILL_PROTECTED
834                 || params_.kind == InsetSpaceParams::DOTFILL
835                 || params_.kind == InsetSpaceParams::HRULEFILL
836                 || params_.kind == InsetSpaceParams::LEFTARROWFILL
837                 || params_.kind == InsetSpaceParams::RIGHTARROWFILL
838                 || params_.kind == InsetSpaceParams::UPBRACEFILL
839                 || params_.kind == InsetSpaceParams::DOWNBRACEFILL;
840 }
841
842
843 string InsetSpace::contextMenuName() const
844 {
845         return "context-space";
846 }
847
848
849 void InsetSpace::string2params(string const & in, InsetSpaceParams & params)
850 {
851         params = InsetSpaceParams();
852         if (in.empty())
853                 return;
854
855         istringstream data(in);
856         Lexer lex;
857         lex.setStream(data);
858         lex.setContext("InsetSpace::string2params");
859         lex.next();
860         string const name = lex.getString();
861         if (name == "mathspace")
862                 params.math = true;
863         else {
864                 params.math = false;
865                 // we can try to read this even if the name is wrong
866                 LATTEST(name == "space");
867         }
868
869         // There are cases, such as when we are called via getStatus() from
870         // Dialog::canApply(), where we are just called with "space" rather
871         // than a full "space \type{}\n\\end_inset".
872         if (lex.isOK())
873                 params.read(lex);
874 }
875
876
877 string InsetSpace::params2string(InsetSpaceParams const & params)
878 {
879         ostringstream data;
880         if (params.math)
881                 data << "math";
882         data << "space" << ' ';
883         params.write(data);
884         return data.str();
885 }
886
887
888 } // namespace lyx