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