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