]> git.lyx.org Git - lyx.git/blob - src/insets/InsetBox.cpp
Fix bug #6315: counters in insets that don't produce output have ghost values.
[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::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 if (params_.use_makebox)
140                         inner = _("Makebox");
141                 else
142                         inner = _("Minipage");
143         }
144
145         docstring frame;
146         if (btype != Frameless)
147                 frame = boxtranslator_loc().find(btype);
148
149         docstring label;
150         if (inner.empty() && frame.empty())
151                 label = type;
152         else if (inner.empty())
153                 label = bformat(_("%1$s (%2$s)"),
154                         type, frame);
155         else if (frame.empty())
156                 label = bformat(_("%1$s (%2$s)"),
157                         type, inner);
158         else
159                 label = bformat(_("%1$s (%2$s, %3$s)"),
160                         type, inner, frame);
161         setLabel(label);
162 }
163
164
165 bool InsetBox::hasFixedWidth() const
166 {
167         return params_.inner_box || params_.special != "width";
168 }
169
170
171 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
172 {
173         // back up textwidth.
174         int textwidth_backup = m.base.textwidth;
175         if (hasFixedWidth())
176                 m.base.textwidth = params_.width.inPixels(m.base.textwidth);
177         InsetCollapsable::metrics(m, dim);
178         // retore textwidth.
179         m.base.textwidth = textwidth_backup;
180 }
181
182
183 bool InsetBox::forcePlainLayout(idx_type) const
184 {
185         return (!params_.inner_box || params_.use_makebox) && params_.type != "Shaded";
186 }
187
188
189 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
190 {
191         switch (cmd.action()) {
192
193         case LFUN_INSET_MODIFY: {
194                 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
195                 if (cmd.getArg(0) == "changetype") {
196                         cur.recordUndoInset(ATOMIC_UNDO, this);
197                         params_.type = cmd.getArg(1);
198                 } else
199                         string2params(to_utf8(cmd.argument()), params_);
200                 setButtonLabel();
201                 break;
202         }
203
204         default:
205                 InsetCollapsable::doDispatch(cur, cmd);
206                 break;
207         }
208 }
209
210
211 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
212                 FuncStatus & flag) const
213 {
214         switch (cmd.action()) {
215
216         case LFUN_INSET_MODIFY:
217                 if (cmd.getArg(0) == "changetype")
218                         flag.setOnOff(cmd.getArg(1) == params_.type);
219                 flag.setEnabled(true);
220                 return true;
221
222         case LFUN_INSET_DIALOG_UPDATE:
223                 flag.setEnabled(true);
224                 return true;
225
226         case LFUN_BREAK_PARAGRAPH:
227                 if ((params_.inner_box && !params_.use_makebox) || params_.type == "Shaded")
228                         return InsetCollapsable::getStatus(cur, cmd, flag);
229                 flag.setEnabled(false);
230                 return true;
231
232         default:
233                 return InsetCollapsable::getStatus(cur, cmd, flag);
234         }
235 }
236
237
238 int InsetBox::latex(odocstream & os, OutputParams const & runparams) const
239 {
240         BoxType btype = boxtranslator().find(params_.type);
241
242         string width_string = params_.width.asLatexString();
243         bool stdwidth = false;
244         if (params_.inner_box &&
245                         (width_string.find("1.0\\columnwidth") != string::npos
246                         || width_string.find("1.0\\textwidth") != string::npos)) {
247                 stdwidth = true;
248                 switch (btype) {
249                 case Frameless:
250                 case Framed:
251                         break;
252                 case Boxed:
253                 case Shaded:
254                         width_string += " - 2\\fboxsep - 2\\fboxrule";
255                         break;
256                 case ovalbox:
257                         width_string += " - 2\\fboxsep - 0.8pt";
258                         break;
259                 case Ovalbox:
260                         width_string += " - 2\\fboxsep - 1.6pt";
261                         break;
262                 case Shadowbox:
263                         // Shadow falls outside right margin... opinions?
264                         width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
265                         break;
266                 case Doublebox:
267                         width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
268                         break;
269                 }
270         }
271
272         int i = 0;
273         os << "%\n";
274         // Adapt to column/text width correctly also if paragraphs indented:
275         if (stdwidth)
276                 os << "\\noindent";
277
278         switch (btype) {
279         case Frameless:
280                 break;
281         case Framed:
282                 os << "\\begin{framed}%\n";
283                 i += 1;
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                 // later
313                 break;
314         case Doublebox:
315                 os << "\\doublebox{";
316                 break;
317         }
318
319         if (params_.inner_box) {
320                 if (params_.use_parbox)
321                         os << "\\parbox";
322                 else if (params_.use_makebox) {
323                         os << "\\makebox";
324                         // FIXME UNICODE
325                         // output the width and horizontal position
326                         if (params_.special != "none") {
327                                 os << "[" << params_.width.value()
328                                    << '\\' << from_utf8(params_.special)
329                                    << ']';
330                         } else
331                                 os << '[' << from_ascii(width_string)
332                                    << ']';
333                         if (params_.hor_pos != 'c')
334                                 os << "[" << params_.hor_pos << "]";
335                         os << "{";
336                 }
337                 else
338                         os << "\\begin{minipage}";
339
340                 // output parameters for parbox and minipage
341                 if (!params_.use_makebox) {
342                         os << "[" << params_.pos << "]";
343                         if (params_.height_special == "none") {
344                                 // FIXME UNICODE
345                                 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
346                         } else {
347                                 // Special heights
348                                 // set no optional argument when the value is the default "1\height"
349                                 // (special units like \height are handled as "in")
350                                 // but when the user has chosen a non-default inner_pos, the height
351                                 // must be given: \minipage[pos][height][inner-pos]{width}
352                                 if ((params_.height != Length("1in") ||
353                                         params_.height_special != "totalheight") ||
354                                         params_.inner_pos != params_.pos) {
355                                                 // FIXME UNICODE
356                                                 os << "[" << params_.height.value()
357                                                         << "\\" << from_utf8(params_.height_special) << "]";
358                                 }
359                         }
360                         if (params_.inner_pos != params_.pos)
361                                 os << "[" << params_.inner_pos << "]";
362                         // FIXME UNICODE
363                         os << '{' << from_ascii(width_string) << '}';
364                         if (params_.use_parbox)
365                                 os << "{";
366                 }
367
368                 os << "%\n";
369                 ++i;
370         } // end if inner_box
371
372         if (btype == Shaded) {
373                 os << "\\begin{shaded}%\n";
374                 ++i;
375         }
376
377         i += InsetText::latex(os, runparams);
378
379         if (btype == Shaded)
380                 os << "\\end{shaded}";
381
382         if (params_.inner_box) {
383                 if (params_.use_parbox || params_.use_makebox)
384                         os << "%\n}";
385                 else
386                         os << "%\n\\end{minipage}";
387         }
388
389         switch (btype) {
390         case Frameless:
391                 break;
392         case Framed:
393                 os << "\\end{framed}";
394                 break;
395         case Boxed:
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 << html::StartTag("div", attrs);
496         XHTMLOptions const opts = InsetText::WriteLabel | InsetText::WriteInnerTag;
497         docstring defer = InsetText::insetAsXHTML(xs, runparams, opts);
498         xs << html::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           use_makebox(false),
588           inner_box(true),
589           width(Length("100col%")),
590           special("none"),
591           pos('t'),
592           hor_pos('c'),
593           inner_pos('t'),
594           height(Length("1in")),
595           height_special("totalheight") // default is 1\\totalheight
596 {}
597
598
599 void InsetBoxParams::write(ostream & os) const
600 {
601         os << "Box " << type << "\n";
602         os << "position \"" << pos << "\"\n";
603         os << "hor_pos \"" << hor_pos << "\"\n";
604         os << "has_inner_box " << inner_box << "\n";
605         os << "inner_pos \"" << inner_pos << "\"\n";
606         os << "use_parbox " << use_parbox << "\n";
607         os << "use_makebox " << use_makebox << "\n";
608         os << "width \"" << width.asString() << "\"\n";
609         os << "special \"" << special << "\"\n";
610         os << "height \"" << height.asString() << "\"\n";
611         os << "height_special \"" << height_special << "\"\n";
612 }
613
614
615 void InsetBoxParams::read(Lexer & lex)
616 {
617         lex.setContext("InsetBoxParams::read");
618         lex >> type;
619         lex >> "position" >> pos;
620         lex >> "hor_pos" >> hor_pos;
621         lex >> "has_inner_box" >> inner_box;
622         if (type == "Framed")
623                 inner_box = false;
624         lex >> "inner_pos" >> inner_pos;
625         lex >> "use_parbox" >> use_parbox;
626         lex >> "use_makebox" >> use_makebox;
627         lex >> "width" >> width;
628         lex >> "special" >> special;
629         lex >> "height" >> height;
630         lex >> "height_special" >> height_special;
631 }
632
633
634 } // namespace lyx