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