]> git.lyx.org Git - features.git/blob - src/insets/InsetBox.cpp
InsetBox.cpp: fix the LaTeX-output, fixes bug 2492 http://bugzilla.lyx.org/show_bug...
[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 "BufferView.h"
19 #include "Cursor.h"
20 #include "DispatchResult.h"
21 #include "support/debug.h"
22 #include "FuncStatus.h"
23 #include "FuncRequest.h"
24 #include "support/gettext.h"
25 #include "LaTeXFeatures.h"
26 #include "Lexer.h"
27 #include "MetricsInfo.h"
28
29 #include "support/Translator.h"
30
31 #include <sstream>
32
33 using namespace std;
34
35 namespace lyx {
36
37 namespace {
38
39 typedef Translator<string, InsetBox::BoxType> BoxTranslator;
40 typedef Translator<docstring, InsetBox::BoxType> BoxTranslatorLoc;
41
42 BoxTranslator const init_boxtranslator()
43 {
44         BoxTranslator translator("Boxed", InsetBox::Boxed);
45         translator.addPair("Frameless", InsetBox::Frameless);
46         translator.addPair("Framed", InsetBox::Framed);
47         translator.addPair("ovalbox", InsetBox::ovalbox);
48         translator.addPair("Ovalbox", InsetBox::Ovalbox);
49         translator.addPair("Shadowbox", InsetBox::Shadowbox);
50         translator.addPair("Shaded", InsetBox::Shaded);
51         translator.addPair("Doublebox",InsetBox::Doublebox);
52         return translator;
53 }
54
55
56 BoxTranslatorLoc const init_boxtranslator_loc()
57 {
58         BoxTranslatorLoc translator(_("simple frame"), InsetBox::Boxed);
59         translator.addPair(_("frameless"), InsetBox::Frameless);
60         translator.addPair(_("simple frame, page breaks"), InsetBox::Framed);
61         translator.addPair(_("oval, thin"), InsetBox::ovalbox);
62         translator.addPair(_("oval, thick"), InsetBox::Ovalbox);
63         translator.addPair(_("drop shadow"), InsetBox::Shadowbox);
64         translator.addPair(_("shaded background"), InsetBox::Shaded);
65         translator.addPair(_("double frame"), InsetBox::Doublebox);
66         return translator;
67 }
68
69
70 BoxTranslator const & boxtranslator()
71 {
72         static BoxTranslator translator = init_boxtranslator();
73         return translator;
74 }
75
76
77 BoxTranslatorLoc const & boxtranslator_loc()
78 {
79         static BoxTranslatorLoc translator = init_boxtranslator_loc();
80         return translator;
81 }
82
83 } // anon
84
85
86 InsetBox::InsetBox(BufferParams const & bp, string const & label)
87         : InsetCollapsable(bp), params_(label)
88 {}
89
90
91 InsetBox::InsetBox(InsetBox const & in)
92         : InsetCollapsable(in), params_(in.params_)
93 {}
94
95
96 InsetBox::~InsetBox()
97 {
98         InsetBoxMailer(*this).hideDialog();
99 }
100
101
102 Inset * InsetBox::clone() const
103 {
104         return new InsetBox(*this);
105 }
106
107
108 docstring const InsetBox::editMessage() const
109 {
110         return _("Opened Box Inset");
111 }
112
113
114 docstring InsetBox::name() const 
115 {
116         // FIXME: UNICODE
117         string name = string("Box");
118         if (boxtranslator().find(params_.type) == Shaded)
119                 name += string(":Shaded");
120         return from_ascii(name);
121 }
122
123
124 void InsetBox::write(Buffer const & buf, ostream & os) const
125 {
126         params_.write(os);
127         InsetCollapsable::write(buf, os);
128 }
129
130
131 void InsetBox::read(Buffer const & buf, Lexer & lex)
132 {
133         params_.read(lex);
134         InsetCollapsable::read(buf, lex);
135 }
136
137
138 void InsetBox::setButtonLabel()
139 {
140         BoxType btype = boxtranslator().find(params_.type);
141
142         docstring label;
143         label += _("Box");
144         label += " (";
145         if (btype == Frameless) {
146                 if (params_.use_parbox)
147                         label += _("Parbox");
148                 else
149                         label += _("Minipage");
150         } else
151                 label += boxtranslator_loc().find(btype);
152         label += ")";
153
154         setLabel(label);
155 }
156
157
158 bool InsetBox::hasFixedWidth() const
159 {
160       return params_.inner_box || params_.special != "width";
161 }
162
163
164 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
165 {
166         // back up textwidth.
167         int textwidth_backup = m.base.textwidth;
168         if (hasFixedWidth())
169                 m.base.textwidth = params_.width.inPixels(m.base.textwidth);
170         InsetCollapsable::metrics(m, dim);
171         // retore textwidth.
172         m.base.textwidth = textwidth_backup;
173 }
174
175
176 bool InsetBox::forceDefaultParagraphs(idx_type) const
177 {
178         return !params_.inner_box;
179 }
180
181
182 bool InsetBox::showInsetDialog(BufferView * bv) const
183 {
184         InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
185         return true;
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                 InsetBoxMailer::string2params(to_utf8(cmd.argument()), params_);
196                 setLayout(cur.buffer().params());
197                 break;
198         }
199
200         case LFUN_INSET_DIALOG_UPDATE:
201                 InsetBoxMailer(*this).updateDialog(&cur.bv());
202                 break;
203
204         case LFUN_MOUSE_RELEASE:
205                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
206                         InsetBoxMailer(*this).showDialog(&cur.bv());
207                         break;
208                 }
209                 InsetCollapsable::doDispatch(cur, cmd);
210                 break;
211
212         default:
213                 InsetCollapsable::doDispatch(cur, cmd);
214                 break;
215         }
216 }
217
218
219 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
220                 FuncStatus & flag) const
221 {
222         switch (cmd.action) {
223
224         case LFUN_INSET_MODIFY:
225         case LFUN_INSET_DIALOG_UPDATE:
226                 flag.enabled(true);
227                 return true;
228         case LFUN_BREAK_PARAGRAPH:
229                 if (params_.inner_box) {
230                         return InsetCollapsable::getStatus(cur, cmd, flag);
231                 } else {
232                         flag.enabled(false);
233                         return true;
234                 }
235
236         default:
237                 return InsetCollapsable::getStatus(cur, cmd, flag);
238         }
239 }
240
241
242 bool InsetBox::isMacroScope(Buffer const &) const
243 {
244         BoxType btype = boxtranslator().find(params_.type);
245         return btype != Frameless || params_.inner_box;
246 }
247
248
249 int InsetBox::latex(Buffer const & buf, odocstream & os,
250                     OutputParams const & runparams) const
251 {
252         BoxType btype = boxtranslator().find(params_.type);
253
254         string width_string = params_.width.asLatexString();
255         bool stdwidth(false);
256         if (params_.inner_box &&
257                         (width_string.find("1.0\\columnwidth") != string::npos
258                         || width_string.find("1.0\\textwidth") != string::npos)) {
259                 stdwidth = true;
260                 switch (btype) {
261                 case Frameless:
262                 case Framed:
263                         break;
264                 case Boxed:
265                 case Shaded:
266                         width_string += " - 2\\fboxsep - 2\\fboxrule";
267                         break;
268                 case ovalbox:
269                         width_string += " - 2\\fboxsep - 0.8pt";
270                         break;
271                 case Ovalbox:
272                         width_string += " - 2\\fboxsep - 1.6pt";
273                         break;
274                 case Shadowbox:
275                         // Shadow falls outside right margin... opinions?
276                         width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
277                         break;
278                 case Doublebox:
279                         width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
280                         break;
281                 }
282         }
283
284         int i = 0;
285         os << "%\n";
286         // Adapt to column/text width correctly also if paragraphs indented:
287         if (stdwidth)
288                 os << "\\noindent";
289
290         switch (btype) {
291         case Frameless:
292                 break;
293         case Framed:
294                 os << "\\begin{framed}%\n";
295                 i += 1;
296                 break;
297         case Boxed:
298                 os << "\\framebox";
299                 if (!params_.inner_box) {
300                         os << "{\\makebox";
301                         // Special widths, see usrguide §3.5
302                         // FIXME UNICODE
303                         if (params_.special != "none") {
304                                 os << "[" << params_.width.value()
305                                    << '\\' << from_utf8(params_.special)
306                                    << ']';
307                         } else
308                                 os << '[' << from_ascii(width_string)
309                                    << ']';
310                         if (params_.hor_pos != 'c')
311                                 os << "[" << params_.hor_pos << "]";
312                 }
313
314                 os << "{";
315                 break;
316         case ovalbox:
317                 os << "\\ovalbox{";
318                 break;
319         case Ovalbox:
320                 os << "\\Ovalbox{";
321                 break;
322         case Shadowbox:
323                 os << "\\shadowbox{";
324                 break;
325         case Shaded:
326                 // later
327                 break;
328         case Doublebox:
329                 os << "\\doublebox{";
330                 break;
331         }
332
333         if (params_.inner_box) {
334                 if (params_.use_parbox)
335                         os << "\\parbox";
336                 else
337                         os << "\\begin{minipage}";
338
339                 os << "[" << params_.pos << "]";
340                 if (params_.height_special == "none") {
341                         // FIXME UNICODE
342                         os << "[" << from_ascii(params_.height.asLatexString()) << "]";
343                 } else {
344                         // Special heights
345                         // set no optional argument when the value is the default "1\height"
346                         // (special units like \height are handled as "in")
347                         // but when the user has chosen a non-default inner_pos, the height
348                         // must be given: \minipage[pos][height][inner-pos]{width}
349                         if ((params_.height != Length("1in") ||
350                                  params_.height_special != "totalheight") ||
351                                 params_.inner_pos != params_.pos) {
352                                 // FIXME UNICODE
353                                 os << "[" << params_.height.value()
354                                         << "\\" << from_utf8(params_.height_special) << "]";
355                         }
356                 }
357                 if (params_.inner_pos != params_.pos)
358                         os << "[" << params_.inner_pos << "]";
359
360                 // FIXME UNICODE
361                 os << '{' << from_ascii(width_string) << '}';
362
363                 if (params_.use_parbox)
364                         os << "{";
365                 os << "%\n";
366                 i += 1;
367         }
368         if (btype == Shaded)
369                 os << "\\begin{shaded}%\n";
370                 i += 1;
371
372         i += InsetText::latex(buf, os, runparams);
373
374         if (btype == Shaded)
375                 os << "\\end{shaded}";
376
377         if (params_.inner_box) {
378                 if (params_.use_parbox)
379                         os << "%\n}";
380                 else
381                         os << "%\n\\end{minipage}";
382         }
383
384         switch (btype) {
385         case Frameless:
386                 break;
387         case Framed:
388                 os << "\\end{framed}";
389                 break;
390         case Boxed:
391                 if (!params_.inner_box)
392                         os << "}"; // for makebox
393                 os << "}";
394                 break;
395         case ovalbox:
396         case Ovalbox:
397         case Doublebox:
398         case Shadowbox:
399                 os << "}";
400                 break;
401         case Shaded:
402                 // already done
403                 break;
404         }
405
406         i += 2;
407
408         return i;
409 }
410
411
412 int InsetBox::plaintext(Buffer const & buf, odocstream & os,
413                         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(buf, 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(Buffer const & buf, odocstream & os,
475                       OutputParams const & runparams) const
476 {
477         return InsetText::docbook(buf, os, runparams);
478 }
479
480
481 void InsetBox::validate(LaTeXFeatures & features) const
482 {
483         BoxType btype = boxtranslator().find(params_.type);
484         switch (btype) {
485         case Frameless:
486                 break;
487         case Framed:
488                 features.require("framed");
489                 break;
490         case Boxed:
491                 features.require("calc");
492                 break;
493         case ovalbox:
494         case Ovalbox:
495         case Shadowbox:
496         case Doublebox:
497                 features.require("calc");
498                 features.require("fancybox");
499                 break;
500         case Shaded:
501                 features.require("color");
502                 features.require("framed");
503                 break;
504         }
505         InsetText::validate(features);
506 }
507
508
509 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
510         : inset_(inset)
511 {}
512
513
514 string const InsetBoxMailer::name_ = "box";
515
516
517 string const InsetBoxMailer::inset2string(Buffer const &) const
518 {
519         return params2string(inset_.params());
520 }
521
522
523 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
524 {
525         ostringstream data;
526         data << "box" << ' ';
527         params.write(data);
528         return data.str();
529 }
530
531
532 void InsetBoxMailer::string2params(string const & in,
533                                    InsetBoxParams & params)
534 {
535         params = InsetBoxParams(string());
536         if (in.empty())
537                 return;
538
539         istringstream data(in);
540         Lexer lex(0,0);
541         lex.setStream(data);
542
543         string name;
544         lex >> name;
545         if (!lex || name != name_)
546                 return print_mailer_error("InsetBoxMailer", in, 1, name_);
547
548         // This is part of the inset proper that is usually swallowed
549         // by Text::readInset
550         string id;
551         lex >> id;
552         if (!lex || id != "Box")
553                 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
554
555         params.read(lex);
556 }
557
558
559 InsetBoxParams::InsetBoxParams(string const & label)
560         : type(label),
561           use_parbox(false),
562           inner_box(true),
563           width(Length("100col%")),
564           special("none"),
565           pos('t'),
566           hor_pos('c'),
567           inner_pos('t'),
568           height(Length("1in")),
569           height_special("totalheight") // default is 1\\totalheight
570 {}
571
572
573 void InsetBoxParams::write(ostream & os) const
574 {
575         os << "Box " << type << "\n";
576         os << "position \"" << pos << "\"\n";
577         os << "hor_pos \"" << hor_pos << "\"\n";
578         os << "has_inner_box " << inner_box << "\n";
579         os << "inner_pos \"" << inner_pos << "\"\n";
580         os << "use_parbox " << use_parbox << "\n";
581         os << "width \"" << width.asString() << "\"\n";
582         os << "special \"" << special << "\"\n";
583         os << "height \"" << height.asString() << "\"\n";
584         os << "height_special \"" << height_special << "\"\n";
585 }
586
587
588 void InsetBoxParams::read(Lexer & lex)
589 {
590         if (!lex.isOK())
591                 return;
592
593         lex.next();
594         type = lex.getString();
595
596         if (!lex)
597                 return;
598
599         lex.next();
600         string token;
601         token = lex.getString();
602         if (!lex)
603                 return;
604         if (token == "position") {
605                 lex.next();
606                 // The [0] is needed. We need the first and only char in
607                 // this string -- MV
608                 pos = lex.getString()[0];
609         } else {
610                 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
611                 lex.pushToken(token);
612         }
613
614         lex.next();
615         token = lex.getString();
616         if (!lex)
617                 return;
618         if (token == "hor_pos") {
619                 lex.next();
620                 hor_pos = lex.getString()[0];
621         } else {
622                 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
623                 lex.pushToken(token);
624         }
625
626         lex.next();
627         token = lex.getString();
628         if (!lex)
629                 return;
630         if (token == "has_inner_box") {
631                 lex.next();
632                 inner_box = lex.getInteger();
633         } else {
634                 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
635                 lex.pushToken(token);
636         }
637
638         lex.next();
639         token = lex.getString();
640         if (!lex)
641                 return;
642         if (token == "inner_pos") {
643                 lex.next();
644                 inner_pos = lex.getString()[0];
645         } else {
646                 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
647                         << token << endl;
648                 lex.pushToken(token);
649         }
650
651         lex.next();
652         token = lex.getString();
653         if (!lex)
654                 return;
655         if (token == "use_parbox") {
656                 lex.next();
657                 use_parbox = lex.getInteger();
658         } else {
659                 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
660                 lex.pushToken(token);
661         }
662
663         lex.next();
664         token = lex.getString();
665         if (!lex)
666                 return;
667         if (token == "width") {
668                 lex.next();
669                 width = Length(lex.getString());
670         } else {
671                 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
672                 lex.pushToken(token);
673         }
674
675         lex.next();
676         token = lex.getString();
677         if (!lex)
678                 return;
679         if (token == "special") {
680                 lex.next();
681                 special = lex.getString();
682         } else {
683                 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
684                 lex.pushToken(token);
685         }
686
687         lex.next();
688         token = lex.getString();
689         if (!lex)
690                 return;
691         if (token == "height") {
692                 lex.next();
693                 height = Length(lex.getString());
694         } else {
695                 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
696                 lex.pushToken(token);
697         }
698
699         lex.next();
700         token = lex.getString();
701         if (!lex)
702                 return;
703         if (token == "height_special") {
704                 lex.next();
705                 height_special = lex.getString();
706         } else {
707                 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
708                 lex.pushToken(token);
709         }
710 }
711
712
713 } // namespace lyx