]> git.lyx.org Git - lyx.git/blob - src/insets/insetbox.C
* src/LyXAction.C: mark goto-clear-bookmark as working without buffer
[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::docbook(Buffer const & buf, odocstream & os,
393                       OutputParams const & runparams) const
394 {
395         return InsetText::docbook(buf, os, runparams);
396 }
397
398
399 int InsetBox::plaintext(Buffer const & buf, odocstream & os,
400                     OutputParams const & runparams) const
401 {
402         BoxType const btype = boxtranslator().find(params_.type);
403
404         switch (btype) {
405                 case Frameless: break;
406                 case Boxed:     os << "[";  break;
407                 case ovalbox:   os << "(";  break;
408                 case Ovalbox:   os << "(("; break;
409                 case Shadowbox: os << "[";  break;
410                 case Doublebox: os << "[["; break;
411         }
412
413         int i = InsetText::plaintext(buf, os, runparams);
414
415         switch (btype) {
416                 case Frameless: break;
417                 case Boxed:     os << "]";  break;
418                 case ovalbox:   os << ")";  break;
419                 case Ovalbox:   os << "))"; break;
420                 case Shadowbox: os << "]/"; break;
421                 case Doublebox: os << "]]"; break;
422         }
423
424         return i;
425 }
426
427
428 void InsetBox::validate(LaTeXFeatures & features) const
429 {
430         features.require("calc");
431         BoxType btype = boxtranslator().find(params_.type);
432         switch (btype) {
433         case Frameless:
434         case Boxed:
435                 break;
436         case ovalbox:
437         case Ovalbox:
438         case Shadowbox:
439         case Doublebox:
440                 features.require("fancybox");
441                 break;
442         }
443         InsetText::validate(features);
444 }
445
446
447 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
448         : inset_(inset)
449 {}
450
451
452 string const InsetBoxMailer::name_ = "box";
453
454
455 string const InsetBoxMailer::inset2string(Buffer const &) const
456 {
457         return params2string(inset_.params());
458 }
459
460
461 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
462 {
463         ostringstream data;
464         data << "box" << ' ';
465         params.write(data);
466         return data.str();
467 }
468
469
470 void InsetBoxMailer::string2params(string const & in,
471                                    InsetBoxParams & params)
472 {
473         params = InsetBoxParams(string());
474         if (in.empty())
475                 return;
476
477         istringstream data(in);
478         LyXLex lex(0,0);
479         lex.setStream(data);
480
481         string name;
482         lex >> name;
483         if (!lex || name != name_)
484                 return print_mailer_error("InsetBoxMailer", in, 1, name_);
485
486         // This is part of the inset proper that is usually swallowed
487         // by LyXText::readInset
488         string id;
489         lex >> id;
490         if (!lex || id != "Box")
491                 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
492
493         params.read(lex);
494 }
495
496
497 InsetBoxParams::InsetBoxParams(string const & label)
498         : type(label),
499           use_parbox(false),
500           inner_box(true),
501           width(LyXLength("100col%")),
502           special("none"),
503           pos('t'),
504           hor_pos('c'),
505           inner_pos('t'),
506           height(LyXLength("1in")),
507           height_special("totalheight") // default is 1\\totalheight
508 {}
509
510
511 void InsetBoxParams::write(ostream & os) const
512 {
513         os << "Box " << type << "\n";
514         os << "position \"" << pos << "\"\n";
515         os << "hor_pos \"" << hor_pos << "\"\n";
516         os << "has_inner_box " << inner_box << "\n";
517         os << "inner_pos \"" << inner_pos << "\"\n";
518         os << "use_parbox " << use_parbox << "\n";
519         os << "width \"" << width.asString() << "\"\n";
520         os << "special \"" << special << "\"\n";
521         os << "height \"" << height.asString() << "\"\n";
522         os << "height_special \"" << height_special << "\"\n";
523 }
524
525
526 void InsetBoxParams::read(LyXLex & lex)
527 {
528         if (!lex.isOK())
529                 return;
530
531         if (lex.isOK()) {
532                 lex.next();
533                 type = lex.getString();
534         }
535         if (!lex.isOK())
536                 return;
537         lex.next();
538         string token;
539         token = lex.getString();
540         if (token == "position") {
541                 lex.next();
542                 // The [0] is needed. We need the first and only char in
543                 // this string -- MV
544                 pos = lex.getString()[0];
545         } else {
546                 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
547                 lex.pushToken(token);
548         }
549         if (!lex.isOK())
550                 return;
551         lex.next();
552         token = lex.getString();
553         if (token == "hor_pos") {
554                 lex.next();
555                 hor_pos = lex.getString()[0];
556         } else {
557                 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
558                 lex.pushToken(token);
559         }
560         if (!lex.isOK())
561                 return;
562         lex.next();
563         token = lex.getString();
564         if (token == "has_inner_box") {
565                 lex.next();
566                 inner_box = lex.getInteger();
567         } else {
568                 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
569                 lex.pushToken(token);
570         }
571
572         if (!lex.isOK())
573                 return;
574         lex.next();
575         token = lex.getString();
576         if (token == "inner_pos") {
577                 lex.next();
578                 inner_pos = lex.getString()[0];
579         } else {
580                 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
581                         << token << endl;
582                 lex.pushToken(token);
583         }
584         if (!lex.isOK())
585                 return;
586         lex.next();
587         token = lex.getString();
588         if (token == "use_parbox") {
589                 lex.next();
590                 use_parbox = lex.getInteger();
591         } else {
592                 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
593                 lex.pushToken(token);
594         }
595         if (!lex.isOK())
596                 return;
597         lex.next();
598         token = lex.getString();
599         if (token == "width") {
600                 lex.next();
601                 width = LyXLength(lex.getString());
602         } else {
603                 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
604                 lex.pushToken(token);
605         }
606         if (!lex.isOK())
607                 return;
608         lex.next();
609         token = lex.getString();
610         if (token == "special") {
611                 lex.next();
612                 special = lex.getString();
613         } else {
614                 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
615                 lex.pushToken(token);
616         }
617         if (!lex.isOK())
618                 return;
619         lex.next();
620         token = lex.getString();
621         if (token == "height") {
622                 lex.next();
623                 height = LyXLength(lex.getString());
624         } else {
625                 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
626                 lex.pushToken(token);
627         }
628         if (!lex.isOK())
629                 return;
630         lex.next();
631         token = lex.getString();
632         if (token == "height_special") {
633                 lex.next();
634                 height_special = lex.getString();
635         } else {
636                 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
637                 lex.pushToken(token);
638         }
639 }
640
641
642 } // namespace lyx