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