]> git.lyx.org Git - lyx.git/blob - src/insets/InsetBox.cpp
InsetBox.cpp: only shaded boxes can have multiple paragraphs when there is no inner box
[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 if (params_.use_makebox)
140                         inner = _("Makebox");
141                 else
142                         inner = _("Minipage");
143         }
144
145         docstring frame;
146         if (btype != Frameless)
147                 frame = boxtranslator_loc().find(btype);
148
149         docstring label;
150         if (inner.empty() && frame.empty())
151                 label = type;
152         else if (inner.empty())
153                 label = bformat(_("%1$s (%2$s)"),
154                         type, frame);
155         else if (frame.empty())
156                 label = bformat(_("%1$s (%2$s)"),
157                         type, inner);
158         else
159                 label = bformat(_("%1$s (%2$s, %3$s)"),
160                         type, inner, frame);
161         setLabel(label);
162 }
163
164
165 bool InsetBox::hasFixedWidth() const
166 {
167         return params_.inner_box || params_.special != "width";
168 }
169
170
171 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
172 {
173         // back up textwidth.
174         int textwidth_backup = m.base.textwidth;
175         if (hasFixedWidth())
176                 m.base.textwidth = params_.width.inPixels(m.base.textwidth);
177         InsetCollapsable::metrics(m, dim);
178         // retore textwidth.
179         m.base.textwidth = textwidth_backup;
180 }
181
182
183 bool InsetBox::forcePlainLayout(idx_type) const
184 {
185         return !params_.inner_box && params_.type != "Framed";
186 }
187
188
189 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
190 {
191         switch (cmd.action()) {
192
193         case LFUN_INSET_MODIFY: {
194                 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
195                 if (cmd.getArg(0) == "changetype")
196                         params_.type = cmd.getArg(1);
197                 else
198                         string2params(to_utf8(cmd.argument()), params_);
199                 setButtonLabel();
200                 break;
201         }
202
203         default:
204                 InsetCollapsable::doDispatch(cur, cmd);
205                 break;
206         }
207 }
208
209
210 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
211                 FuncStatus & flag) const
212 {
213         switch (cmd.action()) {
214
215         case LFUN_INSET_MODIFY:
216                 if (cmd.getArg(0) == "changetype")
217                         flag.setOnOff(cmd.getArg(1) == params_.type);
218                 flag.setEnabled(true);
219                 return true;
220
221         case LFUN_INSET_DIALOG_UPDATE:
222                 flag.setEnabled(true);
223                 return true;
224
225         case LFUN_BREAK_PARAGRAPH:
226                 if ((params_.inner_box && !params_.use_makebox) || params_.type == "Shaded")
227                         return InsetCollapsable::getStatus(cur, cmd, flag);
228                 flag.setEnabled(false);
229                 return true;
230
231         default:
232                 return InsetCollapsable::getStatus(cur, cmd, flag);
233         }
234 }
235
236
237 int InsetBox::latex(odocstream & os, OutputParams const & runparams) const
238 {
239         BoxType btype = boxtranslator().find(params_.type);
240
241         string width_string = params_.width.asLatexString();
242         bool stdwidth = false;
243         if (params_.inner_box &&
244                         (width_string.find("1.0\\columnwidth") != string::npos
245                         || width_string.find("1.0\\textwidth") != string::npos)) {
246                 stdwidth = true;
247                 switch (btype) {
248                 case Frameless:
249                 case Framed:
250                         break;
251                 case Boxed:
252                 case Shaded:
253                         width_string += " - 2\\fboxsep - 2\\fboxrule";
254                         break;
255                 case ovalbox:
256                         width_string += " - 2\\fboxsep - 0.8pt";
257                         break;
258                 case Ovalbox:
259                         width_string += " - 2\\fboxsep - 1.6pt";
260                         break;
261                 case Shadowbox:
262                         // Shadow falls outside right margin... opinions?
263                         width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
264                         break;
265                 case Doublebox:
266                         width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
267                         break;
268                 }
269         }
270
271         int i = 0;
272         os << "%\n";
273         // Adapt to column/text width correctly also if paragraphs indented:
274         if (stdwidth)
275                 os << "\\noindent";
276
277         switch (btype) {
278         case Frameless:
279                 break;
280         case Framed:
281                 os << "\\begin{framed}%\n";
282                 i += 1;
283                 break;
284         case Boxed:
285                 os << "\\framebox";
286                 if (!params_.inner_box) {
287                         // Special widths, see usrguide §3.5
288                         // FIXME UNICODE
289                         if (params_.special != "none") {
290                                 os << "[" << params_.width.value()
291                                    << '\\' << from_utf8(params_.special)
292                                    << ']';
293                         } else
294                                 os << '[' << from_ascii(width_string)
295                                    << ']';
296                         if (params_.hor_pos != 'c')
297                                 os << "[" << params_.hor_pos << "]";
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 if (params_.use_makebox) {
322                         os << "\\makebox";
323                         // FIXME UNICODE
324                         // output the width and horizontal position
325                         if (params_.special != "none") {
326                                 os << "[" << params_.width.value()
327                                    << '\\' << from_utf8(params_.special)
328                                    << ']';
329                         } else
330                                 os << '[' << from_ascii(width_string)
331                                    << ']';
332                         if (params_.hor_pos != 'c')
333                                 os << "[" << params_.hor_pos << "]";
334                         os << "{";
335                 }
336                 else
337                         os << "\\begin{minipage}";
338
339                 // output parameters for parbox and minipage
340                 if (!params_.use_makebox) {
341                         os << "[" << params_.pos << "]";
342                         if (params_.height_special == "none") {
343                                 // FIXME UNICODE
344                                 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
345                         } else {
346                                 // Special heights
347                                 // set no optional argument when the value is the default "1\height"
348                                 // (special units like \height are handled as "in")
349                                 // but when the user has chosen a non-default inner_pos, the height
350                                 // must be given: \minipage[pos][height][inner-pos]{width}
351                                 if ((params_.height != Length("1in") ||
352                                         params_.height_special != "totalheight") ||
353                                         params_.inner_pos != params_.pos) {
354                                                 // FIXME UNICODE
355                                                 os << "[" << params_.height.value()
356                                                         << "\\" << from_utf8(params_.height_special) << "]";
357                                 }
358                         }
359                         if (params_.inner_pos != params_.pos)
360                                 os << "[" << params_.inner_pos << "]";
361                         // FIXME UNICODE
362                         os << '{' << from_ascii(width_string) << '}';
363                         if (params_.use_parbox)
364                                 os << "{";
365                 }
366
367                 os << "%\n";
368                 ++i;
369         } // end if inner_box
370
371         if (btype == Shaded) {
372                 os << "\\begin{shaded}%\n";
373                 ++i;
374         }
375
376         i += InsetText::latex(os, runparams);
377
378         if (btype == Shaded)
379                 os << "\\end{shaded}";
380
381         if (params_.inner_box) {
382                 if (params_.use_parbox || params_.use_makebox)
383                         os << "%\n}";
384                 else
385                         os << "%\n\\end{minipage}";
386         }
387
388         switch (btype) {
389         case Frameless:
390                 break;
391         case Framed:
392                 os << "\\end{framed}";
393                 break;
394         case Boxed:
395                 os << "}";
396                 break;
397         case ovalbox:
398         case Ovalbox:
399         case Doublebox:
400         case Shadowbox:
401                 os << "}";
402                 break;
403         case Shaded:
404                 // already done
405                 break;
406         }
407
408         i += 2;
409
410         return i;
411 }
412
413
414 int InsetBox::plaintext(odocstream & os, OutputParams const & runparams) const
415 {
416         BoxType const btype = boxtranslator().find(params_.type);
417
418         switch (btype) {
419                 case Frameless:
420                         break;
421                 case Framed:
422                 case Boxed:
423                         os << "[\n";
424                         break;
425                 case ovalbox:
426                         os << "(\n";
427                         break;
428                 case Ovalbox:
429                         os << "((\n";
430                         break;
431                 case Shadowbox:
432                 case Shaded:
433                         os << "[/\n";
434                         break;
435                 case Doublebox:
436                         os << "[[\n";
437                         break;
438         }
439
440         InsetText::plaintext(os, runparams);
441
442         int len = 0;
443         switch (btype) {
444                 case Frameless:
445                         os << "\n";
446                         break;
447                 case Framed:
448                 case Boxed:
449                         os << "\n]";
450                         len = 1;
451                         break;
452                 case ovalbox:
453                         os << "\n)";
454                         len = 1;
455                         break;
456                 case Ovalbox:
457                         os << "\n))";
458                         len = 2;
459                         break;
460                 case Shadowbox:
461                 case Shaded:
462                         os << "\n/]";
463                         len = 2;
464                         break;
465                 case Doublebox:
466                         os << "\n]]";
467                         len = 2;
468                         break;
469         }
470
471         return PLAINTEXT_NEWLINE + len; // len chars on a separate line
472 }
473
474
475 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
476 {
477         return InsetText::docbook(os, runparams);
478 }
479
480
481 docstring InsetBox::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
482 {
483         // construct attributes
484         string attrs = "class='" + params_.type + "'";
485         string style;
486         if (!params_.width.empty())
487                 style += ("width: " + params_.width.asHTMLString() + "; ");
488         // The special heights don't really mean anything for us.
489         if (!params_.height.empty() && params_.height_special == "none")
490                 style += ("height: " + params_.height.asHTMLString() + "; ");
491         if (!style.empty())
492                 attrs += " style='" + style + "'";
493
494         xs << html::StartTag("div", attrs);
495         XHTMLOptions const opts = InsetText::WriteLabel | InsetText::WriteInnerTag;
496         docstring defer = InsetText::insetAsXHTML(xs, runparams, opts);
497         xs << html::EndTag("div");
498         xs << defer;
499         return docstring();
500 }
501
502
503 void InsetBox::validate(LaTeXFeatures & features) const
504 {
505         BoxType btype = boxtranslator().find(params_.type);
506         switch (btype) {
507         case Frameless:
508                 break;
509         case Framed:
510                 features.require("framed");
511                 break;
512         case Boxed:
513                 features.require("calc");
514                 break;
515         case ovalbox:
516         case Ovalbox:
517         case Shadowbox:
518         case Doublebox:
519                 features.require("calc");
520                 features.require("fancybox");
521                 break;
522         case Shaded:
523                 features.require("color");
524                 features.require("framed");
525                 break;
526         }
527         InsetCollapsable::validate(features);
528 }
529
530
531 docstring InsetBox::contextMenu(BufferView const &, int, int) const
532 {
533         return from_ascii("context-box");
534 }
535
536
537 string InsetBox::params2string(InsetBoxParams const & params)
538 {
539         ostringstream data;
540         data << "box" << ' ';
541         params.write(data);
542         return data.str();
543 }
544
545
546 void InsetBox::string2params(string const & in, InsetBoxParams & params)
547 {
548         params = InsetBoxParams(string());
549         if (in.empty())
550                 return;
551
552         istringstream data(in);
553         Lexer lex;
554         lex.setStream(data);
555
556         string name;
557         lex >> name;
558         if (!lex || name != "box") {
559                 LYXERR0("InsetBox::string2params(" << in << ")\n"
560                                           "Expected arg 1 to be \"box\"\n");
561                 return;
562         }
563
564         // This is part of the inset proper that is usually swallowed
565         // by Text::readInset
566         string id;
567         lex >> id;
568         if (!lex || id != "Box") {
569                 LYXERR0("InsetBox::string2params(" << in << ")\n"
570                                           "Expected arg 2 to be \"Box\"\n");
571         }
572
573         params.read(lex);
574 }
575
576
577 /////////////////////////////////////////////////////////////////////////
578 //
579 // InsetBoxParams
580 //
581 /////////////////////////////////////////////////////////////////////////
582
583 InsetBoxParams::InsetBoxParams(string const & label)
584         : type(label),
585           use_parbox(false),
586           use_makebox(false),
587           inner_box(true),
588           width(Length("100col%")),
589           special("none"),
590           pos('t'),
591           hor_pos('c'),
592           inner_pos('t'),
593           height(Length("1in")),
594           height_special("totalheight") // default is 1\\totalheight
595 {}
596
597
598 void InsetBoxParams::write(ostream & os) const
599 {
600         os << "Box " << type << "\n";
601         os << "position \"" << pos << "\"\n";
602         os << "hor_pos \"" << hor_pos << "\"\n";
603         os << "has_inner_box " << inner_box << "\n";
604         os << "inner_pos \"" << inner_pos << "\"\n";
605         os << "use_parbox " << use_parbox << "\n";
606         os << "use_makebox " << use_makebox << "\n";
607         os << "width \"" << width.asString() << "\"\n";
608         os << "special \"" << special << "\"\n";
609         os << "height \"" << height.asString() << "\"\n";
610         os << "height_special \"" << height_special << "\"\n";
611 }
612
613
614 void InsetBoxParams::read(Lexer & lex)
615 {
616         lex.setContext("InsetBoxParams::read");
617         lex >> type;
618         lex >> "position" >> pos;
619         lex >> "hor_pos" >> hor_pos;
620         lex >> "has_inner_box" >> inner_box;
621         if (type == "Framed")
622                 inner_box = false;
623         lex >> "inner_pos" >> inner_pos;
624         lex >> "use_parbox" >> use_parbox;
625         lex >> "use_makebox" >> use_makebox;
626         lex >> "width" >> width;
627         lex >> "special" >> special;
628         lex >> "height" >> height;
629         lex >> "height_special" >> height_special;
630 }
631
632
633 } // namespace lyx