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