]> git.lyx.org Git - lyx.git/blob - src/insets/insetbox.C
9f867c502fada1762e10eeffd6f5846e78ee9112
[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 #include "debug.h"
17
18 #include "BufferView.h"
19 #include "funcrequest.h"
20 #include "gettext.h"
21 #include "LaTeXFeatures.h"
22 #include "LColor.h"
23 #include "lyxlex.h"
24 #include "metricsinfo.h"
25 #include "paragraph.h"
26
27 #include "support/std_sstream.h"
28
29 using std::auto_ptr;
30 using std::string;
31 using std::istringstream;
32 using std::ostream;
33 using std::ostringstream;
34 using std::endl;
35
36
37 namespace {
38
39 BoxTranslator const init_boxtranslator() {
40         BoxTranslator translator("Boxed", InsetBox::Boxed);
41         translator.addPair("Frameless", InsetBox::Frameless);
42         translator.addPair("ovalbox", InsetBox::ovalbox);
43         translator.addPair("Ovalbox", InsetBox::Ovalbox);
44         translator.addPair("Shadowbox", InsetBox::Shadowbox);
45         translator.addPair("Doublebox",InsetBox::Doublebox);
46         return translator;
47 }
48
49
50 BoxTranslator const init_boxtranslator_loc() {
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 BoxTranslator const & boxtranslator() {
62         static BoxTranslator translator = init_boxtranslator();
63         return translator;
64 }
65
66
67 BoxTranslator const & boxtranslator_loc() {
68         static BoxTranslator translator = init_boxtranslator_loc();
69         return translator;
70 }
71
72 } // anon
73
74
75 void InsetBox::init()
76 {
77         setInsetName("Box");
78         setButtonLabel();
79 }
80
81
82 InsetBox::InsetBox(BufferParams const & bp, string const & label)
83         : InsetCollapsable(bp), params_(label)
84 {
85         init();
86 }
87
88
89 InsetBox::InsetBox(InsetBox const & in)
90         : InsetCollapsable(in), params_(in.params_)
91 {
92         init();
93 }
94
95
96 InsetBox::~InsetBox()
97 {
98         InsetBoxMailer mailer(*this);
99         mailer.hideDialog();
100 }
101
102
103 auto_ptr<InsetBase> InsetBox::clone() const
104 {
105         return auto_ptr<InsetBase>(new InsetBox(*this));
106 }
107
108
109 string const InsetBox::editMessage() const
110 {
111         return _("Opened Box Inset");
112 }
113
114
115 void InsetBox::write(Buffer const & buf, ostream & os) const
116 {
117         params_.write(os);
118         InsetCollapsable::write(buf, os);
119 }
120
121
122 void InsetBox::read(Buffer const & buf, LyXLex & lex)
123 {
124         params_.read(lex);
125         InsetCollapsable::read(buf, lex);
126         setButtonLabel();
127 }
128
129
130 void InsetBox::setButtonLabel()
131 {
132         LyXFont font(LyXFont::ALL_SANE);
133         font.decSize();
134         font.decSize();
135
136         BoxType btype = boxtranslator().find(params_.type);
137         if (btype == Frameless) {
138                 if (params_.use_parbox)
139                         setLabel(_("Parbox"));
140                 else
141                         setLabel(_("Minipage"));
142         } else
143                 setLabel(boxtranslator_loc().find(btype));
144
145         font.setColor(LColor::foreground);
146         setBackgroundColor(LColor::background);
147         setLabelFont(font);
148 }
149
150
151 void InsetBox::metrics(MetricsInfo & mi, Dimension & dim) const
152 {
153         InsetCollapsable::metrics(mi, dim);
154         if (params_.inner_box && isOpen())
155                 dim.wid = mi.base.textwidth;
156         dim_ = dim;
157 }
158
159
160 bool InsetBox::showInsetDialog(BufferView * bv) const
161 {
162         InsetBoxMailer(const_cast<InsetBox &>(*this)).showDialog(bv);
163         return true;
164 }
165
166
167 dispatch_result
168 InsetBox::priv_dispatch(FuncRequest const & cmd,
169                         idx_type & idx, pos_type & pos)
170 {
171         BufferView * bv = cmd.view();
172
173         switch (cmd.action) {
174
175         case LFUN_INSET_MODIFY: {
176                 InsetBoxMailer::string2params(cmd.argument, params_);
177                 setButtonLabel();
178                 bv->updateInset(this);
179                 return DISPATCHED;
180         }
181         case LFUN_INSET_DIALOG_UPDATE:
182                 InsetBoxMailer(*this).updateDialog(bv);
183                 return DISPATCHED;
184
185         case LFUN_MOUSE_RELEASE:
186                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
187                         InsetBoxMailer(*this).showDialog(bv);
188                         return DISPATCHED;
189                 }
190                 // fallthrough:
191
192         default:
193                 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
194         }
195 }
196
197
198 int InsetBox::latex(Buffer const & buf, ostream & os,
199                                 LatexRunParams const & runparams) const
200 {
201         BoxType btype = boxtranslator().find(params_.type);
202
203         string width_string = params_.width.asLatexString();
204         bool stdwidth(false);
205         if (params_.inner_box &&
206                         (width_string.find("1.0\\columnwidth") != string::npos
207                         || width_string.find("1.0\\textwidth") != string::npos)) {
208                 stdwidth = true;
209                 switch (btype) {
210                 case Frameless:
211                         break;
212                 case Boxed:
213                         width_string += " - 2\\fboxsep - 2\\fboxrule";
214                         break;
215                 case ovalbox:
216                         width_string += " - 2\\fboxsep - 0.8pt";
217                         break;
218                 case Ovalbox:
219                         width_string += " - 2\\fboxsep - 1.6pt";
220                         break;
221                 case Shadowbox:
222                         // Shadow falls outside right margin... opinions?
223                         width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
224                         break;
225                 case Doublebox:
226                         width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
227                         break;
228                 }
229         }
230
231         int i = 0;
232         os << "%\n";
233         // Adapt to column/text width correctly also if paragraphs indented:
234         if (stdwidth)
235                 os << "\\noindent";
236
237         switch (btype) {
238         case Frameless:
239                 break;
240         case Boxed:
241                 os << "\\framebox";
242                 if (!params_.inner_box) {
243                         os << "{\\makebox";
244                         // Special widths, see usrguide §3.5
245                         if (params_.special != "none") {
246                                 os << "[" << params_.width.value()
247                                    << "\\" << params_.special << "]";
248                         } else
249                                 os << "[" << width_string << "]";
250                         if (params_.hor_pos != 'c')
251                                 os << "[" << params_.hor_pos << "]";
252                 }
253
254                 os << "{";
255                 break;
256         case ovalbox:
257                 os << "\\ovalbox{";
258                 break;
259         case Ovalbox:
260                 os << "\\Ovalbox{";
261                 break;
262         case Shadowbox:
263                 os << "\\shadowbox{";
264                 break;
265         case Doublebox:
266                 os << "\\doublebox{";
267                 break;
268         }
269
270         if (params_.inner_box) {
271                 if (params_.use_parbox)
272                         os << "\\parbox";
273                 else
274                         os << "\\begin{minipage}";
275
276                 os << "[" << params_.pos << "]";
277                 if (params_.height_special == "none") {
278                         os << "[" << params_.height.asLatexString() << "]";
279                 } else {
280                         // Special heights
281                         os << "[" << params_.height.value()
282                            << "\\" << params_.height_special << "]";
283                 }
284                 if (params_.inner_pos != params_.pos)
285                         os << "[" << params_.inner_pos << "]";
286
287                 os << "{" << width_string << "}";
288
289                 if (params_.use_parbox)
290                         os << "{";
291                 os << "%\n";
292                 i += 1;
293         }
294
295         i += inset.latex(buf, os, runparams);
296
297         if (params_.inner_box) {
298                 if (params_.use_parbox)
299                         os << "%\n}";
300                 else
301                         os << "%\n\\end{minipage}";
302         }
303
304         switch (btype) {
305         case Frameless:
306                 break;
307         case Boxed:
308                 if (!params_.inner_box)
309                         os << "}"; // for makebox
310                 os << "}";
311                 break;
312         case ovalbox:
313         case Ovalbox:
314         case Doublebox:
315         case Shadowbox:
316                 os << "}";
317                 break;
318         }
319         os << "%\n";
320
321         i += 3;
322
323         return i;
324 }
325
326
327 int InsetBox::linuxdoc(Buffer const & buf, std::ostream & os) const
328 {
329         return inset.linuxdoc(buf, os);
330 }
331
332
333 int InsetBox::docbook(Buffer const & buf, std::ostream & os, bool mixcont) const
334 {
335         return inset.docbook(buf, os, mixcont);
336 }
337
338
339 int InsetBox::ascii(Buffer const & buf, std::ostream & os, int ll) const
340 {
341         int i = 0;
342         string const pt = params_.type;
343         BoxType btype = boxtranslator().find(params_.type);
344         switch (btype) {
345                 case Frameless:
346                 break;
347                 case Boxed:
348                 os << "[";
349                 break;
350                 case ovalbox:
351                 os << "(";
352                 break;
353                 case Ovalbox:
354                 os << "((";
355                 break;
356                 case Shadowbox:
357                 os << "[";
358                 break;
359                 case Doublebox:
360                 os << "[[";
361                 break;
362         }
363
364         i = inset.ascii(buf, os, ll);
365
366         switch (btype) {
367                 case Frameless:
368                 break;
369                 case Boxed:
370                 os << "]";
371                 break;
372                 case ovalbox:
373                 os << ")";
374                 break;
375                 case Ovalbox:
376                 os << "))";
377                 break;
378                 case Shadowbox:
379                 os << "]/";
380                 break;
381                 case Doublebox:
382                 os << "]]";
383                 break;
384         }
385
386         return i;
387 }
388
389
390 void InsetBox::validate(LaTeXFeatures & features) const
391 {
392         features.require("calc");
393         BoxType btype = boxtranslator().find(params_.type);
394         switch (btype) {
395         case Frameless:
396         case Boxed:
397                 break;
398         case ovalbox:
399         case Ovalbox:
400         case Shadowbox:
401         case Doublebox:
402                 features.require("fancybox");
403                 break;
404         }
405         inset.validate(features);
406 }
407
408
409 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
410         : inset_(inset)
411 {
412 }
413
414
415 string const InsetBoxMailer::name_ = "box";
416
417
418 string const InsetBoxMailer::inset2string(Buffer const &) const
419 {
420         return params2string(inset_.params());
421 }
422
423
424 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
425 {
426         ostringstream data;
427         data << "box" << ' ';
428         params.write(data);
429         return data.str();
430 }
431
432
433 void InsetBoxMailer::string2params(string const & in,
434         InsetBoxParams & params)
435 {
436         params = InsetBoxParams(string());
437
438         if (in.empty())
439                 return;
440
441         istringstream data(in);
442         LyXLex lex(0,0);
443         lex.setStream(data);
444
445         string token;
446         lex.next();
447         token = lex.getString();
448         lex.next();
449
450         params.read(lex);
451 }
452
453
454 InsetBoxParams::InsetBoxParams(string const & label)
455         : type(label),
456           use_parbox(false),
457           inner_box(true),
458           width(LyXLength("100col%")),
459           special("none"),
460           pos('t'),
461           hor_pos('c'),
462           inner_pos('t'),
463           height(LyXLength("1in")),
464           height_special("totalheight") // default is 1\\totalheight
465 {}
466
467
468 void InsetBoxParams::write(ostream & os) const
469 {
470         os << type << "\n";
471         os << "position \"" << pos << "\"\n";
472         os << "hor_pos \"" << hor_pos << "\"\n";
473         os << "has_inner_box " << inner_box << "\n";
474         os << "inner_pos \"" << inner_pos << "\"\n";
475         os << "use_parbox " << use_parbox << "\n";
476         os << "width \"" << width.asString() << "\"\n";
477         os << "special \"" << special << "\"\n";
478         os << "height \"" << height.asString() << "\"\n";
479         os << "height_special \"" << height_special << "\"\n";
480 }
481
482
483 void InsetBoxParams::read(LyXLex & lex)
484 {
485         if (lex.isOK()) {
486                 type = lex.getString();
487         }
488         string token;
489         if (!lex.isOK())
490                 return;
491         lex.next();
492         token = lex.getString();
493         if (token == "position") {
494                 lex.next();
495                 // The [0] is needed. We need the first and only char in
496                 // this string -- MV
497                 pos = lex.getString()[0];
498         } else {
499                 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
500                 lex.pushToken(token);
501         }
502         if (!lex.isOK())
503                 return;
504         lex.next();
505         token = lex.getString();
506         if (token == "hor_pos") {
507                 lex.next();
508                 hor_pos = lex.getString()[0];
509         } else {
510                 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
511                 lex.pushToken(token);
512         }
513         if (!lex.isOK())
514                 return;
515         lex.next();
516         token = lex.getString();
517         if (token == "has_inner_box") {
518                 lex.next();
519                 inner_box = lex.getInteger();
520         } else {
521                 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
522                 lex.pushToken(token);
523         }
524
525         if (!lex.isOK())
526                 return;
527         lex.next();
528         token = lex.getString();
529         if (token == "inner_pos") {
530                 lex.next();
531                 inner_pos = lex.getString()[0];
532         } else {
533                 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
534                         << token << endl;
535                 lex.pushToken(token);
536         }
537         if (!lex.isOK())
538                 return;
539         lex.next();
540         token = lex.getString();
541         if (token == "use_parbox") {
542                 lex.next();
543                 use_parbox = lex.getInteger();
544         } else {
545                 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
546                 lex.pushToken(token);
547         }
548         if (!lex.isOK())
549                 return;
550         lex.next();
551         token = lex.getString();
552         if (token == "width") {
553                 lex.next();
554                 width = LyXLength(lex.getString());
555         } else {
556                 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
557                 lex.pushToken(token);
558         }
559         if (!lex.isOK())
560                 return;
561         lex.next();
562         token = lex.getString();
563         if (token == "special") {
564                 lex.next();
565                 special = lex.getString();
566         } else {
567                 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
568                 lex.pushToken(token);
569         }
570         if (!lex.isOK())
571                 return;
572         lex.next();
573         token = lex.getString();
574         if (token == "height") {
575                 lex.next();
576                 height = LyXLength(lex.getString());
577         } else {
578                 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
579                 lex.pushToken(token);
580         }
581         if (!lex.isOK())
582                 return;
583         lex.next();
584         token = lex.getString();
585         if (token == "height_special") {
586                 lex.next();
587                 height_special = lex.getString();
588         } else {
589                 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
590                 lex.pushToken(token);
591         }
592 }