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