]> git.lyx.org Git - lyx.git/blob - src/insets/InsetBox.cpp
InsetListings: change the interface of diaplay function and allow AlignLeft. Applied...
[lyx.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 "BufferView.h"
18 #include "Cursor.h"
19 #include "DispatchResult.h"
20 #include "debug.h"
21 #include "FuncStatus.h"
22 #include "FuncRequest.h"
23 #include "gettext.h"
24 #include "LaTeXFeatures.h"
25 #include "Color.h"
26 #include "Lexer.h"
27 #include "MetricsInfo.h"
28 #include "Paragraph.h"
29 #include "TextMetrics.h"
30
31 #include "support/Translator.h"
32
33 #include <sstream>
34
35
36 namespace lyx {
37
38 using std::auto_ptr;
39 using std::string;
40 using std::istringstream;
41 using std::ostream;
42 using std::ostringstream;
43 using std::endl;
44
45
46 namespace {
47
48 typedef Translator<std::string, InsetBox::BoxType> BoxTranslator;
49 typedef Translator<docstring, InsetBox::BoxType> BoxTranslatorLoc;
50
51 BoxTranslator const init_boxtranslator()
52 {
53         BoxTranslator translator("Boxed", InsetBox::Boxed);
54         translator.addPair("Frameless", InsetBox::Frameless);
55         translator.addPair("ovalbox", InsetBox::ovalbox);
56         translator.addPair("Ovalbox", InsetBox::Ovalbox);
57         translator.addPair("Shadowbox", InsetBox::Shadowbox);
58         translator.addPair("Doublebox",InsetBox::Doublebox);
59         return translator;
60 }
61
62
63 BoxTranslatorLoc const init_boxtranslator_loc()
64 {
65         BoxTranslatorLoc translator(_("Boxed"), InsetBox::Boxed);
66         translator.addPair(_("Frameless"), InsetBox::Frameless);
67         translator.addPair(_("ovalbox"), InsetBox::ovalbox);
68         translator.addPair(_("Ovalbox"), InsetBox::Ovalbox);
69         translator.addPair(_("Shadowbox"), InsetBox::Shadowbox);
70         translator.addPair(_("Doublebox"), InsetBox::Doublebox);
71         return translator;
72 }
73
74
75 BoxTranslator const & boxtranslator()
76 {
77         static BoxTranslator translator = init_boxtranslator();
78         return translator;
79 }
80
81
82 BoxTranslatorLoc const & boxtranslator_loc()
83 {
84         static BoxTranslatorLoc translator = init_boxtranslator_loc();
85         return translator;
86 }
87
88 } // anon
89
90
91 void InsetBox::init()
92 {
93         setButtonLabel();
94 }
95
96
97 InsetBox::InsetBox(BufferParams const & bp, string const & label)
98         : InsetCollapsable(bp), params_(label)
99 {
100         init();
101 }
102
103
104 InsetBox::InsetBox(InsetBox const & in)
105         : InsetCollapsable(in), params_(in.params_)
106 {
107         init();
108 }
109
110
111 InsetBox::~InsetBox()
112 {
113         InsetBoxMailer(*this).hideDialog();
114 }
115
116
117 auto_ptr<Inset> InsetBox::doClone() const
118 {
119         return auto_ptr<Inset>(new InsetBox(*this));
120 }
121
122
123 docstring const InsetBox::editMessage() const
124 {
125         return _("Opened Box Inset");
126 }
127
128
129 void InsetBox::write(Buffer const & buf, ostream & os) const
130 {
131         params_.write(os);
132         InsetCollapsable::write(buf, os);
133 }
134
135
136 void InsetBox::read(Buffer const & buf, Lexer & lex)
137 {
138         params_.read(lex);
139         InsetCollapsable::read(buf, lex);
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         setBackgroundColor(Color::background);
168         setLabelFont(font);
169 }
170
171
172 bool InsetBox::hasFixedWidth() const
173 {
174       return params_.inner_box || params_.special != "width";
175 }
176
177
178 bool InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
179 {
180         MetricsInfo mi = m;
181         // first round in order to know the minimum size.
182         InsetCollapsable::metrics(mi, dim);
183         TextMetrics & tm = mi.base.bv->textMetrics(&text_);
184         if (hasFixedWidth())
185                 mi.base.textwidth =
186                         std::max(tm.width() + 2 * border_ + (int) (2.5 * TEXT_TO_INSET_OFFSET),
187                                  params_.width.inPixels(m.base.textwidth));
188         InsetCollapsable::metrics(mi, dim);
189         bool const changed = dim_ != dim;
190         dim_ = dim;
191         return changed;
192 }
193
194
195 bool InsetBox::forceDefaultParagraphs(idx_type) const
196 {
197         return !params_.inner_box;
198 }
199
200
201 bool InsetBox::showInsetDialog(BufferView * bv) const
202 {
203         InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
204         return true;
205 }
206
207
208 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
209 {
210         switch (cmd.action) {
211
212         case LFUN_INSET_MODIFY: {
213                 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
214                 InsetBoxMailer::string2params(to_utf8(cmd.argument()), params_);
215                 setButtonLabel();
216                 break;
217         }
218
219         case LFUN_INSET_DIALOG_UPDATE:
220                 InsetBoxMailer(*this).updateDialog(&cur.bv());
221                 break;
222
223         case LFUN_MOUSE_RELEASE:
224                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
225                         InsetBoxMailer(*this).showDialog(&cur.bv());
226                         break;
227                 }
228                 InsetCollapsable::doDispatch(cur, cmd);
229                 break;
230
231         default:
232                 InsetCollapsable::doDispatch(cur, cmd);
233                 break;
234         }
235 }
236
237
238 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
239                 FuncStatus & flag) const
240 {
241         switch (cmd.action) {
242
243         case LFUN_INSET_MODIFY:
244         case LFUN_INSET_DIALOG_UPDATE:
245                 flag.enabled(true);
246                 return true;
247         case LFUN_BREAK_PARAGRAPH:
248                 if (params_.inner_box) {
249                         return InsetCollapsable::getStatus(cur, cmd, flag);
250                 } else {
251                         flag.enabled(false);
252                         return true;
253                 }
254
255         default:
256                 return InsetCollapsable::getStatus(cur, cmd, flag);
257         }
258 }
259
260
261 int InsetBox::latex(Buffer const & buf, odocstream & os,
262                     OutputParams const & runparams) const
263 {
264         BoxType btype = boxtranslator().find(params_.type);
265
266         string width_string = params_.width.asLatexString();
267         bool stdwidth(false);
268         if (params_.inner_box &&
269                         (width_string.find("1.0\\columnwidth") != string::npos
270                         || width_string.find("1.0\\textwidth") != string::npos)) {
271                 stdwidth = true;
272                 switch (btype) {
273                 case Frameless:
274                         break;
275                 case Boxed:
276                         width_string += " - 2\\fboxsep - 2\\fboxrule";
277                         break;
278                 case ovalbox:
279                         width_string += " - 2\\fboxsep - 0.8pt";
280                         break;
281                 case Ovalbox:
282                         width_string += " - 2\\fboxsep - 1.6pt";
283                         break;
284                 case Shadowbox:
285                         // Shadow falls outside right margin... opinions?
286                         width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
287                         break;
288                 case Doublebox:
289                         width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
290                         break;
291                 }
292         }
293
294         int i = 0;
295         os << "%\n";
296         // Adapt to column/text width correctly also if paragraphs indented:
297         if (stdwidth)
298                 os << "\\noindent";
299
300         switch (btype) {
301         case Frameless:
302                 break;
303         case Boxed:
304                 os << "\\framebox";
305                 if (!params_.inner_box) {
306                         os << "{\\makebox";
307                         // Special widths, see usrguide §3.5
308                         // FIXME UNICODE
309                         if (params_.special != "none") {
310                                 os << "[" << params_.width.value()
311                                    << '\\' << from_utf8(params_.special)
312                                    << ']';
313                         } else
314                                 os << '[' << from_ascii(width_string)
315                                    << ']';
316                         if (params_.hor_pos != 'c')
317                                 os << "[" << params_.hor_pos << "]";
318                 }
319
320                 os << "{";
321                 break;
322         case ovalbox:
323                 os << "\\ovalbox{";
324                 break;
325         case Ovalbox:
326                 os << "\\Ovalbox{";
327                 break;
328         case Shadowbox:
329                 os << "\\shadowbox{";
330                 break;
331         case Doublebox:
332                 os << "\\doublebox{";
333                 break;
334         }
335
336         if (params_.inner_box) {
337                 if (params_.use_parbox)
338                         os << "\\parbox";
339                 else
340                         os << "\\begin{minipage}";
341
342                 os << "[" << params_.pos << "]";
343                 if (params_.height_special == "none") {
344                         // FIXME UNICODE
345                         os << '[' << from_ascii(params_.height.asLatexString())
346                            << ']';
347                 } else {
348                         // Special heights
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