]> git.lyx.org Git - lyx.git/blob - src/insets/insetert.C
My patch from yesterday
[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         // if selectall is activated then the fontchange was an outside general
247         // fontchange and this messages is not needed
248         if (!selectall)
249                 Alert::alert(_("Impossible operation!"),
250                            _("Not permitted to change font-types inside ERT-insets!"),
251                            _("Sorry."));
252 }
253
254
255 void InsetERT::updateStatus(BufferView * bv, bool swap) const
256 {
257         if (status_ != Inlined) {
258                 if (collapsed_) {
259                         status(bv, swap ? Open : Collapsed);
260                 } else {
261                         status(bv, swap ? Collapsed : Open);
262                 }
263         }
264 }
265
266 void InsetERT::edit(BufferView * bv, int x, int y, mouse_button::state button)
267 {
268         if (button == mouse_button::button3)
269                 return;
270
271         if (status_ == Inlined) {
272                 if (!bv->lockInset(this))
273                         return;
274                 inset.edit(bv, x, y, button);
275         } else {
276                 InsetCollapsable::edit(bv, x, y, button);
277         }
278         set_latex_font(bv);
279         updateStatus(bv);
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::edit(BufferView * bv, bool front)
292 {
293         InsetCollapsable::edit(bv, front);
294         updateStatus(0);
295         set_latex_font(bv);
296 }
297
298
299 void InsetERT::lfunMousePress(FuncRequest const & cmd)
300 {
301         if (status_ == Inlined)
302                 inset.localDispatch(cmd);
303         else
304                 InsetCollapsable::localDispatch(cmd);
305 }
306
307
308 bool InsetERT::lfunMouseRelease(FuncRequest const & cmd)
309 {
310         BufferView * bv = cmd.view();
311
312         if (cmd.button() == mouse_button::button3) {
313                 showInsetDialog(bv);
314                 return true;
315         }
316
317         if (status_ != Inlined && (cmd.x >= 0) && (cmd.x < button_length) &&
318             (cmd.y >= button_top_y) && (cmd.y <= button_bottom_y)) {
319                 updateStatus(bv, true);
320         } else {
321                 LyXFont font(LyXFont::ALL_SANE);
322                 FuncRequest cmd1 = cmd;
323                 cmd1.y = ascent(bv, font) + cmd.y - inset.ascent(bv, font);
324
325                 // inlined is special - the text appears above
326                 // button_bottom_y
327                 if (status_ == Inlined)
328                         inset.localDispatch(cmd1);
329                 else if (!collapsed_ && (cmd.y > button_bottom_y)) {
330                         cmd1.y -= ascent_collapsed() + descent_collapsed();
331                         inset.localDispatch(cmd1);
332                 }
333         }
334         return false;
335 }
336
337
338 void InsetERT::lfunMouseMotion(FuncRequest const & cmd)
339 {
340         if (status_ == Inlined)
341                 inset.localDispatch(cmd);
342         else
343                 InsetCollapsable::localDispatch(cmd);
344 }
345
346
347 int InsetERT::latex(Buffer const *, ostream & os, bool /*fragile*/,
348                     bool /*free_spc*/) const
349 {
350         Paragraph * par = inset.paragraph();
351         int lines = 0;
352         while (par) {
353                 pos_type siz = par->size();
354                 for (pos_type i = 0; i < siz; ++i) {
355                         // ignore all struck out text
356                         if (isDeletedText(*par, i))
357                                 continue;
358
359                         if (par->isNewline(i)) {
360                                 os << '\n';
361                                 ++lines;
362                         } else {
363                                 os << par->getChar(i);
364                         }
365                 }
366                 par = par->next();
367                 if (par) {
368                         os << "\n\n";
369                         lines += 2;
370                 }
371         }
372
373         return lines;
374 }
375
376
377 int InsetERT::ascii(Buffer const *, ostream &, int /*linelen*/) const
378 {
379         return 0;
380 }
381
382
383 int InsetERT::linuxdoc(Buffer const *, ostream & os) const
384 {
385         Paragraph * par = inset.paragraph();
386         int lines = 0;
387         while (par) {
388                 pos_type siz = par->size();
389                 for (pos_type i = 0; i < siz; ++i) {
390                         if (par->isNewline(i)) {
391                                 os << '\n';
392                                 ++lines;
393                         } else {
394                                 os << par->getChar(i);
395                         }
396                 }
397                 par = par->next();
398                 if (par) {
399                         os << "\n";
400                         lines ++;
401                 }
402         }
403
404         return lines;
405 }
406
407
408 int InsetERT::docbook(Buffer const *, ostream & os, bool) const
409 {
410         Paragraph * par = inset.paragraph();
411         int lines = 0;
412         while (par) {
413                 pos_type siz = par->size();
414                 for (pos_type i = 0; i < siz; ++i) {
415                         if (par->isNewline(i)) {
416                                 os << '\n';
417                                 ++lines;
418                         } else {
419                                 os << par->getChar(i);
420                         }
421                 }
422                 par = par->next();
423                 if (par) {
424                         os << "\n";
425                         lines ++;
426                 }
427         }
428
429         return lines;
430 }
431
432
433 Inset::RESULT InsetERT::localDispatch(FuncRequest const & cmd)
434 {
435         Inset::RESULT result = UNDISPATCHED;
436         BufferView * bv = cmd.view();
437
438         if (inset.paragraph()->empty()) {
439                 set_latex_font(bv);
440         }
441
442         switch (cmd.action) {
443         case LFUN_INSET_MODIFY: {
444                 InsetERT::ERTStatus status_;
445                 InsetERTMailer::string2params(cmd.argument, status_);
446
447                 status(bv, status_);
448                 // FIXME: how on holy earth do you actually get
449                 // this thing to reinit the bloody insettext
450                 // and change the size of this inset !!?!
451                 bv->updateInset(this);
452                 result = DISPATCHED;
453         }
454         break;
455
456         case LFUN_MOUSE_PRESS:
457                 lfunMousePress(cmd);
458                 result = DISPATCHED;
459                 break;
460
461         case LFUN_MOUSE_MOTION:
462                 lfunMouseMotion(cmd);
463                 result = DISPATCHED;
464                 break;
465
466         case LFUN_MOUSE_RELEASE:
467                 lfunMouseRelease(cmd);
468                 result = DISPATCHED;
469                 break;
470
471         case LFUN_LAYOUT:
472                 bv->owner()->setLayout(inset.paragraph()->layout()->name());
473                 result = DISPATCHED_NOUPDATE;
474                 break;
475
476         default:
477                 result = InsetCollapsable::localDispatch(cmd);
478         }
479
480         switch (cmd.action) {
481         case LFUN_BREAKPARAGRAPH:
482         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
483         case LFUN_BACKSPACE:
484         case LFUN_BACKSPACE_SKIP:
485         case LFUN_DELETE:
486         case LFUN_DELETE_SKIP:
487         case LFUN_DELETE_LINE_FORWARD:
488         case LFUN_CUT:
489                 set_latex_font(bv);
490                 break;
491
492         default:
493                 break;
494         }
495         return result;
496 }
497
498
499 string const InsetERT::get_new_label() const
500 {
501         string la;
502         pos_type const max_length = 15;
503         pos_type const p_siz = inset.paragraph()->size();
504         pos_type const n = min(max_length, p_siz);
505         pos_type i = 0;
506         pos_type j = 0;
507         for(; i < n && j < p_siz; ++j) {
508                 if (inset.paragraph()->isInset(j))
509                         continue;
510                 la += inset.paragraph()->getChar(j);
511                 ++i;
512         }
513         if (inset.paragraph()->next() || (i > 0 && j < p_siz)) {
514                 la += "...";
515         }
516         if (la.empty()) {
517                 la = _("ERT");
518         }
519         return la;
520 }
521
522
523 void InsetERT::setButtonLabel() const
524 {
525         if (status_ == Collapsed) {
526                 setLabel(get_new_label());
527         } else {
528                 setLabel(_("ERT"));
529         }
530 }
531
532
533 bool InsetERT::checkInsertChar(LyXFont & /* font */)
534 {
535 #ifdef SET_HARD_FONT
536         LyXFont f(LyXFont::ALL_INHERIT, latex_language);
537         font = f;
538         font.setFamily(LyXFont::TYPEWRITER_FAMILY);
539         font.setColor(LColor::latex);
540 #endif
541         return true;
542 }
543
544
545 int InsetERT::ascent(BufferView * bv, LyXFont const & font) const
546 {
547         if (!inlined())
548                 return InsetCollapsable::ascent(bv, font);
549
550         return inset.ascent(bv, font);
551 }
552
553
554 int InsetERT::descent(BufferView * bv, LyXFont const & font) const
555 {
556         if (!inlined())
557                 return InsetCollapsable::descent(bv, font);
558
559         return inset.descent(bv, font);
560 }
561
562
563 int InsetERT::width(BufferView * bv, LyXFont const & font) const
564 {
565         if (!inlined())
566                 return InsetCollapsable::width(bv, font);
567
568         return inset.width(bv, font);
569 }
570
571
572 void InsetERT::draw(BufferView * bv, LyXFont const & f,
573                     int baseline, float & x) const
574 {
575         InsetCollapsable::draw(bv, f, baseline, x, inlined());
576 }
577
578
579 void InsetERT::set_latex_font(BufferView * /* bv */)
580 {
581 #ifdef SET_HARD_FONT
582         LyXFont font(LyXFont::ALL_INHERIT, latex_language);
583
584         font.setFamily(LyXFont::TYPEWRITER_FAMILY);
585         font.setColor(LColor::latex);
586
587         inset.getLyXText(bv)->setFont(bv, font, false);
588 #endif
589 }
590
591
592 // attention this function can be called with bv == 0
593 void InsetERT::status(BufferView * bv, ERTStatus const st) const
594 {
595         if (st != status_) {
596                 status_ = st;
597                 switch (st) {
598                 case Inlined:
599                         if (bv)
600                                 inset.setUpdateStatus(bv, InsetText::INIT);
601                         break;
602                 case Open:
603                         collapsed_ = false;
604                         setButtonLabel();
605                         break;
606                 case Collapsed:
607                         collapsed_ = true;
608                         setButtonLabel();
609                         if (bv)
610                                 bv->unlockInset(const_cast<InsetERT *>(this));
611                         break;
612                 }
613                 if (bv) {
614                         bv->updateInset(const_cast<InsetERT *>(this));
615                         bv->buffer()->markDirty();
616                 }
617         }
618 }
619
620
621 bool InsetERT::showInsetDialog(BufferView * bv) const
622 {
623         InsetERTMailer mailer(const_cast<InsetERT &>(*this));
624         mailer.showDialog(bv);
625         return true;
626 }
627
628
629 void InsetERT::open(BufferView * bv)
630 {
631         if (!collapsed_)
632                 return;
633         status(bv, Open);
634 }
635
636
637 void InsetERT::close(BufferView * bv) const
638 {
639         if (status_ == Collapsed || status_ == Inlined)
640                 return;
641
642         status(bv, Collapsed);
643 }
644
645
646 WordLangTuple const
647 InsetERT::selectNextWordToSpellcheck(BufferView * bv, float &) const
648 {
649         bv->unlockInset(const_cast<InsetERT *>(this));
650         return WordLangTuple();
651 }
652
653
654 void InsetERT::getDrawFont(LyXFont & font) const
655 {
656         LyXFont f(LyXFont::ALL_INHERIT, latex_language);
657         font = f;
658         font.setFamily(LyXFont::TYPEWRITER_FAMILY);
659         font.setColor(LColor::latex);
660 }
661
662
663 int InsetERT::getMaxWidth(BufferView * bv, UpdatableInset const * in) const
664 {
665         int w = InsetCollapsable::getMaxWidth(bv, in);
666         if (status_ != Inlined || w < 0)
667                 return w;
668         LyXText * text = inset.getLyXText(bv);
669         int rw = text->firstRow()->width();
670         if (!rw)
671                 rw = w;
672         rw += 40;
673         if (!text->firstRow()->next() && rw < w)
674                 return -1;
675         return w;
676 }
677
678
679 void InsetERT::update(BufferView * bv, bool reinit)
680 {
681         if (inset.need_update & InsetText::INIT ||
682             inset.need_update & InsetText::FULL) {
683                 setButtonLabel();
684         }
685
686         InsetCollapsable::update(bv, reinit);
687 }
688
689
690 string const InsetERTMailer::name_("ert");
691
692 InsetERTMailer::InsetERTMailer(InsetERT & inset)
693         : inset_(inset)
694 {}
695
696
697 string const InsetERTMailer::inset2string() const
698 {
699         return params2string(inset_.status());
700 }
701
702
703 void InsetERTMailer::string2params(string const & in,
704                                    InsetERT::ERTStatus & status)
705 {
706         status = InsetERT::Collapsed;
707
708         string name;
709         string body = split(in, name, ' ');
710
711         if (body.empty())
712                 return;
713
714         status = static_cast<InsetERT::ERTStatus>(strToInt(body));
715 }
716
717
718 string const
719 InsetERTMailer::params2string(InsetERT::ERTStatus status)
720 {
721         return name_ + ' ' + tostr(status);
722 }