]> git.lyx.org Git - features.git/blob - src/insets/InsetBox.cpp
framed.sty goes 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 "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
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("Framed", InsetBox::Framed);
53         translator.addPair("ovalbox", InsetBox::ovalbox);
54         translator.addPair("Ovalbox", InsetBox::Ovalbox);
55         translator.addPair("Shadowbox", InsetBox::Shadowbox);
56         translator.addPair("Shaded", InsetBox::Shaded);
57         translator.addPair("Doublebox",InsetBox::Doublebox);
58         return translator;
59 }
60
61
62 BoxTranslatorLoc const init_boxtranslator_loc()
63 {
64         BoxTranslatorLoc translator(_("simple frame"), InsetBox::Boxed);
65         translator.addPair(_("frameless"), InsetBox::Frameless);
66         translator.addPair(_("simple frame, page breaks"), InsetBox::Framed);
67         translator.addPair(_("oval, thin"), InsetBox::ovalbox);
68         translator.addPair(_("oval, thick"), InsetBox::Ovalbox);
69         translator.addPair(_("drop shadow"), InsetBox::Shadowbox);
70         translator.addPair(_("shaded background"), InsetBox::Shaded);
71         translator.addPair(_("double frame"), InsetBox::Doublebox);
72         return translator;
73 }
74
75
76 BoxTranslator const & boxtranslator()
77 {
78         static BoxTranslator translator = init_boxtranslator();
79         return translator;
80 }
81
82
83 BoxTranslatorLoc const & boxtranslator_loc()
84 {
85         static BoxTranslatorLoc translator = init_boxtranslator_loc();
86         return translator;
87 }
88
89 } // anon
90
91
92 InsetBox::InsetBox(BufferParams const & bp, string const & label)
93         : InsetCollapsable(bp), params_(label)
94 {}
95
96
97 InsetBox::InsetBox(InsetBox const & in)
98         : InsetCollapsable(in), params_(in.params_)
99 {}
100
101
102 InsetBox::~InsetBox()
103 {
104         InsetBoxMailer(*this).hideDialog();
105 }
106
107
108 Inset * InsetBox::clone() const
109 {
110         return new InsetBox(*this);
111 }
112
113
114 docstring const InsetBox::editMessage() const
115 {
116         return _("Opened Box Inset");
117 }
118
119
120 docstring InsetBox::name() const 
121 {
122         // FIXME: UNICODE
123         string name = string("Box");
124         if (boxtranslator().find(params_.type) == Shaded)
125                 name += string(":Shaded");
126         return from_ascii(name);
127 }
128
129
130 void InsetBox::write(Buffer const & buf, ostream & os) const
131 {
132         params_.write(os);
133         InsetCollapsable::write(buf, os);
134 }
135
136
137 void InsetBox::read(Buffer const & buf, Lexer & lex)
138 {
139         params_.read(lex);
140         InsetCollapsable::read(buf, lex);
141 }
142
143
144 void InsetBox::setButtonLabel()
145 {
146         BoxType btype = boxtranslator().find(params_.type);
147
148         docstring label;
149         label += _("Box");
150         label += " (";
151         if (btype == Frameless) {
152                 if (params_.use_parbox)
153                         label += _("Parbox");
154                 else
155                         label += _("Minipage");
156         } else
157                 label += boxtranslator_loc().find(btype);
158         label += ")";
159
160         setLabel(label);
161 }
162
163
164 bool InsetBox::hasFixedWidth() const
165 {
166       return params_.inner_box || params_.special != "width";
167 }
168
169
170 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
171 {
172         // back up textwidth.
173         int textwidth_backup = m.base.textwidth;
174         if (hasFixedWidth())
175                 m.base.textwidth = params_.width.inPixels(m.base.textwidth);
176         InsetCollapsable::metrics(m, dim);
177         // retore textwidth.
178         m.base.textwidth = textwidth_backup;
179 }
180
181
182 bool InsetBox::forceDefaultParagraphs(idx_type) const
183 {
184         return !params_.inner_box;
185 }
186
187
188 bool InsetBox::showInsetDialog(BufferView * bv) const
189 {
190         InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
191         return true;
192 }
193
194
195 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
196 {
197         switch (cmd.action) {
198
199         case LFUN_INSET_MODIFY: {
200                 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
201                 InsetBoxMailer::string2params(to_utf8(cmd.argument()), params_);
202                 setLayout(cur.buffer().params());
203                 break;
204         }
205
206         case LFUN_INSET_DIALOG_UPDATE:
207                 InsetBoxMailer(*this).updateDialog(&cur.bv());
208                 break;
209
210         case LFUN_MOUSE_RELEASE:
211                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
212                         InsetBoxMailer(*this).showDialog(&cur.bv());
213                         break;
214                 }
215                 InsetCollapsable::doDispatch(cur, cmd);
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         case LFUN_INSET_DIALOG_UPDATE:
232                 flag.enabled(true);
233                 return true;
234         case LFUN_BREAK_PARAGRAPH:
235                 if (params_.inner_box) {
236                         return InsetCollapsable::getStatus(cur, cmd, flag);
237                 } else {
238                         flag.enabled(false);
239                         return true;
240                 }
241
242         default:
243                 return InsetCollapsable::getStatus(cur, cmd, flag);
244         }
245 }
246
247
248 int InsetBox::latex(Buffer const & buf, odocstream & os,
249                     OutputParams const & runparams) const
250 {
251         BoxType btype = boxtranslator().find(params_.type);
252
253         string width_string = params_.width.asLatexString();
254         bool stdwidth(false);
255         if (params_.inner_box &&
256                         (width_string.find("1.0\\columnwidth") != string::npos
257                         || width_string.find("1.0\\textwidth") != string::npos)) {
258                 stdwidth = true;
259                 switch (btype) {
260                 case Frameless:
261                 case Framed:
262                         break;
263                 case Boxed:
264                 case Shaded:
265                         width_string += " - 2\\fboxsep - 2\\fboxrule";
266                         break;
267                 case ovalbox:
268                         width_string += " - 2\\fboxsep - 0.8pt";
269                         break;
270                 case Ovalbox:
271                         width_string += " - 2\\fboxsep - 1.6pt";
272                         break;
273                 case Shadowbox:
274                         // Shadow falls outside right margin... opinions?
275                         width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
276                         break;
277                 case Doublebox:
278                         width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
279                         break;
280                 }
281         }
282
283         int i = 0;
284         os << "%\n";
285         // Adapt to column/text width correctly also if paragraphs indented:
286         if (stdwidth)
287                 os << "\\noindent";
288
289         switch (btype) {
290         case Frameless:
291                 break;
292         case Framed:
293                 os << "\\begin{framed}%\n";
294                 i += 1;
295                 break;
296         case Boxed:
297                 os << "\\framebox";
298                 if (!params_.inner_box) {
299                         os << "{\\makebox";
300                         // Special widths, see usrguide §3.5
301                         // FIXME UNICODE
302                         if (params_.special != "none") {
303                                 os << "[" << params_.width.value()
304                                    << '\\' << from_utf8(params_.special)
305                                    << ']';
306                         } else
307                                 os << '[' << from_ascii(width_string)
308                                    << ']';
309                         if (params_.hor_pos != 'c')
310                                 os << "[" << params_.hor_pos << "]";
311                 }
312
313                 os << "{";
314                 break;
315         case ovalbox:
316                 os << "\\ovalbox{";
317                 break;
318         case Ovalbox:
319                 os << "\\Ovalbox{";
320                 break;
321         case Shadowbox:
322                 os << "\\shadowbox{";
323                 break;
324         case Shaded:
325                 // later
326                 break;
327         case Doublebox:
328                 os << "\\doublebox{";
329                 break;
330         }
331
332         if (params_.inner_box) {
333                 if (params_.use_parbox)
334                         os << "\\parbox";
335                 else
336                         os << "\\begin{minipage}";
337
338                 os << "[" << params_.pos << "]";
339                 if (params_.height_special == "none") {
340                         // FIXME UNICODE
341                         os << "[" << from_ascii(params_.height.asLatexString()) << "]";
342                 } else {
343                         // Special heights
344                         // set no optional argument when the value is the default "1\height"
345                         // (special units like \height are handled as "in")
346                         // but when the user has chosen a non-default inner_pos, the height
347                         // must be given: \minipage[pos][height][inner-pos]{width}
348                         if ((params_.height != Length("1in") ||
349                                  params_.height_special != "totalheight") ||
350                                 params_.inner_pos != params_.pos) {
351                                 // FIXME UNICODE
352                                 os << "[" << params_.height.value()
353                                         << "\\" << from_utf8(params_.height_special) << "]";
354                         }
355                 }
356                 if (params_.inner_pos != params_.pos)
357                         os << "[" << params_.inner_pos << "]";
358
359                 // FIXME UNICODE
360                 os << '{' << from_ascii(width_string) << '}';
361
362                 if (params_.use_parbox)
363                         os << "{";
364                 os << "%\n";
365                 i += 1;
366         }
367         if (btype == Shaded)
368                 os << "\\begin{shaded}%\n";
369                 i += 1;
370
371         i += InsetText::latex(buf, os, runparams);
372
373         if (btype == Shaded)
374                 os << "\\end{shaded}";
375
376         if (params_.inner_box) {
377                 if (params_.use_parbox)
378                         os << "%\n}";
379                 else
380                         os << "%\n\\end{minipage}";
381         }
382
383         switch (btype) {
384         case Frameless:
385                 break;
386         case Framed:
387                 os << "\\end{framed}";
388                 break;
389         case Boxed:
390                 if (!params_.inner_box)
391                         os << "}"; // for makebox
392                 os << "}";
393                 break;
394         case ovalbox:
395         case Ovalbox:
396         case Doublebox:
397         case Shadowbox:
398                 os << "}";
399                 break;
400         case Shaded:
401                 // already done
402                 break;
403         }
404         os << "%\n";
405
406         i += 3;
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