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