3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup Nielsen
7 * \author Jean-Marc Lasgouttes
8 * \author Lars Gullik Bjønnes
9 * \author Jürgen Spitzmüller
11 * Full author contact details are available in file CREDITS.
16 #include "InsetSpace.h"
18 #include "BufferView.h"
20 #include "Dimension.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
23 #include "LaTeXFeatures.h"
26 #include "MetricsInfo.h"
27 #include "OutputParams.h"
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"
35 #include "frontends/Application.h"
36 #include "frontends/FontMetrics.h"
37 #include "frontends/Painter.h"
44 InsetSpace::InsetSpace(InsetSpaceParams const & params)
49 InsetSpaceParams::Kind InsetSpace::kind() const
55 Length InsetSpace::length() const
57 return params_.length;
61 InsetSpace::~InsetSpace()
63 hideDialogs("space", this);
67 docstring InsetSpace::toolTip(BufferView const &, int, int) const
70 switch (params_.kind) {
71 case InsetSpaceParams::NORMAL:
72 message = _("Interword Space");
74 case InsetSpaceParams::PROTECTED:
75 message = _("Protected Space");
77 case InsetSpaceParams::THIN:
78 message = _("Thin Space");
80 case InsetSpaceParams::MEDIUM:
81 message = _("Medium Space");
83 case InsetSpaceParams::THICK:
84 message = _("Thick Space");
86 case InsetSpaceParams::QUAD:
87 message = _("Quad Space");
89 case InsetSpaceParams::QQUAD:
90 message = _("QQuad Space");
92 case InsetSpaceParams::ENSPACE:
93 message = _("Enspace");
95 case InsetSpaceParams::ENSKIP:
96 message = _("Enskip");
98 case InsetSpaceParams::NEGTHIN:
99 message = _("Negative Thin Space");
101 case InsetSpaceParams::NEGMEDIUM:
102 message = _("Negative Medium Space");
104 case InsetSpaceParams::NEGTHICK:
105 message = _("Negative Thick Space");
107 case InsetSpaceParams::HFILL:
108 message = _("Horizontal Fill");
110 case InsetSpaceParams::HFILL_PROTECTED:
111 message = _("Protected Horizontal Fill");
113 case InsetSpaceParams::DOTFILL:
114 message = _("Horizontal Fill (Dots)");
116 case InsetSpaceParams::HRULEFILL:
117 message = _("Horizontal Fill (Rule)");
119 case InsetSpaceParams::LEFTARROWFILL:
120 message = _("Horizontal Fill (Left Arrow)");
122 case InsetSpaceParams::RIGHTARROWFILL:
123 message = _("Horizontal Fill (Right Arrow)");
125 case InsetSpaceParams::UPBRACEFILL:
126 message = _("Horizontal Fill (Up Brace)");
128 case InsetSpaceParams::DOWNBRACEFILL:
129 message = _("Horizontal Fill (Down Brace)");
131 case InsetSpaceParams::CUSTOM:
132 message = support::bformat(_("Horizontal Space (%1$s)"),
133 params_.length.asDocstring());
135 case InsetSpaceParams::CUSTOM_PROTECTED:
136 message = support::bformat(_("Protected Horizontal Space (%1$s)"),
137 params_.length.asDocstring());
144 void InsetSpace::doDispatch(Cursor & cur, FuncRequest & cmd)
146 switch (cmd.action) {
148 case LFUN_INSET_MODIFY:
149 string2params(to_utf8(cmd.argument()), params_);
152 case LFUN_INSET_DIALOG_UPDATE:
153 cur.bv().updateDialog("space", params2string(params()));
157 Inset::doDispatch(cur, cmd);
163 bool InsetSpace::getStatus(Cursor & cur, FuncRequest const & cmd,
164 FuncStatus & status) const
166 switch (cmd.action) {
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);
175 case LFUN_INSET_SETTINGS:
176 case LFUN_INSET_DIALOG_UPDATE:
177 status.setEnabled(true);
180 return Inset::getStatus(cur, cmd, status);
185 bool InsetSpace::showInsetDialog(BufferView * bv) const
187 bv->showDialog("space", params2string(params()),
188 const_cast<InsetSpace *>(this));
194 int const arrow_size = 8;
198 void InsetSpace::metrics(MetricsInfo & mi, Dimension & dim) const
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);
207 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
208 dim.asc = fm.maxAscent();
209 dim.des = fm.maxDescent();
211 switch (params_.kind) {
212 case InsetSpaceParams::THIN:
213 case InsetSpaceParams::NEGTHIN:
214 dim.wid = fm.width(char_type('M')) / 6;
216 case InsetSpaceParams::MEDIUM:
217 case InsetSpaceParams::NEGMEDIUM:
218 dim.wid = fm.width(char_type('M')) / 4;
220 case InsetSpaceParams::THICK:
221 case InsetSpaceParams::NEGTHICK:
222 dim.wid = fm.width(char_type('M')) / 2;
224 case InsetSpaceParams::PROTECTED:
225 case InsetSpaceParams::NORMAL:
226 dim.wid = fm.width(char_type(' '));
228 case InsetSpaceParams::QUAD:
229 dim.wid = fm.width(char_type('M'));
231 case InsetSpaceParams::QQUAD:
232 dim.wid = 2 * fm.width(char_type('M'));
234 case InsetSpaceParams::ENSPACE:
235 case InsetSpaceParams::ENSKIP:
236 dim.wid = int(0.5 * fm.width(char_type('M')));
238 case InsetSpaceParams::CUSTOM:
239 case InsetSpaceParams::CUSTOM_PROTECTED: {
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));
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:
258 // Cache the inset dimension.
259 setDimCache(mi, dim);
263 void InsetSpace::draw(PainterInfo & pi, int x, int y) const
265 Dimension const dim = dimension(*pi.base.bv);
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;
279 // Two tests for very narrow insets
280 if (xoffset > x1 - x0
281 && (params_.kind == InsetSpaceParams::LEFTARROWFILL
282 || params_.kind == InsetSpaceParams::RIGHTARROWFILL))
284 if (xoffset * 6 > (x1 - x0)
285 && (params_.kind == InsetSpaceParams::UPBRACEFILL
286 || params_.kind == InsetSpaceParams::DOWNBRACEFILL))
287 xoffset = (x1 - x0) / 6;
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;
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);
352 int const w = dim.wid;
353 int const h = theFontMetrics(pi.base.font).ascent('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;
363 xp[1] = x; yp[1] = y + max(h / 4, 1);
364 xp[2] = x + w; yp[2] = y + max(h / 4, 1);
367 yp[3] = y - max(h / 4, 1);
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);
377 pi.pain.lines(xp, yp, 4, Color_special);
381 void InsetSpaceParams::write(ostream & os) const
385 case InsetSpaceParams::NORMAL:
388 case InsetSpaceParams::PROTECTED:
391 case InsetSpaceParams::THIN:
392 os << "\\thinspace{}";
394 case InsetSpaceParams::MEDIUM:
395 os << "\\medspace{}";
397 case InsetSpaceParams::THICK:
398 os << "\\thickspace{}";
400 case InsetSpaceParams::QUAD:
403 case InsetSpaceParams::QQUAD:
406 case InsetSpaceParams::ENSPACE:
409 case InsetSpaceParams::ENSKIP:
412 case InsetSpaceParams::NEGTHIN:
413 os << "\\negthinspace{}";
415 case InsetSpaceParams::NEGMEDIUM:
416 os << "\\negmedspace{}";
418 case InsetSpaceParams::NEGTHICK:
419 os << "\\negthickspace{}";
421 case InsetSpaceParams::HFILL:
424 case InsetSpaceParams::HFILL_PROTECTED:
425 os << "\\hspace*{\\fill}";
427 case InsetSpaceParams::DOTFILL:
430 case InsetSpaceParams::HRULEFILL:
431 os << "\\hrulefill{}";
433 case InsetSpaceParams::LEFTARROWFILL:
434 os << "\\leftarrowfill{}";
436 case InsetSpaceParams::RIGHTARROWFILL:
437 os << "\\rightarrowfill{}";
439 case InsetSpaceParams::UPBRACEFILL:
440 os << "\\upbracefill{}";
442 case InsetSpaceParams::DOWNBRACEFILL:
443 os << "\\downbracefill{}";
445 case InsetSpaceParams::CUSTOM:
448 case InsetSpaceParams::CUSTOM_PROTECTED:
454 os << "\n\\length " << length.asString();
458 void InsetSpaceParams::read(Lexer & lex)
460 lex.setContext("InsetSpaceParams::read");
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;
510 lex.printError("InsetSpace: Unknown kind: `$$Token'");
512 if (lex.checkFor("\\length"))
517 void InsetSpace::write(ostream & os) const
524 void InsetSpace::read(Lexer & lex)
527 lex >> "\\end_inset";
531 int InsetSpace::latex(odocstream & os, OutputParams const & runparams) const
533 switch (params_.kind) {
534 case InsetSpaceParams::NORMAL:
535 os << (runparams.free_spacing ? " " : "\\ ");
537 case InsetSpaceParams::PROTECTED:
538 os << (runparams.free_spacing ? ' ' : '~');
540 case InsetSpaceParams::THIN:
541 os << (runparams.free_spacing ? " " : "\\,");
543 case InsetSpaceParams::MEDIUM:
544 os << (runparams.free_spacing ? " " : "\\:");
546 case InsetSpaceParams::THICK:
547 os << (runparams.free_spacing ? " " : "\\;");
549 case InsetSpaceParams::QUAD:
550 os << (runparams.free_spacing ? " " : "\\quad{}");
552 case InsetSpaceParams::QQUAD:
553 os << (runparams.free_spacing ? " " : "\\qquad{}");
555 case InsetSpaceParams::ENSPACE:
556 os << (runparams.free_spacing ? " " : "\\enspace{}");
558 case InsetSpaceParams::ENSKIP:
559 os << (runparams.free_spacing ? " " : "\\enskip{}");
561 case InsetSpaceParams::NEGTHIN:
562 os << (runparams.free_spacing ? " " : "\\negthinspace{}");
564 case InsetSpaceParams::NEGMEDIUM:
565 os << (runparams.free_spacing ? " " : "\\negmedspace{}");
567 case InsetSpaceParams::NEGTHICK:
568 os << (runparams.free_spacing ? " " : "\\negthickspace{}");
570 case InsetSpaceParams::HFILL:
571 os << (runparams.free_spacing ? " " : "\\hfill{}");
573 case InsetSpaceParams::HFILL_PROTECTED:
574 os << (runparams.free_spacing ? " " : "\\hspace*{\\fill}");
576 case InsetSpaceParams::DOTFILL:
577 os << (runparams.free_spacing ? " " : "\\dotfill{}");
579 case InsetSpaceParams::HRULEFILL:
580 os << (runparams.free_spacing ? " " : "\\hrulefill{}");
582 case InsetSpaceParams::LEFTARROWFILL:
583 os << (runparams.free_spacing ? " " : "\\leftarrowfill{}");
585 case InsetSpaceParams::RIGHTARROWFILL:
586 os << (runparams.free_spacing ? " " : "\\rightarrowfill{}");
588 case InsetSpaceParams::UPBRACEFILL:
589 os << (runparams.free_spacing ? " " : "\\upbracefill{}");
591 case InsetSpaceParams::DOWNBRACEFILL:
592 os << (runparams.free_spacing ? " " : "\\downbracefill{}");
594 case InsetSpaceParams::CUSTOM:
595 if (runparams.free_spacing)
598 os << "\\hspace{" << from_ascii(params_.length.asLatexString()) << "}";
600 case InsetSpaceParams::CUSTOM_PROTECTED:
601 if (runparams.free_spacing)
604 os << "\\hspace*{" << from_ascii(params_.length.asLatexString()) << "}";
611 int InsetSpace::plaintext(odocstream & os, OutputParams const &) const
613 switch (params_.kind) {
614 case InsetSpaceParams::HFILL:
615 case InsetSpaceParams::HFILL_PROTECTED:
618 case InsetSpaceParams::DOTFILL:
621 case InsetSpaceParams::HRULEFILL:
624 case InsetSpaceParams::LEFTARROWFILL:
627 case InsetSpaceParams::RIGHTARROWFILL:
630 case InsetSpaceParams::UPBRACEFILL:
633 case InsetSpaceParams::DOWNBRACEFILL:
643 int InsetSpace::docbook(odocstream & os, OutputParams const &) const
645 switch (params_.kind) {
646 case InsetSpaceParams::NORMAL:
647 case InsetSpaceParams::QUAD:
648 case InsetSpaceParams::QQUAD:
649 case InsetSpaceParams::ENSKIP:
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:
662 case InsetSpaceParams::HFILL:
663 case InsetSpaceParams::HFILL_PROTECTED:
665 case InsetSpaceParams::DOTFILL:
668 case InsetSpaceParams::HRULEFILL:
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:
684 void InsetSpace::validate(LaTeXFeatures & features) const
686 if (params_.kind == InsetSpaceParams::NEGMEDIUM ||
687 params_.kind == InsetSpaceParams::NEGTHICK)
688 features.require("amsmath");
692 void InsetSpace::tocString(odocstream & os) const
694 plaintext(os, OutputParams(0));
698 bool InsetSpace::isStretchableSpace() const
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;
711 docstring InsetSpace::contextMenu(BufferView const &, int, int) const
713 return from_ascii("context-space");
717 void InsetSpace::string2params(string const & in, InsetSpaceParams & params)
719 params = InsetSpaceParams();
723 istringstream data(in);
726 lex.setContext("InsetSpace::string2params");
728 string const name = lex.getString();
729 if (name == "mathspace")
733 LASSERT(name == "space", /**/);
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".
744 string InsetSpace::params2string(InsetSpaceParams const & params)
749 data << "space" << ' ';