]> git.lyx.org Git - features.git/blob - src/insets/InsetBox.cpp
Using "using namespace std" with msvc10 makes also std::tr1::bind visible and generat...
[features.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 #if 1
13 #include <config.h>
14
15
16
17 #include "InsetBox.h"
18
19 #include "Buffer.h"
20 #include "BufferParams.h"
21 #include "BufferView.h"
22 #include "Cursor.h"
23 #include "DispatchResult.h"
24 #include "FuncStatus.h"
25 #include "FuncRequest.h"
26 #include "LaTeXFeatures.h"
27 #include "Lexer.h"
28 #include "MetricsInfo.h"
29 #include "output_xhtml.h"
30 #include "TextClass.h"
31
32 #include "support/debug.h"
33 #include "support/docstream.h"
34 #include "support/gettext.h"
35 #include "support/lstrings.h"
36 #include "support/Translator.h"
37
38 #include "frontends/Application.h"
39
40 #include <sstream>
41
42
43 using namespace std;
44 using namespace lyx::support;
45
46 namespace lyx {
47
48 namespace {
49
50 typedef Translator<string, InsetBox::BoxType> BoxTranslator;
51 typedef Translator<docstring, InsetBox::BoxType> BoxTranslatorLoc;
52
53 BoxTranslator initBoxtranslator()
54 {
55         BoxTranslator translator("Boxed", InsetBox::Boxed);
56         translator.addPair("Frameless", InsetBox::Frameless);
57         translator.addPair("Framed", InsetBox::Framed);
58         translator.addPair("ovalbox", InsetBox::ovalbox);
59         translator.addPair("Ovalbox", InsetBox::Ovalbox);
60         translator.addPair("Shadowbox", InsetBox::Shadowbox);
61         translator.addPair("Shaded", InsetBox::Shaded);
62         translator.addPair("Doublebox",InsetBox::Doublebox);
63         return translator;
64 }
65
66
67 BoxTranslatorLoc initBoxtranslatorLoc()
68 {
69         BoxTranslatorLoc translator(_("simple frame"), InsetBox::Boxed);
70         translator.addPair(_("frameless"), InsetBox::Frameless);
71         translator.addPair(_("simple frame, page breaks"), InsetBox::Framed);
72         translator.addPair(_("oval, thin"), InsetBox::ovalbox);
73         translator.addPair(_("oval, thick"), InsetBox::Ovalbox);
74         translator.addPair(_("drop shadow"), InsetBox::Shadowbox);
75         translator.addPair(_("shaded background"), InsetBox::Shaded);
76         translator.addPair(_("double frame"), InsetBox::Doublebox);
77         return translator;
78 }
79
80
81 BoxTranslator const & boxtranslator()
82 {
83         static BoxTranslator translator = initBoxtranslator();
84         return translator;
85 }
86
87
88 BoxTranslatorLoc const & boxtranslator_loc()
89 {
90         static BoxTranslatorLoc translator = initBoxtranslatorLoc();
91         return translator;
92 }
93
94 } // namespace anon
95
96
97 /////////////////////////////////////////////////////////////////////////
98 //
99 // InsetBox
100 //
101 /////////////////////////////////////////////////////////////////////////
102
103 InsetBox::InsetBox(Buffer * buffer, string const & label)
104         : InsetCollapsable(buffer), params_(label)
105 {}
106
107
108 docstring InsetBox::name() const 
109 {
110         // FIXME: UNICODE
111         string name = "Box";
112         if (boxtranslator().find(params_.type) == Shaded)
113                 name += ":Shaded";
114         return from_ascii(name);
115 }
116
117
118 void InsetBox::write(ostream & os) const
119 {
120         params_.write(os);
121         InsetCollapsable::write(os);
122 }
123
124
125 void InsetBox::read(Lexer & lex)
126 {
127         params_.read(lex);
128         InsetCollapsable::read(lex);
129 }
130
131
132 void InsetBox::setButtonLabel()
133 {
134         BoxType const btype = boxtranslator().find(params_.type);
135
136         docstring const type = _("Box");
137
138         docstring inner;
139         if (params_.inner_box) {
140                 if (params_.use_parbox)
141                         inner = _("Parbox");
142                 else
143                         inner = _("Minipage");
144         }
145
146         docstring frame;
147         if (btype != Frameless)
148                 frame = boxtranslator_loc().find(btype);
149
150         docstring label;
151         if (inner.empty() && frame.empty())
152                 label = type;
153         else if (inner.empty())
154                 label = bformat(_("%1$s (%2$s)"),
155                         type, frame);
156         else if (frame.empty())
157                 label = bformat(_("%1$s (%2$s)"),
158                         type, inner);
159         else
160                 label = bformat(_("%1$s (%2$s, %3$s)"),
161                         type, inner, frame);
162         setLabel(label);
163 }
164
165
166 bool InsetBox::hasFixedWidth() const
167 {
168         return params_.inner_box || params_.special != "width";
169 }
170
171
172 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
173 {
174         // back up textwidth.
175         int textwidth_backup = m.base.textwidth;
176         if (hasFixedWidth())
177                 m.base.textwidth = params_.width.inPixels(m.base.textwidth);
178         InsetCollapsable::metrics(m, dim);
179         // retore textwidth.
180         m.base.textwidth = textwidth_backup;
181 }
182
183
184 bool InsetBox::forcePlainLayout(idx_type) const
185 {
186         return !params_.inner_box && 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                 if (cmd.getArg(0) == "changetype")
197                         params_.type = cmd.getArg(1);
198                 else
199                         string2params(to_utf8(cmd.argument()), params_);
200                 setButtonLabel();
201                 break;
202         }
203
204         default:
205                 InsetCollapsable::doDispatch(cur, cmd);
206                 break;
207         }
208 }
209
210
211 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
212                 FuncStatus & flag) const
213 {
214         switch (cmd.action()) {
215
216         case LFUN_INSET_MODIFY:
217                 if (cmd.getArg(0) == "changetype")
218                         flag.setOnOff(cmd.getArg(1) == params_.type);
219                 flag.setEnabled(true);
220                 return true;
221
222         case LFUN_INSET_DIALOG_UPDATE:
223                 flag.setEnabled(true);
224                 return true;
225
226         case LFUN_BREAK_PARAGRAPH:
227                 if (params_.inner_box || params_.type == "Framed")
228                         return InsetCollapsable::getStatus(cur, cmd, flag);
229                 flag.setEnabled(false);
230                 return true;
231
232         default:
233                 return InsetCollapsable::getStatus(cur, cmd, flag);
234         }
235 }
236
237
238 int InsetBox::latex(odocstream & os, OutputParams const & runparams) const
239 {
240         BoxType btype = boxtranslator().find(params_.type);
241
242         string width_string = params_.width.asLatexString();
243         bool stdwidth = false;
244         if (params_.inner_box &&
245                         (width_string.find("1.0\\columnwidth") != string::npos
246                         || width_string.find("1.0\\textwidth") != string::npos)) {
247                 stdwidth = true;
248                 switch (btype) {
249                 case Frameless:
250                 case Framed:
251                         break;
252                 case Boxed:
253                 case Shaded:
254                         width_string += " - 2\\fboxsep - 2\\fboxrule";
255                         break;
256                 case ovalbox:
257                         width_string += " - 2\\fboxsep - 0.8pt";
258                         break;
259                 case Ovalbox:
260                         width_string += " - 2\\fboxsep - 1.6pt";
261                         break;
262                 case Shadowbox:
263                         // Shadow falls outside right margin... opinions?
264                         width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
265                         break;
266                 case Doublebox:
267                         width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
268                         break;
269                 }
270         }
271
272         int i = 0;
273         os << "%\n";
274         // Adapt to column/text width correctly also if paragraphs indented:
275         if (stdwidth)
276                 os << "\\noindent";
277
278
279         switch (btype) {
280         case Frameless:
281                 break;
282         case Framed:
283                 os << "\\begin{framed}%\n";
284                 i += 1;
285                 break;
286         case Boxed:
287                 os << "\\framebox";
288                 if (!params_.inner_box) {
289                         os << "{\\makebox";
290                         // Special widths, see usrguide §3.5
291                         // FIXME UNICODE
292                         if (params_.special != "none") {
293                                 os << "["/* << params_.width.value()*/ << (double) 1
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
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         #if 0
322         if (params_.inner_box) {
323                 if (params_.use_parbox)
324                         os << "\\parbox";
325                 else
326                         os << "\\begin{minipage}";
327
328                 os << "[" << params_.pos << "]";
329                 if (params_.height_special == "none") {
330                         // FIXME UNICODE
331                         os << "[" << from_ascii(params_.height.asLatexString()) << "]";
332                 } else {
333                         // Special heights
334                         // set no optional argument when the value is the default "1\height"
335                         // (special units like \height are handled as "in")
336                         // but when the user has chosen a non-default inner_pos, the height
337                         // must be given: \minipage[pos][height][inner-pos]{width}
338                         if ((params_.height != Length("1in") ||
339                                  params_.height_special != "totalheight") ||
340                                 params_.inner_pos != params_.pos) {
341                                 // FIXME UNICODE
342                                 os << "[" << params_.height.value()
343                                         << "\\" << from_utf8(params_.height_special) << "]";
344                         }
345                 }
346                 if (params_.inner_pos != params_.pos)
347                         os << "[" << params_.inner_pos << "]";
348
349                 // FIXME UNICODE
350                 os << '{' << from_ascii(width_string) << '}';
351
352                 if (params_.use_parbox)
353                         os << "{";
354                 os << "%\n";
355                 ++i;
356         }
357         if (btype == Shaded) {
358                 os << "\\begin{shaded}%\n";
359                 ++i;
360         }
361
362         i += InsetText::latex(os, runparams);
363
364         if (btype == Shaded)
365                 os << "\\end{shaded}";
366
367         if (params_.inner_box) {
368                 if (params_.use_parbox)
369                         os << "%\n}";
370                 else
371                         os << "%\n\\end{minipage}";
372         }
373
374         switch (btype) {
375         case Frameless:
376                 break;
377         case Framed:
378                 os << "\\end{framed}";
379                 break;
380         case Boxed:
381                 if (!params_.inner_box)
382                         os << "}"; // for makebox
383                 os << "}";
384                 break;
385         case ovalbox:
386         case Ovalbox:
387         case Doublebox:
388         case Shadowbox:
389                 os << "}";
390                 break;
391         case Shaded:
392                 // already done
393                 break;
394         }
395
396         i += 2;
397
398         return i;
399 #endif
400         return 1;
401 }
402
403 #else
404 int InsetBox::plaintext(odocstream & os, OutputParams const & runparams) const
405 {
406         BoxType const btype = boxtranslator().find(params_.type);
407
408         switch (btype) {
409                 case Frameless:
410                         break;
411                 case Framed:
412                 case Boxed:
413                         os << "[\n";
414                         break;
415                 case ovalbox:
416                         os << "(\n";
417                         break;
418                 case Ovalbox:
419                         os << "((\n";
420                         break;
421                 case Shadowbox:
422                 case Shaded:
423                         os << "[/\n";
424                         break;
425                 case Doublebox:
426                         os << "[[\n";
427                         break;
428         }
429
430         InsetText::plaintext(os, runparams);
431
432         int len = 0;
433         switch (btype) {
434                 case Frameless:
435                         os << "\n";
436                         break;
437                 case Framed:
438                 case Boxed:
439                         os << "\n]";
440                         len = 1;
441                         break;
442                 case ovalbox:
443                         os << "\n)";
444                         len = 1;
445                         break;
446                 case Ovalbox:
447                         os << "\n))";
448                         len = 2;
449                         break;
450                 case Shadowbox:
451                 case Shaded:
452                         os << "\n/]";
453                         len = 2;
454                         break;
455                 case Doublebox:
456                         os << "\n]]";
457                         len = 2;
458                         break;
459         }
460
461         return PLAINTEXT_NEWLINE + len; // len chars on a separate line
462 }
463
464
465 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
466 {
467         return InsetText::docbook(os, runparams);
468 }
469
470
471 docstring InsetBox::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
472 {
473         // construct attributes
474         string attrs = "class='" + params_.type + "'";
475         string style;
476         if (!params_.width.empty())
477                 style += ("width: " + params_.width.asHTMLString() + "; ");
478         // The special heights don't really mean anything for us.
479         if (!params_.height.empty() && params_.height_special == "none")
480                 style += ("height: " + params_.height.asHTMLString() + "; ");
481         if (!style.empty())
482                 attrs += " style='" + style + "'";
483
484         xs << html::StartTag("div", attrs);
485         XHTMLOptions const opts = InsetText::WriteLabel | InsetText::WriteInnerTag;
486         docstring defer = InsetText::insetAsXHTML(xs, runparams, opts);
487         xs << html::EndTag("div");
488         xs << defer;
489         return docstring();
490 }
491
492
493 void InsetBox::validate(LaTeXFeatures & features) const
494 {
495         BoxType btype = boxtranslator().find(params_.type);
496         switch (btype) {
497         case Frameless:
498                 break;
499         case Framed:
500                 features.require("framed");
501                 break;
502         case Boxed:
503                 features.require("calc");
504                 break;
505         case ovalbox:
506         case Ovalbox:
507         case Shadowbox:
508         case Doublebox:
509                 features.require("calc");
510                 features.require("fancybox");
511                 break;
512         case Shaded:
513                 features.require("color");
514                 features.require("framed");
515                 break;
516         }
517         InsetCollapsable::validate(features);
518 }
519
520
521 docstring InsetBox::contextMenu(BufferView const &, int, int) const
522 {
523         return from_ascii("context-box");
524 }
525
526
527 string InsetBox::params2string(InsetBoxParams const & params)
528 {
529         ostringstream data;
530         data << "box" << ' ';
531         params.write(data);
532         return data.str();
533 }
534
535
536 void InsetBox::string2params(string const & in, InsetBoxParams & params)
537 {
538         params = InsetBoxParams(string());
539         if (in.empty())
540                 return;
541
542         istringstream data(in);
543         Lexer lex;
544         lex.setStream(data);
545
546         string name;
547         lex >> name;
548         if (!lex || name != "box") {
549                 LYXERR0("InsetBox::string2params(" << in << ")\n"
550                                           "Expected arg 1 to be \"box\"\n");
551                 return;
552         }
553
554         // This is part of the inset proper that is usually swallowed
555         // by Text::readInset
556         string id;
557         lex >> id;
558         if (!lex || id != "Box") {
559                 LYXERR0("InsetBox::string2params(" << in << ")\n"
560                                           "Expected arg 2 to be \"Box\"\n");
561         }
562
563         params.read(lex);
564 }
565
566
567 /////////////////////////////////////////////////////////////////////////
568 //
569 // InsetBoxParams
570 //
571 /////////////////////////////////////////////////////////////////////////
572
573 InsetBoxParams::InsetBoxParams(string const & label)
574         : type(label),
575           use_parbox(false),
576           inner_box(true),
577           width(Length("100col%")),
578           special("none"),
579           pos('t'),
580           hor_pos('c'),
581           inner_pos('t'),
582           height(Length("1in")),
583           height_special("totalheight") // default is 1\\totalheight
584 {}
585
586
587 void InsetBoxParams::write(ostream & os) const
588 {
589         os << "Box " << type << "\n";
590         os << "position \"" << pos << "\"\n";
591         os << "hor_pos \"" << hor_pos << "\"\n";
592         os << "has_inner_box " << inner_box << "\n";
593         os << "inner_pos \"" << inner_pos << "\"\n";
594         os << "use_parbox " << use_parbox << "\n";
595         os << "width \"" << width.asString() << "\"\n";
596         os << "special \"" << special << "\"\n";
597         os << "height \"" << height.asString() << "\"\n";
598         os << "height_special \"" << height_special << "\"\n";
599 }
600
601
602 void InsetBoxParams::read(Lexer & lex)
603 {
604         lex.setContext("InsetBoxParams::read");
605         lex >> type;
606         lex >> "position" >> pos;
607         lex >> "hor_pos" >> hor_pos;
608         lex >> "has_inner_box" >> inner_box;
609         if (type == "Framed")
610                 inner_box = false;
611         lex >> "inner_pos" >> inner_pos;
612         lex >> "use_parbox" >> use_parbox;
613         lex >> "width" >> width;
614         lex >> "special" >> special;
615         lex >> "height" >> height;
616         lex >> "height_special" >> height_special;
617 }
618
619 #endif
620 } // namespace lyx
621