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