]> git.lyx.org Git - features.git/blob - src/insets/InsetBox.cpp
InsetBox.cpp: fix #4776; the additional \makebox is superfluous because \makebox...
[features.git] / src / insets / InsetBox.cpp
1 /**
2  * \file InsetBox.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  * \author Martin Vermeer
8  * \author Jürgen Spitzmüller
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "InsetBox.h"
16
17 #include "Buffer.h"
18 #include "BufferParams.h"
19 #include "BufferView.h"
20 #include "Cursor.h"
21 #include "DispatchResult.h"
22 #include "FuncStatus.h"
23 #include "FuncRequest.h"
24 #include "LaTeXFeatures.h"
25 #include "Lexer.h"
26 #include "MetricsInfo.h"
27 #include "output_xhtml.h"
28 #include "TextClass.h"
29
30 #include "support/debug.h"
31 #include "support/docstream.h"
32 #include "support/gettext.h"
33 #include "support/lstrings.h"
34 #include "support/Translator.h"
35
36 #include "frontends/Application.h"
37
38 #include <sstream>
39
40 using namespace std;
41 using namespace lyx::support;
42
43 namespace lyx {
44
45 namespace {
46
47 typedef Translator<string, InsetBox::BoxType> BoxTranslator;
48 typedef Translator<docstring, InsetBox::BoxType> BoxTranslatorLoc;
49
50 BoxTranslator initBoxtranslator()
51 {
52         BoxTranslator translator("Boxed", InsetBox::Boxed);
53         translator.addPair("Frameless", InsetBox::Frameless);
54         translator.addPair("Framed", InsetBox::Framed);
55         translator.addPair("ovalbox", InsetBox::ovalbox);
56         translator.addPair("Ovalbox", InsetBox::Ovalbox);
57         translator.addPair("Shadowbox", InsetBox::Shadowbox);
58         translator.addPair("Shaded", InsetBox::Shaded);
59         translator.addPair("Doublebox",InsetBox::Doublebox);
60         return translator;
61 }
62
63
64 BoxTranslatorLoc initBoxtranslatorLoc()
65 {
66         BoxTranslatorLoc translator(_("simple frame"), InsetBox::Boxed);
67         translator.addPair(_("frameless"), InsetBox::Frameless);
68         translator.addPair(_("simple frame, page breaks"), InsetBox::Framed);
69         translator.addPair(_("oval, thin"), InsetBox::ovalbox);
70         translator.addPair(_("oval, thick"), InsetBox::Ovalbox);
71         translator.addPair(_("drop shadow"), InsetBox::Shadowbox);
72         translator.addPair(_("shaded background"), InsetBox::Shaded);
73         translator.addPair(_("double frame"), InsetBox::Doublebox);
74         return translator;
75 }
76
77
78 BoxTranslator const & boxtranslator()
79 {
80         static BoxTranslator translator = initBoxtranslator();
81         return translator;
82 }
83
84
85 BoxTranslatorLoc const & boxtranslator_loc()
86 {
87         static BoxTranslatorLoc translator = initBoxtranslatorLoc();
88         return translator;
89 }
90
91 } // namespace anon
92
93
94 /////////////////////////////////////////////////////////////////////////
95 //
96 // InsetBox
97 //
98 /////////////////////////////////////////////////////////////////////////
99
100 InsetBox::InsetBox(Buffer * buffer, string const & label)
101         : InsetCollapsable(buffer), params_(label)
102 {}
103
104
105 docstring InsetBox::name() const 
106 {
107         // FIXME: UNICODE
108         string name = "Box";
109         if (boxtranslator().find(params_.type) == Shaded)
110                 name += ":Shaded";
111         return from_ascii(name);
112 }
113
114
115 void InsetBox::write(ostream & os) const
116 {
117         params_.write(os);
118         InsetCollapsable::write(os);
119 }
120
121
122 void InsetBox::read(Lexer & lex)
123 {
124         params_.read(lex);
125         InsetCollapsable::read(lex);
126 }
127
128
129 void InsetBox::setButtonLabel()
130 {
131         BoxType const btype = boxtranslator().find(params_.type);
132
133         docstring const type = _("Box");
134
135         docstring inner;
136         if (params_.inner_box) {
137                 if (params_.use_parbox)
138                         inner = _("Parbox");
139                 else
140                         inner = _("Minipage");
141         }
142
143         docstring frame;
144         if (btype != Frameless)
145                 frame = boxtranslator_loc().find(btype);
146
147         docstring label;
148         if (inner.empty() && frame.empty())
149                 label = type;
150         else if (inner.empty())
151                 label = bformat(_("%1$s (%2$s)"),
152                         type, frame);
153         else if (frame.empty())
154                 label = bformat(_("%1$s (%2$s)"),
155                         type, inner);
156         else
157                 label = bformat(_("%1$s (%2$s, %3$s)"),
158                         type, inner, frame);
159         setLabel(label);
160 }
161
162
163 bool InsetBox::hasFixedWidth() const
164 {
165         return params_.inner_box || params_.special != "width";
166 }
167
168
169 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
170 {
171         // back up textwidth.
172         int textwidth_backup = m.base.textwidth;
173         if (hasFixedWidth())
174                 m.base.textwidth = params_.width.inPixels(m.base.textwidth);
175         InsetCollapsable::metrics(m, dim);
176         // retore textwidth.
177         m.base.textwidth = textwidth_backup;
178 }
179
180
181 bool InsetBox::forcePlainLayout(idx_type) const
182 {
183         return !params_.inner_box && params_.type != "Framed";
184 }
185
186
187 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
188 {
189         switch (cmd.action()) {
190
191         case LFUN_INSET_MODIFY: {
192                 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
193                 if (cmd.getArg(0) == "changetype")
194                         params_.type = cmd.getArg(1);
195                 else
196                         string2params(to_utf8(cmd.argument()), params_);
197                 setButtonLabel();
198                 break;
199         }
200
201         default:
202                 InsetCollapsable::doDispatch(cur, cmd);
203                 break;
204         }
205 }
206
207
208 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
209                 FuncStatus & flag) const
210 {
211         switch (cmd.action()) {
212
213         case LFUN_INSET_MODIFY:
214                 if (cmd.getArg(0) == "changetype")
215                         flag.setOnOff(cmd.getArg(1) == params_.type);
216                 flag.setEnabled(true);
217                 return true;
218
219         case LFUN_INSET_DIALOG_UPDATE:
220                 flag.setEnabled(true);
221                 return true;
222
223         case LFUN_BREAK_PARAGRAPH:
224                 if (params_.inner_box || params_.type == "Framed")
225                         return InsetCollapsable::getStatus(cur, cmd, flag);
226                 flag.setEnabled(false);
227                 return true;
228
229         default:
230                 return InsetCollapsable::getStatus(cur, cmd, flag);
231         }
232 }
233
234
235 int InsetBox::latex(odocstream & os, OutputParams const & runparams) const
236 {
237         BoxType btype = boxtranslator().find(params_.type);
238
239         string width_string = params_.width.asLatexString();
240         bool stdwidth = false;
241         if (params_.inner_box &&
242                         (width_string.find("1.0\\columnwidth") != string::npos
243                         || width_string.find("1.0\\textwidth") != string::npos)) {
244                 stdwidth = true;
245                 switch (btype) {
246                 case Frameless:
247                 case Framed:
248                         break;
249                 case Boxed:
250                 case Shaded:
251                         width_string += " - 2\\fboxsep - 2\\fboxrule";
252                         break;
253                 case ovalbox:
254                         width_string += " - 2\\fboxsep - 0.8pt";
255                         break;
256                 case Ovalbox:
257                         width_string += " - 2\\fboxsep - 1.6pt";
258                         break;
259                 case Shadowbox:
260                         // Shadow falls outside right margin... opinions?
261                         width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
262                         break;
263                 case Doublebox:
264                         width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
265                         break;
266                 }
267         }
268
269         int i = 0;
270         os << "%\n";
271         // Adapt to column/text width correctly also if paragraphs indented:
272         if (stdwidth)
273                 os << "\\noindent";
274
275         switch (btype) {
276         case Frameless:
277                 break;
278         case Framed:
279                 os << "\\begin{framed}%\n";
280                 i += 1;
281                 break;
282         case Boxed:
283                 os << "\\framebox";
284                 if (!params_.inner_box) {
285                         // Special widths, see usrguide §3.5
286                         // FIXME UNICODE
287                         if (params_.special != "none") {
288                                 os << "[" << params_.width.value()
289                                    << '\\' << from_utf8(params_.special)
290                                    << ']';
291                         } else
292                                 os << '[' << from_ascii(width_string)
293                                    << ']';
294                         if (params_.hor_pos != 'c')
295                                 os << "[" << params_.hor_pos << "]";
296                 }
297
298                 os << "{";
299                 break;
300         case ovalbox:
301                 os << "\\ovalbox{";
302                 break;
303         case Ovalbox:
304                 os << "\\Ovalbox{";
305                 break;
306         case Shadowbox:
307                 os << "\\shadowbox{";
308                 break;
309         case Shaded:
310                 // later
311                 break;
312         case Doublebox:
313                 os << "\\doublebox{";
314                 break;
315         }
316
317         if (params_.inner_box) {
318                 if (params_.use_parbox)
319                         os << "\\parbox";
320                 else
321                         os << "\\begin{minipage}";
322
323                 os << "[" << params_.pos << "]";
324                 if (params_.height_special == "none") {
325                         // FIXME UNICODE
326                         os << "[" << from_ascii(params_.height.asLatexString()) << "]";
327                 } else {
328                         // Special heights
329                         // set no optional argument when the value is the default "1\height"
330                         // (special units like \height are handled as "in")
331                         // but when the user has chosen a non-default inner_pos, the height
332                         // must be given: \minipage[pos][height][inner-pos]{width}
333                         if ((params_.height != Length("1in") ||
334                                  params_.height_special != "totalheight") ||
335                                 params_.inner_pos != params_.pos) {
336                                 // FIXME UNICODE
337                                 os << "[" << params_.height.value()
338                                         << "\\" << from_utf8(params_.height_special) << "]";
339                         }
340                 }
341                 if (params_.inner_pos != params_.pos)
342                         os << "[" << params_.inner_pos << "]";
343
344                 // FIXME UNICODE
345                 os << '{' << from_ascii(width_string) << '}';
346
347                 if (params_.use_parbox)
348                         os << "{";
349                 os << "%\n";
350                 ++i;
351         }
352         if (btype == Shaded) {
353                 os << "\\begin{shaded}%\n";
354                 ++i;
355         }
356
357         i += InsetText::latex(os, runparams);
358
359         if (btype == Shaded)
360                 os << "\\end{shaded}";
361
362         if (params_.inner_box) {
363                 if (params_.use_parbox)
364                         os << "%\n}";
365                 else
366                         os << "%\n\\end{minipage}";
367         }
368
369         switch (btype) {
370         case Frameless:
371                 break;
372         case Framed:
373                 os << "\\end{framed}";
374                 break;
375         case Boxed:
376                 os << "}";
377                 break;
378         case ovalbox:
379         case Ovalbox:
380         case Doublebox:
381         case Shadowbox:
382                 os << "}";
383                 break;
384         case Shaded:
385                 // already done
386                 break;
387         }
388
389         i += 2;
390
391         return i;
392 }
393
394
395 int InsetBox::plaintext(odocstream & os, OutputParams const & runparams) const
396 {
397         BoxType const btype = boxtranslator().find(params_.type);
398
399         switch (btype) {
400                 case Frameless:
401                         break;
402                 case Framed:
403                 case Boxed:
404                         os << "[\n";
405                         break;
406                 case ovalbox:
407                         os << "(\n";
408                         break;
409                 case Ovalbox:
410                         os << "((\n";
411                         break;
412                 case Shadowbox:
413                 case Shaded:
414                         os << "[/\n";
415                         break;
416                 case Doublebox:
417                         os << "[[\n";
418                         break;
419         }
420
421         InsetText::plaintext(os, runparams);
422
423         int len = 0;
424         switch (btype) {
425                 case Frameless:
426                         os << "\n";
427                         break;
428                 case Framed:
429                 case Boxed:
430                         os << "\n]";
431                         len = 1;
432                         break;
433                 case ovalbox:
434                         os << "\n)";
435                         len = 1;
436                         break;
437                 case Ovalbox:
438                         os << "\n))";
439                         len = 2;
440                         break;
441                 case Shadowbox:
442                 case Shaded:
443                         os << "\n/]";
444                         len = 2;
445                         break;
446                 case Doublebox:
447                         os << "\n]]";
448                         len = 2;
449                         break;
450         }
451
452         return PLAINTEXT_NEWLINE + len; // len chars on a separate line
453 }
454
455
456 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
457 {
458         return InsetText::docbook(os, runparams);
459 }
460
461
462 docstring InsetBox::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
463 {
464         // construct attributes
465         string attrs = "class='" + params_.type + "'";
466         string style;
467         if (!params_.width.empty())
468                 style += ("width: " + params_.width.asHTMLString() + "; ");
469         // The special heights don't really mean anything for us.
470         if (!params_.height.empty() && params_.height_special == "none")
471                 style += ("height: " + params_.height.asHTMLString() + "; ");
472         if (!style.empty())
473                 attrs += " style='" + style + "'";
474
475         xs << html::StartTag("div", attrs);
476         XHTMLOptions const opts = InsetText::WriteLabel | InsetText::WriteInnerTag;
477         docstring defer = InsetText::insetAsXHTML(xs, runparams, opts);
478         xs << html::EndTag("div");
479         xs << defer;
480         return docstring();
481 }
482
483
484 void InsetBox::validate(LaTeXFeatures & features) const
485 {
486         BoxType btype = boxtranslator().find(params_.type);
487         switch (btype) {
488         case Frameless:
489                 break;
490         case Framed:
491                 features.require("framed");
492                 break;
493         case Boxed:
494                 features.require("calc");
495                 break;
496         case ovalbox:
497         case Ovalbox:
498         case Shadowbox:
499         case Doublebox:
500                 features.require("calc");
501                 features.require("fancybox");
502                 break;
503         case Shaded:
504                 features.require("color");
505                 features.require("framed");
506                 break;
507         }
508         InsetCollapsable::validate(features);
509 }
510
511
512 docstring InsetBox::contextMenu(BufferView const &, int, int) const
513 {
514         return from_ascii("context-box");
515 }
516
517
518 string InsetBox::params2string(InsetBoxParams const & params)
519 {
520         ostringstream data;
521         data << "box" << ' ';
522         params.write(data);
523         return data.str();
524 }
525
526
527 void InsetBox::string2params(string const & in, InsetBoxParams & params)
528 {
529         params = InsetBoxParams(string());
530         if (in.empty())
531                 return;
532
533         istringstream data(in);
534         Lexer lex;
535         lex.setStream(data);
536
537         string name;
538         lex >> name;
539         if (!lex || name != "box") {
540                 LYXERR0("InsetBox::string2params(" << in << ")\n"
541                                           "Expected arg 1 to be \"box\"\n");
542                 return;
543         }
544
545         // This is part of the inset proper that is usually swallowed
546         // by Text::readInset
547         string id;
548         lex >> id;
549         if (!lex || id != "Box") {
550                 LYXERR0("InsetBox::string2params(" << in << ")\n"
551                                           "Expected arg 2 to be \"Box\"\n");
552         }
553
554         params.read(lex);
555 }
556
557
558 /////////////////////////////////////////////////////////////////////////
559 //
560 // InsetBoxParams
561 //
562 /////////////////////////////////////////////////////////////////////////
563
564 InsetBoxParams::InsetBoxParams(string const & label)
565         : type(label),
566           use_parbox(false),
567           inner_box(true),
568           width(Length("100col%")),
569           special("none"),
570           pos('t'),
571           hor_pos('c'),
572           inner_pos('t'),
573           height(Length("1in")),
574           height_special("totalheight") // default is 1\\totalheight
575 {}
576
577
578 void InsetBoxParams::write(ostream & os) const
579 {
580         os << "Box " << type << "\n";
581         os << "position \"" << pos << "\"\n";
582         os << "hor_pos \"" << hor_pos << "\"\n";
583         os << "has_inner_box " << inner_box << "\n";
584         os << "inner_pos \"" << inner_pos << "\"\n";
585         os << "use_parbox " << use_parbox << "\n";
586         os << "width \"" << width.asString() << "\"\n";
587         os << "special \"" << special << "\"\n";
588         os << "height \"" << height.asString() << "\"\n";
589         os << "height_special \"" << height_special << "\"\n";
590 }
591
592
593 void InsetBoxParams::read(Lexer & lex)
594 {
595         lex.setContext("InsetBoxParams::read");
596         lex >> type;
597         lex >> "position" >> pos;
598         lex >> "hor_pos" >> hor_pos;
599         lex >> "has_inner_box" >> inner_box;
600         if (type == "Framed")
601                 inner_box = false;
602         lex >> "inner_pos" >> inner_pos;
603         lex >> "use_parbox" >> use_parbox;
604         lex >> "width" >> width;
605         lex >> "special" >> special;
606         lex >> "height" >> height;
607         lex >> "height_special" >> height_special;
608 }
609
610
611 } // namespace lyx