]> git.lyx.org Git - lyx.git/blob - src/insets/insetbox.C
simple ws changes only
[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 += InsetText::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 InsetText::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 InsetText::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 = InsetText::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         InsetText::validate(features);
390 }
391
392
393 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
394         : inset_(inset)
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 }