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