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