]> git.lyx.org Git - lyx.git/blob - src/insets/InsetBox.cpp
Cocoa based Qt-4.6 needs to paint every character separately to match metrics computa...
[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                 if (cmd.getArg(0) == "changetype")
216                         flag.setOnOff(cmd.getArg(1) == params_.type);
217                 flag.setEnabled(true);
218                 return true;
219
220         case LFUN_INSET_DIALOG_UPDATE:
221                 flag.setEnabled(true);
222                 return true;
223
224         case LFUN_BREAK_PARAGRAPH:
225                 if ((params_.inner_box && !params_.use_makebox)
226                      || params_.type == "Shaded" || params_.type == "Framed")
227                         return InsetCollapsable::getStatus(cur, cmd, flag);
228                 flag.setEnabled(false);
229                 return true;
230
231         default:
232                 return InsetCollapsable::getStatus(cur, cmd, flag);
233         }
234 }
235
236
237 void InsetBox::latex(otexstream & os, OutputParams const & runparams) const
238 {
239         BoxType btype = boxtranslator().find(params_.type);
240
241         string width_string = params_.width.asLatexString();
242         bool stdwidth = false;
243         if (params_.inner_box &&
244                         (width_string.find("1.0\\columnwidth") != string::npos
245                         || width_string.find("1.0\\textwidth") != string::npos)) {
246                 stdwidth = true;
247                 switch (btype) {
248                 case Frameless:
249                 case Framed:
250                         break;
251                 case Boxed:
252                 case Shaded:
253                         width_string += " - 2\\fboxsep - 2\\fboxrule";
254                         break;
255                 case ovalbox:
256                         width_string += " - 2\\fboxsep - 0.8pt";
257                         break;
258                 case Ovalbox:
259                         width_string += " - 2\\fboxsep - 1.6pt";
260                         break;
261                 case Shadowbox:
262                         // Shadow falls outside right margin... opinions?
263                         width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
264                         break;
265                 case Doublebox:
266                         width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
267                         break;
268                 }
269         }
270
271         os << safebreakln;
272         if (runparams.lastid != -1)
273                 os.texrow().start(runparams.lastid, runparams.lastpos);
274
275         // Adapt to column/text width correctly also if paragraphs indented:
276         if (stdwidth)
277                 os << "\\noindent";
278
279         switch (btype) {
280         case Frameless:
281                 break;
282         case Framed:
283                 os << "\\begin{framed}%\n";
284                 break;
285         case Boxed:
286                 os << "\\framebox";
287                 if (!params_.inner_box) {
288                         // Special widths, see usrguide §3.5
289                         // FIXME UNICODE
290                         if (params_.special != "none") {
291                                 os << "[" << params_.width.value()
292                                    << '\\' << from_utf8(params_.special)
293                                    << ']';
294                         } else
295                                 os << '[' << from_ascii(width_string)
296                                    << ']';
297                         if (params_.hor_pos != 'c')
298                                 os << "[" << params_.hor_pos << "]";
299                 }
300                 os << "{";
301                 break;
302         case ovalbox:
303                 os << "\\ovalbox{";
304                 break;
305         case Ovalbox:
306                 os << "\\Ovalbox{";
307                 break;
308         case Shadowbox:
309                 os << "\\shadowbox{";
310                 break;
311         case Shaded:
312                 // later
313                 break;
314         case Doublebox:
315                 os << "\\doublebox{";
316                 break;
317         }
318
319         if (params_.inner_box) {
320                 if (params_.use_parbox)
321                         os << "\\parbox";
322                 else if (params_.use_makebox) {
323                         os << "\\makebox";
324                         // FIXME UNICODE
325                         // output the width and horizontal position
326                         if (params_.special != "none") {
327                                 os << "[" << params_.width.value()
328                                    << '\\' << from_utf8(params_.special)
329                                    << ']';
330                         } else
331                                 os << '[' << from_ascii(width_string)
332                                    << ']';
333                         if (params_.hor_pos != 'c')
334                                 os << "[" << params_.hor_pos << "]";
335                         os << "{";
336                 }
337                 else
338                         os << "\\begin{minipage}";
339
340                 // output parameters for parbox and minipage
341                 if (!params_.use_makebox) {
342                         os << "[" << params_.pos << "]";
343                         if (params_.height_special == "none") {
344                                 // FIXME UNICODE
345                                 os << "[" << from_ascii(params_.height.asLatexString()) << "]";
346                         } else {
347                                 // Special heights
348                                 // set no optional argument when the value is the default "1\height"
349                                 // (special units like \height are handled as "in")
350                                 // but when the user has chosen a non-default inner_pos, the height
351                                 // must be given: \minipage[pos][height][inner-pos]{width}
352                                 if ((params_.height != Length("1in") ||
353                                         params_.height_special != "totalheight") ||
354                                         params_.inner_pos != params_.pos) {
355                                                 // FIXME UNICODE
356                                                 os << "[" << params_.height.value()
357                                                         << "\\" << from_utf8(params_.height_special) << "]";
358                                 }
359                         }
360                         if (params_.inner_pos != params_.pos)
361                                 os << "[" << params_.inner_pos << "]";
362                         // FIXME UNICODE
363                         os << '{' << from_ascii(width_string) << '}';
364                         if (params_.use_parbox)
365                                 os << "{";
366                 }
367
368                 os << "%\n";
369         } // end if inner_box
370
371         if (btype == Shaded) {
372                 os << "\\begin{shaded}%\n";
373         }
374
375         InsetText::latex(os, runparams);
376
377         if (btype == Shaded)
378                 os << "\\end{shaded}";
379
380         if (params_.inner_box) {
381                 if (params_.use_parbox || params_.use_makebox)
382                         os << "%\n}";
383                 else
384                         os << "%\n\\end{minipage}";
385         }
386
387         switch (btype) {
388         case Frameless:
389                 break;
390         case Framed:
391                 os << "\\end{framed}";
392                 break;
393         case Boxed:
394                 os << "}";
395                 break;
396         case ovalbox:
397         case Ovalbox:
398         case Doublebox:
399         case Shadowbox:
400                 os << "}";
401                 break;
402         case Shaded:
403                 // already done
404                 break;
405         }
406 }
407
408
409 int InsetBox::plaintext(odocstream & os, OutputParams const & runparams) const
410 {
411         BoxType const btype = boxtranslator().find(params_.type);
412
413         switch (btype) {
414                 case Frameless:
415                         break;
416                 case Framed:
417                 case Boxed:
418                         os << "[\n";
419                         break;
420                 case ovalbox:
421                         os << "(\n";
422                         break;
423                 case Ovalbox:
424                         os << "((\n";
425                         break;
426                 case Shadowbox:
427                 case Shaded:
428                         os << "[/\n";
429                         break;
430                 case Doublebox:
431                         os << "[[\n";
432                         break;
433         }
434
435         InsetText::plaintext(os, runparams);
436
437         int len = 0;
438         switch (btype) {
439                 case Frameless:
440                         os << "\n";
441                         break;
442                 case Framed:
443                 case Boxed:
444                         os << "\n]";
445                         len = 1;
446                         break;
447                 case ovalbox:
448                         os << "\n)";
449                         len = 1;
450                         break;
451                 case Ovalbox:
452                         os << "\n))";
453                         len = 2;
454                         break;
455                 case Shadowbox:
456                 case Shaded:
457                         os << "\n/]";
458                         len = 2;
459                         break;
460                 case Doublebox:
461                         os << "\n]]";
462                         len = 2;
463                         break;
464         }
465
466         return PLAINTEXT_NEWLINE + len; // len chars on a separate line
467 }
468
469
470 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
471 {
472         return InsetText::docbook(os, runparams);
473 }
474
475
476 docstring InsetBox::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
477 {
478         // construct attributes
479         string attrs = "class='" + params_.type + "'";
480         string style;
481         if (!params_.width.empty()) {
482                 string w = params_.width.asHTMLString();
483                 if (w != "100%")
484                         style += ("width: " + params_.width.asHTMLString() + "; ");
485         }
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