]> git.lyx.org Git - lyx.git/blob - src/insets/InsetBox.cpp
move the validation code from InsetFlex to InsetCollapsable
[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 "TextClass.h"
28
29 #include "support/debug.h"
30 #include "support/gettext.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 (forcePlainLayout())
100                 paragraphs().back().setLayout(buffer.params().documentClass().plainLayout());
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::forcePlainLayout(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                 flag.setEnabled(true);
227                 return true;
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_.type == "Framed")
235                         return InsetCollapsable::getStatus(cur, cmd, flag);
236                 flag.setEnabled(false);
237                 return true;
238
239         default:
240                 return InsetCollapsable::getStatus(cur, cmd, flag);
241         }
242 }
243
244
245 int InsetBox::latex(odocstream & os, OutputParams const & runparams) const
246 {
247         BoxType btype = boxtranslator().find(params_.type);
248
249         string width_string = params_.width.asLatexString();
250         bool stdwidth = false;
251         if (params_.inner_box &&
252                         (width_string.find("1.0\\columnwidth") != string::npos
253                         || width_string.find("1.0\\textwidth") != string::npos)) {
254                 stdwidth = true;
255                 switch (btype) {
256                 case Frameless:
257                 case Framed:
258                         break;
259                 case Boxed:
260                 case Shaded:
261                         width_string += " - 2\\fboxsep - 2\\fboxrule";
262                         break;
263                 case ovalbox:
264                         width_string += " - 2\\fboxsep - 0.8pt";
265                         break;
266                 case Ovalbox:
267                         width_string += " - 2\\fboxsep - 1.6pt";
268                         break;
269                 case Shadowbox:
270                         // Shadow falls outside right margin... opinions?
271                         width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
272                         break;
273                 case Doublebox:
274                         width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1pt";
275                         break;
276                 }
277         }
278
279         int i = 0;
280         os << "%\n";
281         // Adapt to column/text width correctly also if paragraphs indented:
282         if (stdwidth)
283                 os << "\\noindent";
284
285         switch (btype) {
286         case Frameless:
287                 break;
288         case Framed:
289                 os << "\\begin{framed}%\n";
290                 i += 1;
291                 break;
292         case Boxed:
293                 os << "\\framebox";
294                 if (!params_.inner_box) {
295                         os << "{\\makebox";
296                         // Special widths, see usrguide §3.5
297                         // FIXME UNICODE
298                         if (params_.special != "none") {
299                                 os << "[" << params_.width.value()
300                                    << '\\' << from_utf8(params_.special)
301                                    << ']';
302                         } else
303                                 os << '[' << from_ascii(width_string)
304                                    << ']';
305                         if (params_.hor_pos != 'c')
306                                 os << "[" << params_.hor_pos << "]";
307                 }
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                 // later
322                 break;
323         case Doublebox:
324                 os << "\\doublebox{";
325                 break;
326         }
327
328         if (params_.inner_box) {
329                 if (params_.use_parbox)
330                         os << "\\parbox";
331                 else
332                         os << "\\begin{minipage}";
333
334                 os << "[" << params_.pos << "]";
335                 if (params_.height_special == "none") {
336                         // FIXME UNICODE
337                         os << "[" << from_ascii(params_.height.asLatexString()) << "]";
338                 } else {
339                         // Special heights
340                         // set no optional argument when the value is the default "1\height"
341                         // (special units like \height are handled as "in")
342                         // but when the user has chosen a non-default inner_pos, the height
343                         // must be given: \minipage[pos][height][inner-pos]{width}
344                         if ((params_.height != Length("1in") ||
345                                  params_.height_special != "totalheight") ||
346                                 params_.inner_pos != params_.pos) {
347                                 // FIXME UNICODE
348                                 os << "[" << params_.height.value()
349                                         << "\\" << from_utf8(params_.height_special) << "]";
350                         }
351                 }
352                 if (params_.inner_pos != params_.pos)
353                         os << "[" << params_.inner_pos << "]";
354
355                 // FIXME UNICODE
356                 os << '{' << from_ascii(width_string) << '}';
357
358                 if (params_.use_parbox)
359                         os << "{";
360                 os << "%\n";
361                 ++i;
362         }
363         if (btype == Shaded) {
364                 os << "\\begin{shaded}%\n";
365                 ++i;
366         }
367
368         i += InsetText::latex(os, runparams);
369
370         if (btype == Shaded)
371                 os << "\\end{shaded}";
372
373         if (params_.inner_box) {
374                 if (params_.use_parbox)
375                         os << "%\n}";
376                 else
377                         os << "%\n\\end{minipage}";
378         }
379
380         switch (btype) {
381         case Frameless:
382                 break;
383         case Framed:
384                 os << "\\end{framed}";
385                 break;
386         case Boxed:
387                 if (!params_.inner_box)
388                         os << "}"; // for makebox
389                 os << "}";
390                 break;
391         case ovalbox:
392         case Ovalbox:
393         case Doublebox:
394         case Shadowbox:
395                 os << "}";
396                 break;
397         case Shaded:
398                 // already done
399                 break;
400         }
401
402         i += 2;
403
404         return i;
405 }
406
407
408 int InsetBox::plaintext(odocstream & os, OutputParams const & runparams) const
409 {
410         BoxType const btype = boxtranslator().find(params_.type);
411
412         switch (btype) {
413                 case Frameless:
414                         break;
415                 case Framed:
416                 case Boxed:
417                         os << "[\n";
418                         break;
419                 case ovalbox:
420                         os << "(\n";
421                         break;
422                 case Ovalbox:
423                         os << "((\n";
424                         break;
425                 case Shadowbox:
426                 case Shaded:
427                         os << "[/\n";
428                         break;
429                 case Doublebox:
430                         os << "[[\n";
431                         break;
432         }
433
434         InsetText::plaintext(os, runparams);
435
436         int len = 0;
437         switch (btype) {
438                 case Frameless:
439                         os << "\n";
440                         break;
441                 case Framed:
442                 case Boxed:
443                         os << "\n]";
444                         len = 1;
445                         break;
446                 case ovalbox:
447                         os << "\n)";
448                         len = 1;
449                         break;
450                 case Ovalbox:
451                         os << "\n))";
452                         len = 2;
453                         break;
454                 case Shadowbox:
455                 case Shaded:
456                         os << "\n/]";
457                         len = 2;
458                         break;
459                 case Doublebox:
460                         os << "\n]]";
461                         len = 2;
462                         break;
463         }
464
465         return PLAINTEXT_NEWLINE + len; // len chars on a separate line
466 }
467
468
469 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
470 {
471         return InsetText::docbook(os, runparams);
472 }
473
474
475 void InsetBox::validate(LaTeXFeatures & features) const
476 {
477         BoxType btype = boxtranslator().find(params_.type);
478         switch (btype) {
479         case Frameless:
480                 break;
481         case Framed:
482                 features.require("framed");
483                 break;
484         case Boxed:
485                 features.require("calc");
486                 break;
487         case ovalbox:
488         case Ovalbox:
489         case Shadowbox:
490         case Doublebox:
491                 features.require("calc");
492                 features.require("fancybox");
493                 break;
494         case Shaded:
495                 features.require("color");
496                 features.require("framed");
497                 break;
498         }
499         InsetText::validate(features);
500 }
501
502
503 docstring InsetBox::contextMenu(BufferView const &, int, int) const
504 {
505         return from_ascii("context-box");
506 }
507
508
509 string InsetBox::params2string(InsetBoxParams const & params)
510 {
511         ostringstream data;
512         data << "box" << ' ';
513         params.write(data);
514         return data.str();
515 }
516
517
518 void InsetBox::string2params(string const & in, InsetBoxParams & params)
519 {
520         params = InsetBoxParams(string());
521         if (in.empty())
522                 return;
523
524         istringstream data(in);
525         Lexer lex;
526         lex.setStream(data);
527
528         string name;
529         lex >> name;
530         if (!lex || name != "box") {
531                 LYXERR0("InsetBox::string2params(" << in << ")\n"
532                                           "Expected arg 1 to be \"box\"\n");
533                 return;
534         }
535
536         // This is part of the inset proper that is usually swallowed
537         // by Text::readInset
538         string id;
539         lex >> id;
540         if (!lex || id != "Box") {
541                 LYXERR0("InsetBox::string2params(" << in << ")\n"
542                                           "Expected arg 2 to be \"Box\"\n");
543         }
544
545         params.read(lex);
546 }
547
548
549 /////////////////////////////////////////////////////////////////////////
550 //
551 // InsetBoxParams
552 //
553 /////////////////////////////////////////////////////////////////////////
554
555 InsetBoxParams::InsetBoxParams(string const & label)
556         : type(label),
557           use_parbox(false),
558           inner_box(true),
559           width(Length("100col%")),
560           special("none"),
561           pos('t'),
562           hor_pos('c'),
563           inner_pos('t'),
564           height(Length("1in")),
565           height_special("totalheight") // default is 1\\totalheight
566 {}
567
568
569 void InsetBoxParams::write(ostream & os) const
570 {
571         os << "Box " << type << "\n";
572         os << "position \"" << pos << "\"\n";
573         os << "hor_pos \"" << hor_pos << "\"\n";
574         os << "has_inner_box " << inner_box << "\n";
575         os << "inner_pos \"" << inner_pos << "\"\n";
576         os << "use_parbox " << use_parbox << "\n";
577         os << "width \"" << width.asString() << "\"\n";
578         os << "special \"" << special << "\"\n";
579         os << "height \"" << height.asString() << "\"\n";
580         os << "height_special \"" << height_special << "\"\n";
581 }
582
583
584 void InsetBoxParams::read(Lexer & lex)
585 {
586         lex.setContext("InsetBoxParams::read");
587         lex >> type;
588         lex >> "position" >> pos;
589         lex >> "hor_pos" >> hor_pos;
590         lex >> "has_inner_box" >> inner_box;
591         if (type == "Framed")
592                 inner_box = false;
593         lex >> "inner_pos" >> inner_pos;
594         lex >> "use_parbox" >> use_parbox;
595         lex >> "width" >> width;
596         lex >> "special" >> special;
597         lex >> "height" >> height;
598         lex >> "height_special" >> height_special;
599 }
600
601
602 } // namespace lyx