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 Mwedium 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()));
156 case LFUN_MOUSE_RELEASE:
157 if (!cur.selection() && cmd.button() == mouse_button::button1)
158 cur.bv().showDialog("space", params2string(params()), this);
162 Inset::doDispatch(cur, cmd);
168 bool InsetSpace::getStatus(Cursor & cur, FuncRequest const & cmd,
169 FuncStatus & status) const
171 switch (cmd.action) {
173 case LFUN_INSET_MODIFY:
174 if (cmd.getArg(0) == "space") {
175 InsetSpaceParams params;
176 string2params(to_utf8(cmd.argument()), params);
177 status.setOnOff(params_.kind == params.kind);
180 case LFUN_INSET_DIALOG_UPDATE:
181 status.setEnabled(true);
184 return Inset::getStatus(cur, cmd, status);
189 void InsetSpace::edit(Cursor & cur, bool, EntryDirection)
191 cur.bv().showDialog("space", params2string(params()), this);
196 int const arrow_size = 8;
200 void InsetSpace::metrics(MetricsInfo & mi, Dimension & dim) const
202 if (isStretchableSpace()) {
203 // The metrics for this kinds are calculated externally in
204 // \c TextMetrics::computeRowMetrics. Those are dummy value:
205 dim = Dimension(10, 10, 10);
209 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
210 dim.asc = fm.maxAscent();
211 dim.des = fm.maxDescent();
213 switch (params_.kind) {
214 case InsetSpaceParams::THIN:
215 case InsetSpaceParams::NEGTHIN:
216 dim.wid = fm.width(char_type('M')) / 6;
218 case InsetSpaceParams::MEDIUM:
219 case InsetSpaceParams::NEGMEDIUM:
220 dim.wid = fm.width(char_type('M')) / 4;
222 case InsetSpaceParams::THICK:
223 case InsetSpaceParams::NEGTHICK:
224 dim.wid = fm.width(char_type('M')) / 2;
226 case InsetSpaceParams::PROTECTED:
227 case InsetSpaceParams::NORMAL:
228 dim.wid = fm.width(char_type(' '));
230 case InsetSpaceParams::QUAD:
231 dim.wid = fm.width(char_type('M'));
233 case InsetSpaceParams::QQUAD:
234 dim.wid = 2 * fm.width(char_type('M'));
236 case InsetSpaceParams::ENSPACE:
237 case InsetSpaceParams::ENSKIP:
238 dim.wid = int(0.5 * fm.width(char_type('M')));
240 case InsetSpaceParams::CUSTOM:
241 case InsetSpaceParams::CUSTOM_PROTECTED: {
243 params_.length.inPixels(mi.base.textwidth,
244 fm.width(char_type('M')));
245 int const minw = (w < 0) ? 3 * arrow_size : 4;
246 dim.wid = max(minw, abs(w));
249 case InsetSpaceParams::HFILL:
250 case InsetSpaceParams::HFILL_PROTECTED:
251 case InsetSpaceParams::DOTFILL:
252 case InsetSpaceParams::HRULEFILL:
253 case InsetSpaceParams::LEFTARROWFILL:
254 case InsetSpaceParams::RIGHTARROWFILL:
255 case InsetSpaceParams::UPBRACEFILL:
256 case InsetSpaceParams::DOWNBRACEFILL:
260 // Cache the inset dimension.
261 setDimCache(mi, dim);
265 void InsetSpace::draw(PainterInfo & pi, int x, int y) const
267 Dimension const dim = dimension(*pi.base.bv);
269 if (isStretchableSpace() || params_.length.value() < 0) {
270 int const asc = theFontMetrics(pi.base.font).ascent('M');
271 int const desc = theFontMetrics(pi.base.font).descent('M');
272 // Pixel height divisible by 2 for prettier fill graphics:
273 int const oddheight = (asc ^ desc) & 1;
274 int const x0 = x + 1;
275 int const x1 = x + dim.wid - 2;
276 int const y0 = y + desc - 1;
277 int const y1 = y - asc + oddheight - 1;
278 int const y2 = (y0 + y1) / 2;
279 int xoffset = (y0 - y1) / 2;
281 // Two tests for very narrow insets
282 if (xoffset > x1 - x0
283 && (params_.kind == InsetSpaceParams::LEFTARROWFILL
284 || params_.kind == InsetSpaceParams::RIGHTARROWFILL))
286 if (xoffset * 6 > (x1 - x0)
287 && (params_.kind == InsetSpaceParams::UPBRACEFILL
288 || params_.kind == InsetSpaceParams::DOWNBRACEFILL))
289 xoffset = (x1 - x0) / 6;
291 int const x2 = x0 + xoffset;
292 int const x3 = x1 - xoffset;
293 int const xm = (x0 + x1) / 2;
294 int const xml = xm - xoffset;
295 int const xmr = xm + xoffset;
297 if (params_.kind == InsetSpaceParams::HFILL) {
298 pi.pain.line(x0, y1, x0, y0, Color_added_space);
299 pi.pain.line(x0, y2, x1, y2, Color_added_space,
300 frontend::Painter::line_onoffdash);
301 pi.pain.line(x1, y1, x1, y0, Color_added_space);
302 } else if (params_.kind == InsetSpaceParams::HFILL_PROTECTED) {
303 pi.pain.line(x0, y1, x0, y0, Color_latex);
304 pi.pain.line(x0, y2, x1, y2, Color_latex,
305 frontend::Painter::line_onoffdash);
306 pi.pain.line(x1, y1, x1, y0, Color_latex);
307 } else if (params_.kind == InsetSpaceParams::DOTFILL) {
308 pi.pain.line(x0, y1, x0, y0, Color_special);
309 pi.pain.line(x0, y0, x1, y0, Color_special,
310 frontend::Painter::line_onoffdash);
311 pi.pain.line(x1, y1, x1, y0, Color_special);
312 } else if (params_.kind == InsetSpaceParams::HRULEFILL) {
313 pi.pain.line(x0, y1, x0, y0, Color_special);
314 pi.pain.line(x0, y0, x1, y0, Color_special);
315 pi.pain.line(x1, y1, x1, y0, Color_special);
316 } else if (params_.kind == InsetSpaceParams::LEFTARROWFILL) {
317 pi.pain.line(x2, y1 + 1 , x0 + 1, y2, Color_special);
318 pi.pain.line(x0 + 1, y2 + 1 , x2, y0, Color_special);
319 pi.pain.line(x0, y2 , x1, y2, Color_special);
320 } else if (params_.kind == InsetSpaceParams::RIGHTARROWFILL) {
321 pi.pain.line(x3 + 1, y1 + 1 , x1, y2, Color_special);
322 pi.pain.line(x1, y2 + 1 , x3 + 1, y0, Color_special);
323 pi.pain.line(x0, y2 , x1, y2, Color_special);
324 } else if (params_.kind == InsetSpaceParams::UPBRACEFILL) {
325 pi.pain.line(x0 + 1, y1 + 1 , x2, y2, Color_special);
326 pi.pain.line(x2, y2 , xml, y2, Color_special);
327 pi.pain.line(xml + 1, y2 + 1 , xm, y0, Color_special);
328 pi.pain.line(xm + 1, y0 , xmr, y2 + 1, Color_special);
329 pi.pain.line(xmr, y2 , x3, y2, Color_special);
330 pi.pain.line(x3 + 1, y2 , x1, y1 + 1, Color_special);
331 } else if (params_.kind == InsetSpaceParams::DOWNBRACEFILL) {
332 pi.pain.line(x0 + 1, y0 , x2, y2 + 1, Color_special);
333 pi.pain.line(x2, y2 , xml, y2, Color_special);
334 pi.pain.line(xml + 1, y2 , xm, y1 + 1, Color_special);
335 pi.pain.line(xm + 1, y1 + 1 , xmr, y2, Color_special);
336 pi.pain.line(xmr, y2 , x3, y2, Color_special);
337 pi.pain.line(x3 + 1, y2 + 1 , x1, y0, Color_special);
338 } else if (params_.kind == InsetSpaceParams::CUSTOM) {
339 pi.pain.line(x0, y1 + 1 , x2 + 1, y2, Color_special);
340 pi.pain.line(x2 + 1, y2 + 1 , x0, y0, Color_special);
341 pi.pain.line(x1 + 1, y1 + 1 , x3, y2, Color_special);
342 pi.pain.line(x3, y2 + 1 , x1 + 1, y0, Color_special);
343 pi.pain.line(x2, y2 , x3, y2, Color_special);
344 } else if (params_.kind == InsetSpaceParams::CUSTOM_PROTECTED) {
345 pi.pain.line(x0, y1 + 1 , x2 + 1, y2, Color_latex);
346 pi.pain.line(x2 + 1, y2 + 1 , x0, y0, Color_latex);
347 pi.pain.line(x1 + 1, y1 + 1 , x3, y2, Color_latex);
348 pi.pain.line(x3, y2 + 1 , x1 + 1, y0, Color_latex);
349 pi.pain.line(x2, y2 , x3, y2, Color_latex);
354 int const w = dim.wid;
355 int const h = theFontMetrics(pi.base.font).ascent('x');
359 yp[0] = y - max(h / 4, 1);
360 if (params_.kind == InsetSpaceParams::NORMAL ||
361 params_.kind == InsetSpaceParams::PROTECTED) {
362 xp[1] = x; yp[1] = y;
363 xp[2] = x + w; yp[2] = y;
365 xp[1] = x; yp[1] = y + max(h / 4, 1);
366 xp[2] = x + w; yp[2] = y + max(h / 4, 1);
369 yp[3] = y - max(h / 4, 1);
371 if (params_.kind == InsetSpaceParams::PROTECTED ||
372 params_.kind == InsetSpaceParams::ENSPACE ||
373 params_.kind == InsetSpaceParams::NEGTHIN ||
374 params_.kind == InsetSpaceParams::NEGMEDIUM ||
375 params_.kind == InsetSpaceParams::NEGTHICK ||
376 params_.kind == InsetSpaceParams::CUSTOM_PROTECTED)
377 pi.pain.lines(xp, yp, 4, Color_latex);
379 pi.pain.lines(xp, yp, 4, Color_special);
383 void InsetSpaceParams::write(ostream & os) const
387 case InsetSpaceParams::NORMAL:
390 case InsetSpaceParams::PROTECTED:
393 case InsetSpaceParams::THIN:
394 os << "\\thinspace{}";
396 case InsetSpaceParams::MEDIUM:
397 os << "\\medspace{}";
399 case InsetSpaceParams::THICK:
400 os << "\\thickspace{}";
402 case InsetSpaceParams::QUAD:
405 case InsetSpaceParams::QQUAD:
408 case InsetSpaceParams::ENSPACE:
411 case InsetSpaceParams::ENSKIP:
414 case InsetSpaceParams::NEGTHIN:
415 os << "\\negthinspace{}";
417 case InsetSpaceParams::NEGMEDIUM:
418 os << "\\negmedspace{}";
420 case InsetSpaceParams::NEGTHICK:
421 os << "\\negthickspace{}";
423 case InsetSpaceParams::HFILL:
426 case InsetSpaceParams::HFILL_PROTECTED:
427 os << "\\hspace*{\\fill}";
429 case InsetSpaceParams::DOTFILL:
432 case InsetSpaceParams::HRULEFILL:
433 os << "\\hrulefill{}";
435 case InsetSpaceParams::LEFTARROWFILL:
436 os << "\\leftarrowfill{}";
438 case InsetSpaceParams::RIGHTARROWFILL:
439 os << "\\rightarrowfill{}";
441 case InsetSpaceParams::UPBRACEFILL:
442 os << "\\upbracefill{}";
444 case InsetSpaceParams::DOWNBRACEFILL:
445 os << "\\downbracefill{}";
447 case InsetSpaceParams::CUSTOM:
450 case InsetSpaceParams::CUSTOM_PROTECTED:
456 os << "\n\\length " << length.asString();
460 void InsetSpaceParams::read(Lexer & lex)
462 lex.setContext("InsetSpaceParams::read");
466 // The tests for math might be disabled after a file format change
467 if (command == "\\space{}")
468 kind = InsetSpaceParams::NORMAL;
469 else if (command == "~")
470 kind = InsetSpaceParams::PROTECTED;
471 else if (command == "\\thinspace{}")
472 kind = InsetSpaceParams::THIN;
473 else if (math && command == "\\medspace{}")
474 kind = InsetSpaceParams::MEDIUM;
475 else if (math && command == "\\thickspace{}")
476 kind = InsetSpaceParams::THICK;
477 else if (command == "\\quad{}")
478 kind = InsetSpaceParams::QUAD;
479 else if (command == "\\qquad{}")
480 kind = InsetSpaceParams::QQUAD;
481 else if (command == "\\enspace{}")
482 kind = InsetSpaceParams::ENSPACE;
483 else if (command == "\\enskip{}")
484 kind = InsetSpaceParams::ENSKIP;
485 else if (command == "\\negthinspace{}")
486 kind = InsetSpaceParams::NEGTHIN;
487 else if (math && command == "\\negmedspace{}")
488 kind = InsetSpaceParams::NEGMEDIUM;
489 else if (math && command == "\\negthickspace{}")
490 kind = InsetSpaceParams::NEGTHICK;
491 else if (command == "\\hfill{}")
492 kind = InsetSpaceParams::HFILL;
493 else if (command == "\\hspace*{\\fill}")
494 kind = InsetSpaceParams::HFILL_PROTECTED;
495 else if (command == "\\dotfill{}")
496 kind = InsetSpaceParams::DOTFILL;
497 else if (command == "\\hrulefill{}")
498 kind = InsetSpaceParams::HRULEFILL;
499 else if (command == "\\hspace{}")
500 kind = InsetSpaceParams::CUSTOM;
501 else if (command == "\\leftarrowfill{}")
502 kind = InsetSpaceParams::LEFTARROWFILL;
503 else if (command == "\\rightarrowfill{}")
504 kind = InsetSpaceParams::RIGHTARROWFILL;
505 else if (command == "\\upbracefill{}")
506 kind = InsetSpaceParams::UPBRACEFILL;
507 else if (command == "\\downbracefill{}")
508 kind = InsetSpaceParams::DOWNBRACEFILL;
509 else if (command == "\\hspace*{}")
510 kind = InsetSpaceParams::CUSTOM_PROTECTED;
512 lex.printError("InsetSpace: Unknown kind: `$$Token'");
514 if (lex.checkFor("\\length"))
519 void InsetSpace::write(ostream & os) const
526 void InsetSpace::read(Lexer & lex)
529 lex >> "\\end_inset";
533 int InsetSpace::latex(odocstream & os, OutputParams const & runparams) const
535 switch (params_.kind) {
536 case InsetSpaceParams::NORMAL:
537 os << (runparams.free_spacing ? " " : "\\ ");
539 case InsetSpaceParams::PROTECTED:
540 os << (runparams.free_spacing ? ' ' : '~');
542 case InsetSpaceParams::THIN:
543 os << (runparams.free_spacing ? " " : "\\,");
545 case InsetSpaceParams::MEDIUM:
546 os << (runparams.free_spacing ? " " : "\\:");
548 case InsetSpaceParams::THICK:
549 os << (runparams.free_spacing ? " " : "\\;");
551 case InsetSpaceParams::QUAD:
552 os << (runparams.free_spacing ? " " : "\\quad{}");
554 case InsetSpaceParams::QQUAD:
555 os << (runparams.free_spacing ? " " : "\\qquad{}");
557 case InsetSpaceParams::ENSPACE:
558 os << (runparams.free_spacing ? " " : "\\enspace{}");
560 case InsetSpaceParams::ENSKIP:
561 os << (runparams.free_spacing ? " " : "\\enskip{}");
563 case InsetSpaceParams::NEGTHIN:
564 os << (runparams.free_spacing ? " " : "\\negthinspace{}");
566 case InsetSpaceParams::NEGMEDIUM:
567 os << (runparams.free_spacing ? " " : "\\negmedspace{}");
569 case InsetSpaceParams::NEGTHICK:
570 os << (runparams.free_spacing ? " " : "\\negthickspace{}");
572 case InsetSpaceParams::HFILL:
573 os << (runparams.free_spacing ? " " : "\\hfill{}");
575 case InsetSpaceParams::HFILL_PROTECTED:
576 os << (runparams.free_spacing ? " " : "\\hspace*{\\fill}");
578 case InsetSpaceParams::DOTFILL:
579 os << (runparams.free_spacing ? " " : "\\dotfill{}");
581 case InsetSpaceParams::HRULEFILL:
582 os << (runparams.free_spacing ? " " : "\\hrulefill{}");
584 case InsetSpaceParams::LEFTARROWFILL:
585 os << (runparams.free_spacing ? " " : "\\leftarrowfill{}");
587 case InsetSpaceParams::RIGHTARROWFILL:
588 os << (runparams.free_spacing ? " " : "\\rightarrowfill{}");
590 case InsetSpaceParams::UPBRACEFILL:
591 os << (runparams.free_spacing ? " " : "\\upbracefill{}");
593 case InsetSpaceParams::DOWNBRACEFILL:
594 os << (runparams.free_spacing ? " " : "\\downbracefill{}");
596 case InsetSpaceParams::CUSTOM:
597 if (runparams.free_spacing)
600 os << "\\hspace{" << from_ascii(params_.length.asLatexString()) << "}";
602 case InsetSpaceParams::CUSTOM_PROTECTED:
603 if (runparams.free_spacing)
606 os << "\\hspace*{" << from_ascii(params_.length.asLatexString()) << "}";
613 int InsetSpace::plaintext(odocstream & os, OutputParams const &) const
615 switch (params_.kind) {
616 case InsetSpaceParams::HFILL:
617 case InsetSpaceParams::HFILL_PROTECTED:
620 case InsetSpaceParams::DOTFILL:
623 case InsetSpaceParams::HRULEFILL:
626 case InsetSpaceParams::LEFTARROWFILL:
629 case InsetSpaceParams::RIGHTARROWFILL:
632 case InsetSpaceParams::UPBRACEFILL:
635 case InsetSpaceParams::DOWNBRACEFILL:
645 int InsetSpace::docbook(odocstream & os, OutputParams const &) const
647 switch (params_.kind) {
648 case InsetSpaceParams::NORMAL:
649 case InsetSpaceParams::QUAD:
650 case InsetSpaceParams::QQUAD:
651 case InsetSpaceParams::ENSKIP:
654 case InsetSpaceParams::PROTECTED:
655 case InsetSpaceParams::ENSPACE:
656 case InsetSpaceParams::THIN:
657 case InsetSpaceParams::MEDIUM:
658 case InsetSpaceParams::THICK:
659 case InsetSpaceParams::NEGTHIN:
660 case InsetSpaceParams::NEGMEDIUM:
661 case InsetSpaceParams::NEGTHICK:
664 case InsetSpaceParams::HFILL:
665 case InsetSpaceParams::HFILL_PROTECTED:
667 case InsetSpaceParams::DOTFILL:
670 case InsetSpaceParams::HRULEFILL:
673 case InsetSpaceParams::LEFTARROWFILL:
674 case InsetSpaceParams::RIGHTARROWFILL:
675 case InsetSpaceParams::UPBRACEFILL:
676 case InsetSpaceParams::DOWNBRACEFILL:
677 case InsetSpaceParams::CUSTOM:
678 case InsetSpaceParams::CUSTOM_PROTECTED:
686 void InsetSpace::validate(LaTeXFeatures & features) const
688 if (params_.kind == InsetSpaceParams::NEGMEDIUM ||
689 params_.kind == InsetSpaceParams::NEGTHICK)
690 features.require("amsmath");
694 void InsetSpace::tocString(odocstream & os) const
696 plaintext(os, OutputParams(0));
700 bool InsetSpace::isStretchableSpace() const
702 return params_.kind == InsetSpaceParams::HFILL
703 || params_.kind == InsetSpaceParams::HFILL_PROTECTED
704 || params_.kind == InsetSpaceParams::DOTFILL
705 || params_.kind == InsetSpaceParams::HRULEFILL
706 || params_.kind == InsetSpaceParams::LEFTARROWFILL
707 || params_.kind == InsetSpaceParams::RIGHTARROWFILL
708 || params_.kind == InsetSpaceParams::UPBRACEFILL
709 || params_.kind == InsetSpaceParams::DOWNBRACEFILL;
713 docstring InsetSpace::contextMenu(BufferView const &, int, int) const
715 return from_ascii("context-space");
719 void InsetSpace::string2params(string const & in, InsetSpaceParams & params)
721 params = InsetSpaceParams();
725 istringstream data(in);
728 lex.setContext("InsetSpace::string2params");
730 string const name = lex.getString();
731 if (name == "mathspace")
735 LASSERT(name == "space", /**/);
738 // There are cases, such as when we are called via getStatus() from
739 // Dialog::canApply(), where we are just called with "space" rather
740 // than a full "space \type{}\n\\end_inset".
746 string InsetSpace::params2string(InsetSpaceParams const & params)
751 data << "space" << ' ';