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