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