]> git.lyx.org Git - lyx.git/blob - src/insets/insetbox.C
more cleanup:
[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
34 namespace lyx {
35
36 using std::auto_ptr;
37 using std::string;
38 using std::istringstream;
39 using std::ostream;
40 using std::ostringstream;
41 using std::endl;
42
43
44 namespace {
45
46 typedef Translator<std::string, InsetBox::BoxType> BoxTranslator;
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 BoxTranslator const init_boxtranslator_loc()
61 {
62         // FIXME UNICODE
63         BoxTranslator translator(to_utf8(_("Boxed")), InsetBox::Boxed);
64         translator.addPair(to_utf8(_("Frameless")), InsetBox::Frameless);
65         translator.addPair(to_utf8(_("ovalbox")), InsetBox::ovalbox);
66         translator.addPair(to_utf8(_("Ovalbox")), InsetBox::Ovalbox);
67         translator.addPair(to_utf8(_("Shadowbox")), InsetBox::Shadowbox);
68         translator.addPair(to_utf8(_("Doublebox")), InsetBox::Doublebox);
69         return translator;
70 }
71
72
73 BoxTranslator const & boxtranslator()
74 {
75         static BoxTranslator translator = init_boxtranslator();
76         return translator;
77 }
78
79
80 BoxTranslator const & boxtranslator_loc()
81 {
82         static BoxTranslator translator = init_boxtranslator_loc();
83         return translator;
84 }
85
86 } // anon
87
88
89 void InsetBox::init()
90 {
91         setInsetName(from_ascii("Box"));
92         setButtonLabel();
93 }
94
95
96 InsetBox::InsetBox(BufferParams const & bp, string const & label)
97         : InsetCollapsable(bp), params_(label)
98 {
99         init();
100 }
101
102
103 InsetBox::InsetBox(InsetBox const & in)
104         : InsetCollapsable(in), params_(in.params_)
105 {
106         init();
107 }
108
109
110 InsetBox::~InsetBox()
111 {
112         InsetBoxMailer(*this).hideDialog();
113 }
114
115
116 auto_ptr<InsetBase> InsetBox::doClone() const
117 {
118         return auto_ptr<InsetBase>(new InsetBox(*this));
119 }
120
121
122 docstring const InsetBox::editMessage() const
123 {
124         return _("Opened Box Inset");
125 }
126
127
128 void InsetBox::write(Buffer const & buf, ostream & os) const
129 {
130         params_.write(os);
131         InsetCollapsable::write(buf, os);
132 }
133
134
135 void InsetBox::read(Buffer const & buf, LyXLex & lex)
136 {
137         params_.read(lex);
138         InsetCollapsable::read(buf, lex);
139         setButtonLabel();
140 }
141
142
143 void InsetBox::setButtonLabel()
144 {
145         LyXFont font(LyXFont::ALL_SANE);
146         font.decSize();
147         font.decSize();
148
149         BoxType btype = boxtranslator().find(params_.type);
150
151         docstring label;
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                 // FIXME UNICODE
161                 label += 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 bool 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         bool const changed = dim_ != dim;
179         dim_ = dim;
180         return changed;
181 }
182
183
184 bool InsetBox::forceDefaultParagraphs(idx_type) const
185 {
186         return !params_.inner_box;
187 }
188
189
190 bool InsetBox::showInsetDialog(BufferView * bv) const
191 {
192         InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
193         return true;
194 }
195
196
197 void InsetBox::doDispatch(LCursor & cur, FuncRequest & cmd)
198 {
199         switch (cmd.action) {
200
201         case LFUN_INSET_MODIFY: {
202                 //lyxerr << "InsetBox::dispatch MODIFY" << endl;
203                 InsetBoxMailer::string2params(to_utf8(cmd.argument()), params_);
204                 setButtonLabel();
205                 break;
206         }
207
208         case LFUN_INSET_DIALOG_UPDATE:
209                 InsetBoxMailer(*this).updateDialog(&cur.bv());
210                 break;
211
212         case LFUN_MOUSE_RELEASE:
213                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
214                         InsetBoxMailer(*this).showDialog(&cur.bv());
215                         break;
216                 }
217                 InsetCollapsable::doDispatch(cur, cmd);
218                 break;
219
220         default:
221                 InsetCollapsable::doDispatch(cur, cmd);
222                 break;
223         }
224 }
225
226
227 bool InsetBox::getStatus(LCursor & cur, FuncRequest const & cmd,
228                 FuncStatus & flag) const
229 {
230         switch (cmd.action) {
231
232         case LFUN_INSET_MODIFY:
233         case LFUN_INSET_DIALOG_UPDATE:
234                 flag.enabled(true);
235                 return true;
236         case LFUN_BREAK_PARAGRAPH:
237                 if (params_.inner_box) {
238                         return InsetCollapsable::getStatus(cur, cmd, flag);
239                 } else {
240                         flag.enabled(false);
241                         return true;
242                 }
243
244         default:
245                 return InsetCollapsable::getStatus(cur, cmd, flag);
246         }
247 }
248
249
250 int InsetBox::latex(Buffer const & buf, odocstream & os,
251                                 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                         break;
264                 case Boxed:
265                         width_string += " - 2\\fboxsep - 2\\fboxrule";
266                         break;
267                 case ovalbox:
268                         width_string += " - 2\\fboxsep - 0.8pt";
269                         break;
270                 case Ovalbox:
271                         width_string += " - 2\\fboxsep - 1.6pt";
272                         break;
273                 case Shadowbox:
274                         // Shadow falls outside right margin... opinions?
275                         width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
276                         break;
277                 case Doublebox:
278                         width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
279                         break;
280                 }
281         }
282
283         int i = 0;
284         os << "%\n";
285         // Adapt to column/text width correctly also if paragraphs indented:
286         if (stdwidth)
287                 os << "\\noindent";
288
289         switch (btype) {
290         case Frameless:
291                 break;
292         case Boxed:
293                 os << "\\framebox";
294                 if (!params_.inner_box) {
295                         os << "{\\makebox";
296                         // Special widths, see usrguide §3.5
297                         // FIXME UNICODE
298                         if (params_.special != "none") {
299                                 os << "[" << params_.width.value()
300                                    << '\\' << from_utf8(params_.special)
301                                    << ']';
302                         } else
303                                 os << '[' << from_ascii(width_string)
304                                    << ']';
305                         if (params_.hor_pos != 'c')
306                                 os << "[" << params_.hor_pos << "]";
307                 }
308
309                 os << "{";
310                 break;
311         case ovalbox:
312                 os << "\\ovalbox{";
313                 break;
314         case Ovalbox:
315                 os << "\\Ovalbox{";
316                 break;
317         case Shadowbox:
318                 os << "\\shadowbox{";
319                 break;
320         case Doublebox:
321                 os << "\\doublebox{";
322                 break;
323         }
324
325         if (params_.inner_box) {
326                 if (params_.use_parbox)
327                         os << "\\parbox";
328                 else
329                         os << "\\begin{minipage}";
330
331                 os << "[" << params_.pos << "]";
332                 if (params_.height_special == "none") {
333                         // FIXME UNICODE
334                         os << '[' << from_ascii(params_.height.asLatexString())
335                            << ']';
336                 } else {
337                         // Special heights
338                         // FIXME UNICODE
339                         os << "[" << params_.height.value()
340                            << '\\' << from_utf8(params_.height_special)
341                            << ']';
342                 }
343                 if (params_.inner_pos != params_.pos)
344                         os << "[" << params_.inner_pos << "]";
345
346                 // FIXME UNICODE
347                 os << '{' << from_ascii(width_string) << '}';
348
349                 if (params_.use_parbox)
350                         os << "{";
351                 os << "%\n";
352                 i += 1;
353         }
354
355         i += InsetText::latex(buf, os, runparams);
356
357         if (params_.inner_box) {
358                 if (params_.use_parbox)
359                         os << "%\n}";
360                 else
361                         os << "%\n\\end{minipage}";
362         }
363
364         switch (btype) {
365         case Frameless:
366                 break;
367         case Boxed:
368                 if (!params_.inner_box)
369                         os << "}"; // for makebox
370                 os << "}";
371                 break;
372         case ovalbox:
373         case Ovalbox:
374         case Doublebox:
375         case Shadowbox:
376                 os << "}";
377                 break;
378         }
379         os << "%\n";
380
381         i += 3;
382
383         return i;
384 }
385
386
387 int InsetBox::docbook(Buffer const & buf, odocstream & os,
388                       OutputParams const & runparams) const
389 {
390         return InsetText::docbook(buf, os, runparams);
391 }
392
393
394 int InsetBox::plaintext(Buffer const & buf, odocstream & os,
395                     OutputParams const & runparams) const
396 {
397         BoxType const btype = boxtranslator().find(params_.type);
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         int i = InsetText::plaintext(buf, os, runparams);
409
410         switch (btype) {
411                 case Frameless: break;
412                 case Boxed:     os << "]";  break;
413                 case ovalbox:   os << ")";  break;
414                 case Ovalbox:   os << "))"; break;
415                 case Shadowbox: os << "]/"; break;
416                 case Doublebox: os << "]]"; break;
417         }
418
419         return i;
420 }
421
422
423 void InsetBox::validate(LaTeXFeatures & features) const
424 {
425         features.require("calc");
426         BoxType btype = boxtranslator().find(params_.type);
427         switch (btype) {
428         case Frameless:
429         case Boxed:
430                 break;
431         case ovalbox:
432         case Ovalbox:
433         case Shadowbox:
434         case Doublebox:
435                 features.require("fancybox");
436                 break;
437         }
438         InsetText::validate(features);
439 }
440
441
442 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
443         : inset_(inset)
444 {}
445
446
447 string const InsetBoxMailer::name_ = "box";
448
449
450 string const InsetBoxMailer::inset2string(Buffer const &) const
451 {
452         return params2string(inset_.params());
453 }
454
455
456 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
457 {
458         ostringstream data;
459         data << "box" << ' ';
460         params.write(data);
461         return data.str();
462 }
463
464
465 void InsetBoxMailer::string2params(string const & in,
466                                    InsetBoxParams & params)
467 {
468         params = InsetBoxParams(string());
469         if (in.empty())
470                 return;
471
472         istringstream data(in);
473         LyXLex lex(0,0);
474         lex.setStream(data);
475
476         string name;
477         lex >> name;
478         if (!lex || name != name_)
479                 return print_mailer_error("InsetBoxMailer", in, 1, name_);
480
481         // This is part of the inset proper that is usually swallowed
482         // by LyXText::readInset
483         string id;
484         lex >> id;
485         if (!lex || id != "Box")
486                 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
487
488         params.read(lex);
489 }
490
491
492 InsetBoxParams::InsetBoxParams(string const & label)
493         : type(label),
494           use_parbox(false),
495           inner_box(true),
496           width(LyXLength("100col%")),
497           special("none"),
498           pos('t'),
499           hor_pos('c'),
500           inner_pos('t'),
501           height(LyXLength("1in")),
502           height_special("totalheight") // default is 1\\totalheight
503 {}
504
505
506 void InsetBoxParams::write(ostream & os) const
507 {
508         os << "Box " << type << "\n";
509         os << "position \"" << pos << "\"\n";
510         os << "hor_pos \"" << hor_pos << "\"\n";
511         os << "has_inner_box " << inner_box << "\n";
512         os << "inner_pos \"" << inner_pos << "\"\n";
513         os << "use_parbox " << use_parbox << "\n";
514         os << "width \"" << width.asString() << "\"\n";
515         os << "special \"" << special << "\"\n";
516         os << "height \"" << height.asString() << "\"\n";
517         os << "height_special \"" << height_special << "\"\n";
518 }
519
520
521 void InsetBoxParams::read(LyXLex & lex)
522 {
523         if (!lex.isOK())
524                 return;
525
526         if (lex.isOK()) {
527                 lex.next();
528                 type = lex.getString();
529         }
530         if (!lex.isOK())
531                 return;
532         lex.next();
533         string token;
534         token = lex.getString();
535         if (token == "position") {
536                 lex.next();
537                 // The [0] is needed. We need the first and only char in
538                 // this string -- MV
539                 pos = lex.getString()[0];
540         } else {
541                 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
542                 lex.pushToken(token);
543         }
544         if (!lex.isOK())
545                 return;
546         lex.next();
547         token = lex.getString();
548         if (token == "hor_pos") {
549                 lex.next();
550                 hor_pos = lex.getString()[0];
551         } else {
552                 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
553                 lex.pushToken(token);
554         }
555         if (!lex.isOK())
556                 return;
557         lex.next();
558         token = lex.getString();
559         if (token == "has_inner_box") {
560                 lex.next();
561                 inner_box = lex.getInteger();
562         } else {
563                 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
564                 lex.pushToken(token);
565         }
566
567         if (!lex.isOK())
568                 return;
569         lex.next();
570         token = lex.getString();
571         if (token == "inner_pos") {
572                 lex.next();
573                 inner_pos = lex.getString()[0];
574         } else {
575                 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
576                         << token << endl;
577                 lex.pushToken(token);
578         }
579         if (!lex.isOK())
580                 return;
581         lex.next();
582         token = lex.getString();
583         if (token == "use_parbox") {
584                 lex.next();
585                 use_parbox = lex.getInteger();
586         } else {
587                 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
588                 lex.pushToken(token);
589         }
590         if (!lex.isOK())
591                 return;
592         lex.next();
593         token = lex.getString();
594         if (token == "width") {
595                 lex.next();
596                 width = LyXLength(lex.getString());
597         } else {
598                 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
599                 lex.pushToken(token);
600         }
601         if (!lex.isOK())
602                 return;
603         lex.next();
604         token = lex.getString();
605         if (token == "special") {
606                 lex.next();
607                 special = lex.getString();
608         } else {
609                 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
610                 lex.pushToken(token);
611         }
612         if (!lex.isOK())
613                 return;
614         lex.next();
615         token = lex.getString();
616         if (token == "height") {
617                 lex.next();
618                 height = LyXLength(lex.getString());
619         } else {
620                 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
621                 lex.pushToken(token);
622         }
623         if (!lex.isOK())
624                 return;
625         lex.next();
626         token = lex.getString();
627         if (token == "height_special") {
628                 lex.next();
629                 height_special = lex.getString();
630         } else {
631                 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
632                 lex.pushToken(token);
633         }
634 }
635
636
637 } // namespace lyx