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