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