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