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