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