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