]> git.lyx.org Git - lyx.git/blob - src/insets/insetbox.C
layout file converter for layout files in old format
[lyx.git] / src / insets / insetbox.C
1 /**
2  * \file insetbox.C
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 "cursor.h"
18 #include "dispatchresult.h"
19 #include "debug.h"
20 #include "FuncStatus.h"
21 #include "funcrequest.h"
22 #include "gettext.h"
23 #include "LaTeXFeatures.h"
24 #include "LColor.h"
25 #include "lyxlex.h"
26 #include "metricsinfo.h"
27 #include "paragraph.h"
28
29 #include "support/translator.h"
30
31 #include <sstream>
32
33 using std::auto_ptr;
34 using std::string;
35 using std::istringstream;
36 using std::ostream;
37 using std::ostringstream;
38 using std::endl;
39
40
41 namespace {
42
43 typedef Translator<std::string, InsetBox::BoxType> BoxTranslator;
44
45 BoxTranslator const init_boxtranslator() {
46         BoxTranslator translator("Boxed", InsetBox::Boxed);
47         translator.addPair("Frameless", InsetBox::Frameless);
48         translator.addPair("ovalbox", InsetBox::ovalbox);
49         translator.addPair("Ovalbox", InsetBox::Ovalbox);
50         translator.addPair("Shadowbox", InsetBox::Shadowbox);
51         translator.addPair("Doublebox",InsetBox::Doublebox);
52         return translator;
53 }
54
55
56 BoxTranslator const init_boxtranslator_loc() {
57         BoxTranslator translator(_("Boxed"), InsetBox::Boxed);
58         translator.addPair(_("Frameless"), InsetBox::Frameless);
59         translator.addPair(_("ovalbox"), InsetBox::ovalbox);
60         translator.addPair(_("Ovalbox"), InsetBox::Ovalbox);
61         translator.addPair(_("Shadowbox"), InsetBox::Shadowbox);
62         translator.addPair(_("Doublebox"), InsetBox::Doublebox);
63         return translator;
64 }
65
66
67 BoxTranslator const & boxtranslator() {
68         static BoxTranslator translator = init_boxtranslator();
69         return translator;
70 }
71
72
73 BoxTranslator const & boxtranslator_loc() {
74         static BoxTranslator translator = init_boxtranslator_loc();
75         return translator;
76 }
77
78 } // anon
79
80
81 void InsetBox::init()
82 {
83         setInsetName("Box");
84         setButtonLabel();
85 }
86
87
88 InsetBox::InsetBox(BufferParams const & bp, string const & label)
89         : InsetCollapsable(bp), params_(label)
90 {
91         init();
92 }
93
94
95 InsetBox::InsetBox(InsetBox const & in)
96         : InsetCollapsable(in), params_(in.params_)
97 {
98         init();
99 }
100
101
102 InsetBox::~InsetBox()
103 {
104         InsetBoxMailer(*this).hideDialog();
105 }
106
107
108 auto_ptr<InsetBase> InsetBox::doClone() const
109 {
110         return auto_ptr<InsetBase>(new InsetBox(*this));
111 }
112
113
114 string const InsetBox::editMessage() const
115 {
116         return _("Opened Box Inset");
117 }
118
119
120 void InsetBox::write(Buffer const & buf, ostream & os) const
121 {
122         params_.write(os);
123         InsetCollapsable::write(buf, os);
124 }
125
126
127 void InsetBox::read(Buffer const & buf, LyXLex & lex)
128 {
129         params_.read(lex);
130         InsetCollapsable::read(buf, lex);
131         setButtonLabel();
132 }
133
134
135 void InsetBox::setButtonLabel()
136 {
137         LyXFont font(LyXFont::ALL_SANE);
138         font.decSize();
139         font.decSize();
140
141         BoxType btype = boxtranslator().find(params_.type);
142         if (btype == Frameless) {
143                 if (params_.use_parbox)
144                         setLabel(_("Box") + " (" + _("Parbox") + ")");
145                 else
146                         setLabel(_("Box") + " (" + _("Minipage") + ")");
147         } else
148                 setLabel(_("Box") + " (" + boxtranslator_loc().find(btype) + ")");
149
150         font.setColor(LColor::foreground);
151         setBackgroundColor(LColor::background);
152         setLabelFont(font);
153 }
154
155
156 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
157 {
158         MetricsInfo mi = m;
159         mi.base.textwidth = params_.width.inPixels(m.base.textwidth);
160         InsetCollapsable::metrics(mi, dim);
161         //if (params_.inner_box && isOpen())
162         //      dim.wid = mi.base.textwidth;
163         dim_ = dim;
164 }
165
166
167 bool InsetBox::showInsetDialog(BufferView * bv) const
168 {
169         InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
170         return true;
171 }
172
173
174 void InsetBox::doDispatch(LCursor & cur, FuncRequest & cmd)
175 {
176         switch (cmd.action) {
177
178         case LFUN_INSET_MODIFY: {
179                 lyxerr << "InsetBox::dispatch MODIFY" << endl;
180                 InsetBoxMailer::string2params(cmd.argument, params_);
181                 setButtonLabel();
182                 break;
183         }
184
185         case LFUN_INSET_DIALOG_UPDATE:
186                 InsetBoxMailer(*this).updateDialog(&cur.bv());
187                 break;
188
189         case LFUN_MOUSE_RELEASE:
190                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
191                         InsetBoxMailer(*this).showDialog(&cur.bv());
192                         break;
193                 }
194                 InsetCollapsable::doDispatch(cur, cmd);
195                 break;
196
197         default:
198                 InsetCollapsable::doDispatch(cur, cmd);
199                 break;
200         }
201 }
202
203
204 bool InsetBox::getStatus(LCursor & cur, FuncRequest const & cmd,
205                 FuncStatus & flag) const
206 {
207         switch (cmd.action) {
208
209         case LFUN_INSET_MODIFY:
210         case LFUN_INSET_DIALOG_UPDATE:
211                 flag.enabled(true);
212                 return true;
213
214         default:
215                 return InsetCollapsable::getStatus(cur, cmd, flag);
216         }
217 }
218
219
220 int InsetBox::latex(Buffer const & buf, ostream & os,
221                                 OutputParams const & runparams) const
222 {
223         BoxType btype = boxtranslator().find(params_.type);
224
225         string width_string = params_.width.asLatexString();
226         bool stdwidth(false);
227         if (params_.inner_box &&
228                         (width_string.find("1.0\\columnwidth") != string::npos
229                         || width_string.find("1.0\\textwidth") != string::npos)) {
230                 stdwidth = true;
231                 switch (btype) {
232                 case Frameless:
233                         break;
234                 case Boxed:
235                         width_string += " - 2\\fboxsep - 2\\fboxrule";
236                         break;
237                 case ovalbox:
238                         width_string += " - 2\\fboxsep - 0.8pt";
239                         break;
240                 case Ovalbox:
241                         width_string += " - 2\\fboxsep - 1.6pt";
242                         break;
243                 case Shadowbox:
244                         // Shadow falls outside right margin... opinions?
245                         width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
246                         break;
247                 case Doublebox:
248                         width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
249                         break;
250                 }
251         }
252
253         int i = 0;
254         os << "%\n";
255         // Adapt to column/text width correctly also if paragraphs indented:
256         if (stdwidth)
257                 os << "\\noindent";
258
259         switch (btype) {
260         case Frameless:
261                 break;
262         case Boxed:
263                 os << "\\framebox";
264                 if (!params_.inner_box) {
265                         os << "{\\makebox";
266                         // Special widths, see usrguide §3.5
267                         if (params_.special != "none") {
268                                 os << "[" << params_.width.value()
269                                    << "\\" << params_.special << "]";
270                         } else
271                                 os << "[" << width_string << "]";
272                         if (params_.hor_pos != 'c')
273                                 os << "[" << params_.hor_pos << "]";
274                 }
275
276                 os << "{";
277                 break;
278         case ovalbox:
279                 os << "\\ovalbox{";
280                 break;
281         case Ovalbox:
282                 os << "\\Ovalbox{";
283                 break;
284         case Shadowbox:
285                 os << "\\shadowbox{";
286                 break;
287         case Doublebox:
288                 os << "\\doublebox{";
289                 break;
290         }
291
292         if (params_.inner_box) {
293                 if (params_.use_parbox)
294                         os << "\\parbox";
295                 else
296                         os << "\\begin{minipage}";
297
298                 os << "[" << params_.pos << "]";
299                 if (params_.height_special == "none") {
300                         os << "[" << params_.height.asLatexString() << "]";
301                 } else {
302                         // Special heights
303                         os << "[" << params_.height.value()
304                            << "\\" << params_.height_special << "]";
305                 }
306                 if (params_.inner_pos != params_.pos)
307                         os << "[" << params_.inner_pos << "]";
308
309                 os << "{" << width_string << "}";
310
311                 if (params_.use_parbox)
312                         os << "{";
313                 os << "%\n";
314                 i += 1;
315         }
316
317         i += InsetText::latex(buf, os, runparams);
318
319         if (params_.inner_box) {
320                 if (params_.use_parbox)
321                         os << "%\n}";
322                 else
323                         os << "%\n\\end{minipage}";
324         }
325
326         switch (btype) {
327         case Frameless:
328                 break;
329         case Boxed:
330                 if (!params_.inner_box)
331                         os << "}"; // for makebox
332                 os << "}";
333                 break;
334         case ovalbox:
335         case Ovalbox:
336         case Doublebox:
337         case Shadowbox:
338                 os << "}";
339                 break;
340         }
341         os << "%\n";
342
343         i += 3;
344
345         return i;
346 }
347
348
349 int InsetBox::linuxdoc(Buffer const & buf, std::ostream & os,
350                        OutputParams const & runparams) const
351 {
352         return InsetText::linuxdoc(buf, os, runparams);
353 }
354
355
356 int InsetBox::docbook(Buffer const & buf, std::ostream & os,
357                       OutputParams const & runparams) const
358 {
359         return InsetText::docbook(buf, os, runparams);
360 }
361
362
363 int InsetBox::plaintext(Buffer const & buf, std::ostream & os,
364                     OutputParams const & runparams) const
365 {
366         BoxType const btype = boxtranslator().find(params_.type);
367
368         switch (btype) {
369                 case Frameless: break;
370                 case Boxed:     os << "[";  break;
371                 case ovalbox:   os << "(";  break;
372                 case Ovalbox:   os << "(("; break;
373                 case Shadowbox: os << "[";  break;
374                 case Doublebox: os << "[["; break;
375         }
376
377         int i = InsetText::plaintext(buf, os, runparams);
378
379         switch (btype) {
380                 case Frameless: break;
381                 case Boxed:     os << "]";  break;
382                 case ovalbox:   os << ")";  break;
383                 case Ovalbox:   os << "))"; break;
384                 case Shadowbox: os << "]/"; break;
385                 case Doublebox: os << "]]"; break;
386         }
387
388         return i;
389 }
390
391
392 void InsetBox::validate(LaTeXFeatures & features) const
393 {
394         features.require("calc");
395         BoxType btype = boxtranslator().find(params_.type);
396         switch (btype) {
397         case Frameless:
398         case Boxed:
399                 break;
400         case ovalbox:
401         case Ovalbox:
402         case Shadowbox:
403         case Doublebox:
404                 features.require("fancybox");
405                 break;
406         }
407         InsetText::validate(features);
408 }
409
410
411 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
412         : inset_(inset)
413 {}
414
415
416 string const InsetBoxMailer::name_ = "box";
417
418
419 string const InsetBoxMailer::inset2string(Buffer const &) const
420 {
421         return params2string(inset_.params());
422 }
423
424
425 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
426 {
427         ostringstream data;
428         data << "box" << ' ';
429         params.write(data);
430         return data.str();
431 }
432
433
434 void InsetBoxMailer::string2params(string const & in,
435                                    InsetBoxParams & params)
436 {
437         params = InsetBoxParams(string());
438         if (in.empty())
439                 return;
440
441         istringstream data(in);
442         LyXLex lex(0,0);
443         lex.setStream(data);
444
445         string name;
446         lex >> name;
447         if (!lex || name != name_)
448                 return print_mailer_error("InsetBoxMailer", in, 1, name_);
449
450         // This is part of the inset proper that is usually swallowed
451         // by LyXText::readInset
452         string id;
453         lex >> id;
454         if (!lex || id != "Box")
455                 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
456
457         params.read(lex);
458 }
459
460
461 InsetBoxParams::InsetBoxParams(string const & label)
462         : type(label),
463           use_parbox(false),
464           inner_box(true),
465           width(LyXLength("100col%")),
466           special("none"),
467           pos('t'),
468           hor_pos('c'),
469           inner_pos('t'),
470           height(LyXLength("1in")),
471           height_special("totalheight") // default is 1\\totalheight
472 {}
473
474
475 void InsetBoxParams::write(ostream & os) const
476 {
477         os << "Box " << type << "\n";
478         os << "position \"" << pos << "\"\n";
479         os << "hor_pos \"" << hor_pos << "\"\n";
480         os << "has_inner_box " << inner_box << "\n";
481         os << "inner_pos \"" << inner_pos << "\"\n";
482         os << "use_parbox " << use_parbox << "\n";
483         os << "width \"" << width.asString() << "\"\n";
484         os << "special \"" << special << "\"\n";
485         os << "height \"" << height.asString() << "\"\n";
486         os << "height_special \"" << height_special << "\"\n";
487 }
488
489
490 void InsetBoxParams::read(LyXLex & lex)
491 {
492         if (!lex.isOK())
493                 return;
494
495         if (lex.isOK()) {
496                 lex.next();
497                 type = lex.getString();
498         }
499         if (!lex.isOK())
500                 return;
501         lex.next();
502         string token;
503         token = lex.getString();
504         if (token == "position") {
505                 lex.next();
506                 // The [0] is needed. We need the first and only char in
507                 // this string -- MV
508                 pos = lex.getString()[0];
509         } else {
510                 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
511                 lex.pushToken(token);
512         }
513         if (!lex.isOK())
514                 return;
515         lex.next();
516         token = lex.getString();
517         if (token == "hor_pos") {
518                 lex.next();
519                 hor_pos = lex.getString()[0];
520         } else {
521                 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
522                 lex.pushToken(token);
523         }
524         if (!lex.isOK())
525                 return;
526         lex.next();
527         token = lex.getString();
528         if (token == "has_inner_box") {
529                 lex.next();
530                 inner_box = lex.getInteger();
531         } else {
532                 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
533                 lex.pushToken(token);
534         }
535
536         if (!lex.isOK())
537                 return;
538         lex.next();
539         token = lex.getString();
540         if (token == "inner_pos") {
541                 lex.next();
542                 inner_pos = lex.getString()[0];
543         } else {
544                 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
545                         << token << endl;
546                 lex.pushToken(token);
547         }
548         if (!lex.isOK())
549                 return;
550         lex.next();
551         token = lex.getString();
552         if (token == "use_parbox") {
553                 lex.next();
554                 use_parbox = lex.getInteger();
555         } else {
556                 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
557                 lex.pushToken(token);
558         }
559         if (!lex.isOK())
560                 return;
561         lex.next();
562         token = lex.getString();
563         if (token == "width") {
564                 lex.next();
565                 width = LyXLength(lex.getString());
566         } else {
567                 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
568                 lex.pushToken(token);
569         }
570         if (!lex.isOK())
571                 return;
572         lex.next();
573         token = lex.getString();
574         if (token == "special") {
575                 lex.next();
576                 special = lex.getString();
577         } else {
578                 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
579                 lex.pushToken(token);
580         }
581         if (!lex.isOK())
582                 return;
583         lex.next();
584         token = lex.getString();
585         if (token == "height") {
586                 lex.next();
587                 height = LyXLength(lex.getString());
588         } else {
589                 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
590                 lex.pushToken(token);
591         }
592         if (!lex.isOK())
593                 return;
594         lex.next();
595         token = lex.getString();
596         if (token == "height_special") {
597                 lex.next();
598                 height_special = lex.getString();
599         } else {
600                 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
601                 lex.pushToken(token);
602         }
603 }