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::DOTFILL:
100 message = _("Horizontal Fill (Dots)");
102 case InsetSpaceParams::HRULEFILL:
103 message = _("Horizontal Fill (Rule)");
105 case InsetSpaceParams::CUSTOM:
106 message = support::bformat(_("Horizontal Space (%1$s)"),
107 params_.length.asDocstring());
109 case InsetSpaceParams::CUSTOM_PROTECTED:
110 message = support::bformat(_("Protected Horizontal Space (%1$s)"),
111 params_.length.asDocstring());
118 void InsetSpace::doDispatch(Cursor & cur, FuncRequest & cmd)
120 switch (cmd.action) {
122 case LFUN_INSET_MODIFY: {
123 InsetSpaceParams params;
124 InsetSpaceMailer::string2params(to_utf8(cmd.argument()), params);
125 params_.kind = params.kind;
126 params_.length = params.length;
130 case LFUN_MOUSE_RELEASE:
131 if (!cur.selection())
132 InsetSpaceMailer(*this).showDialog(&cur.bv());
136 Inset::doDispatch(cur, cmd);
142 void InsetSpace::metrics(MetricsInfo & mi, Dimension & dim) const
144 if (params_.kind == InsetSpaceParams::HFILL ||
145 params_.kind == InsetSpaceParams::DOTFILL ||
146 params_.kind == InsetSpaceParams::HRULEFILL) {
147 // The metrics for this kinds are calculated externally in
148 // \c TextMetrics::computeRowMetrics. Those are dummy value:
149 dim = Dimension(10, 10, 10);
153 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
154 dim.asc = fm.maxAscent();
155 dim.des = fm.maxDescent();
157 switch (params_.kind) {
158 case InsetSpaceParams::THIN:
159 case InsetSpaceParams::NEGTHIN:
160 dim.wid = fm.width(char_type('M')) / 6;
162 case InsetSpaceParams::PROTECTED:
163 case InsetSpaceParams::NORMAL:
164 dim.wid = fm.width(char_type(' '));
166 case InsetSpaceParams::QUAD:
167 dim.wid = fm.width(char_type('M'));
169 case InsetSpaceParams::QQUAD:
170 dim.wid = 2 * fm.width(char_type('M'));
172 case InsetSpaceParams::ENSPACE:
173 case InsetSpaceParams::ENSKIP:
174 dim.wid = int(0.5 * fm.width(char_type('M')));
176 case InsetSpaceParams::CUSTOM:
177 case InsetSpaceParams::CUSTOM_PROTECTED:
178 dim.wid = params_.length.inBP();
180 case InsetSpaceParams::HFILL:
181 case InsetSpaceParams::DOTFILL:
182 case InsetSpaceParams::HRULEFILL:
186 // Cache the inset dimension.
187 setDimCache(mi, dim);
191 void InsetSpace::draw(PainterInfo & pi, int x, int y) const
193 Dimension const dim = dimension(*pi.base.bv);
195 if (params_.kind == InsetSpaceParams::HFILL ||
196 params_.kind == InsetSpaceParams::DOTFILL ||
197 params_.kind == InsetSpaceParams::HRULEFILL) {
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::DOTFILL) {
212 pi.pain.line(x0, y1, x0, y0, Color_special);
213 pi.pain.line(x0, y, x1, y, Color_special,
214 frontend::Painter::line_onoffdash);
215 pi.pain.line(x1, y1, x1, y0, Color_special);
216 } if (params_.kind == InsetSpaceParams::HRULEFILL) {
217 pi.pain.line(x0, y1, x0, y0, Color_special);
218 pi.pain.line(x0, y, x1, y, Color_special);
219 pi.pain.line(x1, y1, x1, y0, Color_special);
224 int const w = dim.wid;
225 int const h = theFontMetrics(pi.base.font).ascent('x');
229 yp[0] = y - max(h / 4, 1);
230 if (params_.kind == InsetSpaceParams::NORMAL ||
231 params_.kind == InsetSpaceParams::PROTECTED) {
232 xp[1] = x; yp[1] = y;
233 xp[2] = x + w; yp[2] = y;
235 xp[1] = x; yp[1] = y + max(h / 4, 1);
236 xp[2] = x + w; yp[2] = y + max(h / 4, 1);
239 yp[3] = y - max(h / 4, 1);
241 if (params_.kind == InsetSpaceParams::PROTECTED ||
242 params_.kind == InsetSpaceParams::ENSPACE ||
243 params_.kind == InsetSpaceParams::NEGTHIN ||
244 params_.kind == InsetSpaceParams::CUSTOM_PROTECTED)
245 pi.pain.lines(xp, yp, 4, Color_latex);
247 pi.pain.lines(xp, yp, 4, Color_special);
251 void InsetSpaceParams::write(ostream & os) const
255 case InsetSpaceParams::NORMAL:
258 case InsetSpaceParams::PROTECTED:
261 case InsetSpaceParams::THIN:
262 os << "\\thinspace{}";
264 case InsetSpaceParams::QUAD:
267 case InsetSpaceParams::QQUAD:
270 case InsetSpaceParams::ENSPACE:
273 case InsetSpaceParams::ENSKIP:
276 case InsetSpaceParams::NEGTHIN:
277 os << "\\negthinspace{}";
279 case InsetSpaceParams::HFILL:
282 case InsetSpaceParams::DOTFILL:
285 case InsetSpaceParams::HRULEFILL:
286 os << "\\hrulefill{}";
288 case InsetSpaceParams::CUSTOM:
291 case InsetSpaceParams::CUSTOM_PROTECTED:
297 os << "\n\\length " << length.asString();
301 void InsetSpaceParams::read(Lexer & lex)
304 string const command = lex.getString();
306 if (command == "\\space{}")
307 kind = InsetSpaceParams::NORMAL;
308 else if (command == "~")
309 kind = InsetSpaceParams::PROTECTED;
310 else if (command == "\\thinspace{}")
311 kind = InsetSpaceParams::THIN;
312 else if (command == "\\quad{}")
313 kind = InsetSpaceParams::QUAD;
314 else if (command == "\\qquad{}")
315 kind = InsetSpaceParams::QQUAD;
316 else if (command == "\\enspace{}")
317 kind = InsetSpaceParams::ENSPACE;
318 else if (command == "\\enskip{}")
319 kind = InsetSpaceParams::ENSKIP;
320 else if (command == "\\negthinspace{}")
321 kind = InsetSpaceParams::NEGTHIN;
322 else if (command == "\\hfill{}")
323 kind = InsetSpaceParams::HFILL;
324 else if (command == "\\dotfill{}")
325 kind = InsetSpaceParams::DOTFILL;
326 else if (command == "\\hrulefill{}")
327 kind = InsetSpaceParams::HRULEFILL;
328 else if (command == "\\hspace{}")
329 kind = InsetSpaceParams::CUSTOM;
330 else if (command == "\\hspace*{}")
331 kind = InsetSpaceParams::CUSTOM_PROTECTED;
333 lex.printError("InsetSpace: Unknown kind: `$$Token'");
338 if (token == "\\length") {
340 string const len = lex.getString();
341 length = Length(len);
343 token = lex.getString();
347 if (token != "\\end_inset")
348 lex.printError("Missing \\end_inset at this point. "
353 void InsetSpace::write(ostream & os) const
360 void InsetSpace::read(Lexer & lex)
366 int InsetSpace::latex(odocstream & os, OutputParams const & runparams) const
368 switch (params_.kind) {
369 case InsetSpaceParams::NORMAL:
370 os << (runparams.free_spacing ? " " : "\\ ");
372 case InsetSpaceParams::PROTECTED:
373 os << (runparams.free_spacing ? ' ' : '~');
375 case InsetSpaceParams::THIN:
376 os << (runparams.free_spacing ? " " : "\\,");
378 case InsetSpaceParams::QUAD:
379 os << (runparams.free_spacing ? " " : "\\quad{}");
381 case InsetSpaceParams::QQUAD:
382 os << (runparams.free_spacing ? " " : "\\qquad{}");
384 case InsetSpaceParams::ENSPACE:
385 os << (runparams.free_spacing ? " " : "\\enspace{}");
387 case InsetSpaceParams::ENSKIP:
388 os << (runparams.free_spacing ? " " : "\\enskip{}");
390 case InsetSpaceParams::NEGTHIN:
391 os << (runparams.free_spacing ? " " : "\\negthinspace{}");
393 case InsetSpaceParams::HFILL:
394 os << (runparams.free_spacing ? " " : "\\hfill{}");
396 case InsetSpaceParams::DOTFILL:
397 os << (runparams.free_spacing ? " " : "\\dotfill{}");
399 case InsetSpaceParams::HRULEFILL:
400 os << (runparams.free_spacing ? " " : "\\hrulefill{}");
402 case InsetSpaceParams::CUSTOM:
403 if (runparams.free_spacing)
406 os << "\\hspace{" << from_ascii(params_.length.asLatexString()) << "}";
408 case InsetSpaceParams::CUSTOM_PROTECTED:
409 if (runparams.free_spacing)
412 os << "\\hspace*{" << from_ascii(params_.length.asLatexString()) << "}";
419 int InsetSpace::plaintext(odocstream & os, OutputParams const &) const
421 switch (params_.kind) {
422 case InsetSpaceParams::HFILL:
425 case InsetSpaceParams::DOTFILL:
428 case InsetSpaceParams::HRULEFILL:
438 int InsetSpace::docbook(odocstream & os, OutputParams const &) const
440 switch (params_.kind) {
441 case InsetSpaceParams::NORMAL:
442 case InsetSpaceParams::QUAD:
443 case InsetSpaceParams::QQUAD:
444 case InsetSpaceParams::ENSKIP:
447 case InsetSpaceParams::PROTECTED:
448 case InsetSpaceParams::ENSPACE:
449 case InsetSpaceParams::THIN:
450 case InsetSpaceParams::NEGTHIN:
453 case InsetSpaceParams::HFILL:
455 case InsetSpaceParams::DOTFILL:
458 case InsetSpaceParams::HRULEFILL:
461 case InsetSpaceParams::CUSTOM:
462 case InsetSpaceParams::CUSTOM_PROTECTED:
470 void InsetSpace::textString(odocstream & os) const
472 plaintext(os, OutputParams(0));
476 bool InsetSpace::isStretchableSpace() const
478 return (params_.kind == InsetSpaceParams::HFILL ||
479 params_.kind == InsetSpaceParams::DOTFILL ||
480 params_.kind == InsetSpaceParams::HRULEFILL);
484 string const InsetSpaceMailer::name_ = "space";
487 InsetSpaceMailer::InsetSpaceMailer(InsetSpace & inset)
492 string const InsetSpaceMailer::inset2string(Buffer const &) const
494 return params2string(inset_.params());
498 void InsetSpaceMailer::string2params(string const & in, InsetSpaceParams & params)
500 params = InsetSpaceParams();
504 istringstream data(in);
510 if (!lex || name != name_)
511 return print_mailer_error("InsetSpaceMailer", in, 1, name_);
517 string const InsetSpaceMailer::params2string(InsetSpaceParams const & params)
520 data << name_ << ' ';