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