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