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