]> git.lyx.org Git - lyx.git/blob - src/insets/insetbox.C
use new docstring += operator
[lyx.git] / src / insets / insetbox.C
1 /**
2  * \file insetbox.C
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 "cursor.h"
18 #include "dispatchresult.h"
19 #include "debug.h"
20 #include "FuncStatus.h"
21 #include "funcrequest.h"
22 #include "gettext.h"
23 #include "LaTeXFeatures.h"
24 #include "LColor.h"
25 #include "lyxlex.h"
26 #include "metricsinfo.h"
27 #include "paragraph.h"
28
29 #include "support/translator.h"
30
31 #include <sstream>
32
33 using lyx::docstring;
34
35 using std::auto_ptr;
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
47 BoxTranslator const init_boxtranslator()
48 {
49         BoxTranslator translator("Boxed", InsetBox::Boxed);
50         translator.addPair("Frameless", InsetBox::Frameless);
51         translator.addPair("ovalbox", InsetBox::ovalbox);
52         translator.addPair("Ovalbox", InsetBox::Ovalbox);
53         translator.addPair("Shadowbox", InsetBox::Shadowbox);
54         translator.addPair("Doublebox",InsetBox::Doublebox);
55         return translator;
56 }
57
58
59 BoxTranslator const init_boxtranslator_loc()
60 {
61         // FIXME UNICODE
62         BoxTranslator translator(lyx::to_utf8(_("Boxed")), InsetBox::Boxed);
63         translator.addPair(lyx::to_utf8(_("Frameless")), InsetBox::Frameless);
64         translator.addPair(lyx::to_utf8(_("ovalbox")), InsetBox::ovalbox);
65         translator.addPair(lyx::to_utf8(_("Ovalbox")), InsetBox::Ovalbox);
66         translator.addPair(lyx::to_utf8(_("Shadowbox")), InsetBox::Shadowbox);
67         translator.addPair(lyx::to_utf8(_("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 BoxTranslator const & boxtranslator_loc()
80 {
81         static BoxTranslator translator = init_boxtranslator_loc();
82         return translator;
83 }
84
85 } // anon
86
87
88 void InsetBox::init()
89 {
90         setInsetName("Box");
91         setButtonLabel();
92 }
93
94
95 InsetBox::InsetBox(BufferParams const & bp, string const & label)
96         : InsetCollapsable(bp), params_(label)
97 {
98         init();
99 }
100
101
102 InsetBox::InsetBox(InsetBox const & in)
103         : InsetCollapsable(in), params_(in.params_)
104 {
105         init();
106 }
107
108
109 InsetBox::~InsetBox()
110 {
111         InsetBoxMailer(*this).hideDialog();
112 }
113
114
115 auto_ptr<InsetBase> InsetBox::doClone() const
116 {
117         return auto_ptr<InsetBase>(new InsetBox(*this));
118 }
119
120
121 docstring const InsetBox::editMessage() const
122 {
123         return _("Opened Box Inset");
124 }
125
126
127 void InsetBox::write(Buffer const & buf, ostream & os) const
128 {
129         params_.write(os);
130         InsetCollapsable::write(buf, os);
131 }
132
133
134 void InsetBox::read(Buffer const & buf, LyXLex & lex)
135 {
136         params_.read(lex);
137         InsetCollapsable::read(buf, lex);
138         setButtonLabel();
139 }
140
141
142 void InsetBox::setButtonLabel()
143 {
144         LyXFont font(LyXFont::ALL_SANE);
145         font.decSize();
146         font.decSize();
147
148         BoxType btype = boxtranslator().find(params_.type);
149
150         docstring label;
151         // FIXME UNICODE
152         label += _("Box");
153         label += " (";
154         if (btype == Frameless) {
155                 if (params_.use_parbox)
156                         label += _("Parbox");
157                 else
158                         label += _("Minipage");
159         } else
160                 // FXIME unicode
161                 label += lyx::from_utf8(boxtranslator_loc().find(btype));
162         label += ")";
163
164         setLabel(label);
165
166         font.setColor(LColor::foreground);
167         setBackgroundColor(LColor::background);
168         setLabelFont(font);
169 }
170
171
172 void InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
173 {
174         MetricsInfo mi = m;
175         if (params_.inner_box || params_.special != "width")
176                 mi.base.textwidth = params_.width.inPixels(m.base.textwidth);
177         InsetCollapsable::metrics(mi, dim);
178         dim_ = dim;
179 }
180
181
182 bool InsetBox::forceDefaultParagraphs(idx_type) const
183 {
184         return !params_.inner_box;
185 }
186
187
188 bool InsetBox::showInsetDialog(BufferView * bv) const
189 {
190         InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
191         return true;
192 }
193
194
195 void InsetBox::doDispatch(LCursor & cur, FuncRequest & cmd)
196 {
197         switch (cmd.action) {
198
199         case LFUN_INSET_MODIFY: {
200                 lyxerr << "InsetBox::dispatch MODIFY" << endl;
201                 InsetBoxMailer::string2params(lyx::to_utf8(cmd.argument()), 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(LCursor & 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, ostream & 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                         if (params_.special != "none") {
296                                 os << "[" << params_.width.value()
297                                    << "\\" << params_.special << "]";
298                         } else
299                                 os << "[" << width_string << "]";
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                         os << "[" << params_.height.asLatexString() << "]";
329                 } else {
330                         // Special heights
331                         os << "[" << params_.height.value()
332                            << "\\" << params_.height_special << "]";
333                 }
334                 if (params_.inner_pos != params_.pos)
335                         os << "[" << params_.inner_pos << "]";
336
337                 os << "{" << width_string << "}";
338
339                 if (params_.use_parbox)
340                         os << "{";
341                 os << "%\n";
342                 i += 1;
343         }
344
345         i += InsetText::latex(buf, os, runparams);
346
347         if (params_.inner_box) {
348                 if (params_.use_parbox)
349                         os << "%\n}";
350                 else
351                         os << "%\n\\end{minipage}";
352         }
353
354         switch (btype) {
355         case Frameless:
356                 break;
357         case Boxed:
358                 if (!params_.inner_box)
359                         os << "}"; // for makebox
360                 os << "}";
361                 break;
362         case ovalbox:
363         case Ovalbox:
364         case Doublebox:
365         case Shadowbox:
366                 os << "}";
367                 break;
368         }
369         os << "%\n";
370
371         i += 3;
372
373         return i;
374 }
375
376
377 int InsetBox::docbook(Buffer const & buf, std::ostream & os,
378                       OutputParams const & runparams) const
379 {
380         return InsetText::docbook(buf, os, runparams);
381 }
382
383
384 int InsetBox::plaintext(Buffer const & buf, std::ostream & os,
385                     OutputParams const & runparams) const
386 {
387         BoxType const btype = boxtranslator().find(params_.type);
388
389         switch (btype) {
390                 case Frameless: break;
391                 case Boxed:     os << "[";  break;
392                 case ovalbox:   os << "(";  break;
393                 case Ovalbox:   os << "(("; break;
394                 case Shadowbox: os << "[";  break;
395                 case Doublebox: os << "[["; break;
396         }
397
398         int i = InsetText::plaintext(buf, os, runparams);
399
400         switch (btype) {
401                 case Frameless: break;
402                 case Boxed:     os << "]";  break;
403                 case ovalbox:   os << ")";  break;
404                 case Ovalbox:   os << "))"; break;
405                 case Shadowbox: os << "]/"; break;
406                 case Doublebox: os << "]]"; break;
407         }
408
409         return i;
410 }
411
412
413 void InsetBox::validate(LaTeXFeatures & features) const
414 {
415         features.require("calc");
416         BoxType btype = boxtranslator().find(params_.type);
417         switch (btype) {
418         case Frameless:
419         case Boxed:
420                 break;
421         case ovalbox:
422         case Ovalbox:
423         case Shadowbox:
424         case Doublebox:
425                 features.require("fancybox");
426                 break;
427         }
428         InsetText::validate(features);
429 }
430
431
432 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
433         : inset_(inset)
434 {}
435
436
437 string const InsetBoxMailer::name_ = "box";
438
439
440 string const InsetBoxMailer::inset2string(Buffer const &) const
441 {
442         return params2string(inset_.params());
443 }
444
445
446 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
447 {
448         ostringstream data;
449         data << "box" << ' ';
450         params.write(data);
451         return data.str();
452 }
453
454
455 void InsetBoxMailer::string2params(string const & in,
456                                    InsetBoxParams & params)
457 {
458         params = InsetBoxParams(string());
459         if (in.empty())
460                 return;
461
462         istringstream data(in);
463         LyXLex lex(0,0);
464         lex.setStream(data);
465
466         string name;
467         lex >> name;
468         if (!lex || name != name_)
469                 return print_mailer_error("InsetBoxMailer", in, 1, name_);
470
471         // This is part of the inset proper that is usually swallowed
472         // by LyXText::readInset
473         string id;
474         lex >> id;
475         if (!lex || id != "Box")
476                 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
477
478         params.read(lex);
479 }
480
481
482 InsetBoxParams::InsetBoxParams(string const & label)
483         : type(label),
484           use_parbox(false),
485           inner_box(true),
486           width(LyXLength("100col%")),
487           special("none"),
488           pos('t'),
489           hor_pos('c'),
490           inner_pos('t'),
491           height(LyXLength("1in")),
492           height_special("totalheight") // default is 1\\totalheight
493 {}
494
495
496 void InsetBoxParams::write(ostream & os) const
497 {
498         os << "Box " << type << "\n";
499         os << "position \"" << pos << "\"\n";
500         os << "hor_pos \"" << hor_pos << "\"\n";
501         os << "has_inner_box " << inner_box << "\n";
502         os << "inner_pos \"" << inner_pos << "\"\n";
503         os << "use_parbox " << use_parbox << "\n";
504         os << "width \"" << width.asString() << "\"\n";
505         os << "special \"" << special << "\"\n";
506         os << "height \"" << height.asString() << "\"\n";
507         os << "height_special \"" << height_special << "\"\n";
508 }
509
510
511 void InsetBoxParams::read(LyXLex & lex)
512 {
513         if (!lex.isOK())
514                 return;
515
516         if (lex.isOK()) {
517                 lex.next();
518                 type = lex.getString();
519         }
520         if (!lex.isOK())
521                 return;
522         lex.next();
523         string token;
524         token = lex.getString();
525         if (token == "position") {
526                 lex.next();
527                 // The [0] is needed. We need the first and only char in
528                 // this string -- MV
529                 pos = lex.getString()[0];
530         } else {
531                 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
532                 lex.pushToken(token);
533         }
534         if (!lex.isOK())
535                 return;
536         lex.next();
537         token = lex.getString();
538         if (token == "hor_pos") {
539                 lex.next();
540                 hor_pos = lex.getString()[0];
541         } else {
542                 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
543                 lex.pushToken(token);
544         }
545         if (!lex.isOK())
546                 return;
547         lex.next();
548         token = lex.getString();
549         if (token == "has_inner_box") {
550                 lex.next();
551                 inner_box = lex.getInteger();
552         } else {
553                 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
554                 lex.pushToken(token);
555         }
556
557         if (!lex.isOK())
558                 return;
559         lex.next();
560         token = lex.getString();
561         if (token == "inner_pos") {
562                 lex.next();
563                 inner_pos = lex.getString()[0];
564         } else {
565                 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
566                         << token << endl;
567                 lex.pushToken(token);
568         }
569         if (!lex.isOK())
570                 return;
571         lex.next();
572         token = lex.getString();
573         if (token == "use_parbox") {
574                 lex.next();
575                 use_parbox = lex.getInteger();
576         } else {
577                 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
578                 lex.pushToken(token);
579         }
580         if (!lex.isOK())
581                 return;
582         lex.next();
583         token = lex.getString();
584         if (token == "width") {
585                 lex.next();
586                 width = LyXLength(lex.getString());
587         } else {
588                 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
589                 lex.pushToken(token);
590         }
591         if (!lex.isOK())
592                 return;
593         lex.next();
594         token = lex.getString();
595         if (token == "special") {
596                 lex.next();
597                 special = lex.getString();
598         } else {
599                 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
600                 lex.pushToken(token);
601         }
602         if (!lex.isOK())
603                 return;
604         lex.next();
605         token = lex.getString();
606         if (token == "height") {
607                 lex.next();
608                 height = LyXLength(lex.getString());
609         } else {
610                 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
611                 lex.pushToken(token);
612         }
613         if (!lex.isOK())
614                 return;
615         lex.next();
616         token = lex.getString();
617         if (token == "height_special") {
618                 lex.next();
619                 height_special = lex.getString();
620         } else {
621                 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
622                 lex.pushToken(token);
623         }
624 }