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