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