]> git.lyx.org Git - lyx.git/blob - src/insets/insetbox.C
2e5571879a16a786c6f35b1888e5424a1e693a7c
[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         if (params_.inner_box || params_.special != "width")
160                 mi.base.textwidth = params_.width.inPixels(m.base.textwidth);
161         InsetCollapsable::metrics(mi, dim);
162         dim_ = dim;
163 }
164
165
166 bool InsetBox::showInsetDialog(BufferView * bv) const
167 {
168         InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
169         return true;
170 }
171
172
173 void InsetBox::doDispatch(LCursor & cur, FuncRequest & cmd)
174 {
175         switch (cmd.action) {
176
177         case LFUN_INSET_MODIFY: {
178                 lyxerr << "InsetBox::dispatch MODIFY" << endl;
179                 InsetBoxMailer::string2params(cmd.argument, params_);
180                 setButtonLabel();
181                 break;
182         }
183
184         case LFUN_INSET_DIALOG_UPDATE:
185                 InsetBoxMailer(*this).updateDialog(&cur.bv());
186                 break;
187
188         case LFUN_MOUSE_RELEASE:
189                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
190                         InsetBoxMailer(*this).showDialog(&cur.bv());
191                         break;
192                 }
193                 InsetCollapsable::doDispatch(cur, cmd);
194                 break;
195
196         default:
197                 InsetCollapsable::doDispatch(cur, cmd);
198                 break;
199         }
200 }
201
202
203 bool InsetBox::getStatus(LCursor & cur, FuncRequest const & cmd,
204                 FuncStatus & flag) const
205 {
206         switch (cmd.action) {
207
208         case LFUN_INSET_MODIFY:
209         case LFUN_INSET_DIALOG_UPDATE:
210                 flag.enabled(true);
211                 return true;
212
213         default:
214                 return InsetCollapsable::getStatus(cur, cmd, flag);
215         }
216 }
217
218
219 int InsetBox::latex(Buffer const & buf, ostream & os,
220                                 OutputParams const & runparams) const
221 {
222         BoxType btype = boxtranslator().find(params_.type);
223
224         string width_string = params_.width.asLatexString();
225         bool stdwidth(false);
226         if (params_.inner_box &&
227                         (width_string.find("1.0\\columnwidth") != string::npos
228                         || width_string.find("1.0\\textwidth") != string::npos)) {
229                 stdwidth = true;
230                 switch (btype) {
231                 case Frameless:
232                         break;
233                 case Boxed:
234                         width_string += " - 2\\fboxsep - 2\\fboxrule";
235                         break;
236                 case ovalbox:
237                         width_string += " - 2\\fboxsep - 0.8pt";
238                         break;
239                 case Ovalbox:
240                         width_string += " - 2\\fboxsep - 1.6pt";
241                         break;
242                 case Shadowbox:
243                         // Shadow falls outside right margin... opinions?
244                         width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
245                         break;
246                 case Doublebox:
247                         width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
248                         break;
249                 }
250         }
251
252         int i = 0;
253         os << "%\n";
254         // Adapt to column/text width correctly also if paragraphs indented:
255         if (stdwidth)
256                 os << "\\noindent";
257
258         switch (btype) {
259         case Frameless:
260                 break;
261         case Boxed:
262                 os << "\\framebox";
263                 if (!params_.inner_box) {
264                         os << "{\\makebox";
265                         // Special widths, see usrguide §3.5
266                         if (params_.special != "none") {
267                                 os << "[" << params_.width.value()
268                                    << "\\" << params_.special << "]";
269                         } else
270                                 os << "[" << width_string << "]";
271                         if (params_.hor_pos != 'c')
272                                 os << "[" << params_.hor_pos << "]";
273                 }
274
275                 os << "{";
276                 break;
277         case ovalbox:
278                 os << "\\ovalbox{";
279                 break;
280         case Ovalbox:
281                 os << "\\Ovalbox{";
282                 break;
283         case Shadowbox:
284                 os << "\\shadowbox{";
285                 break;
286         case Doublebox:
287                 os << "\\doublebox{";
288                 break;
289         }
290
291         if (params_.inner_box) {
292                 if (params_.use_parbox)
293                         os << "\\parbox";
294                 else
295                         os << "\\begin{minipage}";
296
297                 os << "[" << params_.pos << "]";
298                 if (params_.height_special == "none") {
299                         os << "[" << params_.height.asLatexString() << "]";
300                 } else {
301                         // Special heights
302                         os << "[" << params_.height.value()
303                            << "\\" << params_.height_special << "]";
304                 }
305                 if (params_.inner_pos != params_.pos)
306                         os << "[" << params_.inner_pos << "]";
307
308                 os << "{" << width_string << "}";
309
310                 if (params_.use_parbox)
311                         os << "{";
312                 os << "%\n";
313                 i += 1;
314         }
315
316         i += InsetText::latex(buf, os, runparams);
317
318         if (params_.inner_box) {
319                 if (params_.use_parbox)
320                         os << "%\n}";
321                 else
322                         os << "%\n\\end{minipage}";
323         }
324
325         switch (btype) {
326         case Frameless:
327                 break;
328         case Boxed:
329                 if (!params_.inner_box)
330                         os << "}"; // for makebox
331                 os << "}";
332                 break;
333         case ovalbox:
334         case Ovalbox:
335         case Doublebox:
336         case Shadowbox:
337                 os << "}";
338                 break;
339         }
340         os << "%\n";
341
342         i += 3;
343
344         return i;
345 }
346
347
348 int InsetBox::linuxdoc(Buffer const & buf, std::ostream & os,
349                        OutputParams const & runparams) const
350 {
351         return InsetText::linuxdoc(buf, os, runparams);
352 }
353
354
355 int InsetBox::docbook(Buffer const & buf, std::ostream & os,
356                       OutputParams const & runparams) const
357 {
358         return InsetText::docbook(buf, os, runparams);
359 }
360
361
362 int InsetBox::plaintext(Buffer const & buf, std::ostream & os,
363                     OutputParams const & runparams) const
364 {
365         BoxType const btype = boxtranslator().find(params_.type);
366
367         switch (btype) {
368                 case Frameless: break;
369                 case Boxed:     os << "[";  break;
370                 case ovalbox:   os << "(";  break;
371                 case Ovalbox:   os << "(("; break;
372                 case Shadowbox: os << "[";  break;
373                 case Doublebox: os << "[["; break;
374         }
375
376         int i = InsetText::plaintext(buf, os, runparams);
377
378         switch (btype) {
379                 case Frameless: break;
380                 case Boxed:     os << "]";  break;
381                 case ovalbox:   os << ")";  break;
382                 case Ovalbox:   os << "))"; break;
383                 case Shadowbox: os << "]/"; break;
384                 case Doublebox: os << "]]"; break;
385         }
386
387         return i;
388 }
389
390
391 void InsetBox::validate(LaTeXFeatures & features) const
392 {
393         features.require("calc");
394         BoxType btype = boxtranslator().find(params_.type);
395         switch (btype) {
396         case Frameless:
397         case Boxed:
398                 break;
399         case ovalbox:
400         case Ovalbox:
401         case Shadowbox:
402         case Doublebox:
403                 features.require("fancybox");
404                 break;
405         }
406         InsetText::validate(features);
407 }
408
409
410 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
411         : inset_(inset)
412 {}
413
414
415 string const InsetBoxMailer::name_ = "box";
416
417
418 string const InsetBoxMailer::inset2string(Buffer const &) const
419 {
420         return params2string(inset_.params());
421 }
422
423
424 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
425 {
426         ostringstream data;
427         data << "box" << ' ';
428         params.write(data);
429         return data.str();
430 }
431
432
433 void InsetBoxMailer::string2params(string const & in,
434                                    InsetBoxParams & params)
435 {
436         params = InsetBoxParams(string());
437         if (in.empty())
438                 return;
439
440         istringstream data(in);
441         LyXLex lex(0,0);
442         lex.setStream(data);
443
444         string name;
445         lex >> name;
446         if (!lex || name != name_)
447                 return print_mailer_error("InsetBoxMailer", in, 1, name_);
448
449         // This is part of the inset proper that is usually swallowed
450         // by LyXText::readInset
451         string id;
452         lex >> id;
453         if (!lex || id != "Box")
454                 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
455
456         params.read(lex);
457 }
458
459
460 InsetBoxParams::InsetBoxParams(string const & label)
461         : type(label),
462           use_parbox(false),
463           inner_box(true),
464           width(LyXLength("100col%")),
465           special("none"),
466           pos('t'),
467           hor_pos('c'),
468           inner_pos('t'),
469           height(LyXLength("1in")),
470           height_special("totalheight") // default is 1\\totalheight
471 {}
472
473
474 void InsetBoxParams::write(ostream & os) const
475 {
476         os << "Box " << type << "\n";
477         os << "position \"" << pos << "\"\n";
478         os << "hor_pos \"" << hor_pos << "\"\n";
479         os << "has_inner_box " << inner_box << "\n";
480         os << "inner_pos \"" << inner_pos << "\"\n";
481         os << "use_parbox " << use_parbox << "\n";
482         os << "width \"" << width.asString() << "\"\n";
483         os << "special \"" << special << "\"\n";
484         os << "height \"" << height.asString() << "\"\n";
485         os << "height_special \"" << height_special << "\"\n";
486 }
487
488
489 void InsetBoxParams::read(LyXLex & lex)
490 {
491         if (!lex.isOK())
492                 return;
493
494         if (lex.isOK()) {
495                 lex.next();
496                 type = lex.getString();
497         }
498         if (!lex.isOK())
499                 return;
500         lex.next();
501         string token;
502         token = lex.getString();
503         if (token == "position") {
504                 lex.next();
505                 // The [0] is needed. We need the first and only char in
506                 // this string -- MV
507                 pos = lex.getString()[0];
508         } else {
509                 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
510                 lex.pushToken(token);
511         }
512         if (!lex.isOK())
513                 return;
514         lex.next();
515         token = lex.getString();
516         if (token == "hor_pos") {
517                 lex.next();
518                 hor_pos = lex.getString()[0];
519         } else {
520                 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
521                 lex.pushToken(token);
522         }
523         if (!lex.isOK())
524                 return;
525         lex.next();
526         token = lex.getString();
527         if (token == "has_inner_box") {
528                 lex.next();
529                 inner_box = lex.getInteger();
530         } else {
531                 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
532                 lex.pushToken(token);
533         }
534
535         if (!lex.isOK())
536                 return;
537         lex.next();
538         token = lex.getString();
539         if (token == "inner_pos") {
540                 lex.next();
541                 inner_pos = lex.getString()[0];
542         } else {
543                 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
544                         << token << endl;
545                 lex.pushToken(token);
546         }
547         if (!lex.isOK())
548                 return;
549         lex.next();
550         token = lex.getString();
551         if (token == "use_parbox") {
552                 lex.next();
553                 use_parbox = lex.getInteger();
554         } else {
555                 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
556                 lex.pushToken(token);
557         }
558         if (!lex.isOK())
559                 return;
560         lex.next();
561         token = lex.getString();
562         if (token == "width") {
563                 lex.next();
564                 width = LyXLength(lex.getString());
565         } else {
566                 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
567                 lex.pushToken(token);
568         }
569         if (!lex.isOK())
570                 return;
571         lex.next();
572         token = lex.getString();
573         if (token == "special") {
574                 lex.next();
575                 special = lex.getString();
576         } else {
577                 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
578                 lex.pushToken(token);
579         }
580         if (!lex.isOK())
581                 return;
582         lex.next();
583         token = lex.getString();
584         if (token == "height") {
585                 lex.next();
586                 height = LyXLength(lex.getString());
587         } else {
588                 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
589                 lex.pushToken(token);
590         }
591         if (!lex.isOK())
592                 return;
593         lex.next();
594         token = lex.getString();
595         if (token == "height_special") {
596                 lex.next();
597                 height_special = lex.getString();
598         } else {
599                 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
600                 lex.pushToken(token);
601         }
602 }