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