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