]> git.lyx.org Git - lyx.git/blob - src/insets/InsetBox.cpp
Remove all BufferParam arguments in InsetXXX methods (since insets know about their...
[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::editMessage() const
111 {
112         return _("Opened Box Inset");
113 }
114
115
116 docstring InsetBox::name() const 
117 {
118         // FIXME: UNICODE
119         string name = "Box";
120         if (boxtranslator().find(params_.type) == Shaded)
121                 name += ":Shaded";
122         return from_ascii(name);
123 }
124
125
126 void InsetBox::write(ostream & os) const
127 {
128         params_.write(os);
129         InsetCollapsable::write(os);
130 }
131
132
133 void InsetBox::read(Lexer & lex)
134 {
135         params_.read(lex);
136         InsetCollapsable::read(lex);
137 }
138
139
140 void InsetBox::setButtonLabel()
141 {
142         BoxType const btype = boxtranslator().find(params_.type);
143
144         docstring const type = _("Box");
145
146         docstring inner;
147         if (params_.inner_box) {
148                 if (params_.use_parbox)
149                         inner = _("Parbox");
150                 else
151                         inner = _("Minipage");
152         }
153
154         docstring frame;
155         if (btype != Frameless)
156                 frame = boxtranslator_loc().find(btype);
157
158         docstring label;
159         if (inner.empty() && frame.empty())
160                 label = type;
161         else if (inner.empty())
162                 label = bformat(_("%1$s (%2$s)"),
163                         type, frame);
164         else if (frame.empty())
165                 label = bformat(_("%1$s (%2$s)"),
166                         type, inner);
167         else
168                 label = bformat(_("%1$s (%2$s, %3$s)"),
169                         type, inner, frame);
170         setLabel(label);
171 }
172
173
174 bool InsetBox::hasFixedWidth() const
175 {
176         return params_.inner_box || params_.special != "width";
177 }
178
179
180 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
181 {
182         // back up textwidth.
183         int textwidth_backup = m.base.textwidth;
184         if (hasFixedWidth())
185                 m.base.textwidth = params_.width.inPixels(m.base.textwidth);
186         InsetCollapsable::metrics(m, dim);
187         // retore textwidth.
188         m.base.textwidth = textwidth_backup;
189 }
190
191
192 bool InsetBox::forcePlainLayout(idx_type) const
193 {
194         return !params_.inner_box && params_.type != "Framed";
195 }
196
197
198 bool InsetBox::showInsetDialog(BufferView * bv) const
199 {
200         bv->showDialog("box", params2string(params_),
201                 const_cast<InsetBox *>(this));
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                 if (cmd.getArg(0) == "changetype")
213                         params_.type = cmd.getArg(1);
214                 else
215                         string2params(to_utf8(cmd.argument()), params_);
216                 setLayout();
217                 break;
218         }
219
220         case LFUN_INSET_DIALOG_UPDATE:
221                 cur.bv().updateDialog("box", params2string(params_));
222                 break;
223
224         default:
225                 InsetCollapsable::doDispatch(cur, cmd);
226                 break;
227         }
228 }
229
230
231 bool InsetBox::getStatus(Cursor & cur, FuncRequest const & cmd,
232                 FuncStatus & flag) const
233 {
234         switch (cmd.action) {
235
236         case LFUN_INSET_MODIFY:
237                 if (cmd.getArg(0) == "changetype")
238                         flag.setOnOff(cmd.getArg(1) == params_.type);
239                 flag.setEnabled(true);
240                 return true;
241
242         case LFUN_INSET_DIALOG_UPDATE:
243                 flag.setEnabled(true);
244                 return true;
245
246         case LFUN_BREAK_PARAGRAPH:
247                 if (params_.inner_box || params_.type == "Framed")
248                         return InsetCollapsable::getStatus(cur, cmd, flag);
249                 flag.setEnabled(false);
250                 return true;
251
252         default:
253                 return InsetCollapsable::getStatus(cur, cmd, flag);
254         }
255 }
256
257
258 int InsetBox::latex(odocstream & os, OutputParams const & runparams) const
259 {
260         BoxType btype = boxtranslator().find(params_.type);
261
262         string width_string = params_.width.asLatexString();
263         bool stdwidth = false;
264         if (params_.inner_box &&
265                         (width_string.find("1.0\\columnwidth") != string::npos
266                         || width_string.find("1.0\\textwidth") != string::npos)) {
267                 stdwidth = true;
268                 switch (btype) {
269                 case Frameless:
270                 case Framed:
271                         break;
272                 case Boxed:
273                 case Shaded:
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 - 1pt";
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 Framed:
302                 os << "\\begin{framed}%\n";
303                 i += 1;
304                 break;
305         case Boxed:
306                 os << "\\framebox";
307                 if (!params_.inner_box) {
308                         os << "{\\makebox";
309                         // Special widths, see usrguide §3.5
310                         // FIXME UNICODE
311                         if (params_.special != "none") {
312                                 os << "[" << params_.width.value()
313                                    << '\\' << from_utf8(params_.special)
314                                    << ']';
315                         } else
316                                 os << '[' << from_ascii(width_string)
317                                    << ']';
318                         if (params_.hor_pos != 'c')
319                                 os << "[" << params_.hor_pos << "]";
320                 }
321
322                 os << "{";
323                 break;
324         case ovalbox:
325                 os << "\\ovalbox{";
326                 break;
327         case Ovalbox:
328                 os << "\\Ovalbox{";
329                 break;
330         case Shadowbox:
331                 os << "\\shadowbox{";
332                 break;
333         case Shaded:
334                 // later
335                 break;
336         case Doublebox:
337                 os << "\\doublebox{";
338                 break;
339         }
340
341         if (params_.inner_box) {
342                 if (params_.use_parbox)
343                         os << "\\parbox";
344                 else
345                         os << "\\begin{minipage}";
346
347                 os << "[" << params_.pos << "]";
348                 if (params_.height_special == "none") {
349                         // FIXME UNICODE
350                         os << "[" << from_ascii(params_.height.asLatexString()) << "]";
351                 } else {
352                         // Special heights
353                         // set no optional argument when the value is the default "1\height"
354                         // (special units like \height are handled as "in")
355                         // but when the user has chosen a non-default inner_pos, the height
356                         // must be given: \minipage[pos][height][inner-pos]{width}
357                         if ((params_.height != Length("1in") ||
358                                  params_.height_special != "totalheight") ||
359                                 params_.inner_pos != params_.pos) {
360                                 // FIXME UNICODE
361                                 os << "[" << params_.height.value()
362                                         << "\\" << from_utf8(params_.height_special) << "]";
363                         }
364                 }
365                 if (params_.inner_pos != params_.pos)
366                         os << "[" << params_.inner_pos << "]";
367
368                 // FIXME UNICODE
369                 os << '{' << from_ascii(width_string) << '}';
370
371                 if (params_.use_parbox)
372                         os << "{";
373                 os << "%\n";
374                 ++i;
375         }
376         if (btype == Shaded) {
377                 os << "\\begin{shaded}%\n";
378                 ++i;
379         }
380
381         i += InsetText::latex(os, runparams);
382
383         if (btype == Shaded)
384                 os << "\\end{shaded}";
385
386         if (params_.inner_box) {
387                 if (params_.use_parbox)
388                         os << "%\n}";
389                 else
390                         os << "%\n\\end{minipage}";
391         }
392
393         switch (btype) {
394         case Frameless:
395                 break;
396         case Framed:
397                 os << "\\end{framed}";
398                 break;
399         case Boxed:
400                 if (!params_.inner_box)
401                         os << "}"; // for makebox
402                 os << "}";
403                 break;
404         case ovalbox:
405         case Ovalbox:
406         case Doublebox:
407         case Shadowbox:
408                 os << "}";
409                 break;
410         case Shaded:
411                 // already done
412                 break;
413         }
414
415         i += 2;
416
417         return i;
418 }
419
420
421 int InsetBox::plaintext(odocstream & os, OutputParams const & runparams) const
422 {
423         BoxType const btype = boxtranslator().find(params_.type);
424
425         switch (btype) {
426                 case Frameless:
427                         break;
428                 case Framed:
429                 case Boxed:
430                         os << "[\n";
431                         break;
432                 case ovalbox:
433                         os << "(\n";
434                         break;
435                 case Ovalbox:
436                         os << "((\n";
437                         break;
438                 case Shadowbox:
439                 case Shaded:
440                         os << "[/\n";
441                         break;
442                 case Doublebox:
443                         os << "[[\n";
444                         break;
445         }
446
447         InsetText::plaintext(os, runparams);
448
449         int len = 0;
450         switch (btype) {
451                 case Frameless:
452                         os << "\n";
453                         break;
454                 case Framed:
455                 case Boxed:
456                         os << "\n]";
457                         len = 1;
458                         break;
459                 case ovalbox:
460                         os << "\n)";
461                         len = 1;
462                         break;
463                 case Ovalbox:
464                         os << "\n))";
465                         len = 2;
466                         break;
467                 case Shadowbox:
468                 case Shaded:
469                         os << "\n/]";
470                         len = 2;
471                         break;
472                 case Doublebox:
473                         os << "\n]]";
474                         len = 2;
475                         break;
476         }
477
478         return PLAINTEXT_NEWLINE + len; // len chars on a separate line
479 }
480
481
482 int InsetBox::docbook(odocstream & os, OutputParams const & runparams) const
483 {
484         return InsetText::docbook(os, runparams);
485 }
486
487
488 docstring InsetBox::xhtml(odocstream &, OutputParams const & runparams) const
489 {
490         string style;
491         if (!params_.width.empty())
492                 style += ("width: " + params_.width.asHTMLString() + ";");
493         if (!params_.height.empty())
494                 style += ("height: " + params_.height.asHTMLString() + ";");
495         
496         docstring retval = from_ascii("<div class='" + params_.type + "'");
497         if (!style.empty())
498                 retval += from_ascii(" style='" + style + "'");
499         retval += ">\n";
500         odocstringstream os;
501         docstring defer = InsetText::xhtml(os, runparams);
502         retval += os.str();
503         retval += "</div>\n";
504         retval += defer + "\n";
505         return retval;
506 }
507
508
509 void InsetBox::validate(LaTeXFeatures & features) const
510 {
511         BoxType btype = boxtranslator().find(params_.type);
512         switch (btype) {
513         case Frameless:
514                 break;
515         case Framed:
516                 features.require("framed");
517                 break;
518         case Boxed:
519                 features.require("calc");
520                 break;
521         case ovalbox:
522         case Ovalbox:
523         case Shadowbox:
524         case Doublebox:
525                 features.require("calc");
526                 features.require("fancybox");
527                 break;
528         case Shaded:
529                 features.require("color");
530                 features.require("framed");
531                 break;
532         }
533         InsetCollapsable::validate(features);
534 }
535
536
537 docstring InsetBox::contextMenu(BufferView const &, int, int) const
538 {
539         return from_ascii("context-box");
540 }
541
542
543 string InsetBox::params2string(InsetBoxParams const & params)
544 {
545         ostringstream data;
546         data << "box" << ' ';
547         params.write(data);
548         return data.str();
549 }
550
551
552 void InsetBox::string2params(string const & in, InsetBoxParams & params)
553 {
554         params = InsetBoxParams(string());
555         if (in.empty())
556                 return;
557
558         istringstream data(in);
559         Lexer lex;
560         lex.setStream(data);
561
562         string name;
563         lex >> name;
564         if (!lex || name != "box") {
565                 LYXERR0("InsetBox::string2params(" << in << ")\n"
566                                           "Expected arg 1 to be \"box\"\n");
567                 return;
568         }
569
570         // This is part of the inset proper that is usually swallowed
571         // by Text::readInset
572         string id;
573         lex >> id;
574         if (!lex || id != "Box") {
575                 LYXERR0("InsetBox::string2params(" << in << ")\n"
576                                           "Expected arg 2 to be \"Box\"\n");
577         }
578
579         params.read(lex);
580 }
581
582
583 /////////////////////////////////////////////////////////////////////////
584 //
585 // InsetBoxParams
586 //
587 /////////////////////////////////////////////////////////////////////////
588
589 InsetBoxParams::InsetBoxParams(string const & label)
590         : type(label),
591           use_parbox(false),
592           inner_box(true),
593           width(Length("100col%")),
594           special("none"),
595           pos('t'),
596           hor_pos('c'),
597           inner_pos('t'),
598           height(Length("1in")),
599           height_special("totalheight") // default is 1\\totalheight
600 {}
601
602
603 void InsetBoxParams::write(ostream & os) const
604 {
605         os << "Box " << type << "\n";
606         os << "position \"" << pos << "\"\n";
607         os << "hor_pos \"" << hor_pos << "\"\n";
608         os << "has_inner_box " << inner_box << "\n";
609         os << "inner_pos \"" << inner_pos << "\"\n";
610         os << "use_parbox " << use_parbox << "\n";
611         os << "width \"" << width.asString() << "\"\n";
612         os << "special \"" << special << "\"\n";
613         os << "height \"" << height.asString() << "\"\n";
614         os << "height_special \"" << height_special << "\"\n";
615 }
616
617
618 void InsetBoxParams::read(Lexer & lex)
619 {
620         lex.setContext("InsetBoxParams::read");
621         lex >> type;
622         lex >> "position" >> pos;
623         lex >> "hor_pos" >> hor_pos;
624         lex >> "has_inner_box" >> inner_box;
625         if (type == "Framed")
626                 inner_box = false;
627         lex >> "inner_pos" >> inner_pos;
628         lex >> "use_parbox" >> use_parbox;
629         lex >> "width" >> width;
630         lex >> "special" >> special;
631         lex >> "height" >> height;
632         lex >> "height_special" >> height_special;
633 }
634
635
636 } // namespace lyx