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