]> git.lyx.org Git - features.git/blob - src/insets/InsetBox.cpp
use simpler parser interface
[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
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 "support/gettext.h"
25 #include "LaTeXFeatures.h"
26 #include "Lexer.h"
27 #include "MetricsInfo.h"
28 #include "TextClass.h"
29
30 #include "support/debug.h"
31 #include "support/Translator.h"
32
33 #include "frontends/Application.h"
34
35 #include <sstream>
36
37 using namespace std;
38
39 namespace lyx {
40
41 namespace {
42
43 typedef Translator<string, InsetBox::BoxType> BoxTranslator;
44 typedef Translator<docstring, InsetBox::BoxType> BoxTranslatorLoc;
45
46 BoxTranslator initBoxtranslator()
47 {
48         BoxTranslator translator("Boxed", InsetBox::Boxed);
49         translator.addPair("Frameless", InsetBox::Frameless);
50         translator.addPair("Framed", InsetBox::Framed);
51         translator.addPair("ovalbox", InsetBox::ovalbox);
52         translator.addPair("Ovalbox", InsetBox::Ovalbox);
53         translator.addPair("Shadowbox", InsetBox::Shadowbox);
54         translator.addPair("Shaded", InsetBox::Shaded);
55         translator.addPair("Doublebox",InsetBox::Doublebox);
56         return translator;
57 }
58
59
60 BoxTranslatorLoc initBoxtranslatorLoc()
61 {
62         BoxTranslatorLoc translator(_("simple frame"), InsetBox::Boxed);
63         translator.addPair(_("frameless"), InsetBox::Frameless);
64         translator.addPair(_("simple frame, page breaks"), InsetBox::Framed);
65         translator.addPair(_("oval, thin"), InsetBox::ovalbox);
66         translator.addPair(_("oval, thick"), InsetBox::Ovalbox);
67         translator.addPair(_("drop shadow"), InsetBox::Shadowbox);
68         translator.addPair(_("shaded background"), InsetBox::Shaded);
69         translator.addPair(_("double frame"), InsetBox::Doublebox);
70         return translator;
71 }
72
73
74 BoxTranslator const & boxtranslator()
75 {
76         static BoxTranslator translator = initBoxtranslator();
77         return translator;
78 }
79
80
81 BoxTranslatorLoc const & boxtranslator_loc()
82 {
83         static BoxTranslatorLoc translator = initBoxtranslatorLoc();
84         return translator;
85 }
86
87 } // namespace anon
88
89
90 /////////////////////////////////////////////////////////////////////////
91 //
92 // InsetBox
93 //
94 /////////////////////////////////////////////////////////////////////////
95
96 InsetBox::InsetBox(Buffer const & buffer, string const & label)
97         : InsetCollapsable(buffer), params_(label)
98 {
99         if (forceEmptyLayout())
100                 paragraphs().back().setLayout(buffer.params().documentClass().emptyLayout());
101 }
102
103
104 InsetBox::~InsetBox()
105 {
106         hideDialogs("box", this);
107 }
108
109
110 docstring InsetBox::editMessage() const
111 {
112         return _("Opened Box Inset");
113 }
114
115
116 docstring InsetBox::name() const 
117 {
118         // FIXME: UNICODE
119         string name = "Box";
120         if (boxtranslator().find(params_.type) == Shaded)
121                 name += ":Shaded";
122         return from_ascii(name);
123 }
124
125
126 void InsetBox::write(ostream & os) const
127 {
128         params_.write(os);
129         InsetCollapsable::write(os);
130 }
131
132
133 void InsetBox::read(Lexer & lex)
134 {
135         params_.read(lex);
136         InsetCollapsable::read(lex);
137 }
138
139
140 void InsetBox::setButtonLabel()
141 {
142         BoxType btype = boxtranslator().find(params_.type);
143
144         docstring label;
145         label += _("Box");
146         label += " (";
147         if (btype == Frameless) {
148                 if (params_.use_parbox)
149                         label += _("Parbox");
150                 else
151                         label += _("Minipage");
152         } else {
153                 label += boxtranslator_loc().find(btype);
154         }
155         label += ")";
156
157         setLabel(label);
158 }
159
160
161 bool InsetBox::hasFixedWidth() const
162 {
163         return params_.inner_box || params_.special != "width";
164 }
165
166
167 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
168 {
169         // back up textwidth.
170         int textwidth_backup = m.base.textwidth;
171         if (hasFixedWidth())
172                 m.base.textwidth = params_.width.inPixels(m.base.textwidth);
173         InsetCollapsable::metrics(m, dim);
174         // retore textwidth.
175         m.base.textwidth = textwidth_backup;
176 }
177
178
179 bool InsetBox::forceEmptyLayout(idx_type) const
180 {
181         return !params_.inner_box && params_.type != "Framed";
182 }
183
184
185 bool InsetBox::showInsetDialog(BufferView * bv) const
186 {
187         bv->showDialog("box", params2string(params_),
188                 const_cast<InsetBox *>(this));
189         return true;
190 }
191
192
193 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
194 {
195         switch (cmd.action) {
196
197         case LFUN_INSET_MODIFY: {
198                 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
199                 if (cmd.getArg(0) == "changetype")
200                         params_.type = cmd.getArg(1);
201                 else
202                         string2params(to_utf8(cmd.argument()), params_);
203                 setLayout(cur.buffer().params());
204                 break;
205         }
206
207         case LFUN_INSET_DIALOG_UPDATE:
208                 cur.bv().updateDialog("box", params2string(params_));
209                 break;
210
211         default:
212                 InsetCollapsable::doDispatch(cur, cmd);
213                 break;
214         }
215 }
216
217
218 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
219                 FuncStatus & flag) const
220 {
221         switch (cmd.action) {
222
223         case LFUN_INSET_MODIFY:
224                 if (cmd.getArg(0) == "changetype")
225                         flag.setOnOff(cmd.getArg(1) == params_.type);
226                 else
227                         flag.enabled(true);
228                 return true;
229
230         case LFUN_INSET_DIALOG_UPDATE:
231                 flag.enabled(true);
232                 return true;
233
234         case LFUN_BREAK_PARAGRAPH:
235                 if (params_.inner_box || params_.type == "Framed")
236                         return InsetCollapsable::getStatus(cur, cmd, flag);
237                 flag.enabled(false);
238                 return true;
239
240         default:
241                 return InsetCollapsable::getStatus(cur, cmd, flag);
242         }
243 }
244
245
246 int InsetBox::latex(odocstream & 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         int i = 0;
281         os << "%\n";
282         // Adapt to column/text width correctly also if paragraphs indented:
283         if (stdwidth)
284                 os << "\\noindent";
285
286         switch (btype) {
287         case Frameless:
288                 break;
289         case Framed:
290                 os << "\\begin{framed}%\n";
291                 i += 1;
292                 break;
293         case Boxed:
294                 os << "\\framebox";
295                 if (!params_.inner_box) {
296                         os << "{\\makebox";
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
310                 os << "{";
311                 break;
312         case ovalbox:
313                 os << "\\ovalbox{";
314                 break;
315         case Ovalbox:
316                 os << "\\Ovalbox{";
317                 break;
318         case Shadowbox:
319                 os << "\\shadowbox{";
320                 break;
321         case Shaded:
322                 // later
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
333                         os << "\\begin{minipage}";
334
335                 os << "[" << params_.pos << "]";
336                 if (params_.height_special == "none") {
337                         // FIXME UNICODE
338                         os << "[" << from_ascii(params_.height.asLatexString()) << "]";
339                 } else {
340                         // Special heights
341                         // set no optional argument when the value is the default "1\height"
342                         // (special units like \height are handled as "in")
343                         // but when the user has chosen a non-default inner_pos, the height
344                         // must be given: \minipage[pos][height][inner-pos]{width}
345                         if ((params_.height != Length("1in") ||
346                                  params_.height_special != "totalheight") ||
347                                 params_.inner_pos != params_.pos) {
348                                 // FIXME UNICODE
349                                 os << "[" << params_.height.value()
350                                         << "\\" << from_utf8(params_.height_special) << "]";
351                         }
352                 }
353                 if (params_.inner_pos != params_.pos)
354                         os << "[" << params_.inner_pos << "]";
355
356                 // FIXME UNICODE
357                 os << '{' << from_ascii(width_string) << '}';
358
359                 if (params_.use_parbox)
360                         os << "{";
361                 os << "%\n";
362                 ++i;
363         }
364         if (btype == Shaded) {
365                 os << "\\begin{shaded}%\n";
366                 ++i;
367         }
368
369         i += InsetText::latex(os, runparams);
370
371         if (btype == Shaded)
372                 os << "\\end{shaded}";
373
374         if (params_.inner_box) {
375                 if (params_.use_parbox)
376                         os << "%\n}";
377                 else
378                         os << "%\n\\end{minipage}";
379         }
380
381         switch (btype) {
382         case Frameless:
383                 break;
384         case Framed:
385                 os << "\\end{framed}";
386                 break;
387         case Boxed:
388                 if (!params_.inner_box)
389                         os << "}"; // for makebox
390                 os << "}";
391                 break;
392         case ovalbox:
393         case Ovalbox:
394         case Doublebox:
395         case Shadowbox:
396                 os << "}";
397                 break;
398         case Shaded:
399                 // already done
400                 break;
401         }
402
403         i += 2;
404
405         return i;
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 void InsetBox::validate(LaTeXFeatures & features) const
477 {
478         BoxType btype = boxtranslator().find(params_.type);
479         switch (btype) {
480         case Frameless:
481                 break;
482         case Framed:
483                 features.require("framed");
484                 break;
485         case Boxed:
486                 features.require("calc");
487                 break;
488         case ovalbox:
489         case Ovalbox:
490         case Shadowbox:
491         case Doublebox:
492                 features.require("calc");
493                 features.require("fancybox");
494                 break;
495         case Shaded:
496                 features.require("color");
497                 features.require("framed");
498                 break;
499         }
500         InsetText::validate(features);
501 }
502
503
504 docstring InsetBox::contextMenu(BufferView const &, int, int) const
505 {
506         return from_ascii("context-box");
507 }
508
509
510 string InsetBox::params2string(InsetBoxParams const & params)
511 {
512         ostringstream data;
513         data << "box" << ' ';
514         params.write(data);
515         return data.str();
516 }
517
518
519 void InsetBox::string2params(string const & in, InsetBoxParams & params)
520 {
521         params = InsetBoxParams(string());
522         if (in.empty())
523                 return;
524
525         istringstream data(in);
526         Lexer lex;
527         lex.setStream(data);
528
529         string name;
530         lex >> name;
531         if (!lex || name != "box") {
532                 LYXERR0("InsetBox::string2params(" << in << ")\n"
533                                           "Expected arg 1 to be \"box\"\n");
534                 return;
535         }
536
537         // This is part of the inset proper that is usually swallowed
538         // by Text::readInset
539         string id;
540         lex >> id;
541         if (!lex || id != "Box") {
542                 LYXERR0("InsetBox::string2params(" << in << ")\n"
543                                           "Expected arg 2 to be \"Box\"\n");
544         }
545
546         params.read(lex);
547 }
548
549
550 /////////////////////////////////////////////////////////////////////////
551 //
552 // InsetBoxParams
553 //
554 /////////////////////////////////////////////////////////////////////////
555
556 InsetBoxParams::InsetBoxParams(string const & label)
557         : type(label),
558           use_parbox(false),
559           inner_box(true),
560           width(Length("100col%")),
561           special("none"),
562           pos('t'),
563           hor_pos('c'),
564           inner_pos('t'),
565           height(Length("1in")),
566           height_special("totalheight") // default is 1\\totalheight
567 {}
568
569
570 void InsetBoxParams::write(ostream & os) const
571 {
572         os << "Box " << type << "\n";
573         os << "position \"" << pos << "\"\n";
574         os << "hor_pos \"" << hor_pos << "\"\n";
575         os << "has_inner_box " << inner_box << "\n";
576         os << "inner_pos \"" << inner_pos << "\"\n";
577         os << "use_parbox " << use_parbox << "\n";
578         os << "width \"" << width.asString() << "\"\n";
579         os << "special \"" << special << "\"\n";
580         os << "height \"" << height.asString() << "\"\n";
581         os << "height_special \"" << height_special << "\"\n";
582 }
583
584
585 void InsetBoxParams::read(Lexer & lex)
586 {
587         lex.setContext("InsetBoxParams::read");
588         lex >> type;
589         lex >> "position" >> pos;
590         lex >> "hor_pos" >> hor_pos;
591         lex >> "has_inner_box" >> inner_box;
592         if (type == "Framed")
593                 inner_box = false;
594         lex >> "inner_pos" >> inner_pos;
595         lex >> "use_parbox" >> use_parbox;
596         lex >> "width" >> width;
597         lex >> "special" >> special;
598         lex >> "height" >> height;
599         lex >> "height_special" >> height_special;
600 }
601
602
603 } // namespace lyx