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