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