]> git.lyx.org Git - lyx.git/blob - src/insets/InsetBox.cpp
InsetBox.cpp: add important note
[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::layoutName() const
106 {
107         // FIXME: UNICODE
108         return from_ascii("Box:" + params_.type);
109 }
110
111
112 void InsetBox::write(ostream & os) const
113 {
114         params_.write(os);
115         InsetCollapsable::write(os);
116 }
117
118
119 void InsetBox::read(Lexer & lex)
120 {
121         params_.read(lex);
122         InsetCollapsable::read(lex);
123 }
124
125
126 void InsetBox::setButtonLabel()
127 {
128         BoxType const btype = boxtranslator().find(params_.type);
129
130         docstring const type = _("Box");
131
132         docstring inner;
133         if (params_.inner_box) {
134                 if (params_.use_parbox)
135                         inner = _("Parbox");
136                 else if (params_.use_makebox)
137                         inner = _("Makebox");
138                 else
139                         inner = _("Minipage");
140         }
141
142         docstring frame;
143         if (btype != Frameless)
144                 frame = boxtranslator_loc().find(btype);
145
146         docstring label;
147         if (inner.empty() && frame.empty())
148                 label = type;
149         else if (inner.empty())
150                 label = bformat(_("%1$s (%2$s)"),
151                         type, frame);
152         else if (frame.empty())
153                 label = bformat(_("%1$s (%2$s)"),
154                         type, inner);
155         else
156                 label = bformat(_("%1$s (%2$s, %3$s)"),
157                         type, inner, frame);
158         setLabel(label);
159 }
160
161
162 bool InsetBox::hasFixedWidth() const
163 {
164         return params_.inner_box || params_.special != "width";
165 }
166
167
168 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
169 {
170         // back up textwidth.
171         int textwidth_backup = m.base.textwidth;
172         if (hasFixedWidth())
173                 m.base.textwidth = params_.width.inPixels(m.base.textwidth);
174         InsetCollapsable::metrics(m, dim);
175         // retore textwidth.
176         m.base.textwidth = textwidth_backup;
177 }
178
179
180 bool InsetBox::forcePlainLayout(idx_type) const
181 {
182         return (!params_.inner_box || params_.use_makebox)
183                 && params_.type != "Shaded" && 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                 cur.recordUndoInset(ATOMIC_UNDO, this);
194                 if (cmd.getArg(0) == "changetype") {
195                         params_.type = cmd.getArg(1);
196                 } else
197                         string2params(to_utf8(cmd.argument()), params_);
198                 setButtonLabel();
199                 break;
200         }
201
202         default:
203                 InsetCollapsable::doDispatch(cur, cmd);
204                 break;
205         }
206 }
207
208
209 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
210                 FuncStatus & flag) const
211 {
212         switch (cmd.action()) {
213
214         case LFUN_INSET_MODIFY:
215                 if (cmd.getArg(0) == "changetype")
216                         flag.setOnOff(cmd.getArg(1) == params_.type);
217                 flag.setEnabled(true);
218                 return true;
219
220         case LFUN_INSET_DIALOG_UPDATE:
221                 flag.setEnabled(true);
222                 return true;
223
224         case LFUN_BREAK_PARAGRAPH:
225                 if ((params_.inner_box && !params_.use_makebox)
226                      || params_.type == "Shaded" || params_.type == "Framed")
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 void InsetBox::latex(otexstream & 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         os << safebreakln;
272         if (runparams.lastid != -1)
273                 os.texrow().start(runparams.lastid, runparams.lastpos);
274
275         // Adapt to column/text width correctly also if paragraphs indented:
276         if (stdwidth)
277                 os << "\\noindent";
278
279         switch (btype) {
280         case Frameless:
281                 break;
282         case Framed:
283                 os << "\\begin{framed}%\n";
284                 break;
285         case Boxed:
286                 os << "\\framebox";
287                 if (!params_.inner_box) {
288                         // Special widths, see usrguide §3.5
289                         // FIXME UNICODE
290                         if (params_.special != "none") {
291                                 os << "[" << params_.width.value()
292                                    << '\\' << from_utf8(params_.special)
293                                    << ']';
294                         } else
295                                 os << '[' << from_ascii(width_string)
296                                    << ']';
297                         if (params_.hor_pos != 'c')
298                                 os << "[" << params_.hor_pos << "]";
299                 }
300                 os << "{";
301                 break;
302         case ovalbox:
303                 os << "\\ovalbox{";
304                 break;
305         case Ovalbox:
306                 os << "\\Ovalbox{";
307                 break;
308         case Shadowbox:
309                 os << "\\shadowbox{";
310                 break;
311         case Shaded:
312                 // must be set later becaue e.g. the width settings only work when
313                 // it is inside a minipage or parbox
314                 break;
315         case Doublebox:
316                 os << "\\doublebox{";
317                 break;
318         }
319
320         if (params_.inner_box) {
321                 if (params_.use_parbox)
322                         os << "\\parbox";
323                 else if (params_.use_makebox) {
324                         os << "\\makebox";
325                         // FIXME UNICODE
326                         // output the width and horizontal position
327                         if (params_.special != "none") {
328                                 os << "[" << params_.width.value()
329                                    << '\\' << from_utf8(params_.special)
330                                    << ']';
331                         } else
332                                 os << '[' << from_ascii(width_string)
333                                    << ']';
334                         if (params_.hor_pos != 'c')
335                                 os << "[" << params_.hor_pos << "]";
336                         os << "{";
337                 }
338                 else
339                         os << "\\begin{minipage}";
340
341                 // output parameters for parbox and minipage
342                 if (!params_.use_makebox) {
343                         os << "[" << params_.pos << "]";
344                         if (params_.height_special == "none") {
345                                 // FIXME UNICODE
346                                 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
347                         } else {
348                                 // Special heights
349                                 // set no optional argument when the value is the default "1\height"
350                                 // (special units like \height are handled as "in")
351                                 // but when the user has chosen a non-default inner_pos, the height
352                                 // must be given: \minipage[pos][height][inner-pos]{width}
353                                 if ((params_.height != Length("1in") ||
354                                         params_.height_special != "totalheight") ||
355                                         params_.inner_pos != params_.pos) {
356                                                 // FIXME UNICODE
357                                                 os << "[" << params_.height.value()
358                                                         << "\\" << from_utf8(params_.height_special) << "]";
359                                 }
360                         }
361                         if (params_.inner_pos != params_.pos)
362                                 os << "[" << params_.inner_pos << "]";
363                         // FIXME UNICODE
364                         os << '{' << from_ascii(width_string) << '}';
365                         if (params_.use_parbox)
366                                 os << "{";
367                 }
368
369                 os << "%\n";
370         } // end if inner_box
371
372         if (btype == Shaded) {
373                 os << "\\begin{shaded}%\n";
374         }
375
376         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
409
410 int InsetBox::plaintext(odocstream & os, OutputParams const & runparams) const
411 {
412         BoxType const btype = boxtranslator().find(params_.type);
413
414         switch (btype) {
415                 case Frameless:
416                         break;
417                 case Framed:
418                 case Boxed:
419                         os << "[\n";
420                         break;
421                 case ovalbox:
422                         os << "(\n";
423                         break;
424                 case Ovalbox:
425                         os << "((\n";
426                         break;
427                 case Shadowbox:
428                 case Shaded:
429                         os << "[/\n";
430                         break;
431                 case Doublebox:
432                         os << "[[\n";
433                         break;
434         }
435
436         InsetText::plaintext(os, runparams);
437
438         int len = 0;
439         switch (btype) {
440                 case Frameless:
441                         os << "\n";
442                         break;
443                 case Framed:
444                 case Boxed:
445                         os << "\n]";
446                         len = 1;
447                         break;
448                 case ovalbox:
449                         os << "\n)";
450                         len = 1;
451                         break;
452                 case Ovalbox:
453                         os << "\n))";
454                         len = 2;
455                         break;
456                 case Shadowbox:
457                 case Shaded:
458                         os << "\n/]";
459                         len = 2;
460                         break;
461                 case Doublebox:
462                         os << "\n]]";
463                         len = 2;
464                         break;
465         }
466
467         return PLAINTEXT_NEWLINE + len; // len chars on a separate line
468 }
469
470
471 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
472 {
473         return InsetText::docbook(os, runparams);
474 }
475
476
477 docstring InsetBox::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
478 {
479         // construct attributes
480         string attrs = "class='" + params_.type + "'";
481         string style;
482         if (!params_.width.empty()) {
483                 string w = params_.width.asHTMLString();
484                 if (w != "100%")
485                         style += ("width: " + params_.width.asHTMLString() + "; ");
486         }
487         // The special heights don't really mean anything for us.
488         if (!params_.height.empty() && params_.height_special == "none")
489                 style += ("height: " + params_.height.asHTMLString() + "; ");
490         if (!style.empty())
491                 attrs += " style='" + style + "'";
492
493         xs << html::StartTag("div", attrs);
494         XHTMLOptions const opts = InsetText::WriteLabel | InsetText::WriteInnerTag;
495         docstring defer = InsetText::insetAsXHTML(xs, runparams, opts);
496         xs << html::EndTag("div");
497         xs << defer;
498         return docstring();
499 }
500
501
502 void InsetBox::validate(LaTeXFeatures & features) const
503 {
504         BoxType btype = boxtranslator().find(params_.type);
505         switch (btype) {
506         case Frameless:
507                 break;
508         case Framed:
509                 features.require("framed");
510                 break;
511         case Boxed:
512                 features.require("calc");
513                 break;
514         case ovalbox:
515         case Ovalbox:
516         case Shadowbox:
517         case Doublebox:
518                 features.require("calc");
519                 features.require("fancybox");
520                 break;
521         case Shaded:
522                 features.require("color");
523                 features.require("framed");
524                 break;
525         }
526         InsetCollapsable::validate(features);
527 }
528
529
530 docstring InsetBox::contextMenuName() const
531 {
532         return from_ascii("context-box");
533 }
534
535
536 string InsetBox::params2string(InsetBoxParams const & params)
537 {
538         ostringstream data;
539         data << "box" << ' ';
540         params.write(data);
541         return data.str();
542 }
543
544
545 void InsetBox::string2params(string const & in, InsetBoxParams & params)
546 {
547         params = InsetBoxParams(string());
548         if (in.empty())
549                 return;
550
551         istringstream data(in);
552         Lexer lex;
553         lex.setStream(data);
554
555         string name;
556         lex >> name;
557         if (!lex || name != "box") {
558                 LYXERR0("InsetBox::string2params(" << in << ")\n"
559                                           "Expected arg 1 to be \"box\"\n");
560                 return;
561         }
562
563         // This is part of the inset proper that is usually swallowed
564         // by Text::readInset
565         string id;
566         lex >> id;
567         if (!lex || id != "Box") {
568                 LYXERR0("InsetBox::string2params(" << in << ")\n"
569                                           "Expected arg 2 to be \"Box\"\n");
570         }
571
572         params.read(lex);
573 }
574
575
576 /////////////////////////////////////////////////////////////////////////
577 //
578 // InsetBoxParams
579 //
580 /////////////////////////////////////////////////////////////////////////
581
582 InsetBoxParams::InsetBoxParams(string const & label)
583         : type(label),
584           use_parbox(false),
585           use_makebox(false),
586           inner_box(true),
587           width(Length("100col%")),
588           special("none"),
589           pos('t'),
590           hor_pos('c'),
591           inner_pos('t'),
592           height(Length("1in")),
593           height_special("totalheight") // default is 1\\totalheight
594 {}
595
596
597 void InsetBoxParams::write(ostream & os) const
598 {
599         os << "Box " << type << "\n";
600         os << "position \"" << pos << "\"\n";
601         os << "hor_pos \"" << hor_pos << "\"\n";
602         os << "has_inner_box " << inner_box << "\n";
603         os << "inner_pos \"" << inner_pos << "\"\n";
604         os << "use_parbox " << use_parbox << "\n";
605         os << "use_makebox " << use_makebox << "\n";
606         os << "width \"" << width.asString() << "\"\n";
607         os << "special \"" << special << "\"\n";
608         os << "height \"" << height.asString() << "\"\n";
609         os << "height_special \"" << height_special << "\"\n";
610 }
611
612
613 void InsetBoxParams::read(Lexer & lex)
614 {
615         lex.setContext("InsetBoxParams::read");
616         lex >> type;
617         lex >> "position" >> pos;
618         lex >> "hor_pos" >> hor_pos;
619         lex >> "has_inner_box" >> inner_box;
620         if (type == "Framed")
621                 inner_box = false;
622         lex >> "inner_pos" >> inner_pos;
623         lex >> "use_parbox" >> use_parbox;
624         lex >> "use_makebox" >> use_makebox;
625         lex >> "width" >> width;
626         lex >> "special" >> special;
627         lex >> "height" >> height;
628         lex >> "height_special" >> height_special;
629 }
630
631
632 } // namespace lyx