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