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