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