]> git.lyx.org Git - lyx.git/blob - src/insets/InsetBox.cpp
fe4f44b2ccc00bb4eee051d482218e5014eeb568
[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
140                         inner = _("Minipage");
141         }
142
143         docstring frame;
144         if (btype != Frameless)
145                 frame = boxtranslator_loc().find(btype);
146
147         docstring label;
148         if (inner.empty() && frame.empty())
149                 label = type;
150         else if (inner.empty())
151                 label = bformat(_("%1$s (%2$s)"),
152                         type, frame);
153         else if (frame.empty())
154                 label = bformat(_("%1$s (%2$s)"),
155                         type, inner);
156         else
157                 label = bformat(_("%1$s (%2$s, %3$s)"),
158                         type, inner, frame);
159         setLabel(label);
160 }
161
162
163 bool InsetBox::hasFixedWidth() const
164 {
165         return params_.inner_box || params_.special != "width";
166 }
167
168
169 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
170 {
171         // back up textwidth.
172         int textwidth_backup = m.base.textwidth;
173         if (hasFixedWidth())
174                 m.base.textwidth = params_.width.inPixels(m.base.textwidth);
175         InsetCollapsable::metrics(m, dim);
176         // retore textwidth.
177         m.base.textwidth = textwidth_backup;
178 }
179
180
181 bool InsetBox::forcePlainLayout(idx_type) const
182 {
183         return !params_.inner_box && params_.type != "Framed";
184 }
185
186
187 bool InsetBox::showInsetDialog(BufferView * bv) const
188 {
189         bv->showDialog("box");
190         return true;
191 }
192
193
194 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
195 {
196         switch (cmd.action) {
197
198         case LFUN_INSET_MODIFY: {
199                 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
200                 if (cmd.getArg(0) == "changetype")
201                         params_.type = cmd.getArg(1);
202                 else
203                         string2params(to_utf8(cmd.argument()), params_);
204                 break;
205         }
206
207         case LFUN_INSET_DIALOG_UPDATE:
208                 cur.bv().updateDialog("box", params2string(params_));
209                 break;
210
211         default:
212                 InsetCollapsable::doDispatch(cur, cmd);
213                 break;
214         }
215 }
216
217
218 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
219                 FuncStatus & flag) const
220 {
221         switch (cmd.action) {
222
223         case LFUN_INSET_MODIFY:
224                 if (cmd.getArg(0) == "changetype")
225                         flag.setOnOff(cmd.getArg(1) == params_.type);
226                 flag.setEnabled(true);
227                 return true;
228
229         case LFUN_INSET_DIALOG_UPDATE:
230                 flag.setEnabled(true);
231                 return true;
232
233         case LFUN_BREAK_PARAGRAPH:
234                 if (params_.inner_box || params_.type == "Framed")
235                         return InsetCollapsable::getStatus(cur, cmd, flag);
236                 flag.setEnabled(false);
237                 return true;
238
239         default:
240                 return InsetCollapsable::getStatus(cur, cmd, flag);
241         }
242 }
243
244
245 int InsetBox::latex(odocstream & os, OutputParams const & runparams) const
246 {
247         BoxType btype = boxtranslator().find(params_.type);
248
249         string width_string = params_.width.asLatexString();
250         bool stdwidth = false;
251         if (params_.inner_box &&
252                         (width_string.find("1.0\\columnwidth") != string::npos
253                         || width_string.find("1.0\\textwidth") != string::npos)) {
254                 stdwidth = true;
255                 switch (btype) {
256                 case Frameless:
257                 case Framed:
258                         break;
259                 case Boxed:
260                 case Shaded:
261                         width_string += " - 2\\fboxsep - 2\\fboxrule";
262                         break;
263                 case ovalbox:
264                         width_string += " - 2\\fboxsep - 0.8pt";
265                         break;
266                 case Ovalbox:
267                         width_string += " - 2\\fboxsep - 1.6pt";
268                         break;
269                 case Shadowbox:
270                         // Shadow falls outside right margin... opinions?
271                         width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
272                         break;
273                 case Doublebox:
274                         width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
275                         break;
276                 }
277         }
278
279         int i = 0;
280         os << "%\n";
281         // Adapt to column/text width correctly also if paragraphs indented:
282         if (stdwidth)
283                 os << "\\noindent";
284
285         switch (btype) {
286         case Frameless:
287                 break;
288         case Framed:
289                 os << "\\begin{framed}%\n";
290                 i += 1;
291                 break;
292         case Boxed:
293                 os << "\\framebox";
294                 if (!params_.inner_box) {
295                         os << "{\\makebox";
296                         // Special widths, see usrguide §3.5
297                         // FIXME UNICODE
298                         if (params_.special != "none") {
299                                 os << "[" << params_.width.value()
300                                    << '\\' << from_utf8(params_.special)
301                                    << ']';
302                         } else
303                                 os << '[' << from_ascii(width_string)
304                                    << ']';
305                         if (params_.hor_pos != 'c')
306                                 os << "[" << params_.hor_pos << "]";
307                 }
308
309                 os << "{";
310                 break;
311         case ovalbox:
312                 os << "\\ovalbox{";
313                 break;
314         case Ovalbox:
315                 os << "\\Ovalbox{";
316                 break;
317         case Shadowbox:
318                 os << "\\shadowbox{";
319                 break;
320         case Shaded:
321                 // later
322                 break;
323         case Doublebox:
324                 os << "\\doublebox{";
325                 break;
326         }
327
328         if (params_.inner_box) {
329                 if (params_.use_parbox)
330                         os << "\\parbox";
331                 else
332                         os << "\\begin{minipage}";
333
334                 os << "[" << params_.pos << "]";
335                 if (params_.height_special == "none") {
336                         // FIXME UNICODE
337                         os << "[" << from_ascii(params_.height.asLatexString()) << "]";
338                 } else {
339                         // Special heights
340                         // set no optional argument when the value is the default "1\height"
341                         // (special units like \height are handled as "in")
342                         // but when the user has chosen a non-default inner_pos, the height
343                         // must be given: \minipage[pos][height][inner-pos]{width}
344                         if ((params_.height != Length("1in") ||
345                                  params_.height_special != "totalheight") ||
346                                 params_.inner_pos != params_.pos) {
347                                 // FIXME UNICODE
348                                 os << "[" << params_.height.value()
349                                         << "\\" << from_utf8(params_.height_special) << "]";
350                         }
351                 }
352                 if (params_.inner_pos != params_.pos)
353                         os << "[" << params_.inner_pos << "]";
354
355                 // FIXME UNICODE
356                 os << '{' << from_ascii(width_string) << '}';
357
358                 if (params_.use_parbox)
359                         os << "{";
360                 os << "%\n";
361                 ++i;
362         }
363         if (btype == Shaded) {
364                 os << "\\begin{shaded}%\n";
365                 ++i;
366         }
367
368         i += InsetText::latex(os, runparams);
369
370         if (btype == Shaded)
371                 os << "\\end{shaded}";
372
373         if (params_.inner_box) {
374                 if (params_.use_parbox)
375                         os << "%\n}";
376                 else
377                         os << "%\n\\end{minipage}";
378         }
379
380         switch (btype) {
381         case Frameless:
382                 break;
383         case Framed:
384                 os << "\\end{framed}";
385                 break;
386         case Boxed:
387                 if (!params_.inner_box)
388                         os << "}"; // for makebox
389                 os << "}";
390                 break;
391         case ovalbox:
392         case Ovalbox:
393         case Doublebox:
394         case Shadowbox:
395                 os << "}";
396                 break;
397         case Shaded:
398                 // already done
399                 break;
400         }
401
402         i += 2;
403
404         return i;
405 }
406
407
408 int InsetBox::plaintext(odocstream & os, OutputParams const & runparams) const
409 {
410         BoxType const btype = boxtranslator().find(params_.type);
411
412         switch (btype) {
413                 case Frameless:
414                         break;
415                 case Framed:
416                 case Boxed:
417                         os << "[\n";
418                         break;
419                 case ovalbox:
420                         os << "(\n";
421                         break;
422                 case Ovalbox:
423                         os << "((\n";
424                         break;
425                 case Shadowbox:
426                 case Shaded:
427                         os << "[/\n";
428                         break;
429                 case Doublebox:
430                         os << "[[\n";
431                         break;
432         }
433
434         InsetText::plaintext(os, runparams);
435
436         int len = 0;
437         switch (btype) {
438                 case Frameless:
439                         os << "\n";
440                         break;
441                 case Framed:
442                 case Boxed:
443                         os << "\n]";
444                         len = 1;
445                         break;
446                 case ovalbox:
447                         os << "\n)";
448                         len = 1;
449                         break;
450                 case Ovalbox:
451                         os << "\n))";
452                         len = 2;
453                         break;
454                 case Shadowbox:
455                 case Shaded:
456                         os << "\n/]";
457                         len = 2;
458                         break;
459                 case Doublebox:
460                         os << "\n]]";
461                         len = 2;
462                         break;
463         }
464
465         return PLAINTEXT_NEWLINE + len; // len chars on a separate line
466 }
467
468
469 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
470 {
471         return InsetText::docbook(os, runparams);
472 }
473
474
475 docstring InsetBox::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
476 {
477         // construct attributes
478         string attrs = "class='" + params_.type + "'";
479         string style;
480         if (!params_.width.empty())
481                 style += ("width: " + params_.width.asHTMLString() + "; ");
482         // The special heights don't really mean anything for us.
483         if (!params_.height.empty() && params_.height_special == "none")
484                 style += ("height: " + params_.height.asHTMLString() + "; ");
485         if (!style.empty())
486                 attrs += " style='" + style + "'";
487
488         xs << html::StartTag("div", attrs);
489         XHTMLOptions const opts = InsetText::WriteLabel | InsetText::WriteInnerTag;
490         docstring defer = InsetText::insetAsXHTML(xs, runparams, opts);
491         xs << html::EndTag("div");
492         xs << defer;
493         return docstring();
494 }
495
496
497 void InsetBox::validate(LaTeXFeatures & features) const
498 {
499         BoxType btype = boxtranslator().find(params_.type);
500         switch (btype) {
501         case Frameless:
502                 break;
503         case Framed:
504                 features.require("framed");
505                 break;
506         case Boxed:
507                 features.require("calc");
508                 break;
509         case ovalbox:
510         case Ovalbox:
511         case Shadowbox:
512         case Doublebox:
513                 features.require("calc");
514                 features.require("fancybox");
515                 break;
516         case Shaded:
517                 features.require("color");
518                 features.require("framed");
519                 break;
520         }
521         InsetCollapsable::validate(features);
522 }
523
524
525 docstring InsetBox::contextMenu(BufferView const &, int, int) const
526 {
527         return from_ascii("context-box");
528 }
529
530
531 string InsetBox::params2string(InsetBoxParams const & params)
532 {
533         ostringstream data;
534         data << "box" << ' ';
535         params.write(data);
536         return data.str();
537 }
538
539
540 void InsetBox::string2params(string const & in, InsetBoxParams & params)
541 {
542         params = InsetBoxParams(string());
543         if (in.empty())
544                 return;
545
546         istringstream data(in);
547         Lexer lex;
548         lex.setStream(data);
549
550         string name;
551         lex >> name;
552         if (!lex || name != "box") {
553                 LYXERR0("InsetBox::string2params(" << in << ")\n"
554                                           "Expected arg 1 to be \"box\"\n");
555                 return;
556         }
557
558         // This is part of the inset proper that is usually swallowed
559         // by Text::readInset
560         string id;
561         lex >> id;
562         if (!lex || id != "Box") {
563                 LYXERR0("InsetBox::string2params(" << in << ")\n"
564                                           "Expected arg 2 to be \"Box\"\n");
565         }
566
567         params.read(lex);
568 }
569
570
571 /////////////////////////////////////////////////////////////////////////
572 //
573 // InsetBoxParams
574 //
575 /////////////////////////////////////////////////////////////////////////
576
577 InsetBoxParams::InsetBoxParams(string const & label)
578         : type(label),
579           use_parbox(false),
580           inner_box(true),
581           width(Length("100col%")),
582           special("none"),
583           pos('t'),
584           hor_pos('c'),
585           inner_pos('t'),
586           height(Length("1in")),
587           height_special("totalheight") // default is 1\\totalheight
588 {}
589
590
591 void InsetBoxParams::write(ostream & os) const
592 {
593         os << "Box " << type << "\n";
594         os << "position \"" << pos << "\"\n";
595         os << "hor_pos \"" << hor_pos << "\"\n";
596         os << "has_inner_box " << inner_box << "\n";
597         os << "inner_pos \"" << inner_pos << "\"\n";
598         os << "use_parbox " << use_parbox << "\n";
599         os << "width \"" << width.asString() << "\"\n";
600         os << "special \"" << special << "\"\n";
601         os << "height \"" << height.asString() << "\"\n";
602         os << "height_special \"" << height_special << "\"\n";
603 }
604
605
606 void InsetBoxParams::read(Lexer & lex)
607 {
608         lex.setContext("InsetBoxParams::read");
609         lex >> type;
610         lex >> "position" >> pos;
611         lex >> "hor_pos" >> hor_pos;
612         lex >> "has_inner_box" >> inner_box;
613         if (type == "Framed")
614                 inner_box = false;
615         lex >> "inner_pos" >> inner_pos;
616         lex >> "use_parbox" >> use_parbox;
617         lex >> "width" >> width;
618         lex >> "special" >> special;
619         lex >> "height" >> height;
620         lex >> "height_special" >> height_special;
621 }
622
623
624 } // namespace lyx