]> git.lyx.org Git - lyx.git/blob - src/insets/insetert.C
7d51c0a0ea3fcdcb5f443bd2dcb53a5ed96fb6d1
[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                     OutputParams 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::plaintext(Buffer const &, ostream &,
365                     OutputParams const & /*runparams*/) const
366 {
367         return 0;
368 }
369
370
371 int InsetERT::linuxdoc(Buffer const &, ostream & os,
372                        OutputParams 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                       OutputParams 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 void InsetERT::edit(BufferView * bv, bool left)
428 {
429         if (status_ == Inlined) {
430                 if (!bv->lockInset(this))
431                         return;
432                 inset.edit(bv, left);
433         } else {
434                 InsetCollapsable::edit(bv, left);
435         }
436         set_latex_font(bv);
437         updateStatus(bv);
438 }
439
440
441 DispatchResult
442 InsetERT::priv_dispatch(FuncRequest const & cmd,
443                         idx_type & idx, pos_type & pos)
444 {
445         BufferView * bv = cmd.view();
446
447         if (inset.paragraphs.begin()->empty())
448                 set_latex_font(bv);
449
450         switch (cmd.action) {
451
452         case LFUN_INSET_MODIFY: {
453                 InsetERT::ERTStatus status_;
454                 InsetERTMailer::string2params(cmd.argument, status_);
455
456                 status(bv, status_);
457
458                 /* FIXME: I refuse to believe we have to live
459                  * with ugliness like this ! Note that this
460                  * rebreak *is* needed. Consider a change from
461                  * Open (needfullrow) to Inlined (only the space
462                  * taken by the text).
463                  */
464                 inset.getLyXText(cmd.view())->fullRebreak();
465                 bv->updateInset(this);
466                 return DispatchResult(true, true);
467         }
468
469         case LFUN_MOUSE_PRESS:
470                 lfunMousePress(cmd);
471                 return DispatchResult(true, true);
472
473         case LFUN_MOUSE_MOTION:
474                 lfunMouseMotion(cmd);
475                 return DispatchResult(true, true);
476
477         case LFUN_MOUSE_RELEASE:
478                 lfunMouseRelease(cmd);
479                 return DispatchResult(true, true);
480
481         case LFUN_LAYOUT:
482                 bv->owner()->setLayout(inset.paragraphs.begin()->layout()->name());
483                 return DispatchResult(true);
484
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                 return DispatchResult(false);
495
496         default:
497                 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
498         }
499 }
500
501
502 string const InsetERT::get_new_label() const
503 {
504         string la;
505         pos_type const max_length = 15;
506         pos_type const p_siz = inset.paragraphs.begin()->size();
507         pos_type const n = min(max_length, p_siz);
508         pos_type i = 0;
509         pos_type j = 0;
510         for(; i < n && j < p_siz; ++j) {
511                 if (inset.paragraphs.begin()->isInset(j))
512                         continue;
513                 la += inset.paragraphs.begin()->getChar(j);
514                 ++i;
515         }
516         if (inset.paragraphs.size() > 1 || (i > 0 && j < p_siz)) {
517                 la += "...";
518         }
519         if (la.empty()) {
520                 la = _("ERT");
521         }
522         return la;
523 }
524
525
526 void InsetERT::setButtonLabel() const
527 {
528         if (status_ == Collapsed) {
529                 setLabel(get_new_label());
530         } else {
531                 setLabel(_("ERT"));
532         }
533 }
534
535
536 bool InsetERT::checkInsertChar(LyXFont & /* font */)
537 {
538 #ifdef SET_HARD_FONT
539         LyXFont f(LyXFont::ALL_INHERIT, latex_language);
540         font = f;
541         font.setFamily(LyXFont::TYPEWRITER_FAMILY);
542         font.setColor(LColor::latex);
543 #endif
544         return true;
545 }
546
547
548 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
549 {
550         setButtonLabel();
551         if (inlined())
552                 inset.metrics(mi, dim);
553         else
554                 InsetCollapsable::metrics(mi, dim);
555         // Make it stand out on its own as it is code, not part of running
556         // text:
557         if (isOpen() && !inlined())
558                 dim.wid = mi.base.textwidth;
559         dim_ = dim;
560 }
561
562
563 void InsetERT::draw(PainterInfo & pi, int x, int y) const
564 {
565         InsetCollapsable::draw(pi, x, y, inlined());
566 }
567
568
569 void InsetERT::set_latex_font(BufferView * /* bv */)
570 {
571 #ifdef SET_HARD_FONT
572         LyXFont font(LyXFont::ALL_INHERIT, latex_language);
573
574         font.setFamily(LyXFont::TYPEWRITER_FAMILY);
575         font.setColor(LColor::latex);
576
577         inset.getLyXText(bv)->setFont(bv, font, false);
578 #endif
579 }
580
581
582 // attention this function can be called with bv == 0
583 void InsetERT::status(BufferView * bv, ERTStatus const st) const
584 {
585         if (st != status_) {
586                 status_ = st;
587                 switch (st) {
588                 case Inlined:
589                         break;
590                 case Open:
591                         setCollapsed(false);
592                         setButtonLabel();
593                         break;
594                 case Collapsed:
595                         setCollapsed(true);
596                         setButtonLabel();
597                         if (bv)
598                                 bv->unlockInset(const_cast<InsetERT *>(this));
599                         break;
600                 }
601                 if (bv) {
602                         bv->updateInset(this);
603                         bv->buffer()->markDirty();
604                 }
605         }
606 }
607
608
609 bool InsetERT::showInsetDialog(BufferView * bv) const
610 {
611         InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
612         return true;
613 }
614
615
616 void InsetERT::open(BufferView * bv)
617 {
618         if (isOpen())
619                 return;
620         status(bv, Open);
621 }
622
623
624 void InsetERT::close(BufferView * bv) const
625 {
626         if (status_ == Collapsed || status_ == Inlined)
627                 return;
628
629         status(bv, Collapsed);
630 }
631
632
633 void InsetERT::getDrawFont(LyXFont & font) const
634 {
635         LyXFont f(LyXFont::ALL_INHERIT, latex_language);
636         font = f;
637         font.setFamily(LyXFont::TYPEWRITER_FAMILY);
638         font.setColor(LColor::latex);
639 }
640
641
642 string const InsetERTMailer::name_("ert");
643
644 InsetERTMailer::InsetERTMailer(InsetERT & inset)
645         : inset_(inset)
646 {}
647
648
649 string const InsetERTMailer::inset2string(Buffer const &) const
650 {
651         return params2string(inset_.status());
652 }
653
654
655 void InsetERTMailer::string2params(string const & in,
656                                    InsetERT::ERTStatus & status)
657 {
658         status = InsetERT::Collapsed;
659
660         string name;
661         string body = split(in, name, ' ');
662
663         if (body.empty())
664                 return;
665
666         status = static_cast<InsetERT::ERTStatus>(strToInt(body));
667 }
668
669
670 string const
671 InsetERTMailer::params2string(InsetERT::ERTStatus status)
672 {
673         return name_ + ' ' + tostr(status);
674 }