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