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