]> git.lyx.org Git - features.git/blob - src/insets/InsetSpace.cpp
* InsetSpace drawing cosmetics, from Helge.
[features.git] / src / insets / InsetSpace.cpp
1 /**
2  * \file InsetSpace.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup Nielsen
7  * \author Jean-Marc Lasgouttes
8  * \author Lars Gullik Bjønnes
9  * \author Jürgen Spitzmüller
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "InsetSpace.h"
17
18 #include "BufferView.h"
19 #include "Cursor.h"
20 #include "Dimension.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
23 #include "Length.h"
24 #include "Lexer.h"
25 #include "MetricsInfo.h"
26 #include "OutputParams.h"
27
28 #include "support/debug.h"
29 #include "support/docstream.h"
30 #include "support/gettext.h"
31 #include "support/lstrings.h"
32
33 #include "frontends/Application.h"
34 #include "frontends/FontMetrics.h"
35 #include "frontends/Painter.h"
36
37 using namespace std;
38
39 namespace lyx {
40
41
42 InsetSpace::InsetSpace(InsetSpaceParams const & params)
43         : params_(params)
44 {}
45
46
47 InsetSpaceParams::Kind InsetSpace::kind() const
48 {
49         return params_.kind;
50 }
51
52
53 Length InsetSpace::length() const
54 {
55         return params_.length;
56 }
57
58
59 InsetSpace::~InsetSpace()
60 {
61         hideDialogs("space", this);
62 }
63
64
65 docstring InsetSpace::toolTip(BufferView const &, int, int) const
66 {
67         docstring message;
68         switch (params_.kind) {
69         case InsetSpaceParams::NORMAL:
70                 message = _("Interword Space");
71                 break;
72         case InsetSpaceParams::PROTECTED:
73                 message = _("Protected Space");
74                 break;
75         case InsetSpaceParams::THIN:
76                 message = _("Thin Space");
77                 break;
78         case InsetSpaceParams::QUAD:
79                 message = _("Quad Space");
80                 break;
81         case InsetSpaceParams::QQUAD:
82                 message = _("QQuad Space");
83                 break;
84         case InsetSpaceParams::ENSPACE:
85                 message = _("Enspace");
86                 break;
87         case InsetSpaceParams::ENSKIP:
88                 message = _("Enskip");
89                 break;
90         case InsetSpaceParams::NEGTHIN:
91                 message = _("Negative Thin Space");
92                 break;
93         case InsetSpaceParams::HFILL:
94                 message = _("Horizontal Fill");
95                 break;
96         case InsetSpaceParams::HFILL_PROTECTED:
97                 message = _("Protected Horizontal Fill");
98                 break;
99         case InsetSpaceParams::DOTFILL:
100                 message = _("Horizontal Fill (Dots)");
101                 break;
102         case InsetSpaceParams::HRULEFILL:
103                 message = _("Horizontal Fill (Rule)");
104                 break;
105         case InsetSpaceParams::LEFTARROWFILL:
106                 message = _("Horizontal Fill (Left Arrow)");
107                 break;
108         case InsetSpaceParams::RIGHTARROWFILL:
109                 message = _("Horizontal Fill (Right Arrow)");
110                 break;
111         case InsetSpaceParams::UPBRACEFILL:
112                 message = _("Horizontal Fill (Up Brace)");
113                 break;
114         case InsetSpaceParams::DOWNBRACEFILL:
115                 message = _("Horizontal Fill (Down Brace)");
116                 break;
117         case InsetSpaceParams::CUSTOM:
118                 message = support::bformat(_("Horizontal Space (%1$s)"),
119                                 params_.length.asDocstring());
120                 break;
121         case InsetSpaceParams::CUSTOM_PROTECTED:
122                 message = support::bformat(_("Protected Horizontal Space (%1$s)"),
123                                 params_.length.asDocstring());
124                 break;
125         }
126         return message;
127 }
128
129
130 void InsetSpace::doDispatch(Cursor & cur, FuncRequest & cmd)
131 {
132         switch (cmd.action) {
133
134         case LFUN_INSET_MODIFY: {
135                 string2params(to_utf8(cmd.argument()), params_);
136                 break;
137         }
138
139         case LFUN_MOUSE_RELEASE:
140                 if (!cur.selection() && cmd.button() == mouse_button::button1)
141                         cur.bv().showDialog("space", params2string(params()), this);
142                 break;
143
144         default:
145                 Inset::doDispatch(cur, cmd);
146                 break;
147         }
148 }
149
150
151 bool InsetSpace::getStatus(Cursor & cur, FuncRequest const & cmd,
152         FuncStatus & status) const
153 {
154         switch (cmd.action) {
155         // we handle these
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);
161                 } else {
162                         status.enabled(true);
163                 }
164                 return true;
165         default:
166                 return Inset::getStatus(cur, cmd, status);
167         }
168 }
169
170
171 void InsetSpace::edit(Cursor & cur, bool, EntryDirection)
172 {
173         cur.bv().showDialog("space", params2string(params()), this);
174 }
175
176
177 void InsetSpace::metrics(MetricsInfo & mi, Dimension & dim) const
178 {
179         if (isStretchableSpace()) {
180                 // The metrics for this kinds are calculated externally in
181                 // \c TextMetrics::computeRowMetrics. Those are dummy value:
182                 dim = Dimension(10, 10, 10);
183                 return;
184         }
185
186         frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
187         dim.asc = fm.maxAscent();
188         dim.des = fm.maxDescent();
189
190         switch (params_.kind) {
191                 case InsetSpaceParams::THIN:
192                 case InsetSpaceParams::NEGTHIN:
193                         dim.wid = fm.width(char_type('M')) / 6;
194                         break;
195                 case InsetSpaceParams::PROTECTED:
196                 case InsetSpaceParams::NORMAL:
197                         dim.wid = fm.width(char_type(' '));
198                         break;
199                 case InsetSpaceParams::QUAD:
200                         dim.wid = fm.width(char_type('M'));
201                         break;
202                 case InsetSpaceParams::QQUAD:
203                         dim.wid = 2 * fm.width(char_type('M'));
204                         break;
205                 case InsetSpaceParams::ENSPACE:
206                 case InsetSpaceParams::ENSKIP:
207                         dim.wid = int(0.5 * fm.width(char_type('M')));
208                         break;
209                 case InsetSpaceParams::CUSTOM:
210                 case InsetSpaceParams::CUSTOM_PROTECTED:
211                         dim.wid = params_.length.inBP();
212                         break;
213                 case InsetSpaceParams::HFILL:
214                 case InsetSpaceParams::HFILL_PROTECTED:
215                 case InsetSpaceParams::DOTFILL:
216                 case InsetSpaceParams::HRULEFILL:
217                 case InsetSpaceParams::LEFTARROWFILL:
218                 case InsetSpaceParams::RIGHTARROWFILL:
219                 case InsetSpaceParams::UPBRACEFILL:
220                 case InsetSpaceParams::DOWNBRACEFILL:
221                         // shut up compiler
222                         break;
223         }
224         // Cache the inset dimension.
225         setDimCache(mi, dim);
226 }
227
228
229 void InsetSpace::draw(PainterInfo & pi, int x, int y) const
230 {
231         Dimension const dim = dimension(*pi.base.bv);
232
233         if (isStretchableSpace()) {
234                 int const asc = theFontMetrics(pi.base.font).ascent('M');
235                 int const desc = theFontMetrics(pi.base.font).descent('M');
236                 //Pixel height divisible by 2 for prettier fill graphics:
237                 int const oddheight = (asc ^ desc) & 1;
238                 int const x0 = x + 1;
239                 int const x1 = x + dim.wid - 2;
240                 int const y0 = y + desc - 1;
241                 int const y1 = y - asc + oddheight - 1;
242                 int const y2 = (y0 + y1) / 2;
243                 int const xoffset = (y0 - y1) / 2;
244                 int const x2 = x0 + xoffset;
245                 int const x3 = x1 - xoffset;
246                 int const xm = (x0 + x1) / 2;
247                 int const xml = xm - xoffset;
248                 int const xmr = xm + xoffset;
249
250                 if (params_.kind == InsetSpaceParams::HFILL) {
251                         pi.pain.line(x0, y1, x0, y0, Color_added_space);
252                         pi.pain.line(x0, y2 , x1, y2, Color_added_space,
253                                 frontend::Painter::line_onoffdash);
254                         pi.pain.line(x1, y1, x1, y0, Color_added_space);
255                 } else if (params_.kind == InsetSpaceParams::HFILL_PROTECTED) {
256                         pi.pain.line(x0, y1, x0, y0, Color_latex);
257                         pi.pain.line(x0, y2 , x1, y2, Color_latex,
258                                 frontend::Painter::line_onoffdash);
259                         pi.pain.line(x1, y1, x1, y0, Color_latex);
260                 } else if (params_.kind == InsetSpaceParams::DOTFILL) {
261                         pi.pain.line(x0, y1, x0, y0, Color_special);
262                         pi.pain.line(x0, y0, x1, y0, Color_special,
263                                 frontend::Painter::line_onoffdash);
264                         pi.pain.line(x1, y1, x1, y0, Color_special);
265                 } else if (params_.kind == InsetSpaceParams::HRULEFILL) {
266                         pi.pain.line(x0, y1, x0, y0, Color_special);
267                         pi.pain.line(x0, y0, x1, y0, Color_special);
268                         pi.pain.line(x1, y1, x1, y0, Color_special);
269                 } else if (params_.kind == InsetSpaceParams::LEFTARROWFILL) {
270                         pi.pain.line(x2, y1 + 1 , x0 + 1, y2, Color_special);
271                         pi.pain.line(x0 + 1, y2 + 1 , x2, y0, Color_special);
272                         pi.pain.line(x0, y2 , x1, y2, Color_special);
273                 } else if (params_.kind == InsetSpaceParams::RIGHTARROWFILL) {
274                         pi.pain.line(x3 + 1, y1 + 1 , x1, y2, Color_special);
275                         pi.pain.line(x1, y2 + 1 , x3 + 1, y0, Color_special);
276                         pi.pain.line(x0, y2 , x1, y2, Color_special);
277                 } else if (params_.kind == InsetSpaceParams::UPBRACEFILL) {
278                         pi.pain.line(x0 + 1, y1 + 1 , x2, y2, Color_special);
279                         pi.pain.line(x2, y2 , xml, y2, Color_special);
280                         pi.pain.line(xml + 1, y2 + 1 , xm, y0, Color_special);
281                         pi.pain.line(xm + 1, y0 , xmr, y2 + 1, Color_special);
282                         pi.pain.line(xmr, y2 , x3, y2, Color_special);
283                         pi.pain.line(x3 + 1, y2 , x1, y1 + 1, Color_special);
284                 } else if (params_.kind == InsetSpaceParams::DOWNBRACEFILL) {
285                         pi.pain.line(x0 + 1, y0 , x2, y2 + 1, Color_special);
286                         pi.pain.line(x2, y2 , xml, y2, Color_special);
287                         pi.pain.line(xml + 1, y2 , xm, y1 + 1, Color_special);
288                         pi.pain.line(xm + 1, y1 + 1 , xmr, y2, Color_special);
289                         pi.pain.line(xmr, y2 , x3, y2, Color_special);
290                         pi.pain.line(x3 + 1, y2 + 1 , x1, y0, Color_special);
291                 }
292                 return;
293         }
294
295         int const w = dim.wid;
296         int const h = theFontMetrics(pi.base.font).ascent('x');
297         int xp[4], yp[4];
298
299         xp[0] = x;
300         yp[0] = y - max(h / 4, 1);
301         if (params_.kind == InsetSpaceParams::NORMAL ||
302             params_.kind == InsetSpaceParams::PROTECTED) {
303                 xp[1] = x;     yp[1] = y;
304                 xp[2] = x + w; yp[2] = y;
305         } else {
306                 xp[1] = x;     yp[1] = y + max(h / 4, 1);
307                 xp[2] = x + w; yp[2] = y + max(h / 4, 1);
308         }
309         xp[3] = x + w;
310         yp[3] = y - max(h / 4, 1);
311
312         if (params_.kind == InsetSpaceParams::PROTECTED ||
313             params_.kind == InsetSpaceParams::ENSPACE ||
314             params_.kind == InsetSpaceParams::NEGTHIN ||
315             params_.kind == InsetSpaceParams::CUSTOM_PROTECTED)
316                 pi.pain.lines(xp, yp, 4, Color_latex);
317         else
318                 pi.pain.lines(xp, yp, 4, Color_special);
319 }
320
321
322 void InsetSpaceParams::write(ostream & os) const
323 {
324         string command;
325         switch (kind) {
326         case InsetSpaceParams::NORMAL:
327                 os << "\\space{}";
328                 break;
329         case InsetSpaceParams::PROTECTED:
330                 os <<  "~";
331                 break;
332         case InsetSpaceParams::THIN:
333                 os <<  "\\thinspace{}";
334                 break;
335         case InsetSpaceParams::QUAD:
336                 os <<  "\\quad{}";
337                 break;
338         case InsetSpaceParams::QQUAD:
339                 os <<  "\\qquad{}";
340                 break;
341         case InsetSpaceParams::ENSPACE:
342                 os <<  "\\enspace{}";
343                 break;
344         case InsetSpaceParams::ENSKIP:
345                 os <<  "\\enskip{}";
346                 break;
347         case InsetSpaceParams::NEGTHIN:
348                 os <<  "\\negthinspace{}";
349                 break;
350         case InsetSpaceParams::HFILL:
351                 os <<  "\\hfill{}";
352                 break;
353         case InsetSpaceParams::HFILL_PROTECTED:
354                 os <<  "\\hspace*{\\fill}";
355                 break;
356         case InsetSpaceParams::DOTFILL:
357                 os <<  "\\dotfill{}";
358                 break;
359         case InsetSpaceParams::HRULEFILL:
360                 os <<  "\\hrulefill{}";
361                 break;
362         case InsetSpaceParams::LEFTARROWFILL:
363                 os <<  "\\leftarrowfill{}";
364                 break;
365         case InsetSpaceParams::RIGHTARROWFILL:
366                 os <<  "\\rightarrowfill{}";
367                 break;
368         case InsetSpaceParams::UPBRACEFILL:
369                 os <<  "\\upbracefill{}";
370                 break;
371         case InsetSpaceParams::DOWNBRACEFILL:
372                 os <<  "\\downbracefill{}";
373                 break;
374         case InsetSpaceParams::CUSTOM:
375                 os <<  "\\hspace{}";
376                 break;
377         case InsetSpaceParams::CUSTOM_PROTECTED:
378                 os <<  "\\hspace*{}";
379                 break;
380         }
381         
382         if (!length.empty())
383                 os << "\n\\length " << length.asString();
384 }
385
386
387 void InsetSpaceParams::read(Lexer & lex)
388 {
389         lex.setContext("InsetSpaceParams::read");
390         string command;
391         lex >> command;
392
393         if (command == "\\space{}")
394                 kind = InsetSpaceParams::NORMAL;
395         else if (command == "~")
396                 kind = InsetSpaceParams::PROTECTED;
397         else if (command == "\\thinspace{}")
398                 kind = InsetSpaceParams::THIN;
399         else if (command == "\\quad{}")
400                 kind = InsetSpaceParams::QUAD;
401         else if (command == "\\qquad{}")
402                 kind = InsetSpaceParams::QQUAD;
403         else if (command == "\\enspace{}")
404                 kind = InsetSpaceParams::ENSPACE;
405         else if (command == "\\enskip{}")
406                 kind = InsetSpaceParams::ENSKIP;
407         else if (command == "\\negthinspace{}")
408                 kind = InsetSpaceParams::NEGTHIN;
409         else if (command == "\\hfill{}")
410                 kind = InsetSpaceParams::HFILL;
411         else if (command == "\\hspace*{\\fill}")
412                 kind = InsetSpaceParams::HFILL_PROTECTED;
413         else if (command == "\\dotfill{}")
414                 kind = InsetSpaceParams::DOTFILL;
415         else if (command == "\\hrulefill{}")
416                 kind = InsetSpaceParams::HRULEFILL;
417         else if (command == "\\hspace{}")
418                 kind = InsetSpaceParams::CUSTOM;
419         else if (command == "\\leftarrowfill{}")
420                 kind = InsetSpaceParams::LEFTARROWFILL;
421         else if (command == "\\rightarrowfill{}")
422                 kind = InsetSpaceParams::RIGHTARROWFILL;
423         else if (command == "\\upbracefill{}")
424                 kind = InsetSpaceParams::UPBRACEFILL;
425         else if (command == "\\downbracefill{}")
426                 kind = InsetSpaceParams::DOWNBRACEFILL;
427         else if (command == "\\hspace*{}")
428                 kind = InsetSpaceParams::CUSTOM_PROTECTED;
429         else
430                 lex.printError("InsetSpace: Unknown kind: `$$Token'");
431
432         if (lex.checkFor("\\length"))
433                 lex >> length;
434         lex >> "\\end_inset";
435 }
436
437
438 void InsetSpace::write(ostream & os) const
439 {
440         os << "Space ";
441         params_.write(os);
442 }
443
444
445 void InsetSpace::read(Lexer & lex)
446 {
447         params_.read(lex);
448 }
449
450
451 int InsetSpace::latex(odocstream & os, OutputParams const & runparams) const
452 {
453         switch (params_.kind) {
454         case InsetSpaceParams::NORMAL:
455                 os << (runparams.free_spacing ? " " : "\\ ");
456                 break;
457         case InsetSpaceParams::PROTECTED:
458                 os << (runparams.free_spacing ? ' ' : '~');
459                 break;
460         case InsetSpaceParams::THIN:
461                 os << (runparams.free_spacing ? " " : "\\,");
462                 break;
463         case InsetSpaceParams::QUAD:
464                 os << (runparams.free_spacing ? " " : "\\quad{}");
465                 break;
466         case InsetSpaceParams::QQUAD:
467                 os << (runparams.free_spacing ? " " : "\\qquad{}");
468                 break;
469         case InsetSpaceParams::ENSPACE:
470                 os << (runparams.free_spacing ? " " : "\\enspace{}");
471                 break;
472         case InsetSpaceParams::ENSKIP:
473                 os << (runparams.free_spacing ? " " : "\\enskip{}");
474                 break;
475         case InsetSpaceParams::NEGTHIN:
476                 os << (runparams.free_spacing ? " " : "\\negthinspace{}");
477                 break;
478         case InsetSpaceParams::HFILL:
479                 os << (runparams.free_spacing ? " " : "\\hfill{}");
480                 break;
481         case InsetSpaceParams::HFILL_PROTECTED:
482                 os << (runparams.free_spacing ? " " : "\\hspace*{\\fill}");
483                 break;
484         case InsetSpaceParams::DOTFILL:
485                 os << (runparams.free_spacing ? " " : "\\dotfill{}");
486                 break;
487         case InsetSpaceParams::HRULEFILL:
488                 os << (runparams.free_spacing ? " " : "\\hrulefill{}");
489                 break;
490         case InsetSpaceParams::LEFTARROWFILL:
491                 os << (runparams.free_spacing ? " " : "\\leftarrowfill{}");
492                 break;
493         case InsetSpaceParams::RIGHTARROWFILL:
494                 os << (runparams.free_spacing ? " " : "\\rightarrowfill{}");
495                 break;
496         case InsetSpaceParams::UPBRACEFILL:
497                 os << (runparams.free_spacing ? " " : "\\upbracefill{}");
498                 break;
499         case InsetSpaceParams::DOWNBRACEFILL:
500                 os << (runparams.free_spacing ? " " : "\\downbracefill{}");
501                 break;
502         case InsetSpaceParams::CUSTOM:
503                 if (runparams.free_spacing)
504                         os << " ";
505                 else
506                         os << "\\hspace{" << from_ascii(params_.length.asLatexString()) << "}";
507                 break;
508         case InsetSpaceParams::CUSTOM_PROTECTED:
509                 if (runparams.free_spacing)
510                         os << " ";
511                 else
512                         os << "\\hspace*{" << from_ascii(params_.length.asLatexString()) << "}";
513                 break;
514         }
515         return 0;
516 }
517
518
519 int InsetSpace::plaintext(odocstream & os, OutputParams const &) const
520 {
521         switch (params_.kind) {
522         case InsetSpaceParams::HFILL:
523         case InsetSpaceParams::HFILL_PROTECTED:
524                 os << "     ";
525                 return 5;
526         case InsetSpaceParams::DOTFILL:
527                 os << ".....";
528                 return 5;
529         case InsetSpaceParams::HRULEFILL:
530                 os << "_____";
531                 return 5;
532         case InsetSpaceParams::LEFTARROWFILL:
533                 os << "<----";
534                 return 5;
535         case InsetSpaceParams::RIGHTARROWFILL:
536                 os << "---->";
537                 return 5;
538         case InsetSpaceParams::UPBRACEFILL:
539                 os << "\\-v-/";
540                 return 5;
541         case InsetSpaceParams::DOWNBRACEFILL:
542                 os << "/-^-\\";
543                 return 5;
544         default:
545                 os << ' ';
546                 return 1;
547         }
548 }
549
550
551 int InsetSpace::docbook(odocstream & os, OutputParams const &) const
552 {
553         switch (params_.kind) {
554         case InsetSpaceParams::NORMAL:
555         case InsetSpaceParams::QUAD:
556         case InsetSpaceParams::QQUAD:
557         case InsetSpaceParams::ENSKIP:
558                 os << " ";
559                 break;
560         case InsetSpaceParams::PROTECTED:
561         case InsetSpaceParams::ENSPACE:
562         case InsetSpaceParams::THIN:
563         case InsetSpaceParams::NEGTHIN:
564                 os << "&nbsp;";
565                 break;
566         case InsetSpaceParams::HFILL:
567         case InsetSpaceParams::HFILL_PROTECTED:
568                 os << '\n';
569         case InsetSpaceParams::DOTFILL:
570                 // FIXME
571                 os << '\n';
572         case InsetSpaceParams::HRULEFILL:
573                 // FIXME
574                 os << '\n';
575         case InsetSpaceParams::LEFTARROWFILL:
576         case InsetSpaceParams::RIGHTARROWFILL:
577         case InsetSpaceParams::UPBRACEFILL:
578         case InsetSpaceParams::DOWNBRACEFILL:
579         case InsetSpaceParams::CUSTOM:
580         case InsetSpaceParams::CUSTOM_PROTECTED:
581                 // FIXME
582                 os << '\n';
583         }
584         return 0;
585 }
586
587
588 void InsetSpace::textString(odocstream & os) const
589 {
590         plaintext(os, OutputParams(0));
591 }
592
593
594 bool InsetSpace::isStretchableSpace() const
595 {
596         return params_.kind == InsetSpaceParams::HFILL
597                 || params_.kind == InsetSpaceParams::HFILL_PROTECTED
598                 || params_.kind == InsetSpaceParams::DOTFILL
599                 || params_.kind == InsetSpaceParams::HRULEFILL
600                 || params_.kind == InsetSpaceParams::LEFTARROWFILL
601                 || params_.kind == InsetSpaceParams::RIGHTARROWFILL
602                 || params_.kind == InsetSpaceParams::UPBRACEFILL
603                 || params_.kind == InsetSpaceParams::DOWNBRACEFILL;
604 }
605
606
607 docstring InsetSpace::contextMenu(BufferView const &, int, int) const
608 {
609         return from_ascii("context-space");
610 }
611
612
613 void InsetSpace::string2params(string const & in, InsetSpaceParams & params)
614 {
615         params = InsetSpaceParams();
616         if (in.empty())
617                 return;
618
619         istringstream data(in);
620         Lexer lex;
621         lex.setStream(data);
622         lex.setContext("InsetSpace::string2params");
623         lex >> "space";
624
625         params.read(lex);
626 }
627
628
629 string InsetSpace::params2string(InsetSpaceParams const & params)
630 {
631         ostringstream data;
632         data << "space" << ' ';
633         params.write(data);
634         return data.str();
635 }
636
637
638 } // namespace lyx