]> git.lyx.org Git - features.git/blob - src/insets/InsetSpace.cpp
Move handling of LFUN_MOUSE_RELEASE up to Inset and remove the edit() functions for...
[features.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 "LaTeXFeatures.h"
24 #include "Length.h"
25 #include "Lexer.h"
26 #include "MetricsInfo.h"
27 #include "OutputParams.h"
28
29 #include "support/debug.h"
30 #include "support/docstream.h"
31 #include "support/gettext.h"
32 #include "support/lassert.h"
33 #include "support/lstrings.h"
34
35 #include "frontends/Application.h"
36 #include "frontends/FontMetrics.h"
37 #include "frontends/Painter.h"
38
39 using namespace std;
40
41 namespace lyx {
42
43
44 InsetSpace::InsetSpace(InsetSpaceParams const & params)
45         : params_(params)
46 {}
47
48
49 InsetSpaceParams::Kind InsetSpace::kind() const
50 {
51         return params_.kind;
52 }
53
54
55 Length InsetSpace::length() const
56 {
57         return params_.length;
58 }
59
60
61 InsetSpace::~InsetSpace()
62 {
63         hideDialogs("space", this);
64 }
65
66
67 docstring InsetSpace::toolTip(BufferView const &, int, int) const
68 {
69         docstring message;
70         switch (params_.kind) {
71         case InsetSpaceParams::NORMAL:
72                 message = _("Interword Space");
73                 break;
74         case InsetSpaceParams::PROTECTED:
75                 message = _("Protected 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 = _("QQuad 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                 message = support::bformat(_("Horizontal Space (%1$s)"),
133                                 params_.length.asDocstring());
134                 break;
135         case InsetSpaceParams::CUSTOM_PROTECTED:
136                 message = support::bformat(_("Protected Horizontal Space (%1$s)"),
137                                 params_.length.asDocstring());
138                 break;
139         }
140         return message;
141 }
142
143
144 void InsetSpace::doDispatch(Cursor & cur, FuncRequest & cmd)
145 {
146         switch (cmd.action) {
147
148         case LFUN_INSET_MODIFY:
149                 string2params(to_utf8(cmd.argument()), params_);
150                 break;
151
152         case LFUN_INSET_DIALOG_UPDATE:
153                 cur.bv().updateDialog("space", params2string(params()));
154                 break;
155
156         default:
157                 Inset::doDispatch(cur, cmd);
158                 break;
159         }
160 }
161
162
163 bool InsetSpace::getStatus(Cursor & cur, FuncRequest const & cmd,
164         FuncStatus & status) const
165 {
166         switch (cmd.action) {
167         // we handle these
168         case LFUN_INSET_MODIFY:
169                 if (cmd.getArg(0) == "space") {
170                         InsetSpaceParams params;
171                         string2params(to_utf8(cmd.argument()), params);
172                         status.setOnOff(params_.kind == params.kind);
173                 }
174                 // fall through
175         case LFUN_INSET_SETTINGS:
176         case LFUN_INSET_DIALOG_UPDATE:
177                 status.setEnabled(true);
178                 return true;
179         default:
180                 return Inset::getStatus(cur, cmd, status);
181         }
182 }
183
184
185 bool InsetSpace::showInsetDialog(BufferView * bv) const
186 {
187         bv->showDialog("space", params2string(params()),
188                 const_cast<InsetSpace *>(this));
189         return true;
190 }
191
192
193 namespace {
194 int const arrow_size = 8;
195 }
196
197
198 void InsetSpace::metrics(MetricsInfo & mi, Dimension & dim) const
199 {
200         if (isStretchableSpace()) {
201                 // The metrics for this kinds are calculated externally in
202                 // \c TextMetrics::computeRowMetrics. Those are dummy value:
203                 dim = Dimension(10, 10, 10);
204                 return;
205         }
206
207         frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
208         dim.asc = fm.maxAscent();
209         dim.des = fm.maxDescent();
210
211         switch (params_.kind) {
212                 case InsetSpaceParams::THIN:
213                 case InsetSpaceParams::NEGTHIN:
214                         dim.wid = fm.width(char_type('M')) / 6;
215                         break;
216                 case InsetSpaceParams::MEDIUM:
217                 case InsetSpaceParams::NEGMEDIUM:
218                         dim.wid = fm.width(char_type('M')) / 4;
219                         break;
220                 case InsetSpaceParams::THICK:
221                 case InsetSpaceParams::NEGTHICK:
222                         dim.wid = fm.width(char_type('M')) / 2;
223                         break;
224                 case InsetSpaceParams::PROTECTED:
225                 case InsetSpaceParams::NORMAL:
226                         dim.wid = fm.width(char_type(' '));
227                         break;
228                 case InsetSpaceParams::QUAD:
229                         dim.wid = fm.width(char_type('M'));
230                         break;
231                 case InsetSpaceParams::QQUAD:
232                         dim.wid = 2 * fm.width(char_type('M'));
233                         break;
234                 case InsetSpaceParams::ENSPACE:
235                 case InsetSpaceParams::ENSKIP:
236                         dim.wid = int(0.5 * fm.width(char_type('M')));
237                         break;
238                 case InsetSpaceParams::CUSTOM:
239                 case InsetSpaceParams::CUSTOM_PROTECTED: {
240                         int const w = 
241                                 params_.length.inPixels(mi.base.textwidth,
242                                                         fm.width(char_type('M')));
243                         int const minw = (w < 0) ? 3 * arrow_size : 4;
244                         dim.wid = max(minw, abs(w));
245                         break;
246                 }
247                 case InsetSpaceParams::HFILL:
248                 case InsetSpaceParams::HFILL_PROTECTED:
249                 case InsetSpaceParams::DOTFILL:
250                 case InsetSpaceParams::HRULEFILL:
251                 case InsetSpaceParams::LEFTARROWFILL:
252                 case InsetSpaceParams::RIGHTARROWFILL:
253                 case InsetSpaceParams::UPBRACEFILL:
254                 case InsetSpaceParams::DOWNBRACEFILL:
255                         // shut up compiler
256                         break;
257         }
258         // Cache the inset dimension.
259         setDimCache(mi, dim);
260 }
261
262
263 void InsetSpace::draw(PainterInfo & pi, int x, int y) const
264 {
265         Dimension const dim = dimension(*pi.base.bv);
266
267         if (isStretchableSpace() || params_.length.value() < 0) {
268                 int const asc = theFontMetrics(pi.base.font).ascent('M');
269                 int const desc = theFontMetrics(pi.base.font).descent('M');
270                 // Pixel height divisible by 2 for prettier fill graphics:
271                 int const oddheight = (asc ^ desc) & 1;
272                 int const x0 = x + 1;
273                 int const x1 = x + dim.wid - 2;
274                 int const y0 = y + desc - 1;
275                 int const y1 = y - asc + oddheight - 1;
276                 int const y2 = (y0 + y1) / 2;
277                 int xoffset = (y0 - y1) / 2;
278
279                 // Two tests for very narrow insets
280                 if (xoffset > x1 - x0
281                      && (params_.kind == InsetSpaceParams::LEFTARROWFILL
282                          || params_.kind == InsetSpaceParams::RIGHTARROWFILL))
283                                 xoffset = x1 - x0;
284                 if (xoffset * 6 > (x1 - x0)
285                      && (params_.kind == InsetSpaceParams::UPBRACEFILL
286                          || params_.kind == InsetSpaceParams::DOWNBRACEFILL))
287                                 xoffset = (x1 - x0) / 6;
288
289                 int const x2 = x0 + xoffset;
290                 int const x3 = x1 - xoffset;
291                 int const xm = (x0 + x1) / 2;
292                 int const xml = xm - xoffset;
293                 int const xmr = xm + xoffset;
294
295                 if (params_.kind == InsetSpaceParams::HFILL) {
296                         pi.pain.line(x0, y1, x0, y0, Color_added_space);
297                         pi.pain.line(x0, y2, x1, y2, Color_added_space,
298                                 frontend::Painter::line_onoffdash);
299                         pi.pain.line(x1, y1, x1, y0, Color_added_space);
300                 } else if (params_.kind == InsetSpaceParams::HFILL_PROTECTED) {
301                         pi.pain.line(x0, y1, x0, y0, Color_latex);
302                         pi.pain.line(x0, y2, x1, y2, Color_latex,
303                                 frontend::Painter::line_onoffdash);
304                         pi.pain.line(x1, y1, x1, y0, Color_latex);
305                 } else if (params_.kind == InsetSpaceParams::DOTFILL) {
306                         pi.pain.line(x0, y1, x0, y0, Color_special);
307                         pi.pain.line(x0, y0, x1, y0, Color_special,
308                                 frontend::Painter::line_onoffdash);
309                         pi.pain.line(x1, y1, x1, y0, Color_special);
310                 } else if (params_.kind == InsetSpaceParams::HRULEFILL) {
311                         pi.pain.line(x0, y1, x0, y0, Color_special);
312                         pi.pain.line(x0, y0, x1, y0, Color_special);
313                         pi.pain.line(x1, y1, x1, y0, Color_special);
314                 } else if (params_.kind == InsetSpaceParams::LEFTARROWFILL) {
315                         pi.pain.line(x2, y1 + 1 , x0 + 1, y2, Color_special);
316                         pi.pain.line(x0 + 1, y2 + 1 , x2, y0, Color_special);
317                         pi.pain.line(x0, y2 , x1, y2, Color_special);
318                 } else if (params_.kind == InsetSpaceParams::RIGHTARROWFILL) {
319                         pi.pain.line(x3 + 1, y1 + 1 , x1, y2, Color_special);
320                         pi.pain.line(x1, y2 + 1 , x3 + 1, y0, Color_special);
321                         pi.pain.line(x0, y2 , x1, y2, Color_special);
322                 } else if (params_.kind == InsetSpaceParams::UPBRACEFILL) {
323                         pi.pain.line(x0 + 1, y1 + 1 , x2, y2, Color_special);
324                         pi.pain.line(x2, y2 , xml, y2, Color_special);
325                         pi.pain.line(xml + 1, y2 + 1 , xm, y0, Color_special);
326                         pi.pain.line(xm + 1, y0 , xmr, y2 + 1, Color_special);
327                         pi.pain.line(xmr, y2 , x3, y2, Color_special);
328                         pi.pain.line(x3 + 1, y2 , x1, y1 + 1, Color_special);
329                 } else if (params_.kind == InsetSpaceParams::DOWNBRACEFILL) {
330                         pi.pain.line(x0 + 1, y0 , x2, y2 + 1, Color_special);
331                         pi.pain.line(x2, y2 , xml, y2, Color_special);
332                         pi.pain.line(xml + 1, y2 , xm, y1 + 1, Color_special);
333                         pi.pain.line(xm + 1, y1 + 1 , xmr, y2, Color_special);
334                         pi.pain.line(xmr, y2 , x3, y2, Color_special);
335                         pi.pain.line(x3 + 1, y2 + 1 , x1, y0, Color_special);
336                 } else if (params_.kind == InsetSpaceParams::CUSTOM) {
337                         pi.pain.line(x0, y1 + 1 , x2 + 1, y2, Color_special);
338                         pi.pain.line(x2 + 1, y2 + 1 , x0, y0, Color_special);
339                         pi.pain.line(x1 + 1, y1 + 1 , x3, y2, Color_special);
340                         pi.pain.line(x3, y2 + 1 , x1 + 1, y0, Color_special);
341                         pi.pain.line(x2, y2 , x3, y2, Color_special);
342                 } else if (params_.kind == InsetSpaceParams::CUSTOM_PROTECTED) {
343                         pi.pain.line(x0, y1 + 1 , x2 + 1, y2, Color_latex);
344                         pi.pain.line(x2 + 1, y2 + 1 , x0, y0, Color_latex);
345                         pi.pain.line(x1 + 1, y1 + 1 , x3, y2, Color_latex);
346                         pi.pain.line(x3, y2 + 1 , x1 + 1, y0, Color_latex);
347                         pi.pain.line(x2, y2 , x3, y2, Color_latex);
348                 }
349                 return;
350         }
351
352         int const w = dim.wid;
353         int const h = theFontMetrics(pi.base.font).ascent('x');
354         int xp[4], yp[4];
355
356         xp[0] = x;
357         yp[0] = y - max(h / 4, 1);
358         if (params_.kind == InsetSpaceParams::NORMAL ||
359             params_.kind == InsetSpaceParams::PROTECTED) {
360                 xp[1] = x;     yp[1] = y;
361                 xp[2] = x + w; yp[2] = y;
362         } else {
363                 xp[1] = x;     yp[1] = y + max(h / 4, 1);
364                 xp[2] = x + w; yp[2] = y + max(h / 4, 1);
365         }
366         xp[3] = x + w;
367         yp[3] = y - max(h / 4, 1);
368
369         if (params_.kind == InsetSpaceParams::PROTECTED ||
370             params_.kind == InsetSpaceParams::ENSPACE ||
371             params_.kind == InsetSpaceParams::NEGTHIN ||
372             params_.kind == InsetSpaceParams::NEGMEDIUM ||
373             params_.kind == InsetSpaceParams::NEGTHICK ||
374             params_.kind == InsetSpaceParams::CUSTOM_PROTECTED)
375                 pi.pain.lines(xp, yp, 4, Color_latex);
376         else
377                 pi.pain.lines(xp, yp, 4, Color_special);
378 }
379
380
381 void InsetSpaceParams::write(ostream & os) const
382 {
383         string command;
384         switch (kind) {
385         case InsetSpaceParams::NORMAL:
386                 os << "\\space{}";
387                 break;
388         case InsetSpaceParams::PROTECTED:
389                 os <<  "~";
390                 break;
391         case InsetSpaceParams::THIN:
392                 os <<  "\\thinspace{}";
393                 break;
394         case InsetSpaceParams::MEDIUM:
395                 os <<  "\\medspace{}";
396                 break;
397         case InsetSpaceParams::THICK:
398                 os <<  "\\thickspace{}";
399                 break;
400         case InsetSpaceParams::QUAD:
401                 os <<  "\\quad{}";
402                 break;
403         case InsetSpaceParams::QQUAD:
404                 os <<  "\\qquad{}";
405                 break;
406         case InsetSpaceParams::ENSPACE:
407                 os <<  "\\enspace{}";
408                 break;
409         case InsetSpaceParams::ENSKIP:
410                 os <<  "\\enskip{}";
411                 break;
412         case InsetSpaceParams::NEGTHIN:
413                 os <<  "\\negthinspace{}";
414                 break;
415         case InsetSpaceParams::NEGMEDIUM:
416                 os <<  "\\negmedspace{}";
417                 break;
418         case InsetSpaceParams::NEGTHICK:
419                 os <<  "\\negthickspace{}";
420                 break;
421         case InsetSpaceParams::HFILL:
422                 os <<  "\\hfill{}";
423                 break;
424         case InsetSpaceParams::HFILL_PROTECTED:
425                 os <<  "\\hspace*{\\fill}";
426                 break;
427         case InsetSpaceParams::DOTFILL:
428                 os <<  "\\dotfill{}";
429                 break;
430         case InsetSpaceParams::HRULEFILL:
431                 os <<  "\\hrulefill{}";
432                 break;
433         case InsetSpaceParams::LEFTARROWFILL:
434                 os <<  "\\leftarrowfill{}";
435                 break;
436         case InsetSpaceParams::RIGHTARROWFILL:
437                 os <<  "\\rightarrowfill{}";
438                 break;
439         case InsetSpaceParams::UPBRACEFILL:
440                 os <<  "\\upbracefill{}";
441                 break;
442         case InsetSpaceParams::DOWNBRACEFILL:
443                 os <<  "\\downbracefill{}";
444                 break;
445         case InsetSpaceParams::CUSTOM:
446                 os <<  "\\hspace{}";
447                 break;
448         case InsetSpaceParams::CUSTOM_PROTECTED:
449                 os <<  "\\hspace*{}";
450                 break;
451         }
452         
453         if (!length.empty())
454                 os << "\n\\length " << length.asString();
455 }
456
457
458 void InsetSpaceParams::read(Lexer & lex)
459 {
460         lex.setContext("InsetSpaceParams::read");
461         string command;
462         lex >> command;
463
464         // The tests for math might be disabled after a file format change
465         if (command == "\\space{}")
466                 kind = InsetSpaceParams::NORMAL;
467         else if (command == "~")
468                 kind = InsetSpaceParams::PROTECTED;
469         else if (command == "\\thinspace{}")
470                 kind = InsetSpaceParams::THIN;
471         else if (math && command == "\\medspace{}")
472                 kind = InsetSpaceParams::MEDIUM;
473         else if (math && command == "\\thickspace{}")
474                 kind = InsetSpaceParams::THICK;
475         else if (command == "\\quad{}")
476                 kind = InsetSpaceParams::QUAD;
477         else if (command == "\\qquad{}")
478                 kind = InsetSpaceParams::QQUAD;
479         else if (command == "\\enspace{}")
480                 kind = InsetSpaceParams::ENSPACE;
481         else if (command == "\\enskip{}")
482                 kind = InsetSpaceParams::ENSKIP;
483         else if (command == "\\negthinspace{}")
484                 kind = InsetSpaceParams::NEGTHIN;
485         else if (math && command == "\\negmedspace{}")
486                 kind = InsetSpaceParams::NEGMEDIUM;
487         else if (math && command == "\\negthickspace{}")
488                 kind = InsetSpaceParams::NEGTHICK;
489         else if (command == "\\hfill{}")
490                 kind = InsetSpaceParams::HFILL;
491         else if (command == "\\hspace*{\\fill}")
492                 kind = InsetSpaceParams::HFILL_PROTECTED;
493         else if (command == "\\dotfill{}")
494                 kind = InsetSpaceParams::DOTFILL;
495         else if (command == "\\hrulefill{}")
496                 kind = InsetSpaceParams::HRULEFILL;
497         else if (command == "\\hspace{}")
498                 kind = InsetSpaceParams::CUSTOM;
499         else if (command == "\\leftarrowfill{}")
500                 kind = InsetSpaceParams::LEFTARROWFILL;
501         else if (command == "\\rightarrowfill{}")
502                 kind = InsetSpaceParams::RIGHTARROWFILL;
503         else if (command == "\\upbracefill{}")
504                 kind = InsetSpaceParams::UPBRACEFILL;
505         else if (command == "\\downbracefill{}")
506                 kind = InsetSpaceParams::DOWNBRACEFILL;
507         else if (command == "\\hspace*{}")
508                 kind = InsetSpaceParams::CUSTOM_PROTECTED;
509         else
510                 lex.printError("InsetSpace: Unknown kind: `$$Token'");
511
512         if (lex.checkFor("\\length"))
513                 lex >> length;
514 }
515
516
517 void InsetSpace::write(ostream & os) const
518 {
519         os << "space ";
520         params_.write(os);
521 }
522
523
524 void InsetSpace::read(Lexer & lex)
525 {
526         params_.read(lex);
527         lex >> "\\end_inset";
528 }
529
530
531 int InsetSpace::latex(odocstream & os, OutputParams const & runparams) const
532 {
533         switch (params_.kind) {
534         case InsetSpaceParams::NORMAL:
535                 os << (runparams.free_spacing ? " " : "\\ ");
536                 break;
537         case InsetSpaceParams::PROTECTED:
538                 os << (runparams.free_spacing ? ' ' : '~');
539                 break;
540         case InsetSpaceParams::THIN:
541                 os << (runparams.free_spacing ? " " : "\\,");
542                 break;
543         case InsetSpaceParams::MEDIUM:
544                 os << (runparams.free_spacing ? " " : "\\:");
545                 break;
546         case InsetSpaceParams::THICK:
547                 os << (runparams.free_spacing ? " " : "\\;");
548                 break;
549         case InsetSpaceParams::QUAD:
550                 os << (runparams.free_spacing ? " " : "\\quad{}");
551                 break;
552         case InsetSpaceParams::QQUAD:
553                 os << (runparams.free_spacing ? " " : "\\qquad{}");
554                 break;
555         case InsetSpaceParams::ENSPACE:
556                 os << (runparams.free_spacing ? " " : "\\enspace{}");
557                 break;
558         case InsetSpaceParams::ENSKIP:
559                 os << (runparams.free_spacing ? " " : "\\enskip{}");
560                 break;
561         case InsetSpaceParams::NEGTHIN:
562                 os << (runparams.free_spacing ? " " : "\\negthinspace{}");
563                 break;
564         case InsetSpaceParams::NEGMEDIUM:
565                 os << (runparams.free_spacing ? " " : "\\negmedspace{}");
566                 break;
567         case InsetSpaceParams::NEGTHICK:
568                 os << (runparams.free_spacing ? " " : "\\negthickspace{}");
569                 break;
570         case InsetSpaceParams::HFILL:
571                 os << (runparams.free_spacing ? " " : "\\hfill{}");
572                 break;
573         case InsetSpaceParams::HFILL_PROTECTED:
574                 os << (runparams.free_spacing ? " " : "\\hspace*{\\fill}");
575                 break;
576         case InsetSpaceParams::DOTFILL:
577                 os << (runparams.free_spacing ? " " : "\\dotfill{}");
578                 break;
579         case InsetSpaceParams::HRULEFILL:
580                 os << (runparams.free_spacing ? " " : "\\hrulefill{}");
581                 break;
582         case InsetSpaceParams::LEFTARROWFILL:
583                 os << (runparams.free_spacing ? " " : "\\leftarrowfill{}");
584                 break;
585         case InsetSpaceParams::RIGHTARROWFILL:
586                 os << (runparams.free_spacing ? " " : "\\rightarrowfill{}");
587                 break;
588         case InsetSpaceParams::UPBRACEFILL:
589                 os << (runparams.free_spacing ? " " : "\\upbracefill{}");
590                 break;
591         case InsetSpaceParams::DOWNBRACEFILL:
592                 os << (runparams.free_spacing ? " " : "\\downbracefill{}");
593                 break;
594         case InsetSpaceParams::CUSTOM:
595                 if (runparams.free_spacing)
596                         os << " ";
597                 else
598                         os << "\\hspace{" << from_ascii(params_.length.asLatexString()) << "}";
599                 break;
600         case InsetSpaceParams::CUSTOM_PROTECTED:
601                 if (runparams.free_spacing)
602                         os << " ";
603                 else
604                         os << "\\hspace*{" << from_ascii(params_.length.asLatexString()) << "}";
605                 break;
606         }
607         return 0;
608 }
609
610
611 int InsetSpace::plaintext(odocstream & os, OutputParams const &) const
612 {
613         switch (params_.kind) {
614         case InsetSpaceParams::HFILL:
615         case InsetSpaceParams::HFILL_PROTECTED:
616                 os << "     ";
617                 return 5;
618         case InsetSpaceParams::DOTFILL:
619                 os << ".....";
620                 return 5;
621         case InsetSpaceParams::HRULEFILL:
622                 os << "_____";
623                 return 5;
624         case InsetSpaceParams::LEFTARROWFILL:
625                 os << "<----";
626                 return 5;
627         case InsetSpaceParams::RIGHTARROWFILL:
628                 os << "---->";
629                 return 5;
630         case InsetSpaceParams::UPBRACEFILL:
631                 os << "\\-v-/";
632                 return 5;
633         case InsetSpaceParams::DOWNBRACEFILL:
634                 os << "/-^-\\";
635                 return 5;
636         default:
637                 os << ' ';
638                 return 1;
639         }
640 }
641
642
643 int InsetSpace::docbook(odocstream & os, OutputParams const &) const
644 {
645         switch (params_.kind) {
646         case InsetSpaceParams::NORMAL:
647         case InsetSpaceParams::QUAD:
648         case InsetSpaceParams::QQUAD:
649         case InsetSpaceParams::ENSKIP:
650                 os << " ";
651                 break;
652         case InsetSpaceParams::PROTECTED:
653         case InsetSpaceParams::ENSPACE:
654         case InsetSpaceParams::THIN:
655         case InsetSpaceParams::MEDIUM:
656         case InsetSpaceParams::THICK:
657         case InsetSpaceParams::NEGTHIN:
658         case InsetSpaceParams::NEGMEDIUM:
659         case InsetSpaceParams::NEGTHICK:
660                 os << "&nbsp;";
661                 break;
662         case InsetSpaceParams::HFILL:
663         case InsetSpaceParams::HFILL_PROTECTED:
664                 os << '\n';
665         case InsetSpaceParams::DOTFILL:
666                 // FIXME
667                 os << '\n';
668         case InsetSpaceParams::HRULEFILL:
669                 // FIXME
670                 os << '\n';
671         case InsetSpaceParams::LEFTARROWFILL:
672         case InsetSpaceParams::RIGHTARROWFILL:
673         case InsetSpaceParams::UPBRACEFILL:
674         case InsetSpaceParams::DOWNBRACEFILL:
675         case InsetSpaceParams::CUSTOM:
676         case InsetSpaceParams::CUSTOM_PROTECTED:
677                 // FIXME
678                 os << '\n';
679         }
680         return 0;
681 }
682
683
684 void InsetSpace::validate(LaTeXFeatures & features) const
685 {
686         if (params_.kind == InsetSpaceParams::NEGMEDIUM ||
687             params_.kind == InsetSpaceParams::NEGTHICK) 
688                 features.require("amsmath");
689 }
690
691
692 void InsetSpace::tocString(odocstream & os) const
693 {
694         plaintext(os, OutputParams(0));
695 }
696
697
698 bool InsetSpace::isStretchableSpace() const
699 {
700         return params_.kind == InsetSpaceParams::HFILL
701                 || params_.kind == InsetSpaceParams::HFILL_PROTECTED
702                 || params_.kind == InsetSpaceParams::DOTFILL
703                 || params_.kind == InsetSpaceParams::HRULEFILL
704                 || params_.kind == InsetSpaceParams::LEFTARROWFILL
705                 || params_.kind == InsetSpaceParams::RIGHTARROWFILL
706                 || params_.kind == InsetSpaceParams::UPBRACEFILL
707                 || params_.kind == InsetSpaceParams::DOWNBRACEFILL;
708 }
709
710
711 docstring InsetSpace::contextMenu(BufferView const &, int, int) const
712 {
713         return from_ascii("context-space");
714 }
715
716
717 void InsetSpace::string2params(string const & in, InsetSpaceParams & params)
718 {
719         params = InsetSpaceParams();
720         if (in.empty())
721                 return;
722
723         istringstream data(in);
724         Lexer lex;
725         lex.setStream(data);
726         lex.setContext("InsetSpace::string2params");
727         lex.next();
728         string const name = lex.getString();
729         if (name == "mathspace")
730                 params.math = true;
731         else {
732                 params.math = false;
733                 LASSERT(name == "space", /**/);
734         }
735
736         // There are cases, such as when we are called via getStatus() from
737         // Dialog::canApply(), where we are just called with "space" rather
738         // than a full "space \type{}\n\\end_inset".
739         if (lex.isOK())
740                 params.read(lex);
741 }
742
743
744 string InsetSpace::params2string(InsetSpaceParams const & params)
745 {
746         ostringstream data;
747         if (params.math)
748                 data << "math";
749         data << "space" << ' ';
750         params.write(data);
751         return data.str();
752 }
753
754
755 } // namespace lyx