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