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"
19 #include "Dimension.h"
20 #include "FuncRequest.h"
23 #include "MetricsInfo.h"
24 #include "OutputParams.h"
26 #include "frontends/FontMetrics.h"
27 #include "frontends/Painter.h"
29 #include "support/debug.h"
30 #include "support/docstream.h"
31 #include "support/gettext.h"
32 #include "support/lstrings.h"
39 InsetSpace::InsetSpace()
43 InsetSpace::InsetSpace(InsetSpaceParams par)
45 params_.kind = par.kind;
46 params_.length = par.length;
50 InsetSpaceParams::Kind InsetSpace::kind() const
56 Length InsetSpace::length() const
58 return params_.length;
62 InsetSpace::~InsetSpace()
64 InsetSpaceMailer(*this).hideDialog();
68 docstring InsetSpace::toolTip(BufferView const &, int, int) const
71 switch (params_.kind) {
72 case InsetSpaceParams::NORMAL:
73 message = _("Interword Space");
75 case InsetSpaceParams::PROTECTED:
76 message = _("Protected Space");
78 case InsetSpaceParams::THIN:
79 message = _("Thin Space");
81 case InsetSpaceParams::QUAD:
82 message = _("Quad Space");
84 case InsetSpaceParams::QQUAD:
85 message = _("QQuad Space");
87 case InsetSpaceParams::ENSPACE:
88 message = _("Enspace");
90 case InsetSpaceParams::ENSKIP:
91 message = _("Enskip");
93 case InsetSpaceParams::NEGTHIN:
94 message = _("Negative Thin Space");
96 case InsetSpaceParams::HFILL:
97 message = _("Horizontal Fill");
99 case InsetSpaceParams::HFILL_PROTECTED:
100 message = _("Protected Horizontal Fill");
102 case InsetSpaceParams::DOTFILL:
103 message = _("Horizontal Fill (Dots)");
105 case InsetSpaceParams::HRULEFILL:
106 message = _("Horizontal Fill (Rule)");
108 case InsetSpaceParams::CUSTOM:
109 message = support::bformat(_("Horizontal Space (%1$s)"),
110 params_.length.asDocstring());
112 case InsetSpaceParams::CUSTOM_PROTECTED:
113 message = support::bformat(_("Protected Horizontal Space (%1$s)"),
114 params_.length.asDocstring());
121 void InsetSpace::doDispatch(Cursor & cur, FuncRequest & cmd)
123 switch (cmd.action) {
125 case LFUN_INSET_MODIFY: {
126 InsetSpaceParams params;
127 InsetSpaceMailer::string2params(to_utf8(cmd.argument()), params);
128 params_.kind = params.kind;
129 params_.length = params.length;
133 case LFUN_MOUSE_RELEASE:
134 if (!cur.selection())
135 InsetSpaceMailer(*this).showDialog(&cur.bv());
139 Inset::doDispatch(cur, cmd);
145 void InsetSpace::metrics(MetricsInfo & mi, Dimension & dim) const
147 if (isStretchableSpace()) {
148 // The metrics for this kinds are calculated externally in
149 // \c TextMetrics::computeRowMetrics. Those are dummy value:
150 dim = Dimension(10, 10, 10);
154 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
155 dim.asc = fm.maxAscent();
156 dim.des = fm.maxDescent();
158 switch (params_.kind) {
159 case InsetSpaceParams::THIN:
160 case InsetSpaceParams::NEGTHIN:
161 dim.wid = fm.width(char_type('M')) / 6;
163 case InsetSpaceParams::PROTECTED:
164 case InsetSpaceParams::NORMAL:
165 dim.wid = fm.width(char_type(' '));
167 case InsetSpaceParams::QUAD:
168 dim.wid = fm.width(char_type('M'));
170 case InsetSpaceParams::QQUAD:
171 dim.wid = 2 * fm.width(char_type('M'));
173 case InsetSpaceParams::ENSPACE:
174 case InsetSpaceParams::ENSKIP:
175 dim.wid = int(0.5 * fm.width(char_type('M')));
177 case InsetSpaceParams::CUSTOM:
178 case InsetSpaceParams::CUSTOM_PROTECTED:
179 dim.wid = params_.length.inBP();
181 case InsetSpaceParams::HFILL:
182 case InsetSpaceParams::HFILL_PROTECTED:
183 case InsetSpaceParams::DOTFILL:
184 case InsetSpaceParams::HRULEFILL:
188 // Cache the inset dimension.
189 setDimCache(mi, dim);
193 void InsetSpace::draw(PainterInfo & pi, int x, int y) const
195 Dimension const dim = dimension(*pi.base.bv);
197 if (isStretchableSpace()) {
198 int const asc = theFontMetrics(pi.base.font).ascent('M');
199 int const desc = theFontMetrics(pi.base.font).descent('M');
200 int const x0 = x + 1;
201 int const x1 = x + dim.wid - 2;
202 int const y0 = y + desc;
203 int const y1 = y - asc;
204 int const y2 = y - asc / 2;
206 if (params_.kind == InsetSpaceParams::HFILL) {
207 pi.pain.line(x0, y1, x0, y0, Color_added_space);
208 pi.pain.line(x0, y2 , x1, y2, Color_added_space,
209 frontend::Painter::line_onoffdash);
210 pi.pain.line(x1, y1, x1, y0, Color_added_space);
211 } else if (params_.kind == InsetSpaceParams::HFILL_PROTECTED) {
212 pi.pain.line(x0, y1, x0, y0, Color_latex);
213 pi.pain.line(x0, y2 , x1, y2, Color_latex,
214 frontend::Painter::line_onoffdash);
215 pi.pain.line(x1, y1, x1, y0, Color_latex);
216 } else if (params_.kind == InsetSpaceParams::DOTFILL) {
217 pi.pain.line(x0, y1, x0, y0, Color_special);
218 pi.pain.line(x0, y, x1, y, Color_special,
219 frontend::Painter::line_onoffdash);
220 pi.pain.line(x1, y1, x1, y0, Color_special);
221 } if (params_.kind == InsetSpaceParams::HRULEFILL) {
222 pi.pain.line(x0, y1, x0, y0, Color_special);
223 pi.pain.line(x0, y, x1, y, Color_special);
224 pi.pain.line(x1, y1, x1, y0, Color_special);
229 int const w = dim.wid;
230 int const h = theFontMetrics(pi.base.font).ascent('x');
234 yp[0] = y - max(h / 4, 1);
235 if (params_.kind == InsetSpaceParams::NORMAL ||
236 params_.kind == InsetSpaceParams::PROTECTED) {
237 xp[1] = x; yp[1] = y;
238 xp[2] = x + w; yp[2] = y;
240 xp[1] = x; yp[1] = y + max(h / 4, 1);
241 xp[2] = x + w; yp[2] = y + max(h / 4, 1);
244 yp[3] = y - max(h / 4, 1);
246 if (params_.kind == InsetSpaceParams::PROTECTED ||
247 params_.kind == InsetSpaceParams::ENSPACE ||
248 params_.kind == InsetSpaceParams::NEGTHIN ||
249 params_.kind == InsetSpaceParams::CUSTOM_PROTECTED)
250 pi.pain.lines(xp, yp, 4, Color_latex);
252 pi.pain.lines(xp, yp, 4, Color_special);
256 void InsetSpaceParams::write(ostream & os) const
260 case InsetSpaceParams::NORMAL:
263 case InsetSpaceParams::PROTECTED:
266 case InsetSpaceParams::THIN:
267 os << "\\thinspace{}";
269 case InsetSpaceParams::QUAD:
272 case InsetSpaceParams::QQUAD:
275 case InsetSpaceParams::ENSPACE:
278 case InsetSpaceParams::ENSKIP:
281 case InsetSpaceParams::NEGTHIN:
282 os << "\\negthinspace{}";
284 case InsetSpaceParams::HFILL:
287 case InsetSpaceParams::HFILL_PROTECTED:
288 os << "\\hspace*{\\fill}";
290 case InsetSpaceParams::DOTFILL:
293 case InsetSpaceParams::HRULEFILL:
294 os << "\\hrulefill{}";
296 case InsetSpaceParams::CUSTOM:
299 case InsetSpaceParams::CUSTOM_PROTECTED:
305 os << "\n\\length " << length.asString();
309 void InsetSpaceParams::read(Lexer & lex)
312 string const command = lex.getString();
314 if (command == "\\space{}")
315 kind = InsetSpaceParams::NORMAL;
316 else if (command == "~")
317 kind = InsetSpaceParams::PROTECTED;
318 else if (command == "\\thinspace{}")
319 kind = InsetSpaceParams::THIN;
320 else if (command == "\\quad{}")
321 kind = InsetSpaceParams::QUAD;
322 else if (command == "\\qquad{}")
323 kind = InsetSpaceParams::QQUAD;
324 else if (command == "\\enspace{}")
325 kind = InsetSpaceParams::ENSPACE;
326 else if (command == "\\enskip{}")
327 kind = InsetSpaceParams::ENSKIP;
328 else if (command == "\\negthinspace{}")
329 kind = InsetSpaceParams::NEGTHIN;
330 else if (command == "\\hfill{}")
331 kind = InsetSpaceParams::HFILL;
332 else if (command == "\\hspace*{\\fill}")
333 kind = InsetSpaceParams::HFILL_PROTECTED;
334 else if (command == "\\dotfill{}")
335 kind = InsetSpaceParams::DOTFILL;
336 else if (command == "\\hrulefill{}")
337 kind = InsetSpaceParams::HRULEFILL;
338 else if (command == "\\hspace{}")
339 kind = InsetSpaceParams::CUSTOM;
340 else if (command == "\\hspace*{}")
341 kind = InsetSpaceParams::CUSTOM_PROTECTED;
343 lex.printError("InsetSpace: Unknown kind: `$$Token'");
348 if (token == "\\length") {
350 string const len = lex.getString();
351 length = Length(len);
353 token = lex.getString();
357 if (token != "\\end_inset")
358 lex.printError("Missing \\end_inset at this point. "
363 void InsetSpace::write(ostream & os) const
370 void InsetSpace::read(Lexer & lex)
376 int InsetSpace::latex(odocstream & os, OutputParams const & runparams) const
378 switch (params_.kind) {
379 case InsetSpaceParams::NORMAL:
380 os << (runparams.free_spacing ? " " : "\\ ");
382 case InsetSpaceParams::PROTECTED:
383 os << (runparams.free_spacing ? ' ' : '~');
385 case InsetSpaceParams::THIN:
386 os << (runparams.free_spacing ? " " : "\\,");
388 case InsetSpaceParams::QUAD:
389 os << (runparams.free_spacing ? " " : "\\quad{}");
391 case InsetSpaceParams::QQUAD:
392 os << (runparams.free_spacing ? " " : "\\qquad{}");
394 case InsetSpaceParams::ENSPACE:
395 os << (runparams.free_spacing ? " " : "\\enspace{}");
397 case InsetSpaceParams::ENSKIP:
398 os << (runparams.free_spacing ? " " : "\\enskip{}");
400 case InsetSpaceParams::NEGTHIN:
401 os << (runparams.free_spacing ? " " : "\\negthinspace{}");
403 case InsetSpaceParams::HFILL:
404 os << (runparams.free_spacing ? " " : "\\hfill{}");
406 case InsetSpaceParams::HFILL_PROTECTED:
407 os << (runparams.free_spacing ? " " : "\\hspace*{\\fill}");
409 case InsetSpaceParams::DOTFILL:
410 os << (runparams.free_spacing ? " " : "\\dotfill{}");
412 case InsetSpaceParams::HRULEFILL:
413 os << (runparams.free_spacing ? " " : "\\hrulefill{}");
415 case InsetSpaceParams::CUSTOM:
416 if (runparams.free_spacing)
419 os << "\\hspace{" << from_ascii(params_.length.asLatexString()) << "}";
421 case InsetSpaceParams::CUSTOM_PROTECTED:
422 if (runparams.free_spacing)
425 os << "\\hspace*{" << from_ascii(params_.length.asLatexString()) << "}";
432 int InsetSpace::plaintext(odocstream & os, OutputParams const &) const
434 switch (params_.kind) {
435 case InsetSpaceParams::HFILL:
436 case InsetSpaceParams::HFILL_PROTECTED:
439 case InsetSpaceParams::DOTFILL:
442 case InsetSpaceParams::HRULEFILL:
452 int InsetSpace::docbook(odocstream & os, OutputParams const &) const
454 switch (params_.kind) {
455 case InsetSpaceParams::NORMAL:
456 case InsetSpaceParams::QUAD:
457 case InsetSpaceParams::QQUAD:
458 case InsetSpaceParams::ENSKIP:
461 case InsetSpaceParams::PROTECTED:
462 case InsetSpaceParams::ENSPACE:
463 case InsetSpaceParams::THIN:
464 case InsetSpaceParams::NEGTHIN:
467 case InsetSpaceParams::HFILL:
468 case InsetSpaceParams::HFILL_PROTECTED:
470 case InsetSpaceParams::DOTFILL:
473 case InsetSpaceParams::HRULEFILL:
476 case InsetSpaceParams::CUSTOM:
477 case InsetSpaceParams::CUSTOM_PROTECTED:
485 void InsetSpace::textString(odocstream & os) const
487 plaintext(os, OutputParams(0));
491 bool InsetSpace::isStretchableSpace() const
493 return (params_.kind == InsetSpaceParams::HFILL ||
494 params_.kind == InsetSpaceParams::HFILL_PROTECTED ||
495 params_.kind == InsetSpaceParams::DOTFILL ||
496 params_.kind == InsetSpaceParams::HRULEFILL);
500 string const InsetSpaceMailer::name_ = "space";
503 InsetSpaceMailer::InsetSpaceMailer(InsetSpace & inset)
508 string const InsetSpaceMailer::inset2string(Buffer const &) const
510 return params2string(inset_.params());
514 void InsetSpaceMailer::string2params(string const & in, InsetSpaceParams & params)
516 params = InsetSpaceParams();
520 istringstream data(in);
526 if (!lex || name != name_)
527 return print_mailer_error("InsetSpaceMailer", in, 1, name_);
533 string const InsetSpaceMailer::params2string(InsetSpaceParams const & params)
536 data << name_ << ' ';