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