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