]> git.lyx.org Git - lyx.git/blob - src/insets/insetbox.C
Split clipboard and X selection
[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         case LFUN_PASTE:
202         case LFUN_CLIPBOARD_PASTE:
203         case LFUN_PRIMARY_SELECTION_PASTE:
204                 InsetCollapsable::doDispatch(cur, cmd);
205                 if (!params_.inner_box)
206                         forceParagraphsToDefault(cur);
207                 break;
208
209         default:
210                 InsetCollapsable::doDispatch(cur, cmd);
211                 break;
212         }
213 }
214
215
216 bool InsetBox::getStatus(LCursor & cur, FuncRequest const & cmd,
217                 FuncStatus & flag) const
218 {
219         switch (cmd.action) {
220
221         case LFUN_INSET_MODIFY:
222         case LFUN_INSET_DIALOG_UPDATE:
223                 flag.enabled(true);
224                 return true;
225         case LFUN_BREAK_PARAGRAPH:
226                 if (params_.inner_box) {
227                         return InsetCollapsable::getStatus(cur, cmd, flag);
228                 } else {
229                         flag.enabled(false);
230                         return true;
231                 }
232
233         default:
234                 return InsetCollapsable::getStatus(cur, cmd, flag);
235         }
236 }
237
238
239 int InsetBox::latex(Buffer const & buf, ostream & os,
240                                 OutputParams const & runparams) const
241 {
242         BoxType btype = boxtranslator().find(params_.type);
243
244         string width_string = params_.width.asLatexString();
245         bool stdwidth(false);
246         if (params_.inner_box &&
247                         (width_string.find("1.0\\columnwidth") != string::npos
248                         || width_string.find("1.0\\textwidth") != string::npos)) {
249                 stdwidth = true;
250                 switch (btype) {
251                 case Frameless:
252                         break;
253                 case Boxed:
254                         width_string += " - 2\\fboxsep - 2\\fboxrule";
255                         break;
256                 case ovalbox:
257                         width_string += " - 2\\fboxsep - 0.8pt";
258                         break;
259                 case Ovalbox:
260                         width_string += " - 2\\fboxsep - 1.6pt";
261                         break;
262                 case Shadowbox:
263                         // Shadow falls outside right margin... opinions?
264                         width_string += " - 2\\fboxsep - 2\\fboxrule"/* "-\\shadowsize"*/;
265                         break;
266                 case Doublebox:
267                         width_string += " - 2\\fboxsep - 7.5\\fboxrule - 1.0pt";
268                         break;
269                 }
270         }
271
272         int i = 0;
273         os << "%\n";
274         // Adapt to column/text width correctly also if paragraphs indented:
275         if (stdwidth)
276                 os << "\\noindent";
277
278         switch (btype) {
279         case Frameless:
280                 break;
281         case Boxed:
282                 os << "\\framebox";
283                 if (!params_.inner_box) {
284                         os << "{\\makebox";
285                         // Special widths, see usrguide §3.5
286                         if (params_.special != "none") {
287                                 os << "[" << params_.width.value()
288                                    << "\\" << params_.special << "]";
289                         } else
290                                 os << "[" << width_string << "]";
291                         if (params_.hor_pos != 'c')
292                                 os << "[" << params_.hor_pos << "]";
293                 }
294
295                 os << "{";
296                 break;
297         case ovalbox:
298                 os << "\\ovalbox{";
299                 break;
300         case Ovalbox:
301                 os << "\\Ovalbox{";
302                 break;
303         case Shadowbox:
304                 os << "\\shadowbox{";
305                 break;
306         case Doublebox:
307                 os << "\\doublebox{";
308                 break;
309         }
310
311         if (params_.inner_box) {
312                 if (params_.use_parbox)
313                         os << "\\parbox";
314                 else
315                         os << "\\begin{minipage}";
316
317                 os << "[" << params_.pos << "]";
318                 if (params_.height_special == "none") {
319                         os << "[" << params_.height.asLatexString() << "]";
320                 } else {
321                         // Special heights
322                         os << "[" << params_.height.value()
323                            << "\\" << params_.height_special << "]";
324                 }
325                 if (params_.inner_pos != params_.pos)
326                         os << "[" << params_.inner_pos << "]";
327
328                 os << "{" << width_string << "}";
329
330                 if (params_.use_parbox)
331                         os << "{";
332                 os << "%\n";
333                 i += 1;
334         }
335
336         i += InsetText::latex(buf, os, runparams);
337
338         if (params_.inner_box) {
339                 if (params_.use_parbox)
340                         os << "%\n}";
341                 else
342                         os << "%\n\\end{minipage}";
343         }
344
345         switch (btype) {
346         case Frameless:
347                 break;
348         case Boxed:
349                 if (!params_.inner_box)
350                         os << "}"; // for makebox
351                 os << "}";
352                 break;
353         case ovalbox:
354         case Ovalbox:
355         case Doublebox:
356         case Shadowbox:
357                 os << "}";
358                 break;
359         }
360         os << "%\n";
361
362         i += 3;
363
364         return i;
365 }
366
367
368 int InsetBox::linuxdoc(Buffer const & buf, std::ostream & os,
369                        OutputParams const & runparams) const
370 {
371         return InsetText::linuxdoc(buf, os, runparams);
372 }
373
374
375 int InsetBox::docbook(Buffer const & buf, std::ostream & os,
376                       OutputParams const & runparams) const
377 {
378         return InsetText::docbook(buf, os, runparams);
379 }
380
381
382 int InsetBox::plaintext(Buffer const & buf, std::ostream & os,
383                     OutputParams const & runparams) const
384 {
385         BoxType const btype = boxtranslator().find(params_.type);
386
387         switch (btype) {
388                 case Frameless: break;
389                 case Boxed:     os << "[";  break;
390                 case ovalbox:   os << "(";  break;
391                 case Ovalbox:   os << "(("; break;
392                 case Shadowbox: os << "[";  break;
393                 case Doublebox: os << "[["; break;
394         }
395
396         int i = InsetText::plaintext(buf, os, runparams);
397
398         switch (btype) {
399                 case Frameless: break;
400                 case Boxed:     os << "]";  break;
401                 case ovalbox:   os << ")";  break;
402                 case Ovalbox:   os << "))"; break;
403                 case Shadowbox: os << "]/"; break;
404                 case Doublebox: os << "]]"; break;
405         }
406
407         return i;
408 }
409
410
411 void InsetBox::validate(LaTeXFeatures & features) const
412 {
413         features.require("calc");
414         BoxType btype = boxtranslator().find(params_.type);
415         switch (btype) {
416         case Frameless:
417         case Boxed:
418                 break;
419         case ovalbox:
420         case Ovalbox:
421         case Shadowbox:
422         case Doublebox:
423                 features.require("fancybox");
424                 break;
425         }
426         InsetText::validate(features);
427 }
428
429
430 InsetBoxMailer::InsetBoxMailer(InsetBox & inset)
431         : inset_(inset)
432 {}
433
434
435 string const InsetBoxMailer::name_ = "box";
436
437
438 string const InsetBoxMailer::inset2string(Buffer const &) const
439 {
440         return params2string(inset_.params());
441 }
442
443
444 string const InsetBoxMailer::params2string(InsetBoxParams const & params)
445 {
446         ostringstream data;
447         data << "box" << ' ';
448         params.write(data);
449         return data.str();
450 }
451
452
453 void InsetBoxMailer::string2params(string const & in,
454                                    InsetBoxParams & params)
455 {
456         params = InsetBoxParams(string());
457         if (in.empty())
458                 return;
459
460         istringstream data(in);
461         LyXLex lex(0,0);
462         lex.setStream(data);
463
464         string name;
465         lex >> name;
466         if (!lex || name != name_)
467                 return print_mailer_error("InsetBoxMailer", in, 1, name_);
468
469         // This is part of the inset proper that is usually swallowed
470         // by LyXText::readInset
471         string id;
472         lex >> id;
473         if (!lex || id != "Box")
474                 return print_mailer_error("InsetBoxMailer", in, 2, "Box");
475
476         params.read(lex);
477 }
478
479
480 InsetBoxParams::InsetBoxParams(string const & label)
481         : type(label),
482           use_parbox(false),
483           inner_box(true),
484           width(LyXLength("100col%")),
485           special("none"),
486           pos('t'),
487           hor_pos('c'),
488           inner_pos('t'),
489           height(LyXLength("1in")),
490           height_special("totalheight") // default is 1\\totalheight
491 {}
492
493
494 void InsetBoxParams::write(ostream & os) const
495 {
496         os << "Box " << type << "\n";
497         os << "position \"" << pos << "\"\n";
498         os << "hor_pos \"" << hor_pos << "\"\n";
499         os << "has_inner_box " << inner_box << "\n";
500         os << "inner_pos \"" << inner_pos << "\"\n";
501         os << "use_parbox " << use_parbox << "\n";
502         os << "width \"" << width.asString() << "\"\n";
503         os << "special \"" << special << "\"\n";
504         os << "height \"" << height.asString() << "\"\n";
505         os << "height_special \"" << height_special << "\"\n";
506 }
507
508
509 void InsetBoxParams::read(LyXLex & lex)
510 {
511         if (!lex.isOK())
512                 return;
513
514         if (lex.isOK()) {
515                 lex.next();
516                 type = lex.getString();
517         }
518         if (!lex.isOK())
519                 return;
520         lex.next();
521         string token;
522         token = lex.getString();
523         if (token == "position") {
524                 lex.next();
525                 // The [0] is needed. We need the first and only char in
526                 // this string -- MV
527                 pos = lex.getString()[0];
528         } else {
529                 lyxerr << "InsetBox::Read: Missing 'position'-tag!" << token << endl;
530                 lex.pushToken(token);
531         }
532         if (!lex.isOK())
533                 return;
534         lex.next();
535         token = lex.getString();
536         if (token == "hor_pos") {
537                 lex.next();
538                 hor_pos = lex.getString()[0];
539         } else {
540                 lyxerr << "InsetBox::Read: Missing 'hor_pos'-tag!" << token << endl;
541                 lex.pushToken(token);
542         }
543         if (!lex.isOK())
544                 return;
545         lex.next();
546         token = lex.getString();
547         if (token == "has_inner_box") {
548                 lex.next();
549                 inner_box = lex.getInteger();
550         } else {
551                 lyxerr << "InsetBox::Read: Missing 'has_inner_box'-tag!" << endl;
552                 lex.pushToken(token);
553         }
554
555         if (!lex.isOK())
556                 return;
557         lex.next();
558         token = lex.getString();
559         if (token == "inner_pos") {
560                 lex.next();
561                 inner_pos = lex.getString()[0];
562         } else {
563                 lyxerr << "InsetBox::Read: Missing 'inner_pos'-tag!"
564                         << token << endl;
565                 lex.pushToken(token);
566         }
567         if (!lex.isOK())
568                 return;
569         lex.next();
570         token = lex.getString();
571         if (token == "use_parbox") {
572                 lex.next();
573                 use_parbox = lex.getInteger();
574         } else {
575                 lyxerr << "InsetBox::Read: Missing 'use_parbox'-tag!" << endl;
576                 lex.pushToken(token);
577         }
578         if (!lex.isOK())
579                 return;
580         lex.next();
581         token = lex.getString();
582         if (token == "width") {
583                 lex.next();
584                 width = LyXLength(lex.getString());
585         } else {
586                 lyxerr << "InsetBox::Read: Missing 'width'-tag!" << endl;
587                 lex.pushToken(token);
588         }
589         if (!lex.isOK())
590                 return;
591         lex.next();
592         token = lex.getString();
593         if (token == "special") {
594                 lex.next();
595                 special = lex.getString();
596         } else {
597                 lyxerr << "InsetBox::Read: Missing 'special'-tag!" << endl;
598                 lex.pushToken(token);
599         }
600         if (!lex.isOK())
601                 return;
602         lex.next();
603         token = lex.getString();
604         if (token == "height") {
605                 lex.next();
606                 height = LyXLength(lex.getString());
607         } else {
608                 lyxerr << "InsetBox::Read: Missing 'height'-tag!" << endl;
609                 lex.pushToken(token);
610         }
611         if (!lex.isOK())
612                 return;
613         lex.next();
614         token = lex.getString();
615         if (token == "height_special") {
616                 lex.next();
617                 height_special = lex.getString();
618         } else {
619                 lyxerr << "InsetBox::Read: Missing 'height_special'-tag!" << endl;
620                 lex.pushToken(token);
621         }
622 }