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