]> git.lyx.org Git - lyx.git/blob - src/insets/InsetBox.cpp
Pure HTML output for math macros.
[lyx.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                         os << "{\\makebox";
286                         // Special widths, see usrguide §3.5
287                         // FIXME UNICODE
288                         if (params_.special != "none") {
289                                 os << "[" << params_.width.value()
290                                    << '\\' << from_utf8(params_.special)
291                                    << ']';
292                         } else
293                                 os << '[' << from_ascii(width_string)
294                                    << ']';
295                         if (params_.hor_pos != 'c')
296                                 os << "[" << params_.hor_pos << "]";
297                 }
298
299                 os << "{";
300                 break;
301         case ovalbox:
302                 os << "\\ovalbox{";
303                 break;
304         case Ovalbox:
305                 os << "\\Ovalbox{";
306                 break;
307         case Shadowbox:
308                 os << "\\shadowbox{";
309                 break;
310         case Shaded:
311                 // later
312                 break;
313         case Doublebox:
314                 os << "\\doublebox{";
315                 break;
316         }
317
318         if (params_.inner_box) {
319                 if (params_.use_parbox)
320                         os << "\\parbox";
321                 else
322                         os << "\\begin{minipage}";
323
324                 os << "[" << params_.pos << "]";
325                 if (params_.height_special == "none") {
326                         // FIXME UNICODE
327                         os << "[" << from_ascii(params_.height.asLatexString()) << "]";
328                 } else {
329                         // Special heights
330                         // set no optional argument when the value is the default "1\height"
331                         // (special units like \height are handled as "in")
332                         // but when the user has chosen a non-default inner_pos, the height
333                         // must be given: \minipage[pos][height][inner-pos]{width}
334                         if ((params_.height != Length("1in") ||
335                                  params_.height_special != "totalheight") ||
336                                 params_.inner_pos != params_.pos) {
337                                 // FIXME UNICODE
338                                 os << "[" << params_.height.value()
339                                         << "\\" << from_utf8(params_.height_special) << "]";
340                         }
341                 }
342                 if (params_.inner_pos != params_.pos)
343                         os << "[" << params_.inner_pos << "]";
344
345                 // FIXME UNICODE
346                 os << '{' << from_ascii(width_string) << '}';
347
348                 if (params_.use_parbox)
349                         os << "{";
350                 os << "%\n";
351                 ++i;
352         }
353         if (btype == Shaded) {
354                 os << "\\begin{shaded}%\n";
355                 ++i;
356         }
357
358         i += InsetText::latex(os, runparams);
359
360         if (btype == Shaded)
361                 os << "\\end{shaded}";
362
363         if (params_.inner_box) {
364                 if (params_.use_parbox)
365                         os << "%\n}";
366                 else
367                         os << "%\n\\end{minipage}";
368         }
369
370         switch (btype) {
371         case Frameless:
372                 break;
373         case Framed:
374                 os << "\\end{framed}";
375                 break;
376         case Boxed:
377                 if (!params_.inner_box)
378                         os << "}"; // for makebox
379                 os << "}";
380                 break;
381         case ovalbox:
382         case Ovalbox:
383         case Doublebox:
384         case Shadowbox:
385                 os << "}";
386                 break;
387         case Shaded:
388                 // already done
389                 break;
390         }
391
392         i += 2;
393
394         return i;
395 }
396
397
398 int InsetBox::plaintext(odocstream & os, OutputParams const & runparams) const
399 {
400         BoxType const btype = boxtranslator().find(params_.type);
401
402         switch (btype) {
403                 case Frameless:
404                         break;
405                 case Framed:
406                 case Boxed:
407                         os << "[\n";
408                         break;
409                 case ovalbox:
410                         os << "(\n";
411                         break;
412                 case Ovalbox:
413                         os << "((\n";
414                         break;
415                 case Shadowbox:
416                 case Shaded:
417                         os << "[/\n";
418                         break;
419                 case Doublebox:
420                         os << "[[\n";
421                         break;
422         }
423
424         InsetText::plaintext(os, runparams);
425
426         int len = 0;
427         switch (btype) {
428                 case Frameless:
429                         os << "\n";
430                         break;
431                 case Framed:
432                 case Boxed:
433                         os << "\n]";
434                         len = 1;
435                         break;
436                 case ovalbox:
437                         os << "\n)";
438                         len = 1;
439                         break;
440                 case Ovalbox:
441                         os << "\n))";
442                         len = 2;
443                         break;
444                 case Shadowbox:
445                 case Shaded:
446                         os << "\n/]";
447                         len = 2;
448                         break;
449                 case Doublebox:
450                         os << "\n]]";
451                         len = 2;
452                         break;
453         }
454
455         return PLAINTEXT_NEWLINE + len; // len chars on a separate line
456 }
457
458
459 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
460 {
461         return InsetText::docbook(os, runparams);
462 }
463
464
465 docstring InsetBox::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
466 {
467         // construct attributes
468         string attrs = "class='" + params_.type + "'";
469         string style;
470         if (!params_.width.empty())
471                 style += ("width: " + params_.width.asHTMLString() + "; ");
472         // The special heights don't really mean anything for us.
473         if (!params_.height.empty() && params_.height_special == "none")
474                 style += ("height: " + params_.height.asHTMLString() + "; ");
475         if (!style.empty())
476                 attrs += " style='" + style + "'";
477
478         xs << html::StartTag("div", attrs);
479         XHTMLOptions const opts = InsetText::WriteLabel | InsetText::WriteInnerTag;
480         docstring defer = InsetText::insetAsXHTML(xs, runparams, opts);
481         xs << html::EndTag("div");
482         xs << defer;
483         return docstring();
484 }
485
486
487 void InsetBox::validate(LaTeXFeatures & features) const
488 {
489         BoxType btype = boxtranslator().find(params_.type);
490         switch (btype) {
491         case Frameless:
492                 break;
493         case Framed:
494                 features.require("framed");
495                 break;
496         case Boxed:
497                 features.require("calc");
498                 break;
499         case ovalbox:
500         case Ovalbox:
501         case Shadowbox:
502         case Doublebox:
503                 features.require("calc");
504                 features.require("fancybox");
505                 break;
506         case Shaded:
507                 features.require("color");
508                 features.require("framed");
509                 break;
510         }
511         InsetCollapsable::validate(features);
512 }
513
514
515 docstring InsetBox::contextMenu(BufferView const &, int, int) const
516 {
517         return from_ascii("context-box");
518 }
519
520
521 string InsetBox::params2string(InsetBoxParams const & params)
522 {
523         ostringstream data;
524         data << "box" << ' ';
525         params.write(data);
526         return data.str();
527 }
528
529
530 void InsetBox::string2params(string const & in, InsetBoxParams & params)
531 {
532         params = InsetBoxParams(string());
533         if (in.empty())
534                 return;
535
536         istringstream data(in);
537         Lexer lex;
538         lex.setStream(data);
539
540         string name;
541         lex >> name;
542         if (!lex || name != "box") {
543                 LYXERR0("InsetBox::string2params(" << in << ")\n"
544                                           "Expected arg 1 to be \"box\"\n");
545                 return;
546         }
547
548         // This is part of the inset proper that is usually swallowed
549         // by Text::readInset
550         string id;
551         lex >> id;
552         if (!lex || id != "Box") {
553                 LYXERR0("InsetBox::string2params(" << in << ")\n"
554                                           "Expected arg 2 to be \"Box\"\n");
555         }
556
557         params.read(lex);
558 }
559
560
561 /////////////////////////////////////////////////////////////////////////
562 //
563 // InsetBoxParams
564 //
565 /////////////////////////////////////////////////////////////////////////
566
567 InsetBoxParams::InsetBoxParams(string const & label)
568         : type(label),
569           use_parbox(false),
570           inner_box(true),
571           width(Length("100col%")),
572           special("none"),
573           pos('t'),
574           hor_pos('c'),
575           inner_pos('t'),
576           height(Length("1in")),
577           height_special("totalheight") // default is 1\\totalheight
578 {}
579
580
581 void InsetBoxParams::write(ostream & os) const
582 {
583         os << "Box " << type << "\n";
584         os << "position \"" << pos << "\"\n";
585         os << "hor_pos \"" << hor_pos << "\"\n";
586         os << "has_inner_box " << inner_box << "\n";
587         os << "inner_pos \"" << inner_pos << "\"\n";
588         os << "use_parbox " << use_parbox << "\n";
589         os << "width \"" << width.asString() << "\"\n";
590         os << "special \"" << special << "\"\n";
591         os << "height \"" << height.asString() << "\"\n";
592         os << "height_special \"" << height_special << "\"\n";
593 }
594
595
596 void InsetBoxParams::read(Lexer & lex)
597 {
598         lex.setContext("InsetBoxParams::read");
599         lex >> type;
600         lex >> "position" >> pos;
601         lex >> "hor_pos" >> hor_pos;
602         lex >> "has_inner_box" >> inner_box;
603         if (type == "Framed")
604                 inner_box = false;
605         lex >> "inner_pos" >> inner_pos;
606         lex >> "use_parbox" >> use_parbox;
607         lex >> "width" >> width;
608         lex >> "special" >> special;
609         lex >> "height" >> height;
610         lex >> "height_special" >> height_special;
611 }
612
613
614 } // namespace lyx