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