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