]> git.lyx.org Git - lyx.git/blob - src/insets/InsetBox.cpp
a07df279c3de2e513ad0df7cd0bb8057e15a1d81
[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 "ColorSet.h"
21 #include "Cursor.h"
22 #include "DispatchResult.h"
23 #include "FuncStatus.h"
24 #include "FuncRequest.h"
25 #include "LaTeXFeatures.h"
26 #include "Lexer.h"
27 #include "MetricsInfo.h"
28 #include "output_xhtml.h"
29 #include "TextClass.h"
30
31 #include "support/debug.h"
32 #include "support/docstream.h"
33 #include "support/gettext.h"
34 #include "support/lstrings.h"
35 #include "support/Translator.h"
36
37 #include "frontends/Application.h"
38
39 #include <sstream>
40
41 using namespace std;
42 using namespace lyx::support;
43
44 namespace lyx {
45
46 namespace {
47
48 typedef Translator<string, InsetBox::BoxType> BoxTranslator;
49 typedef Translator<docstring, InsetBox::BoxType> BoxTranslatorLoc;
50
51 BoxTranslator initBoxtranslator()
52 {
53         BoxTranslator translator("Boxed", InsetBox::Boxed);
54         translator.addPair("Frameless", InsetBox::Frameless);
55         translator.addPair("Framed", InsetBox::Framed);
56         translator.addPair("ovalbox", InsetBox::ovalbox);
57         translator.addPair("Ovalbox", InsetBox::Ovalbox);
58         translator.addPair("Shadowbox", InsetBox::Shadowbox);
59         translator.addPair("Shaded", InsetBox::Shaded);
60         translator.addPair("Doublebox",InsetBox::Doublebox);
61         return translator;
62 }
63
64
65 BoxTranslatorLoc initBoxtranslatorLoc()
66 {
67         BoxTranslatorLoc translator(_("simple frame"), InsetBox::Boxed);
68         translator.addPair(_("frameless"), InsetBox::Frameless);
69         translator.addPair(_("simple frame, page breaks"), InsetBox::Framed);
70         translator.addPair(_("oval, thin"), InsetBox::ovalbox);
71         translator.addPair(_("oval, thick"), InsetBox::Ovalbox);
72         translator.addPair(_("drop shadow"), InsetBox::Shadowbox);
73         translator.addPair(_("shaded background"), InsetBox::Shaded);
74         translator.addPair(_("double frame"), InsetBox::Doublebox);
75         return translator;
76 }
77
78
79 BoxTranslator const & boxtranslator()
80 {
81         static BoxTranslator translator = initBoxtranslator();
82         return translator;
83 }
84
85
86 BoxTranslatorLoc const & boxtranslator_loc()
87 {
88         static BoxTranslatorLoc translator = initBoxtranslatorLoc();
89         return translator;
90 }
91
92 } // namespace anon
93
94
95 /////////////////////////////////////////////////////////////////////////
96 //
97 // InsetBox
98 //
99 /////////////////////////////////////////////////////////////////////////
100
101 InsetBox::InsetBox(Buffer * buffer, string const & label)
102         : InsetCollapsable(buffer), params_(label)
103 {}
104
105
106 docstring InsetBox::layoutName() const
107 {
108         // FIXME: UNICODE
109         return from_ascii("Box:" + params_.type);
110 }
111
112
113 void InsetBox::write(ostream & os) const
114 {
115         params_.write(os);
116         InsetCollapsable::write(os);
117 }
118
119
120 void InsetBox::read(Lexer & lex)
121 {
122         params_.read(lex);
123         InsetCollapsable::read(lex);
124 }
125
126
127 void InsetBox::setButtonLabel()
128 {
129         BoxType const btype = boxtranslator().find(params_.type);
130
131         docstring const type = _("Box");
132
133         docstring inner;
134         if (params_.inner_box) {
135                 if (params_.use_parbox)
136                         inner = _("Parbox");
137                 else if (params_.use_makebox)
138                         inner = _("Makebox");
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_.width.empty();
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_.use_makebox)
184                 && params_.type != "Shaded" && params_.type != "Framed";
185 }
186
187
188 ColorCode InsetBox::backgroundColor(PainterInfo const &) const
189 {
190         if (params_.type != "Shaded")
191                 return getLayout().bgcolor();
192         // FIXME: This hardcoded color is a hack!
193         if (buffer().params().boxbgcolor == lyx::rgbFromHexName("#ff0000"))
194                 return getLayout().bgcolor();
195         ColorCode c = lcolor.getFromLyXName("boxbgcolor");
196         if (c == Color_none)
197                 return getLayout().bgcolor();
198         return c;
199 }
200
201
202 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
203 {
204         switch (cmd.action()) {
205
206         case LFUN_INSET_MODIFY: {
207                 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
208                 string const first_arg = cmd.getArg(0);
209                 bool const change_type = first_arg == "changetype";
210                 bool const for_box = first_arg == "box";
211                 if (!change_type && !for_box) {
212                         // not for us
213                         // this will not be handled higher up
214                         cur.undispatched();
215                         return;
216                 }
217                 cur.recordUndoInset(ATOMIC_UNDO, this);
218                 if (change_type)
219                         params_.type = cmd.getArg(1);
220                 else // if (for_box)
221                         string2params(to_utf8(cmd.argument()), params_);
222                 setButtonLabel();
223                 break;
224         }
225
226         default:
227                 InsetCollapsable::doDispatch(cur, cmd);
228                 break;
229         }
230 }
231
232
233 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
234                 FuncStatus & flag) const
235 {
236         switch (cmd.action()) {
237
238         case LFUN_INSET_MODIFY: {
239                 string const first_arg = cmd.getArg(0);
240                 if (first_arg == "changetype") {
241                         string const type = cmd.getArg(1);
242                         flag.setOnOff(type == params_.type);
243                         flag.setEnabled(!params_.inner_box || type != "Framed");
244                         return true;
245                 }
246                 if (first_arg == "box") {
247                         flag.setEnabled(true);
248                         return true;
249                 }
250                 return InsetCollapsable::getStatus(cur, cmd, flag);
251         }
252
253         case LFUN_INSET_DIALOG_UPDATE:
254                 flag.setEnabled(true);
255                 return true;
256
257         case LFUN_PARAGRAPH_BREAK:
258                 if ((params_.inner_box && !params_.use_makebox)
259                      || params_.type == "Shaded" || params_.type == "Framed")
260                         return InsetCollapsable::getStatus(cur, cmd, flag);
261                 flag.setEnabled(false);
262                 return true;
263
264         default:
265                 return InsetCollapsable::getStatus(cur, cmd, flag);
266         }
267 }
268
269
270 void InsetBox::latex(otexstream & os, OutputParams const & runparams) const
271 {
272         BoxType btype = boxtranslator().find(params_.type);
273
274         string width_string = params_.width.asLatexString();
275         bool stdwidth = false;
276         // FIXME: do not test explicitely values of width_string
277         if (params_.inner_box &&
278                         (width_string.find("1.0\\columnwidth") != string::npos
279                         || width_string.find("1.0\\textwidth") != string::npos)) {
280                 stdwidth = true;
281                 switch (btype) {
282                 case Frameless:
283                 case Framed:
284                         break;
285                 case Boxed:
286                 case Shaded:
287                         width_string += " - 2\\fboxsep - 2\\fboxrule";
288                         break;
289                 case ovalbox:
290                         width_string += " - 2\\fboxsep - 0.8pt";
291                         break;
292                 case Ovalbox:
293                         width_string += " - 2\\fboxsep - 1.6pt";
294                         break;
295                 case Shadowbox:
296                         // Shadow falls outside right margin... opinions?
297                         width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
298                         break;
299                 case Doublebox:
300                         width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
301                         break;
302                 }
303         }
304
305         os << safebreakln;
306         if (runparams.lastid != -1)
307                 os.texrow().start(runparams.lastid, runparams.lastpos);
308
309         // Adapt to column/text width correctly also if paragraphs indented:
310         if (stdwidth)
311                 os << "\\noindent";
312
313         switch (btype) {
314         case Frameless:
315                 break;
316         case Framed:
317                 os << "\\begin{framed}%\n";
318                 break;
319         case Boxed:
320                 if (width_string.empty()) {
321                         os << "\\framebox";
322                         if (!params_.inner_box) {
323                                 // Special widths, see usrguide sec. 3.5
324                                 // FIXME UNICODE
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                         }
335                 } else
336                         os << "\\fbox";
337                 os << "{";
338                 break;
339         case ovalbox:
340                 os << "\\ovalbox{";
341                 break;
342         case Ovalbox:
343                 os << "\\Ovalbox{";
344                 break;
345         case Shadowbox:
346                 os << "\\shadowbox{";
347                 break;
348         case Shaded:
349                 // must be set later because e.g. the width settings only work when
350                 // it is inside a minipage or parbox
351                 break;
352         case Doublebox:
353                 os << "\\doublebox{";
354                 break;
355         }
356
357         if (params_.inner_box) {
358                 if (params_.use_parbox)
359                         os << "\\parbox";
360                 else if (params_.use_makebox) {
361                         if (!width_string.empty()) {
362                                 os << "\\makebox";
363                                 // FIXME UNICODE
364                                 // output the width and horizontal position
365                                 if (params_.special != "none") {
366                                         os << "[" << params_.width.value()
367                                            << '\\' << from_utf8(params_.special)
368                                            << ']';
369                                 } else
370                                         os << '[' << from_ascii(width_string)
371                                            << ']';
372                                 if (params_.hor_pos != 'c')
373                                         os << "[" << params_.hor_pos << "]";
374                         } else
375                                 os << "\\mbox";
376                         os << "{";
377                 }
378                 else
379                         os << "\\begin{minipage}";
380
381                 // output parameters for parbox and minipage
382                 if (!params_.use_makebox) {
383                         os << "[" << params_.pos << "]";
384                         if (params_.height_special == "none") {
385                                 // FIXME UNICODE
386                                 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
387                         } else {
388                                 // Special heights
389                                 // set no optional argument when the value is the default "1\height"
390                                 // (special units like \height are handled as "in")
391                                 // but when the user has chosen a non-default inner_pos, the height
392                                 // must be given: \minipage[pos][height][inner-pos]{width}
393                                 if ((params_.height != Length("1in") ||
394                                         params_.height_special != "totalheight") ||
395                                         params_.inner_pos != params_.pos) {
396                                                 // FIXME UNICODE
397                                                 os << "[" << params_.height.value()
398                                                         << "\\" << from_utf8(params_.height_special) << "]";
399                                 }
400                         }
401                         if (params_.inner_pos != params_.pos)
402                                 os << "[" << params_.inner_pos << "]";
403                         // FIXME UNICODE
404                         os << '{' << from_ascii(width_string) << '}';
405                         if (params_.use_parbox)
406                                 os << "{";
407                 }
408
409                 os << "%\n";
410         } // end if inner_box
411
412         if (btype == Shaded) {
413                 os << "\\begin{shaded}%\n";
414         }
415
416         InsetText::latex(os, runparams);
417
418         if (btype == Shaded)
419                 os << "\\end{shaded}";
420
421         if (params_.inner_box) {
422                 if (params_.use_parbox || params_.use_makebox)
423                         os << "%\n}";
424                 else
425                         os << "%\n\\end{minipage}";
426         }
427
428         switch (btype) {
429         case Frameless:
430                 break;
431         case Framed:
432                 os << "\\end{framed}";
433                 break;
434         case Boxed:
435                 os << "}";
436                 break;
437         case ovalbox:
438         case Ovalbox:
439         case Doublebox:
440         case Shadowbox:
441                 os << "}";
442                 break;
443         case Shaded:
444                 // already done
445                 break;
446         }
447 }
448
449
450 int InsetBox::plaintext(odocstringstream & os,
451        OutputParams const & runparams, size_t max_length) const
452 {
453         BoxType const btype = boxtranslator().find(params_.type);
454
455         switch (btype) {
456                 case Frameless:
457                         break;
458                 case Framed:
459                 case Boxed:
460                         os << "[\n";
461                         break;
462                 case ovalbox:
463                         os << "(\n";
464                         break;
465                 case Ovalbox:
466                         os << "((\n";
467                         break;
468                 case Shadowbox:
469                 case Shaded:
470                         os << "[/\n";
471                         break;
472                 case Doublebox:
473                         os << "[[\n";
474                         break;
475         }
476
477         InsetText::plaintext(os, runparams, max_length);
478
479         int len = 0;
480         switch (btype) {
481                 case Frameless:
482                         os << "\n";
483                         break;
484                 case Framed:
485                 case Boxed:
486                         os << "\n]";
487                         len = 1;
488                         break;
489                 case ovalbox:
490                         os << "\n)";
491                         len = 1;
492                         break;
493                 case Ovalbox:
494                         os << "\n))";
495                         len = 2;
496                         break;
497                 case Shadowbox:
498                 case Shaded:
499                         os << "\n/]";
500                         len = 2;
501                         break;
502                 case Doublebox:
503                         os << "\n]]";
504                         len = 2;
505                         break;
506         }
507
508         return PLAINTEXT_NEWLINE + len; // len chars on a separate line
509 }
510
511
512 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
513 {
514         return InsetText::docbook(os, runparams);
515 }
516
517
518 docstring InsetBox::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
519 {
520         // construct attributes
521         string attrs = "class='" + params_.type + "'";
522         string style;
523         if (!params_.width.empty()) {
524                 string w = params_.width.asHTMLString();
525                 if (w != "100%")
526                         style += ("width: " + params_.width.asHTMLString() + "; ");
527         }
528         // The special heights don't really mean anything for us.
529         if (!params_.height.empty() && params_.height_special == "none")
530                 style += ("height: " + params_.height.asHTMLString() + "; ");
531         if (!style.empty())
532                 attrs += " style='" + style + "'";
533
534         xs << html::StartTag("div", attrs);
535         XHTMLOptions const opts = InsetText::WriteLabel | InsetText::WriteInnerTag;
536         docstring defer = InsetText::insetAsXHTML(xs, runparams, opts);
537         xs << html::EndTag("div");
538         xs << defer;
539         return docstring();
540 }
541
542
543 void InsetBox::validate(LaTeXFeatures & features) const
544 {
545         BoxType btype = boxtranslator().find(params_.type);
546         switch (btype) {
547         case Frameless:
548                 break;
549         case Framed:
550                 features.require("framed");
551                 break;
552         case Boxed:
553                 features.require("calc");
554                 break;
555         case ovalbox:
556         case Ovalbox:
557         case Shadowbox:
558         case Doublebox:
559                 features.require("calc");
560                 features.require("fancybox");
561                 break;
562         case Shaded:
563                 features.require("color");
564                 features.require("framed");
565                 break;
566         }
567         InsetCollapsable::validate(features);
568 }
569
570
571 string InsetBox::contextMenuName() const
572 {
573         return "context-box";
574 }
575
576
577 string InsetBox::params2string(InsetBoxParams const & params)
578 {
579         ostringstream data;
580         data << "box" << ' ';
581         params.write(data);
582         return data.str();
583 }
584
585
586 void InsetBox::string2params(string const & in, InsetBoxParams & params)
587 {
588         if (in.empty())
589                 return;
590
591         istringstream data(in);
592         Lexer lex;
593         lex.setStream(data);
594
595         string name;
596         lex >> name;
597         if (!lex || name != "box") {
598                 LYXERR0("InsetBox::string2params(" << in << ")\n"
599                                           "Expected arg 1 to be \"box\"\n");
600                 return;
601         }
602
603         // This is part of the inset proper that is usually swallowed
604         // by Text::readInset
605         string id;
606         lex >> id;
607         if (!lex || id != "Box") {
608                 LYXERR0("InsetBox::string2params(" << in << ")\n"
609                                           "Expected arg 2 to be \"Box\"\n");
610         }
611
612         params = InsetBoxParams(string());
613         params.read(lex);
614 }
615
616
617 /////////////////////////////////////////////////////////////////////////
618 //
619 // InsetBoxParams
620 //
621 /////////////////////////////////////////////////////////////////////////
622
623 InsetBoxParams::InsetBoxParams(string const & label)
624         : type(label),
625           use_parbox(false),
626           use_makebox(false),
627           inner_box(true),
628           width(Length("100col%")),
629           special("none"),
630           pos('t'),
631           hor_pos('c'),
632           inner_pos('t'),
633           height(Length("1in")),
634           height_special("totalheight") // default is 1\\totalheight
635 {}
636
637
638 void InsetBoxParams::write(ostream & os) const
639 {
640         os << "Box " << type << "\n";
641         os << "position \"" << pos << "\"\n";
642         os << "hor_pos \"" << hor_pos << "\"\n";
643         os << "has_inner_box " << inner_box << "\n";
644         os << "inner_pos \"" << inner_pos << "\"\n";
645         os << "use_parbox " << use_parbox << "\n";
646         os << "use_makebox " << use_makebox << "\n";
647         os << "width \"" << width.asString() << "\"\n";
648         os << "special \"" << special << "\"\n";
649         os << "height \"" << height.asString() << "\"\n";
650         os << "height_special \"" << height_special << "\"\n";
651 }
652
653
654 void InsetBoxParams::read(Lexer & lex)
655 {
656         lex.setContext("InsetBoxParams::read");
657         lex >> type;
658         lex >> "position" >> pos;
659         lex >> "hor_pos" >> hor_pos;
660         lex >> "has_inner_box" >> inner_box;
661         if (type == "Framed")
662                 inner_box = false;
663         lex >> "inner_pos" >> inner_pos;
664         lex >> "use_parbox" >> use_parbox;
665         lex >> "use_makebox" >> use_makebox;
666         lex >> "width" >> width;
667         lex >> "special" >> special;
668         lex >> "height" >> height;
669         lex >> "height_special" >> height_special;
670 }
671
672
673 } // namespace lyx