]> git.lyx.org Git - lyx.git/blob - src/insets/InsetBox.cpp
fix a crash when the inset containing the new word at cursor is deleted
[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 int 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         int i = 0;
275         os << "%\n";
276         // Adapt to column/text width correctly also if paragraphs indented:
277         if (stdwidth)
278                 os << "\\noindent";
279
280         switch (btype) {
281         case Frameless:
282                 break;
283         case Framed:
284                 os << "\\begin{framed}%\n";
285                 i += 1;
286                 break;
287         case Boxed:
288                 os << "\\framebox";
289                 if (!params_.inner_box) {
290                         // Special widths, see usrguide §3.5
291                         // FIXME UNICODE
292                         if (params_.special != "none") {
293                                 os << "[" << params_.width.value()
294                                    << '\\' << from_utf8(params_.special)
295                                    << ']';
296                         } else
297                                 os << '[' << from_ascii(width_string)
298                                    << ']';
299                         if (params_.hor_pos != 'c')
300                                 os << "[" << params_.hor_pos << "]";
301                 }
302                 os << "{";
303                 break;
304         case ovalbox:
305                 os << "\\ovalbox{";
306                 break;
307         case Ovalbox:
308                 os << "\\Ovalbox{";
309                 break;
310         case Shadowbox:
311                 os << "\\shadowbox{";
312                 break;
313         case Shaded:
314                 // later
315                 break;
316         case Doublebox:
317                 os << "\\doublebox{";
318                 break;
319         }
320
321         if (params_.inner_box) {
322                 if (params_.use_parbox)
323                         os << "\\parbox";
324                 else if (params_.use_makebox) {
325                         os << "\\makebox";
326                         // FIXME UNICODE
327                         // output the width and horizontal position
328                         if (params_.special != "none") {
329                                 os << "[" << params_.width.value()
330                                    << '\\' << from_utf8(params_.special)
331                                    << ']';
332                         } else
333                                 os << '[' << from_ascii(width_string)
334                                    << ']';
335                         if (params_.hor_pos != 'c')
336                                 os << "[" << params_.hor_pos << "]";
337                         os << "{";
338                 }
339                 else
340                         os << "\\begin{minipage}";
341
342                 // output parameters for parbox and minipage
343                 if (!params_.use_makebox) {
344                         os << "[" << params_.pos << "]";
345                         if (params_.height_special == "none") {
346                                 // FIXME UNICODE
347                                 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
348                         } else {
349                                 // Special heights
350                                 // set no optional argument when the value is the default "1\height"
351                                 // (special units like \height are handled as "in")
352                                 // but when the user has chosen a non-default inner_pos, the height
353                                 // must be given: \minipage[pos][height][inner-pos]{width}
354                                 if ((params_.height != Length("1in") ||
355                                         params_.height_special != "totalheight") ||
356                                         params_.inner_pos != params_.pos) {
357                                                 // FIXME UNICODE
358                                                 os << "[" << params_.height.value()
359                                                         << "\\" << from_utf8(params_.height_special) << "]";
360                                 }
361                         }
362                         if (params_.inner_pos != params_.pos)
363                                 os << "[" << params_.inner_pos << "]";
364                         // FIXME UNICODE
365                         os << '{' << from_ascii(width_string) << '}';
366                         if (params_.use_parbox)
367                                 os << "{";
368                 }
369
370                 os << "%\n";
371                 ++i;
372         } // end if inner_box
373
374         if (btype == Shaded) {
375                 os << "\\begin{shaded}%\n";
376                 ++i;
377         }
378
379         i += InsetText::latex(os, runparams);
380
381         if (btype == Shaded)
382                 os << "\\end{shaded}";
383
384         if (params_.inner_box) {
385                 if (params_.use_parbox || params_.use_makebox)
386                         os << "%\n}";
387                 else
388                         os << "%\n\\end{minipage}";
389         }
390
391         switch (btype) {
392         case Frameless:
393                 break;
394         case Framed:
395                 os << "\\end{framed}";
396                 break;
397         case Boxed:
398                 os << "}";
399                 break;
400         case ovalbox:
401         case Ovalbox:
402         case Doublebox:
403         case Shadowbox:
404                 os << "}";
405                 break;
406         case Shaded:
407                 // already done
408                 break;
409         }
410
411         i += 2;
412
413         return i;
414 }
415
416
417 int InsetBox::plaintext(odocstream & os, OutputParams const & runparams) const
418 {
419         BoxType const btype = boxtranslator().find(params_.type);
420
421         switch (btype) {
422                 case Frameless:
423                         break;
424                 case Framed:
425                 case Boxed:
426                         os << "[\n";
427                         break;
428                 case ovalbox:
429                         os << "(\n";
430                         break;
431                 case Ovalbox:
432                         os << "((\n";
433                         break;
434                 case Shadowbox:
435                 case Shaded:
436                         os << "[/\n";
437                         break;
438                 case Doublebox:
439                         os << "[[\n";
440                         break;
441         }
442
443         InsetText::plaintext(os, runparams);
444
445         int len = 0;
446         switch (btype) {
447                 case Frameless:
448                         os << "\n";
449                         break;
450                 case Framed:
451                 case Boxed:
452                         os << "\n]";
453                         len = 1;
454                         break;
455                 case ovalbox:
456                         os << "\n)";
457                         len = 1;
458                         break;
459                 case Ovalbox:
460                         os << "\n))";
461                         len = 2;
462                         break;
463                 case Shadowbox:
464                 case Shaded:
465                         os << "\n/]";
466                         len = 2;
467                         break;
468                 case Doublebox:
469                         os << "\n]]";
470                         len = 2;
471                         break;
472         }
473
474         return PLAINTEXT_NEWLINE + len; // len chars on a separate line
475 }
476
477
478 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
479 {
480         return InsetText::docbook(os, runparams);
481 }
482
483
484 docstring InsetBox::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
485 {
486         // construct attributes
487         string attrs = "class='" + params_.type + "'";
488         string style;
489         if (!params_.width.empty())
490                 style += ("width: " + params_.width.asHTMLString() + "; ");
491         // The special heights don't really mean anything for us.
492         if (!params_.height.empty() && params_.height_special == "none")
493                 style += ("height: " + params_.height.asHTMLString() + "; ");
494         if (!style.empty())
495                 attrs += " style='" + style + "'";
496
497         xs << html::StartTag("div", attrs);
498         XHTMLOptions const opts = InsetText::WriteLabel | InsetText::WriteInnerTag;
499         docstring defer = InsetText::insetAsXHTML(xs, runparams, opts);
500         xs << html::EndTag("div");
501         xs << defer;
502         return docstring();
503 }
504
505
506 void InsetBox::validate(LaTeXFeatures & features) const
507 {
508         BoxType btype = boxtranslator().find(params_.type);
509         switch (btype) {
510         case Frameless:
511                 break;
512         case Framed:
513                 features.require("framed");
514                 break;
515         case Boxed:
516                 features.require("calc");
517                 break;
518         case ovalbox:
519         case Ovalbox:
520         case Shadowbox:
521         case Doublebox:
522                 features.require("calc");
523                 features.require("fancybox");
524                 break;
525         case Shaded:
526                 features.require("color");
527                 features.require("framed");
528                 break;
529         }
530         InsetCollapsable::validate(features);
531 }
532
533
534 docstring InsetBox::contextMenuName() const
535 {
536         return from_ascii("context-box");
537 }
538
539
540 string InsetBox::params2string(InsetBoxParams const & params)
541 {
542         ostringstream data;
543         data << "box" << ' ';
544         params.write(data);
545         return data.str();
546 }
547
548
549 void InsetBox::string2params(string const & in, InsetBoxParams & params)
550 {
551         params = InsetBoxParams(string());
552         if (in.empty())
553                 return;
554
555         istringstream data(in);
556         Lexer lex;
557         lex.setStream(data);
558
559         string name;
560         lex >> name;
561         if (!lex || name != "box") {
562                 LYXERR0("InsetBox::string2params(" << in << ")\n"
563                                           "Expected arg 1 to be \"box\"\n");
564                 return;
565         }
566
567         // This is part of the inset proper that is usually swallowed
568         // by Text::readInset
569         string id;
570         lex >> id;
571         if (!lex || id != "Box") {
572                 LYXERR0("InsetBox::string2params(" << in << ")\n"
573                                           "Expected arg 2 to be \"Box\"\n");
574         }
575
576         params.read(lex);
577 }
578
579
580 /////////////////////////////////////////////////////////////////////////
581 //
582 // InsetBoxParams
583 //
584 /////////////////////////////////////////////////////////////////////////
585
586 InsetBoxParams::InsetBoxParams(string const & label)
587         : type(label),
588           use_parbox(false),
589           use_makebox(false),
590           inner_box(true),
591           width(Length("100col%")),
592           special("none"),
593           pos('t'),
594           hor_pos('c'),
595           inner_pos('t'),
596           height(Length("1in")),
597           height_special("totalheight") // default is 1\\totalheight
598 {}
599
600
601 void InsetBoxParams::write(ostream & os) const
602 {
603         os << "Box " << type << "\n";
604         os << "position \"" << pos << "\"\n";
605         os << "hor_pos \"" << hor_pos << "\"\n";
606         os << "has_inner_box " << inner_box << "\n";
607         os << "inner_pos \"" << inner_pos << "\"\n";
608         os << "use_parbox " << use_parbox << "\n";
609         os << "use_makebox " << use_makebox << "\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 >> "use_makebox" >> use_makebox;
629         lex >> "width" >> width;
630         lex >> "special" >> special;
631         lex >> "height" >> height;
632         lex >> "height_special" >> height_special;
633 }
634
635
636 } // namespace lyx