]> git.lyx.org Git - lyx.git/blob - src/insets/insetbox.C
The Buffer::LyXText -> Buffer::InsetText 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 "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/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 void InsetBox::priv_dispatch(LCursor & cur, FuncRequest & cmd)
173 {
174         switch (cmd.action) {
175
176         case LFUN_INSET_MODIFY: {
177                 lyxerr << "InsetBox::dispatch MODIFY" << endl;
178                 InsetBoxMailer::string2params(cmd.argument, params_);
179                 setButtonLabel();
180                 break;
181         }
182
183         case LFUN_INSET_DIALOG_UPDATE:
184                 InsetBoxMailer(*this).updateDialog(&cur.bv());
185                 break;
186
187         case LFUN_MOUSE_RELEASE:
188                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
189                         InsetBoxMailer(*this).showDialog(&cur.bv());
190                         break;
191                 }
192                 InsetCollapsable::priv_dispatch(cur, cmd);
193                 break;
194
195         default:
196                 InsetCollapsable::priv_dispatch(cur, cmd);
197                 break;
198         }
199 }
200
201
202 int InsetBox::latex(Buffer const & buf, ostream & os,
203                                 OutputParams const & runparams) const
204 {
205         BoxType btype = boxtranslator().find(params_.type);
206
207         string width_string = params_.width.asLatexString();
208         bool stdwidth(false);
209         if (params_.inner_box &&
210                         (width_string.find("1.0\\columnwidth") != string::npos
211                         || width_string.find("1.0\\textwidth") != string::npos)) {
212                 stdwidth = true;
213                 switch (btype) {
214                 case Frameless:
215                         break;
216                 case Boxed:
217                         width_string += " - 2\\fboxsep - 2\\fboxrule";
218                         break;
219                 case ovalbox:
220                         width_string += " - 2\\fboxsep - 0.8pt";
221                         break;
222                 case Ovalbox:
223                         width_string += " - 2\\fboxsep - 1.6pt";
224                         break;
225                 case Shadowbox:
226                         // Shadow falls outside right margin... opinions?
227                         width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
228                         break;
229                 case Doublebox:
230                         width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
231                         break;
232                 }
233         }
234
235         int i = 0;
236         os << "%\n";
237         // Adapt to column/text width correctly also if paragraphs indented:
238         if (stdwidth)
239                 os << "\\noindent";
240
241         switch (btype) {
242         case Frameless:
243                 break;
244         case Boxed:
245                 os << "\\framebox";
246                 if (!params_.inner_box) {
247                         os << "{\\makebox";
248                         // Special widths, see usrguide §3.5
249                         if (params_.special != "none") {
250                                 os << "[" << params_.width.value()
251                                    << "\\" << params_.special << "]";
252                         } else
253                                 os << "[" << width_string << "]";
254                         if (params_.hor_pos != 'c')
255                                 os << "[" << params_.hor_pos << "]";
256                 }
257
258                 os << "{";
259                 break;
260         case ovalbox:
261                 os << "\\ovalbox{";
262                 break;
263         case Ovalbox:
264                 os << "\\Ovalbox{";
265                 break;
266         case Shadowbox:
267                 os << "\\shadowbox{";
268                 break;
269         case Doublebox:
270                 os << "\\doublebox{";
271                 break;
272         }
273
274         if (params_.inner_box) {
275                 if (params_.use_parbox)
276                         os << "\\parbox";
277                 else
278                         os << "\\begin{minipage}";
279
280                 os << "[" << params_.pos << "]";
281                 if (params_.height_special == "none") {
282                         os << "[" << params_.height.asLatexString() << "]";
283                 } else {
284                         // Special heights
285                         os << "[" << params_.height.value()
286                            << "\\" << params_.height_special << "]";
287                 }
288                 if (params_.inner_pos != params_.pos)
289                         os << "[" << params_.inner_pos << "]";
290
291                 os << "{" << width_string << "}";
292
293                 if (params_.use_parbox)
294                         os << "{";
295                 os << "%\n";
296                 i += 1;
297         }
298
299         i += inset.latex(buf, os, runparams);
300
301         if (params_.inner_box) {
302                 if (params_.use_parbox)
303                         os << "%\n}";
304                 else
305                         os << "%\n\\end{minipage}";
306         }
307
308         switch (btype) {
309         case Frameless:
310                 break;
311         case Boxed:
312                 if (!params_.inner_box)
313                         os << "}"; // for makebox
314                 os << "}";
315                 break;
316         case ovalbox:
317         case Ovalbox:
318         case Doublebox:
319         case Shadowbox:
320                 os << "}";
321                 break;
322         }
323         os << "%\n";
324
325         i += 3;
326
327         return i;
328 }
329
330
331 int InsetBox::linuxdoc(Buffer const & buf, std::ostream & os,
332                        OutputParams const & runparams) const
333 {
334         return inset.linuxdoc(buf, os, runparams);
335 }
336
337
338 int InsetBox::docbook(Buffer const & buf, std::ostream & os,
339                       OutputParams const & runparams) const
340 {
341         return inset.docbook(buf, os, runparams);
342 }
343
344
345 int InsetBox::plaintext(Buffer const & buf, std::ostream & os,
346                     OutputParams const & runparams) const
347 {
348         BoxType const btype = boxtranslator().find(params_.type);
349
350         switch (btype) {
351                 case Frameless: break;
352                 case Boxed:     os << "[";  break;
353                 case ovalbox:   os << "(";  break;
354                 case Ovalbox:   os << "(("; break;
355                 case Shadowbox: os << "[";  break;
356                 case Doublebox: os << "[["; break;
357         }
358
359         int i = inset.plaintext(buf, os, runparams);
360
361         switch (btype) {
362                 case Frameless: break;
363                 case Boxed:     os << "]";  break;
364                 case ovalbox:   os << ")";  break;
365                 case Ovalbox:   os << "))"; break;
366                 case Shadowbox: os << "]/"; break;
367                 case Doublebox: os << "]]"; break;
368         }
369
370         return i;
371 }
372
373
374 void InsetBox::validate(LaTeXFeatures & features) const
375 {
376         features.require("calc");
377         BoxType btype = boxtranslator().find(params_.type);
378         switch (btype) {
379         case Frameless:
380         case Boxed:
381                 break;
382         case ovalbox:
383         case Ovalbox:
384         case Shadowbox:
385         case Doublebox:
386                 features.require("fancybox");
387                 break;
388         }
389         inset.validate(features);
390 }
391
392
393 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
394         : inset_(inset)
395 {
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 }