]> git.lyx.org Git - lyx.git/blob - src/insets/insetert.C
1621b2016c9d5ce19687c49829e2481c4672cdd3
[lyx.git] / src / insets / insetert.C
1 /**
2  * \file insetert.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Vigna
7  * \author Lars Gullik Bjønnes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11 #include <config.h>
12
13 #include "insetert.h"
14
15 #include "buffer.h"
16 #include "bufferparams.h"
17 #include "BufferView.h"
18 #include "debug.h"
19 #include "dispatchresult.h"
20 #include "funcrequest.h"
21 #include "gettext.h"
22 #include "language.h"
23 #include "LColor.h"
24 #include "lyxlex.h"
25 #include "metricsinfo.h"
26 #include "paragraph.h"
27
28 #include "frontends/Alert.h"
29 #include "frontends/LyXView.h"
30
31 #include "support/tostr.h"
32
33 using lyx::pos_type;
34
35 using lyx::support::split;
36 using lyx::support::strToInt;
37
38 using std::endl;
39 using std::min;
40 using std::string;
41 using std::auto_ptr;
42 using std::ostream;
43
44
45 void InsetERT::init()
46 {
47         setButtonLabel();
48
49         LyXFont font(LyXFont::ALL_SANE);
50         font.decSize();
51         font.decSize();
52         font.setColor(LColor::latex);
53         setLabelFont(font);
54
55         setInsetName("ERT");
56 }
57
58
59 InsetERT::InsetERT(BufferParams const & bp, bool collapsed)
60         : InsetCollapsable(bp, collapsed)
61 {
62         if (collapsed)
63                 status_ = Collapsed;
64         else
65                 status_ = Open;
66         init();
67 }
68
69
70 InsetERT::InsetERT(InsetERT const & in)
71         : InsetCollapsable(in), status_(in.status_)
72 {
73         init();
74 }
75
76
77 auto_ptr<InsetBase> InsetERT::clone() const
78 {
79         return auto_ptr<InsetBase>(new InsetERT(*this));
80 }
81
82
83 InsetERT::InsetERT(BufferParams const & bp,
84                    Language const * l, string const & contents, bool collapsed)
85         : InsetCollapsable(bp, collapsed)
86 {
87         if (collapsed)
88                 status_ = Collapsed;
89         else
90                 status_ = Open;
91
92         LyXFont font(LyXFont::ALL_INHERIT, l);
93 #ifdef SET_HARD_FONT
94         font.setFamily(LyXFont::TYPEWRITER_FAMILY);
95         font.setColor(LColor::latex);
96 #endif
97
98         string::const_iterator cit = contents.begin();
99         string::const_iterator end = contents.end();
100         pos_type pos = 0;
101         for (; cit != end; ++cit) {
102                 inset.paragraphs.begin()->insertChar(pos++, *cit, font);
103         }
104         // the init has to be after the initialization of the paragraph
105         // because of the label settings (draw_label for ert insets).
106         init();
107 }
108
109
110 InsetERT::~InsetERT()
111 {
112         InsetERTMailer(*this).hideDialog();
113 }
114
115
116 void InsetERT::read(Buffer const & buf, LyXLex & lex)
117 {
118         bool token_found = false;
119         if (lex.isOK()) {
120                 lex.next();
121                 string const token = lex.getString();
122                 if (token == "status") {
123                         lex.next();
124                         string const tmp_token = lex.getString();
125
126                         if (tmp_token == "Inlined") {
127                                 status(0, Inlined);
128                         } else if (tmp_token == "Collapsed") {
129                                 status(0, Collapsed);
130                         } else {
131                                 // leave this as default!
132                                 status(0, Open);
133                         }
134
135                         token_found = true;
136                 } else {
137                         lyxerr << "InsetERT::Read: Missing 'status'-tag!"
138                                    << endl;
139                         // take countermeasures
140                         lex.pushToken(token);
141                 }
142         }
143 #if 0
144 #warning this should be really short lived only for compatibility to
145 #warning files written 07/08/2001 so this has to go before 1.2.0! (Jug)
146         if (lex.isOK()) {
147                 lex.next();
148                 string const token = lex.getString();
149                 if (token == "collapsed") {
150                         lex.next();
151                         setCollapsed(lex.getBool());
152                 } else {
153                         // Take countermeasures
154                         lex.pushToken(token);
155                 }
156         }
157 #endif
158         inset.read(buf, lex);
159
160 #ifdef SET_HARD_FONT
161         LyXFont font(LyXFont::ALL_INHERIT, latex_language);
162         font.setFamily(LyXFont::TYPEWRITER_FAMILY);
163         font.setColor(LColor::latex);
164
165         ParagraphList::iterator pit = inset.paragraphs.begin();
166         ParagraphList::iterator pend = inset.paragraphs.end();
167         for (; pit != pend; ++pit) {
168                 pos_type siz = pit->size();
169                 for (pos_type i = 0; i < siz; ++i) {
170                         pit->setFont(i, font);
171                 }
172         }
173 #endif
174
175         if (!token_found) {
176                 if (isOpen())
177                         status(0, Open);
178                 else
179                         status(0, Collapsed);
180         }
181         setButtonLabel();
182 }
183
184
185 void InsetERT::write(Buffer const & buf, ostream & os) const
186 {
187         string st;
188
189         switch (status_) {
190         case Open:
191                 st = "Open";
192                 break;
193         case Collapsed:
194                 st = "Collapsed";
195                 break;
196         case Inlined:
197                 st = "Inlined";
198                 break;
199         }
200
201         os << getInsetName() << "\n"
202            << "status "<< st << "\n";
203
204         //inset.writeParagraphData(buf, os);
205         string const layout(buf.params().getLyXTextClass().defaultLayoutName());
206         ParagraphList::iterator par = inset.paragraphs.begin();
207         ParagraphList::iterator end = inset.paragraphs.end();
208         for (; par != end; ++par) {
209                 os << "\n\\begin_layout " << layout << "\n";
210                 pos_type siz = par->size();
211                 for (pos_type i = 0; i < siz; ++i) {
212                         Paragraph::value_type c = par->getChar(i);
213                         switch (c) {
214                         case Paragraph::META_INSET:
215                                 if (par->getInset(i)->lyxCode() != InsetOld::NEWLINE_CODE) {
216                                         lyxerr << "Element is not allowed in insertERT"
217                                                << endl;
218                                 } else {
219                                         par->getInset(i)->write(buf, os);
220                                 }
221                                 break;
222
223                         case '\\':
224                                 os << "\n\\backslash \n";
225                                 break;
226                         default:
227                                 os << c;
228                                 break;
229                         }
230                 }
231                 os << "\n\\end_layout\n";
232         }
233 }
234
235
236 string const InsetERT::editMessage() const
237 {
238         return _("Opened ERT Inset");
239 }
240
241
242 bool InsetERT::insertInset(BufferView *, InsetOld *)
243 {
244         return false;
245 }
246
247
248 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
249 {
250 #ifdef WITH_WARNINGS
251 #warning FIXME. More UI stupidity...
252 #endif
253         // if selectall is activated then the fontchange was an outside general
254         // fontchange and this messages is not needed
255         if (!selectall)
256                 Alert::error(_("Cannot change font"),
257                            _("You cannot change font settings inside TeX code."));
258 }
259
260
261 void InsetERT::updateStatus(BufferView * bv, bool swap) const
262 {
263         if (status_ != Inlined) {
264                 if (isOpen())
265                         status(bv, swap ? Collapsed : Open);
266                 else
267                         status(bv, swap ? Open : Collapsed);
268         }
269 }
270
271
272 InsetOld::EDITABLE InsetERT::editable() const
273 {
274         if (status_ == Collapsed)
275                 return IS_EDITABLE;
276         return HIGHLY_EDITABLE;
277 }
278
279
280 void InsetERT::lfunMousePress(FuncRequest const & cmd)
281 {
282         if (status_ == Inlined)
283                 inset.dispatch(cmd);
284         else {
285                 idx_type idx = 0;
286                 pos_type pos = 0;
287                 InsetCollapsable::priv_dispatch(cmd, idx, pos);
288         }
289 }
290
291
292 bool InsetERT::lfunMouseRelease(FuncRequest const & cmd)
293 {
294         BufferView * bv = cmd.view();
295
296         if (cmd.button() == mouse_button::button3) {
297                 showInsetDialog(bv);
298                 return true;
299         }
300
301         if (status_ != Inlined && hitButton(cmd)) {
302                 updateStatus(bv, true);
303         } else {
304                 FuncRequest cmd1 = cmd;
305 #warning metrics?
306                 cmd1.y = ascent() + cmd.y - inset.ascent();
307
308                 // inlined is special - the text appears above
309                 if (status_ == Inlined)
310                         inset.dispatch(cmd1);
311                 else if (isOpen() && (cmd.y > buttonDim().y2)) {
312                         cmd1.y -= height_collapsed();
313                         inset.dispatch(cmd1);
314                 }
315         }
316         return false;
317 }
318
319
320 void InsetERT::lfunMouseMotion(FuncRequest const & cmd)
321 {
322         if (status_ == Inlined)
323                 inset.dispatch(cmd);
324         else {
325                 idx_type idx = 0;
326                 pos_type pos = 0;
327                 InsetCollapsable::priv_dispatch(cmd, idx, pos);
328         }
329 }
330
331
332 int InsetERT::latex(Buffer const &, ostream & os,
333                     LatexRunParams const &) const
334 {
335         ParagraphList::iterator par = inset.paragraphs.begin();
336         ParagraphList::iterator end = inset.paragraphs.end();
337
338         int lines = 0;
339         while (par != end) {
340                 pos_type siz = par->size();
341                 for (pos_type i = 0; i < siz; ++i) {
342                         // ignore all struck out text
343                         if (isDeletedText(*par, i))
344                                 continue;
345
346                         if (par->isNewline(i)) {
347                                 os << '\n';
348                                 ++lines;
349                         } else {
350                                 os << par->getChar(i);
351                         }
352                 }
353                 ++par;
354                 if (par != end) {
355                         os << "\n";
356                         ++lines;
357                 }
358         }
359
360         return lines;
361 }
362
363
364 int InsetERT::ascii(Buffer const &, ostream &,
365                     LatexRunParams const & /*runparams*/) const
366 {
367         return 0;
368 }
369
370
371 int InsetERT::linuxdoc(Buffer const &, ostream & os,
372                        LatexRunParams const &)const
373 {
374         ParagraphList::iterator par = inset.paragraphs.begin();
375         ParagraphList::iterator end = inset.paragraphs.end();
376
377         int lines = 0;
378         while (par != end) {
379                 pos_type siz = par->size();
380                 for (pos_type i = 0; i < siz; ++i) {
381                         if (par->isNewline(i)) {
382                                 os << '\n';
383                                 ++lines;
384                         } else {
385                                 os << par->getChar(i);
386                         }
387                 }
388                 ++par;
389                 if (par != end) {
390                         os << "\n";
391                         lines ++;
392                 }
393         }
394
395         return lines;
396 }
397
398
399 int InsetERT::docbook(Buffer const &, ostream & os,
400                       LatexRunParams const &) const
401 {
402         ParagraphList::iterator par = inset.paragraphs.begin();
403         ParagraphList::iterator end = inset.paragraphs.end();
404
405         int lines = 0;
406         while (par != end) {
407                 pos_type siz = par->size();
408                 for (pos_type i = 0; i < siz; ++i) {
409                         if (par->isNewline(i)) {
410                                 os << '\n';
411                                 ++lines;
412                         } else {
413                                 os << par->getChar(i);
414                         }
415                 }
416                 ++par;
417                 if (par != end) {
418                         os << "\n";
419                         lines ++;
420                 }
421         }
422
423         return lines;
424 }
425
426
427 DispatchResult
428 InsetERT::priv_dispatch(FuncRequest const & cmd,
429                         idx_type & idx, pos_type & pos)
430 {
431         DispatchResult result = DispatchResult(false);
432         BufferView * bv = cmd.view();
433
434         if (inset.paragraphs.begin()->empty()) {
435                 set_latex_font(bv);
436         }
437
438         switch (cmd.action) {
439
440         case LFUN_INSET_EDIT:
441                 if (cmd.button() == mouse_button::button3)
442                         break;
443                 if (status_ == Inlined) {
444                         if (!bv->lockInset(this))
445                                 break;
446                         result = inset.dispatch(cmd);
447                 } else {
448                         // Is the following line correct? Ab
449                         open(bv);
450                         result = InsetCollapsable::priv_dispatch(cmd, idx, pos);
451                 }
452                 set_latex_font(bv);
453                 updateStatus(bv);
454                 break;
455
456         case LFUN_INSET_MODIFY: {
457                 InsetERT::ERTStatus status_;
458                 InsetERTMailer::string2params(cmd.argument, status_);
459
460                 status(bv, status_);
461
462                 /* FIXME: I refuse to believe we have to live
463                  * with ugliness like this ! Note that this
464                  * rebreak *is* needed. Consider a change from
465                  * Open (needfullrow) to Inlined (only the space
466                  * taken by the text).
467                  */
468                 inset.getLyXText(cmd.view())->fullRebreak();
469                 bv->updateInset(this);
470                 result = DispatchResult(true, true);
471         }
472         break;
473
474         case LFUN_MOUSE_PRESS:
475                 lfunMousePress(cmd);
476                 result = DispatchResult(true, true);
477                 break;
478
479         case LFUN_MOUSE_MOTION:
480                 lfunMouseMotion(cmd);
481                 result = DispatchResult(true, true);
482                 break;
483
484         case LFUN_MOUSE_RELEASE:
485                 lfunMouseRelease(cmd);
486                 result = DispatchResult(true, true);
487                 break;
488
489         case LFUN_LAYOUT:
490                 bv->owner()->setLayout(inset.paragraphs.begin()->layout()->name());
491                 result = DispatchResult(true);
492                 break;
493
494         default:
495                 result = InsetCollapsable::priv_dispatch(cmd, idx, pos);
496         }
497
498         switch (cmd.action) {
499         case LFUN_BREAKPARAGRAPH:
500         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
501         case LFUN_BACKSPACE:
502         case LFUN_BACKSPACE_SKIP:
503         case LFUN_DELETE:
504         case LFUN_DELETE_SKIP:
505         case LFUN_DELETE_LINE_FORWARD:
506         case LFUN_CUT:
507                 set_latex_font(bv);
508                 break;
509
510         default:
511                 break;
512         }
513         return result;
514 }
515
516
517 string const InsetERT::get_new_label() const
518 {
519         string la;
520         pos_type const max_length = 15;
521         pos_type const p_siz = inset.paragraphs.begin()->size();
522         pos_type const n = min(max_length, p_siz);
523         pos_type i = 0;
524         pos_type j = 0;
525         for(; i < n && j < p_siz; ++j) {
526                 if (inset.paragraphs.begin()->isInset(j))
527                         continue;
528                 la += inset.paragraphs.begin()->getChar(j);
529                 ++i;
530         }
531         if (inset.paragraphs.size() > 1 || (i > 0 && j < p_siz)) {
532                 la += "...";
533         }
534         if (la.empty()) {
535                 la = _("ERT");
536         }
537         return la;
538 }
539
540
541 void InsetERT::setButtonLabel() const
542 {
543         if (status_ == Collapsed) {
544                 setLabel(get_new_label());
545         } else {
546                 setLabel(_("ERT"));
547         }
548 }
549
550
551 bool InsetERT::checkInsertChar(LyXFont & /* font */)
552 {
553 #ifdef SET_HARD_FONT
554         LyXFont f(LyXFont::ALL_INHERIT, latex_language);
555         font = f;
556         font.setFamily(LyXFont::TYPEWRITER_FAMILY);
557         font.setColor(LColor::latex);
558 #endif
559         return true;
560 }
561
562
563 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
564 {
565         setButtonLabel();
566         if (inlined())
567                 inset.metrics(mi, dim);
568         else
569                 InsetCollapsable::metrics(mi, dim);
570         // Make it stand out on its own as it is code, not part of running
571         // text:
572         if (isOpen() && !inlined())
573                 dim.wid = mi.base.textwidth;
574         dim_ = dim;
575 }
576
577
578 void InsetERT::draw(PainterInfo & pi, int x, int y) const
579 {
580         InsetCollapsable::draw(pi, x, y, inlined());
581 }
582
583
584 void InsetERT::set_latex_font(BufferView * /* bv */)
585 {
586 #ifdef SET_HARD_FONT
587         LyXFont font(LyXFont::ALL_INHERIT, latex_language);
588
589         font.setFamily(LyXFont::TYPEWRITER_FAMILY);
590         font.setColor(LColor::latex);
591
592         inset.getLyXText(bv)->setFont(bv, font, false);
593 #endif
594 }
595
596
597 // attention this function can be called with bv == 0
598 void InsetERT::status(BufferView * bv, ERTStatus const st) const
599 {
600         if (st != status_) {
601                 status_ = st;
602                 switch (st) {
603                 case Inlined:
604                         break;
605                 case Open:
606                         setCollapsed(false);
607                         setButtonLabel();
608                         break;
609                 case Collapsed:
610                         setCollapsed(true);
611                         setButtonLabel();
612                         if (bv)
613                                 bv->unlockInset(const_cast<InsetERT *>(this));
614                         break;
615                 }
616                 if (bv) {
617                         bv->updateInset(this);
618                         bv->buffer()->markDirty();
619                 }
620         }
621 }
622
623
624 bool InsetERT::showInsetDialog(BufferView * bv) const
625 {
626         InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
627         return true;
628 }
629
630
631 void InsetERT::open(BufferView * bv)
632 {
633         if (isOpen())
634                 return;
635         status(bv, Open);
636 }
637
638
639 void InsetERT::close(BufferView * bv) const
640 {
641         if (status_ == Collapsed || status_ == Inlined)
642                 return;
643
644         status(bv, Collapsed);
645 }
646
647
648 void InsetERT::getDrawFont(LyXFont & font) const
649 {
650         LyXFont f(LyXFont::ALL_INHERIT, latex_language);
651         font = f;
652         font.setFamily(LyXFont::TYPEWRITER_FAMILY);
653         font.setColor(LColor::latex);
654 }
655
656
657 string const InsetERTMailer::name_("ert");
658
659 InsetERTMailer::InsetERTMailer(InsetERT & inset)
660         : inset_(inset)
661 {}
662
663
664 string const InsetERTMailer::inset2string(Buffer const &) const
665 {
666         return params2string(inset_.status());
667 }
668
669
670 void InsetERTMailer::string2params(string const & in,
671                                    InsetERT::ERTStatus & status)
672 {
673         status = InsetERT::Collapsed;
674
675         string name;
676         string body = split(in, name, ' ');
677
678         if (body.empty())
679                 return;
680
681         status = static_cast<InsetERT::ERTStatus>(strToInt(body));
682 }
683
684
685 string const
686 InsetERTMailer::params2string(InsetERT::ERTStatus status)
687 {
688         return name_ + ' ' + tostr(status);
689 }