]> git.lyx.org Git - lyx.git/blob - src/insets/insetert.C
326f75c52e3fd5cf36a2a8c6c172f5cef5db38e2
[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 &, int /*linelen*/) const
366 {
367         return 0;
368 }
369
370
371 int InsetERT::linuxdoc(Buffer const &, ostream & os) const
372 {
373         ParagraphList::iterator par = inset.paragraphs.begin();
374         ParagraphList::iterator end = inset.paragraphs.end();
375
376         int lines = 0;
377         while (par != end) {
378                 pos_type siz = par->size();
379                 for (pos_type i = 0; i < siz; ++i) {
380                         if (par->isNewline(i)) {
381                                 os << '\n';
382                                 ++lines;
383                         } else {
384                                 os << par->getChar(i);
385                         }
386                 }
387                 ++par;
388                 if (par != end) {
389                         os << "\n";
390                         lines ++;
391                 }
392         }
393
394         return lines;
395 }
396
397
398 int InsetERT::docbook(Buffer const &, ostream & os, bool) const
399 {
400         ParagraphList::iterator par = inset.paragraphs.begin();
401         ParagraphList::iterator end = inset.paragraphs.end();
402
403         int lines = 0;
404         while (par != end) {
405                 pos_type siz = par->size();
406                 for (pos_type i = 0; i < siz; ++i) {
407                         if (par->isNewline(i)) {
408                                 os << '\n';
409                                 ++lines;
410                         } else {
411                                 os << par->getChar(i);
412                         }
413                 }
414                 ++par;
415                 if (par != end) {
416                         os << "\n";
417                         lines ++;
418                 }
419         }
420
421         return lines;
422 }
423
424
425 DispatchResult
426 InsetERT::priv_dispatch(FuncRequest const & cmd,
427                         idx_type & idx, pos_type & pos)
428 {
429         DispatchResult result = UNDISPATCHED;
430         BufferView * bv = cmd.view();
431
432         if (inset.paragraphs.begin()->empty()) {
433                 set_latex_font(bv);
434         }
435
436         switch (cmd.action) {
437
438         case LFUN_INSET_EDIT:
439                 if (cmd.button() == mouse_button::button3)
440                         break;
441                 if (status_ == Inlined) {
442                         if (!bv->lockInset(this))
443                                 break;
444                         result = inset.dispatch(cmd);
445                 } else {
446                         result = InsetCollapsable::priv_dispatch(cmd, idx, pos);
447                 }
448                 set_latex_font(bv);
449                 updateStatus(bv);
450                 break;
451
452         case LFUN_INSET_MODIFY: {
453                 InsetERT::ERTStatus status_;
454                 InsetERTMailer::string2params(cmd.argument, status_);
455
456                 status(bv, status_);
457
458                 /* FIXME: I refuse to believe we have to live
459                  * with ugliness like this ! Note that this
460                  * rebreak *is* needed. Consider a change from
461                  * Open (needfullrow) to Inlined (only the space
462                  * taken by the text).
463                  */
464                 inset.getLyXText(cmd.view())->fullRebreak();
465                 bv->updateInset(this);
466                 result = DISPATCHED;
467         }
468         break;
469
470         case LFUN_MOUSE_PRESS:
471                 lfunMousePress(cmd);
472                 result = DISPATCHED;
473                 break;
474
475         case LFUN_MOUSE_MOTION:
476                 lfunMouseMotion(cmd);
477                 result = DISPATCHED;
478                 break;
479
480         case LFUN_MOUSE_RELEASE:
481                 lfunMouseRelease(cmd);
482                 result = DISPATCHED;
483                 break;
484
485         case LFUN_LAYOUT:
486                 bv->owner()->setLayout(inset.paragraphs.begin()->layout()->name());
487                 result = DISPATCHED_NOUPDATE;
488                 break;
489
490         default:
491                 result = InsetCollapsable::priv_dispatch(cmd, idx, pos);
492         }
493
494         switch (cmd.action) {
495         case LFUN_BREAKPARAGRAPH:
496         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
497         case LFUN_BACKSPACE:
498         case LFUN_BACKSPACE_SKIP:
499         case LFUN_DELETE:
500         case LFUN_DELETE_SKIP:
501         case LFUN_DELETE_LINE_FORWARD:
502         case LFUN_CUT:
503                 set_latex_font(bv);
504                 break;
505
506         default:
507                 break;
508         }
509         return result;
510 }
511
512
513 string const InsetERT::get_new_label() const
514 {
515         string la;
516         pos_type const max_length = 15;
517         pos_type const p_siz = inset.paragraphs.begin()->size();
518         pos_type const n = min(max_length, p_siz);
519         pos_type i = 0;
520         pos_type j = 0;
521         for(; i < n && j < p_siz; ++j) {
522                 if (inset.paragraphs.begin()->isInset(j))
523                         continue;
524                 la += inset.paragraphs.begin()->getChar(j);
525                 ++i;
526         }
527         if (inset.paragraphs.size() > 1 || (i > 0 && j < p_siz)) {
528                 la += "...";
529         }
530         if (la.empty()) {
531                 la = _("ERT");
532         }
533         return la;
534 }
535
536
537 void InsetERT::setButtonLabel() const
538 {
539         if (status_ == Collapsed) {
540                 setLabel(get_new_label());
541         } else {
542                 setLabel(_("ERT"));
543         }
544 }
545
546
547 bool InsetERT::checkInsertChar(LyXFont & /* font */)
548 {
549 #ifdef SET_HARD_FONT
550         LyXFont f(LyXFont::ALL_INHERIT, latex_language);
551         font = f;
552         font.setFamily(LyXFont::TYPEWRITER_FAMILY);
553         font.setColor(LColor::latex);
554 #endif
555         return true;
556 }
557
558
559 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
560 {
561         setButtonLabel();
562         if (inlined())
563                 inset.metrics(mi, dim);
564         else
565                 InsetCollapsable::metrics(mi, dim);
566         // Make it stand out on its own as it is code, not part of running
567         // text:
568         if (isOpen() && !inlined())
569                 dim.wid = mi.base.textwidth;
570         dim_ = dim;
571 }
572
573
574 void InsetERT::draw(PainterInfo & pi, int x, int y) const
575 {
576         InsetCollapsable::draw(pi, x, y, inlined());
577 }
578
579
580 void InsetERT::set_latex_font(BufferView * /* bv */)
581 {
582 #ifdef SET_HARD_FONT
583         LyXFont font(LyXFont::ALL_INHERIT, latex_language);
584
585         font.setFamily(LyXFont::TYPEWRITER_FAMILY);
586         font.setColor(LColor::latex);
587
588         inset.getLyXText(bv)->setFont(bv, font, false);
589 #endif
590 }
591
592
593 // attention this function can be called with bv == 0
594 void InsetERT::status(BufferView * bv, ERTStatus const st) const
595 {
596         if (st != status_) {
597                 status_ = st;
598                 switch (st) {
599                 case Inlined:
600                         break;
601                 case Open:
602                         setCollapsed(false);
603                         setButtonLabel();
604                         break;
605                 case Collapsed:
606                         setCollapsed(true);
607                         setButtonLabel();
608                         if (bv)
609                                 bv->unlockInset(const_cast<InsetERT *>(this));
610                         break;
611                 }
612                 if (bv) {
613                         bv->updateInset(this);
614                         bv->buffer()->markDirty();
615                 }
616         }
617 }
618
619
620 bool InsetERT::showInsetDialog(BufferView * bv) const
621 {
622         InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
623         return true;
624 }
625
626
627 void InsetERT::open(BufferView * bv)
628 {
629         if (isOpen())
630                 return;
631         status(bv, Open);
632 }
633
634
635 void InsetERT::close(BufferView * bv) const
636 {
637         if (status_ == Collapsed || status_ == Inlined)
638                 return;
639
640         status(bv, Collapsed);
641 }
642
643
644 WordLangTuple const
645 InsetERT::selectNextWordToSpellcheck(BufferView * bv, float &) const
646 {
647         bv->unlockInset(const_cast<InsetERT *>(this));
648         return WordLangTuple();
649 }
650
651
652 void InsetERT::getDrawFont(LyXFont & font) const
653 {
654         LyXFont f(LyXFont::ALL_INHERIT, latex_language);
655         font = f;
656         font.setFamily(LyXFont::TYPEWRITER_FAMILY);
657         font.setColor(LColor::latex);
658 }
659
660
661 string const InsetERTMailer::name_("ert");
662
663 InsetERTMailer::InsetERTMailer(InsetERT & inset)
664         : inset_(inset)
665 {}
666
667
668 string const InsetERTMailer::inset2string(Buffer const &) const
669 {
670         return params2string(inset_.status());
671 }
672
673
674 void InsetERTMailer::string2params(string const & in,
675                                    InsetERT::ERTStatus & status)
676 {
677         status = InsetERT::Collapsed;
678
679         string name;
680         string body = split(in, name, ' ');
681
682         if (body.empty())
683                 return;
684
685         status = static_cast<InsetERT::ERTStatus>(strToInt(body));
686 }
687
688
689 string const
690 InsetERTMailer::params2string(InsetERT::ERTStatus status)
691 {
692         return name_ + ' ' + tostr(status);
693 }