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