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