]> git.lyx.org Git - features.git/blob - src/insets/InsetBox.cpp
Cleanup the TEXT_TO_INSET_OFFSET mess. This correction is done now once in InsetText...
[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 "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         if (hasFixedWidth())
181                 mi.base.textwidth =     std::max(dim.width(),
182                         params_.width.inPixels(m.base.textwidth));
183         InsetCollapsable::metrics(mi, dim);
184         bool const changed = dim_ != dim;
185         dim_ = dim;
186         return changed;
187 }
188
189
190 bool InsetBox::forceDefaultParagraphs(idx_type) const
191 {
192         return !params_.inner_box;
193 }
194
195
196 bool InsetBox::showInsetDialog(BufferView * bv) const
197 {
198         InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
199         return true;
200 }
201
202
203 void InsetBox::doDispatch(Cursor & cur, FuncRequest & cmd)
204 {
205         switch (cmd.action) {
206
207         case LFUN_INSET_MODIFY: {
208                 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
209                 InsetBoxMailer::string2params(to_utf8(cmd.argument()), 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                            << ']';
342                 } else {
343                         // Special heights
344                         // FIXME UNICODE
345                         os << "[" << params_.height.value()
346                            << '\\' << from_utf8(params_.height_special)
347                            << ']';
348                 }
349                 if (params_.inner_pos != params_.pos)
350                         os << "[" << params_.inner_pos << "]";
351
352                 // FIXME UNICODE
353                 os << '{' << from_ascii(width_string) << '}';
354
355                 if (params_.use_parbox)
356                         os << "{";
357                 os << "%\n";
358                 i += 1;
359         }
360
361         i += InsetText::latex(buf, os, runparams);
362
363         if (params_.inner_box) {
364                 if (params_.use_parbox)
365                         os << "%\n}";
366                 else
367                         os << "%\n\\end{minipage}";
368         }
369
370         switch (btype) {
371         case Frameless:
372                 break;
373         case Boxed:
374                 if (!params_.inner_box)
375                         os << "}"; // for makebox
376                 os << "}";
377                 break;
378         case ovalbox:
379         case Ovalbox:
380         case Doublebox:
381         case Shadowbox:
382                 os << "}";
383                 break;
384         }
385         os << "%\n";
386
387         i += 3;
388
389         return i;
390 }
391
392
393 int InsetBox::plaintext(Buffer const & buf, odocstream & os,
394                         OutputParams const & runparams) const
395 {
396         BoxType const btype = boxtranslator().find(params_.type);
397
398         switch (btype) {
399                 case Frameless: break;
400                 case Boxed:     os << "[\n";  break;
401                 case ovalbox:   os << "(\n";  break;
402                 case Ovalbox:   os << "((\n"; break;
403                 case Shadowbox: os << "[/\n"; break;
404                 case Doublebox: os << "[[\n"; break;
405         }
406
407         InsetText::plaintext(buf, os, runparams);
408
409         int len = 0;
410         switch (btype) {
411                 case Frameless: os << "\n";            break;
412                 case Boxed:     os << "\n]";  len = 1; break;
413                 case ovalbox:   os << "\n)";  len = 1; break;
414                 case Ovalbox:   os << "\n))"; len = 2; break;
415                 case Shadowbox: os << "\n/]"; len = 2; break;
416                 case Doublebox: os << "\n]]"; len = 2; break;
417         }
418
419         return PLAINTEXT_NEWLINE + len; // len chars on a separate line
420 }
421
422
423 int InsetBox::docbook(Buffer const & buf, odocstream & os,
424                       OutputParams const & runparams) const
425 {
426         return InsetText::docbook(buf, os, runparams);
427 }
428
429
430 void InsetBox::validate(LaTeXFeatures & features) const
431 {
432         BoxType btype = boxtranslator().find(params_.type);
433         switch (btype) {
434         case Frameless:
435                 break;
436         case Boxed:
437                 features.require("calc");
438                 break;
439         case ovalbox:
440         case Ovalbox:
441         case Shadowbox:
442         case Doublebox:
443                 features.require("calc");
444                 features.require("fancybox");
445                 break;
446         }
447         InsetText::validate(features);
448 }
449
450
451 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
452         : inset_(inset)
453 {}
454
455
456 string const InsetBoxMailer::name_ = "box";
457
458
459 string const InsetBoxMailer::inset2string(Buffer const &) const
460 {
461         return params2string(inset_.params());
462 }
463
464
465 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
466 {
467         ostringstream data;
468         data << "box" << ' ';
469         params.write(data);
470         return data.str();
471 }
472
473
474 void InsetBoxMailer::string2params(string const & in,
475                                    InsetBoxParams & params)
476 {
477         params = InsetBoxParams(string());
478         if (in.empty())
479                 return;
480
481         istringstream data(in);
482         Lexer lex(0,0);
483         lex.setStream(data);
484
485         string name;
486         lex >> name;
487         if (!lex || name != name_)
488                 return print_mailer_error("InsetBoxMailer", in, 1, name_);
489
490         // This is part of the inset proper that is usually swallowed
491         // by Text::readInset
492         string id;
493         lex >> id;
494         if (!lex || id != "Box")
495                 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
496
497         params.read(lex);
498 }
499
500
501 InsetBoxParams::InsetBoxParams(string const & label)
502         : type(label),
503           use_parbox(false),
504           inner_box(true),
505           width(Length("100col%")),
506           special("none"),
507           pos('t'),
508           hor_pos('c'),
509           inner_pos('t'),
510           height(Length("1in")),
511           height_special("totalheight") // default is 1\\totalheight
512 {}
513
514
515 void InsetBoxParams::write(ostream & os) const
516 {
517         os << "Box " << type << "\n";
518         os << "position \"" << pos << "\"\n";
519         os << "hor_pos \"" << hor_pos << "\"\n";
520         os << "has_inner_box " << inner_box << "\n";
521         os << "inner_pos \"" << inner_pos << "\"\n";
522         os << "use_parbox " << use_parbox << "\n";
523         os << "width \"" << width.asString() << "\"\n";
524         os << "special \"" << special << "\"\n";
525         os << "height \"" << height.asString() << "\"\n";
526         os << "height_special \"" << height_special << "\"\n";
527 }
528
529
530 void InsetBoxParams::read(Lexer & lex)
531 {
532         if (!lex.isOK())
533                 return;
534
535         lex.next();
536         type = lex.getString();
537
538         if (!lex)
539                 return;
540
541         lex.next();
542         string token;
543         token = lex.getString();
544         if (!lex)
545                 return;
546         if (token == "position") {
547                 lex.next();
548                 // The [0] is needed. We need the first and only char in
549                 // this string -- MV
550                 pos = lex.getString()[0];
551         } else {
552                 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
553                 lex.pushToken(token);
554         }
555
556         lex.next();
557         token = lex.getString();
558         if (!lex)
559                 return;
560         if (token == "hor_pos") {
561                 lex.next();
562                 hor_pos = lex.getString()[0];
563         } else {
564                 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
565                 lex.pushToken(token);
566         }
567
568         lex.next();
569         token = lex.getString();
570         if (!lex)
571                 return;
572         if (token == "has_inner_box") {
573                 lex.next();
574                 inner_box = lex.getInteger();
575         } else {
576                 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
577                 lex.pushToken(token);
578         }
579
580         lex.next();
581         token = lex.getString();
582         if (!lex)
583                 return;
584         if (token == "inner_pos") {
585                 lex.next();
586                 inner_pos = lex.getString()[0];
587         } else {
588                 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
589                         << token << endl;
590                 lex.pushToken(token);
591         }
592
593         lex.next();
594         token = lex.getString();
595         if (!lex)
596                 return;
597         if (token == "use_parbox") {
598                 lex.next();
599                 use_parbox = lex.getInteger();
600         } else {
601                 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
602                 lex.pushToken(token);
603         }
604
605         lex.next();
606         token = lex.getString();
607         if (!lex)
608                 return;
609         if (token == "width") {
610                 lex.next();
611                 width = Length(lex.getString());
612         } else {
613                 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
614                 lex.pushToken(token);
615         }
616
617         lex.next();
618         token = lex.getString();
619         if (!lex)
620                 return;
621         if (token == "special") {
622                 lex.next();
623                 special = lex.getString();
624         } else {
625                 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
626                 lex.pushToken(token);
627         }
628
629         lex.next();
630         token = lex.getString();
631         if (!lex)
632                 return;
633         if (token == "height") {
634                 lex.next();
635                 height = Length(lex.getString());
636         } else {
637                 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
638                 lex.pushToken(token);
639         }
640
641         lex.next();
642         token = lex.getString();
643         if (!lex)
644                 return;
645         if (token == "height_special") {
646                 lex.next();
647                 height_special = lex.getString();
648         } else {
649                 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
650                 lex.pushToken(token);
651         }
652 }
653
654
655 } // namespace lyx