]> git.lyx.org Git - lyx.git/blob - src/insets/insetbox.C
Michael's latest minipage cleanup 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(FuncRequest const & cmd,
174                         idx_type & idx, pos_type & pos)
175 {
176         DispatchResult result(false);
177         BufferView * bv = cmd.view();
178
179         switch (cmd.action) {
180
181         case LFUN_INSET_MODIFY: {
182                 lyxerr << "InsetBox::dispatch MODIFY" << endl;
183                 InsetBoxMailer::string2params(cmd.argument, params_);
184                 setButtonLabel();
185                 result.dispatched(true);
186                 result.update(true);
187                 return result;
188         }
189
190         case LFUN_INSET_DIALOG_UPDATE:
191                 InsetBoxMailer(*this).updateDialog(bv);
192                 result.dispatched(true);
193                 return result;
194
195         case LFUN_MOUSE_RELEASE:
196                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
197                         InsetBoxMailer(*this).showDialog(bv);
198                         return DispatchResult(true);
199                 }
200                 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
201
202         default:
203                 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
204         }
205 }
206
207
208 int InsetBox::latex(Buffer const & buf, ostream & os,
209                                 OutputParams const & runparams) const
210 {
211         BoxType btype = boxtranslator().find(params_.type);
212
213         string width_string = params_.width.asLatexString();
214         bool stdwidth(false);
215         if (params_.inner_box &&
216                         (width_string.find("1.0\\columnwidth") != string::npos
217                         || width_string.find("1.0\\textwidth") != string::npos)) {
218                 stdwidth = true;
219                 switch (btype) {
220                 case Frameless:
221                         break;
222                 case Boxed:
223                         width_string += " - 2\\fboxsep - 2\\fboxrule";
224                         break;
225                 case ovalbox:
226                         width_string += " - 2\\fboxsep - 0.8pt";
227                         break;
228                 case Ovalbox:
229                         width_string += " - 2\\fboxsep - 1.6pt";
230                         break;
231                 case Shadowbox:
232                         // Shadow falls outside right margin... opinions?
233                         width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
234                         break;
235                 case Doublebox:
236                         width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
237                         break;
238                 }
239         }
240
241         int i = 0;
242         os << "%\n";
243         // Adapt to column/text width correctly also if paragraphs indented:
244         if (stdwidth)
245                 os << "\\noindent";
246
247         switch (btype) {
248         case Frameless:
249                 break;
250         case Boxed:
251                 os << "\\framebox";
252                 if (!params_.inner_box) {
253                         os << "{\\makebox";
254                         // Special widths, see usrguide §3.5
255                         if (params_.special != "none") {
256                                 os << "[" << params_.width.value()
257                                    << "\\" << params_.special << "]";
258                         } else
259                                 os << "[" << width_string << "]";
260                         if (params_.hor_pos != 'c')
261                                 os << "[" << params_.hor_pos << "]";
262                 }
263
264                 os << "{";
265                 break;
266         case ovalbox:
267                 os << "\\ovalbox{";
268                 break;
269         case Ovalbox:
270                 os << "\\Ovalbox{";
271                 break;
272         case Shadowbox:
273                 os << "\\shadowbox{";
274                 break;
275         case Doublebox:
276                 os << "\\doublebox{";
277                 break;
278         }
279
280         if (params_.inner_box) {
281                 if (params_.use_parbox)
282                         os << "\\parbox";
283                 else
284                         os << "\\begin{minipage}";
285
286                 os << "[" << params_.pos << "]";
287                 if (params_.height_special == "none") {
288                         os << "[" << params_.height.asLatexString() << "]";
289                 } else {
290                         // Special heights
291                         os << "[" << params_.height.value()
292                            << "\\" << params_.height_special << "]";
293                 }
294                 if (params_.inner_pos != params_.pos)
295                         os << "[" << params_.inner_pos << "]";
296
297                 os << "{" << width_string << "}";
298
299                 if (params_.use_parbox)
300                         os << "{";
301                 os << "%\n";
302                 i += 1;
303         }
304
305         i += inset.latex(buf, os, runparams);
306
307         if (params_.inner_box) {
308                 if (params_.use_parbox)
309                         os << "%\n}";
310                 else
311                         os << "%\n\\end{minipage}";
312         }
313
314         switch (btype) {
315         case Frameless:
316                 break;
317         case Boxed:
318                 if (!params_.inner_box)
319                         os << "}"; // for makebox
320                 os << "}";
321                 break;
322         case ovalbox:
323         case Ovalbox:
324         case Doublebox:
325         case Shadowbox:
326                 os << "}";
327                 break;
328         }
329         os << "%\n";
330
331         i += 3;
332
333         return i;
334 }
335
336
337 int InsetBox::linuxdoc(Buffer const & buf, std::ostream & os,
338                        OutputParams const & runparams) const
339 {
340         return inset.linuxdoc(buf, os, runparams);
341 }
342
343
344 int InsetBox::docbook(Buffer const & buf, std::ostream & os,
345                       OutputParams const & runparams) const
346 {
347         return inset.docbook(buf, os, runparams);
348 }
349
350
351 int InsetBox::plaintext(Buffer const & buf, std::ostream & os,
352                     OutputParams const & runparams) const
353 {
354         BoxType const btype = boxtranslator().find(params_.type);
355
356         switch (btype) {
357                 case Frameless: break;
358                 case Boxed:     os << "[";  break;
359                 case ovalbox:   os << "(";  break;
360                 case Ovalbox:   os << "(("; break;
361                 case Shadowbox: os << "[";  break;
362                 case Doublebox: os << "[["; break;
363         }
364
365         int i = inset.plaintext(buf, os, runparams);
366
367         switch (btype) {
368                 case Frameless: break;
369                 case Boxed:     os << "]";  break;
370                 case ovalbox:   os << ")";  break;
371                 case Ovalbox:   os << "))"; break;
372                 case Shadowbox: os << "]/"; break;
373                 case Doublebox: os << "]]"; break;
374         }
375
376         return i;
377 }
378
379
380 void InsetBox::validate(LaTeXFeatures & features) const
381 {
382         features.require("calc");
383         BoxType btype = boxtranslator().find(params_.type);
384         switch (btype) {
385         case Frameless:
386         case Boxed:
387                 break;
388         case ovalbox:
389         case Ovalbox:
390         case Shadowbox:
391         case Doublebox:
392                 features.require("fancybox");
393                 break;
394         }
395         inset.validate(features);
396 }
397
398
399 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
400         : inset_(inset)
401 {
402 }
403
404
405 string const InsetBoxMailer::name_ = "box";
406
407
408 string const InsetBoxMailer::inset2string(Buffer const &) const
409 {
410         return params2string(inset_.params());
411 }
412
413
414 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
415 {
416         ostringstream data;
417         data << "box" << ' ';
418         params.write(data);
419         return data.str();
420 }
421
422
423 void InsetBoxMailer::string2params(string const & in,
424                                    InsetBoxParams & params)
425 {
426         params = InsetBoxParams(string());
427         if (in.empty())
428                 return;
429
430         istringstream data(in);
431         LyXLex lex(0,0);
432         lex.setStream(data);
433
434         string name;
435         lex >> name;
436         if (!lex || name != name_)
437                 return print_mailer_error("InsetBoxMailer", in, 1, name_);
438
439         // This is part of the inset proper that is usually swallowed
440         // by LyXText::readInset
441         string id;
442         lex >> id;
443         if (!lex || id != "Box")
444                 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
445
446         params.read(lex);
447 }
448
449
450 InsetBoxParams::InsetBoxParams(string const & label)
451         : type(label),
452           use_parbox(false),
453           inner_box(true),
454           width(LyXLength("100col%")),
455           special("none"),
456           pos('t'),
457           hor_pos('c'),
458           inner_pos('t'),
459           height(LyXLength("1in")),
460           height_special("totalheight") // default is 1\\totalheight
461 {}
462
463
464 void InsetBoxParams::write(ostream & os) const
465 {
466         os << "Box " << type << "\n";
467         os << "position \"" << pos << "\"\n";
468         os << "hor_pos \"" << hor_pos << "\"\n";
469         os << "has_inner_box " << inner_box << "\n";
470         os << "inner_pos \"" << inner_pos << "\"\n";
471         os << "use_parbox " << use_parbox << "\n";
472         os << "width \"" << width.asString() << "\"\n";
473         os << "special \"" << special << "\"\n";
474         os << "height \"" << height.asString() << "\"\n";
475         os << "height_special \"" << height_special << "\"\n";
476 }
477
478
479 void InsetBoxParams::read(LyXLex & lex)
480 {
481         if (!lex.isOK())
482                 return;
483
484         if (lex.isOK()) {
485                 lex.next();
486                 type = lex.getString();
487         }
488         if (!lex.isOK())
489                 return;
490         lex.next();
491         string token;
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 }