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"
25 #include "MetricsInfo.h"
26 #include "OutputParams.h"
28 #include "support/debug.h"
29 #include "support/docstream.h"
30 #include "support/gettext.h"
31 #include "support/lstrings.h"
33 #include "frontends/Application.h"
34 #include "frontends/FontMetrics.h"
35 #include "frontends/Painter.h"
42 InsetSpace::InsetSpace(InsetSpaceParams const & params)
47 InsetSpaceParams::Kind InsetSpace::kind() const
53 Length InsetSpace::length() const
55 return params_.length;
59 InsetSpace::~InsetSpace()
61 hideDialogs("space", this);
65 docstring InsetSpace::toolTip(BufferView const &, int, int) const
68 switch (params_.kind) {
69 case InsetSpaceParams::NORMAL:
70 message = _("Interword Space");
72 case InsetSpaceParams::PROTECTED:
73 message = _("Protected Space");
75 case InsetSpaceParams::THIN:
76 message = _("Thin Space");
78 case InsetSpaceParams::QUAD:
79 message = _("Quad Space");
81 case InsetSpaceParams::QQUAD:
82 message = _("QQuad Space");
84 case InsetSpaceParams::ENSPACE:
85 message = _("Enspace");
87 case InsetSpaceParams::ENSKIP:
88 message = _("Enskip");
90 case InsetSpaceParams::NEGTHIN:
91 message = _("Negative Thin Space");
93 case InsetSpaceParams::HFILL:
94 message = _("Horizontal Fill");
96 case InsetSpaceParams::HFILL_PROTECTED:
97 message = _("Protected Horizontal Fill");
99 case InsetSpaceParams::DOTFILL:
100 message = _("Horizontal Fill (Dots)");
102 case InsetSpaceParams::HRULEFILL:
103 message = _("Horizontal Fill (Rule)");
105 case InsetSpaceParams::LEFTARROWFILL:
106 message = _("Horizontal Fill (Left Arrow)");
108 case InsetSpaceParams::RIGHTARROWFILL:
109 message = _("Horizontal Fill (Right Arrow)");
111 case InsetSpaceParams::UPBRACEFILL:
112 message = _("Horizontal Fill (Up Brace)");
114 case InsetSpaceParams::DOWNBRACEFILL:
115 message = _("Horizontal Fill (Down Brace)");
117 case InsetSpaceParams::CUSTOM:
118 message = support::bformat(_("Horizontal Space (%1$s)"),
119 params_.length.asDocstring());
121 case InsetSpaceParams::CUSTOM_PROTECTED:
122 message = support::bformat(_("Protected Horizontal Space (%1$s)"),
123 params_.length.asDocstring());
130 void InsetSpace::doDispatch(Cursor & cur, FuncRequest & cmd)
132 switch (cmd.action) {
134 case LFUN_INSET_MODIFY: {
135 string2params(to_utf8(cmd.argument()), params_);
139 case LFUN_MOUSE_RELEASE:
140 if (!cur.selection() && cmd.button() == mouse_button::button1)
141 cur.bv().showDialog("space", params2string(params()), this);
145 Inset::doDispatch(cur, cmd);
151 bool InsetSpace::getStatus(Cursor & cur, FuncRequest const & cmd,
152 FuncStatus & status) const
154 switch (cmd.action) {
156 case LFUN_INSET_MODIFY:
157 if (cmd.getArg(0) == "space") {
158 InsetSpaceParams params;
159 string2params(to_utf8(cmd.argument()), params);
160 status.setOnOff(params_.kind == params.kind);
162 status.setEnabled(true);
165 return Inset::getStatus(cur, cmd, status);
170 void InsetSpace::edit(Cursor & cur, bool, EntryDirection)
172 cur.bv().showDialog("space", params2string(params()), this);
176 void InsetSpace::metrics(MetricsInfo & mi, Dimension & dim) const
178 if (isStretchableSpace()) {
179 // The metrics for this kinds are calculated externally in
180 // \c TextMetrics::computeRowMetrics. Those are dummy value:
181 dim = Dimension(10, 10, 10);
185 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
186 dim.asc = fm.maxAscent();
187 dim.des = fm.maxDescent();
189 switch (params_.kind) {
190 case InsetSpaceParams::THIN:
191 case InsetSpaceParams::NEGTHIN:
192 dim.wid = fm.width(char_type('M')) / 6;
194 case InsetSpaceParams::PROTECTED:
195 case InsetSpaceParams::NORMAL:
196 dim.wid = fm.width(char_type(' '));
198 case InsetSpaceParams::QUAD:
199 dim.wid = fm.width(char_type('M'));
201 case InsetSpaceParams::QQUAD:
202 dim.wid = 2 * fm.width(char_type('M'));
204 case InsetSpaceParams::ENSPACE:
205 case InsetSpaceParams::ENSKIP:
206 dim.wid = int(0.5 * fm.width(char_type('M')));
208 case InsetSpaceParams::CUSTOM:
209 case InsetSpaceParams::CUSTOM_PROTECTED: {
210 int length = params_.length.inBP();
212 length = -1 * length;
216 case InsetSpaceParams::HFILL:
217 case InsetSpaceParams::HFILL_PROTECTED:
218 case InsetSpaceParams::DOTFILL:
219 case InsetSpaceParams::HRULEFILL:
220 case InsetSpaceParams::LEFTARROWFILL:
221 case InsetSpaceParams::RIGHTARROWFILL:
222 case InsetSpaceParams::UPBRACEFILL:
223 case InsetSpaceParams::DOWNBRACEFILL:
227 // Cache the inset dimension.
228 setDimCache(mi, dim);
232 void InsetSpace::draw(PainterInfo & pi, int x, int y) const
234 Dimension const dim = dimension(*pi.base.bv);
236 if (isStretchableSpace()) {
237 int const asc = theFontMetrics(pi.base.font).ascent('M');
238 int const desc = theFontMetrics(pi.base.font).descent('M');
239 //Pixel height divisible by 2 for prettier fill graphics:
240 int const oddheight = (asc ^ desc) & 1;
241 int const x0 = x + 1;
242 int const x1 = x + dim.wid - 2;
243 int const y0 = y + desc - 1;
244 int const y1 = y - asc + oddheight - 1;
245 int const y2 = (y0 + y1) / 2;
246 int xoffset = (y0 - y1) / 2;
248 //Two tests for very narrow insets
249 if (xoffset > x1 - x0
250 && (params_.kind == InsetSpaceParams::LEFTARROWFILL
251 || params_.kind == InsetSpaceParams::RIGHTARROWFILL))
253 if (xoffset * 6 > (x1 - x0)
254 && (params_.kind == InsetSpaceParams::UPBRACEFILL
255 || params_.kind == InsetSpaceParams::DOWNBRACEFILL))
256 xoffset = (x1 - x0) / 6;
258 int const x2 = x0 + xoffset;
259 int const x3 = x1 - xoffset;
260 int const xm = (x0 + x1) / 2;
261 int const xml = xm - xoffset;
262 int const xmr = xm + xoffset;
264 if (params_.kind == InsetSpaceParams::HFILL) {
265 pi.pain.line(x0, y1, x0, y0, Color_added_space);
266 pi.pain.line(x0, y2 , x1, y2, Color_added_space,
267 frontend::Painter::line_onoffdash);
268 pi.pain.line(x1, y1, x1, y0, Color_added_space);
269 } else if (params_.kind == InsetSpaceParams::HFILL_PROTECTED) {
270 pi.pain.line(x0, y1, x0, y0, Color_latex);
271 pi.pain.line(x0, y2 , x1, y2, Color_latex,
272 frontend::Painter::line_onoffdash);
273 pi.pain.line(x1, y1, x1, y0, Color_latex);
274 } else if (params_.kind == InsetSpaceParams::DOTFILL) {
275 pi.pain.line(x0, y1, x0, y0, Color_special);
276 pi.pain.line(x0, y0, x1, y0, Color_special,
277 frontend::Painter::line_onoffdash);
278 pi.pain.line(x1, y1, x1, y0, Color_special);
279 } else if (params_.kind == InsetSpaceParams::HRULEFILL) {
280 pi.pain.line(x0, y1, x0, y0, Color_special);
281 pi.pain.line(x0, y0, x1, y0, Color_special);
282 pi.pain.line(x1, y1, x1, y0, Color_special);
283 } else if (params_.kind == InsetSpaceParams::LEFTARROWFILL) {
284 pi.pain.line(x2, y1 + 1 , x0 + 1, y2, Color_special);
285 pi.pain.line(x0 + 1, y2 + 1 , x2, y0, Color_special);
286 pi.pain.line(x0, y2 , x1, y2, Color_special);
287 } else if (params_.kind == InsetSpaceParams::RIGHTARROWFILL) {
288 pi.pain.line(x3 + 1, y1 + 1 , x1, y2, Color_special);
289 pi.pain.line(x1, y2 + 1 , x3 + 1, y0, Color_special);
290 pi.pain.line(x0, y2 , x1, y2, Color_special);
291 } else if (params_.kind == InsetSpaceParams::UPBRACEFILL) {
292 pi.pain.line(x0 + 1, y1 + 1 , x2, y2, Color_special);
293 pi.pain.line(x2, y2 , xml, y2, Color_special);
294 pi.pain.line(xml + 1, y2 + 1 , xm, y0, Color_special);
295 pi.pain.line(xm + 1, y0 , xmr, y2 + 1, Color_special);
296 pi.pain.line(xmr, y2 , x3, y2, Color_special);
297 pi.pain.line(x3 + 1, y2 , x1, y1 + 1, Color_special);
298 } else if (params_.kind == InsetSpaceParams::DOWNBRACEFILL) {
299 pi.pain.line(x0 + 1, y0 , x2, y2 + 1, Color_special);
300 pi.pain.line(x2, y2 , xml, y2, Color_special);
301 pi.pain.line(xml + 1, y2 , xm, y1 + 1, Color_special);
302 pi.pain.line(xm + 1, y1 + 1 , xmr, y2, Color_special);
303 pi.pain.line(xmr, y2 , x3, y2, Color_special);
304 pi.pain.line(x3 + 1, y2 + 1 , x1, y0, Color_special);
309 int const w = dim.wid;
310 int const h = theFontMetrics(pi.base.font).ascent('x');
314 yp[0] = y - max(h / 4, 1);
315 if (params_.kind == InsetSpaceParams::NORMAL ||
316 params_.kind == InsetSpaceParams::PROTECTED) {
317 xp[1] = x; yp[1] = y;
318 xp[2] = x + w; yp[2] = y;
320 xp[1] = x; yp[1] = y + max(h / 4, 1);
321 xp[2] = x + w; yp[2] = y + max(h / 4, 1);
324 yp[3] = y - max(h / 4, 1);
326 if (params_.kind == InsetSpaceParams::PROTECTED ||
327 params_.kind == InsetSpaceParams::ENSPACE ||
328 params_.kind == InsetSpaceParams::NEGTHIN ||
329 params_.kind == InsetSpaceParams::CUSTOM_PROTECTED)
330 pi.pain.lines(xp, yp, 4, Color_latex);
332 pi.pain.lines(xp, yp, 4, Color_special);
336 void InsetSpaceParams::write(ostream & os) const
340 case InsetSpaceParams::NORMAL:
343 case InsetSpaceParams::PROTECTED:
346 case InsetSpaceParams::THIN:
347 os << "\\thinspace{}";
349 case InsetSpaceParams::QUAD:
352 case InsetSpaceParams::QQUAD:
355 case InsetSpaceParams::ENSPACE:
358 case InsetSpaceParams::ENSKIP:
361 case InsetSpaceParams::NEGTHIN:
362 os << "\\negthinspace{}";
364 case InsetSpaceParams::HFILL:
367 case InsetSpaceParams::HFILL_PROTECTED:
368 os << "\\hspace*{\\fill}";
370 case InsetSpaceParams::DOTFILL:
373 case InsetSpaceParams::HRULEFILL:
374 os << "\\hrulefill{}";
376 case InsetSpaceParams::LEFTARROWFILL:
377 os << "\\leftarrowfill{}";
379 case InsetSpaceParams::RIGHTARROWFILL:
380 os << "\\rightarrowfill{}";
382 case InsetSpaceParams::UPBRACEFILL:
383 os << "\\upbracefill{}";
385 case InsetSpaceParams::DOWNBRACEFILL:
386 os << "\\downbracefill{}";
388 case InsetSpaceParams::CUSTOM:
391 case InsetSpaceParams::CUSTOM_PROTECTED:
397 os << "\n\\length " << length.asString();
401 void InsetSpaceParams::read(Lexer & lex)
403 lex.setContext("InsetSpaceParams::read");
407 if (command == "\\space{}")
408 kind = InsetSpaceParams::NORMAL;
409 else if (command == "~")
410 kind = InsetSpaceParams::PROTECTED;
411 else if (command == "\\thinspace{}")
412 kind = InsetSpaceParams::THIN;
413 else if (command == "\\quad{}")
414 kind = InsetSpaceParams::QUAD;
415 else if (command == "\\qquad{}")
416 kind = InsetSpaceParams::QQUAD;
417 else if (command == "\\enspace{}")
418 kind = InsetSpaceParams::ENSPACE;
419 else if (command == "\\enskip{}")
420 kind = InsetSpaceParams::ENSKIP;
421 else if (command == "\\negthinspace{}")
422 kind = InsetSpaceParams::NEGTHIN;
423 else if (command == "\\hfill{}")
424 kind = InsetSpaceParams::HFILL;
425 else if (command == "\\hspace*{\\fill}")
426 kind = InsetSpaceParams::HFILL_PROTECTED;
427 else if (command == "\\dotfill{}")
428 kind = InsetSpaceParams::DOTFILL;
429 else if (command == "\\hrulefill{}")
430 kind = InsetSpaceParams::HRULEFILL;
431 else if (command == "\\hspace{}")
432 kind = InsetSpaceParams::CUSTOM;
433 else if (command == "\\leftarrowfill{}")
434 kind = InsetSpaceParams::LEFTARROWFILL;
435 else if (command == "\\rightarrowfill{}")
436 kind = InsetSpaceParams::RIGHTARROWFILL;
437 else if (command == "\\upbracefill{}")
438 kind = InsetSpaceParams::UPBRACEFILL;
439 else if (command == "\\downbracefill{}")
440 kind = InsetSpaceParams::DOWNBRACEFILL;
441 else if (command == "\\hspace*{}")
442 kind = InsetSpaceParams::CUSTOM_PROTECTED;
444 lex.printError("InsetSpace: Unknown kind: `$$Token'");
446 if (lex.checkFor("\\length"))
451 void InsetSpace::write(ostream & os) const
458 void InsetSpace::read(Lexer & lex)
461 lex >> "\\end_inset";
465 int InsetSpace::latex(odocstream & os, OutputParams const & runparams) const
467 switch (params_.kind) {
468 case InsetSpaceParams::NORMAL:
469 os << (runparams.free_spacing ? " " : "\\ ");
471 case InsetSpaceParams::PROTECTED:
472 os << (runparams.free_spacing ? ' ' : '~');
474 case InsetSpaceParams::THIN:
475 os << (runparams.free_spacing ? " " : "\\,");
477 case InsetSpaceParams::QUAD:
478 os << (runparams.free_spacing ? " " : "\\quad{}");
480 case InsetSpaceParams::QQUAD:
481 os << (runparams.free_spacing ? " " : "\\qquad{}");
483 case InsetSpaceParams::ENSPACE:
484 os << (runparams.free_spacing ? " " : "\\enspace{}");
486 case InsetSpaceParams::ENSKIP:
487 os << (runparams.free_spacing ? " " : "\\enskip{}");
489 case InsetSpaceParams::NEGTHIN:
490 os << (runparams.free_spacing ? " " : "\\negthinspace{}");
492 case InsetSpaceParams::HFILL:
493 os << (runparams.free_spacing ? " " : "\\hfill{}");
495 case InsetSpaceParams::HFILL_PROTECTED:
496 os << (runparams.free_spacing ? " " : "\\hspace*{\\fill}");
498 case InsetSpaceParams::DOTFILL:
499 os << (runparams.free_spacing ? " " : "\\dotfill{}");
501 case InsetSpaceParams::HRULEFILL:
502 os << (runparams.free_spacing ? " " : "\\hrulefill{}");
504 case InsetSpaceParams::LEFTARROWFILL:
505 os << (runparams.free_spacing ? " " : "\\leftarrowfill{}");
507 case InsetSpaceParams::RIGHTARROWFILL:
508 os << (runparams.free_spacing ? " " : "\\rightarrowfill{}");
510 case InsetSpaceParams::UPBRACEFILL:
511 os << (runparams.free_spacing ? " " : "\\upbracefill{}");
513 case InsetSpaceParams::DOWNBRACEFILL:
514 os << (runparams.free_spacing ? " " : "\\downbracefill{}");
516 case InsetSpaceParams::CUSTOM:
517 if (runparams.free_spacing)
520 os << "\\hspace{" << from_ascii(params_.length.asLatexString()) << "}";
522 case InsetSpaceParams::CUSTOM_PROTECTED:
523 if (runparams.free_spacing)
526 os << "\\hspace*{" << from_ascii(params_.length.asLatexString()) << "}";
533 int InsetSpace::plaintext(odocstream & os, OutputParams const &) const
535 switch (params_.kind) {
536 case InsetSpaceParams::HFILL:
537 case InsetSpaceParams::HFILL_PROTECTED:
540 case InsetSpaceParams::DOTFILL:
543 case InsetSpaceParams::HRULEFILL:
546 case InsetSpaceParams::LEFTARROWFILL:
549 case InsetSpaceParams::RIGHTARROWFILL:
552 case InsetSpaceParams::UPBRACEFILL:
555 case InsetSpaceParams::DOWNBRACEFILL:
565 int InsetSpace::docbook(odocstream & os, OutputParams const &) const
567 switch (params_.kind) {
568 case InsetSpaceParams::NORMAL:
569 case InsetSpaceParams::QUAD:
570 case InsetSpaceParams::QQUAD:
571 case InsetSpaceParams::ENSKIP:
574 case InsetSpaceParams::PROTECTED:
575 case InsetSpaceParams::ENSPACE:
576 case InsetSpaceParams::THIN:
577 case InsetSpaceParams::NEGTHIN:
580 case InsetSpaceParams::HFILL:
581 case InsetSpaceParams::HFILL_PROTECTED:
583 case InsetSpaceParams::DOTFILL:
586 case InsetSpaceParams::HRULEFILL:
589 case InsetSpaceParams::LEFTARROWFILL:
590 case InsetSpaceParams::RIGHTARROWFILL:
591 case InsetSpaceParams::UPBRACEFILL:
592 case InsetSpaceParams::DOWNBRACEFILL:
593 case InsetSpaceParams::CUSTOM:
594 case InsetSpaceParams::CUSTOM_PROTECTED:
602 void InsetSpace::textString(odocstream & os) const
604 plaintext(os, OutputParams(0));
608 bool InsetSpace::isStretchableSpace() const
610 return params_.kind == InsetSpaceParams::HFILL
611 || params_.kind == InsetSpaceParams::HFILL_PROTECTED
612 || params_.kind == InsetSpaceParams::DOTFILL
613 || params_.kind == InsetSpaceParams::HRULEFILL
614 || params_.kind == InsetSpaceParams::LEFTARROWFILL
615 || params_.kind == InsetSpaceParams::RIGHTARROWFILL
616 || params_.kind == InsetSpaceParams::UPBRACEFILL
617 || params_.kind == InsetSpaceParams::DOWNBRACEFILL;
621 docstring InsetSpace::contextMenu(BufferView const &, int, int) const
623 return from_ascii("context-space");
627 void InsetSpace::string2params(string const & in, InsetSpaceParams & params)
629 params = InsetSpaceParams();
633 istringstream data(in);
636 lex.setContext("InsetSpace::string2params");
639 // There are cases, such as when we are called via getStatus() from
640 // Dialog::canApply(), where we are just called with "space" rather
641 // than a full "space \type{}\n\\end_inset".
647 string InsetSpace::params2string(InsetSpaceParams const & params)
650 data << "space" << ' ';