3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
9 * Full author contact details are available in file CREDITS
14 #include "insettext.h"
17 #include "BufferView.h"
19 #include "funcrequest.h"
26 #include "WordLangTuple.h"
28 #include "frontends/Alert.h"
29 #include "frontends/Dialogs.h"
30 #include "frontends/LyXView.h"
32 #include "support/LOstream.h"
33 #include "support/LAssert.h"
34 #include "support/lstrings.h"
47 labelfont = LyXFont(LyXFont::ALL_SANE);
50 labelfont.setColor(LColor::latex);
55 InsetERT::InsetERT(BufferParams const & bp, bool collapsed)
56 : InsetCollapsable(bp, collapsed)
66 InsetERT::InsetERT(InsetERT const & in, bool same_id)
67 : InsetCollapsable(in, same_id), status_(in.status_)
73 Inset * InsetERT::clone(Buffer const &, bool same_id) const
75 return new InsetERT(*const_cast<InsetERT *>(this), same_id);
79 InsetERT::InsetERT(BufferParams const & bp,
80 Language const * l, string const & contents, bool collapsed)
81 : InsetCollapsable(bp, collapsed)
88 LyXFont font(LyXFont::ALL_INHERIT, l);
90 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
91 font.setColor(LColor::latex);
94 string::const_iterator cit = contents.begin();
95 string::const_iterator end = contents.end();
97 for (; cit != end; ++cit) {
98 inset.paragraphs.begin()->insertChar(pos++, *cit, font);
100 // the init has to be after the initialization of the paragraph
101 // because of the label settings (draw_label for ert insets).
106 InsetERT::~InsetERT()
108 InsetERTMailer mailer(*this);
113 void InsetERT::read(Buffer const * buf, LyXLex & lex)
115 bool token_found = false;
118 string const token = lex.getString();
119 if (token == "status") {
121 string const tmp_token = lex.getString();
123 if (tmp_token == "Inlined") {
125 } else if (tmp_token == "Collapsed") {
126 status(0, Collapsed);
128 // leave this as default!
134 lyxerr << "InsetERT::Read: Missing 'status'-tag!"
136 // take countermeasures
137 lex.pushToken(token);
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)
145 string const token = lex.getString();
146 if (token == "collapsed") {
148 collapsed_ = lex.getBool();
150 // Take countermeasures
151 lex.pushToken(token);
155 inset.read(buf, lex);
158 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
159 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
160 font.setColor(LColor::latex);
161 Paragraph * par = inset.paragraph();
163 pos_type siz = par->size();
164 for (pos_type i = 0; i < siz; ++i) {
165 par->setFont(i, font);
173 status(0, Collapsed);
182 void InsetERT::write(Buffer const * buf, ostream & os) const
198 os << getInsetName() << "\n"
199 << "status "<< st << "\n";
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\\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);
211 case Paragraph::META_INSET:
212 if (par->getInset(i)->lyxCode() != Inset::NEWLINE_CODE) {
213 lyxerr << "Element is not allowed in insertERT"
216 par->getInset(i)->write(buf, os);
221 os << "\n\\backslash \n";
232 string const InsetERT::editMessage() const
234 return _("Opened ERT Inset");
238 bool InsetERT::insertInset(BufferView *, Inset *)
244 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
247 #warning FIXME. More UI stupidity...
249 // if selectall is activated then the fontchange was an outside general
250 // fontchange and this messages is not needed
252 Alert::error(_("Cannot change font"),
253 _("You cannot change font settings inside TeX code."));
257 void InsetERT::updateStatus(BufferView * bv, bool swap) const
259 if (status_ != Inlined) {
261 status(bv, swap ? Open : Collapsed);
263 status(bv, swap ? Collapsed : Open);
268 void InsetERT::edit(BufferView * bv, int x, int y, mouse_button::state button)
270 if (button == mouse_button::button3)
273 if (status_ == Inlined) {
274 if (!bv->lockInset(this))
276 inset.edit(bv, x, y, button);
278 InsetCollapsable::edit(bv, x, y, button);
285 Inset::EDITABLE InsetERT::editable() const
287 if (status_ == Collapsed)
289 return HIGHLY_EDITABLE;
293 void InsetERT::edit(BufferView * bv, bool front)
295 InsetCollapsable::edit(bv, front);
301 void InsetERT::lfunMousePress(FuncRequest const & cmd)
303 if (status_ == Inlined)
304 inset.localDispatch(cmd);
306 InsetCollapsable::localDispatch(cmd);
310 bool InsetERT::lfunMouseRelease(FuncRequest const & cmd)
312 BufferView * bv = cmd.view();
314 if (cmd.button() == mouse_button::button3) {
319 if (status_ != Inlined && (cmd.x >= 0) && (cmd.x < button_length) &&
320 (cmd.y >= button_top_y) && (cmd.y <= button_bottom_y)) {
321 updateStatus(bv, true);
323 LyXFont font(LyXFont::ALL_SANE);
324 FuncRequest cmd1 = cmd;
325 cmd1.y = ascent(bv, font) + cmd.y - inset.ascent(bv, font);
327 // inlined is special - the text appears above
329 if (status_ == Inlined)
330 inset.localDispatch(cmd1);
331 else if (!collapsed_ && (cmd.y > button_bottom_y)) {
332 cmd1.y -= ascent_collapsed() + descent_collapsed();
333 inset.localDispatch(cmd1);
340 void InsetERT::lfunMouseMotion(FuncRequest const & cmd)
342 if (status_ == Inlined)
343 inset.localDispatch(cmd);
345 InsetCollapsable::localDispatch(cmd);
349 int InsetERT::latex(Buffer const *, ostream & os, bool /*fragile*/,
350 bool /*free_spc*/) const
352 ParagraphList::iterator par = inset.paragraphs.begin();
353 ParagraphList::iterator end = inset.paragraphs.end();
357 pos_type siz = par->size();
358 for (pos_type i = 0; i < siz; ++i) {
359 // ignore all struck out text
360 if (isDeletedText(*par, i))
363 if (par->isNewline(i)) {
367 os << par->getChar(i);
381 int InsetERT::ascii(Buffer const *, ostream &, int /*linelen*/) const
387 int InsetERT::linuxdoc(Buffer const *, ostream & os) const
389 ParagraphList::iterator par = inset.paragraphs.begin();
390 ParagraphList::iterator end = inset.paragraphs.end();
394 pos_type siz = par->size();
395 for (pos_type i = 0; i < siz; ++i) {
396 if (par->isNewline(i)) {
400 os << par->getChar(i);
414 int InsetERT::docbook(Buffer const *, ostream & os, bool) const
416 ParagraphList::iterator par = inset.paragraphs.begin();
417 ParagraphList::iterator end = inset.paragraphs.end();
421 pos_type siz = par->size();
422 for (pos_type i = 0; i < siz; ++i) {
423 if (par->isNewline(i)) {
427 os << par->getChar(i);
441 Inset::RESULT InsetERT::localDispatch(FuncRequest const & cmd)
443 Inset::RESULT result = UNDISPATCHED;
444 BufferView * bv = cmd.view();
446 if (inset.paragraphs.begin()->empty()) {
450 switch (cmd.action) {
451 case LFUN_INSET_MODIFY: {
452 InsetERT::ERTStatus status_;
453 InsetERTMailer::string2params(cmd.argument, status_);
457 /* FIXME: I refuse to believe we have to live
458 * with ugliness like this ! Note that this
459 * rebreak *is* needed. Consider a change from
460 * Open (needfullrow) to Inlined (only the space
461 * taken by the text).
463 LyXText * t = inset.getLyXText(cmd.view());
464 t->need_break_row = t->rows().begin();
466 t->setCursorIntern(t->cursor.par(), t->cursor.pos());
467 inset.update(cmd.view(), true);
468 bv->updateInset(this);
473 case LFUN_MOUSE_PRESS:
478 case LFUN_MOUSE_MOTION:
479 lfunMouseMotion(cmd);
483 case LFUN_MOUSE_RELEASE:
484 lfunMouseRelease(cmd);
489 bv->owner()->setLayout(inset.paragraphs.begin()->layout()->name());
490 result = DISPATCHED_NOUPDATE;
494 result = InsetCollapsable::localDispatch(cmd);
497 switch (cmd.action) {
498 case LFUN_BREAKPARAGRAPH:
499 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
501 case LFUN_BACKSPACE_SKIP:
503 case LFUN_DELETE_SKIP:
504 case LFUN_DELETE_LINE_FORWARD:
516 string const InsetERT::get_new_label() const
519 pos_type const max_length = 15;
520 pos_type const p_siz = inset.paragraphs.begin()->size();
521 pos_type const n = min(max_length, p_siz);
524 for(; i < n && j < p_siz; ++j) {
525 if (inset.paragraphs.begin()->isInset(j))
527 la += inset.paragraphs.begin()->getChar(j);
530 if (boost::next(inset.paragraphs.begin()) != inset.paragraphs.end() ||
531 (i > 0 && j < p_siz)) {
541 void InsetERT::setButtonLabel() const
543 if (status_ == Collapsed) {
544 setLabel(get_new_label());
551 bool InsetERT::checkInsertChar(LyXFont & /* font */)
554 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
556 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
557 font.setColor(LColor::latex);
563 int InsetERT::ascent(BufferView * bv, LyXFont const & font) const
566 return InsetCollapsable::ascent(bv, font);
568 return inset.ascent(bv, font);
572 int InsetERT::descent(BufferView * bv, LyXFont const & font) const
575 return InsetCollapsable::descent(bv, font);
577 return inset.descent(bv, font);
581 int InsetERT::width(BufferView * bv, LyXFont const & font) const
584 return InsetCollapsable::width(bv, font);
586 return inset.width(bv, font);
590 void InsetERT::draw(BufferView * bv, LyXFont const & f,
591 int baseline, float & x) const
593 InsetCollapsable::draw(bv, f, baseline, x, inlined());
597 void InsetERT::set_latex_font(BufferView * /* bv */)
600 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
602 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
603 font.setColor(LColor::latex);
605 inset.getLyXText(bv)->setFont(bv, font, false);
610 // attention this function can be called with bv == 0
611 void InsetERT::status(BufferView * bv, ERTStatus const st) const
618 inset.setUpdateStatus(bv, InsetText::INIT);
628 bv->unlockInset(const_cast<InsetERT *>(this));
632 bv->updateInset(const_cast<InsetERT *>(this));
633 bv->buffer()->markDirty();
639 bool InsetERT::showInsetDialog(BufferView * bv) const
641 InsetERTMailer mailer(const_cast<InsetERT &>(*this));
642 mailer.showDialog(bv);
647 void InsetERT::open(BufferView * bv)
655 void InsetERT::close(BufferView * bv) const
657 if (status_ == Collapsed || status_ == Inlined)
660 status(bv, Collapsed);
665 InsetERT::selectNextWordToSpellcheck(BufferView * bv, float &) const
667 bv->unlockInset(const_cast<InsetERT *>(this));
668 return WordLangTuple();
672 void InsetERT::getDrawFont(LyXFont & font) const
674 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
676 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
677 font.setColor(LColor::latex);
681 int InsetERT::getMaxWidth(BufferView * bv, UpdatableInset const * in) const
683 int w = InsetCollapsable::getMaxWidth(bv, in);
684 if (status_ != Inlined || w < 0)
686 LyXText * text = inset.getLyXText(bv);
687 int rw = text->rows().begin()->width();
691 if (text->rows().size() == 1 && rw < w)
697 void InsetERT::update(BufferView * bv, bool reinit)
699 if (inset.need_update & InsetText::INIT ||
700 inset.need_update & InsetText::FULL) {
704 InsetCollapsable::update(bv, reinit);
708 string const InsetERTMailer::name_("ert");
710 InsetERTMailer::InsetERTMailer(InsetERT & inset)
715 string const InsetERTMailer::inset2string() const
717 return params2string(inset_.status());
721 void InsetERTMailer::string2params(string const & in,
722 InsetERT::ERTStatus & status)
724 status = InsetERT::Collapsed;
727 string body = split(in, name, ' ');
732 status = static_cast<InsetERT::ERTStatus>(strToInt(body));
737 InsetERTMailer::params2string(InsetERT::ERTStatus status)
739 return name_ + ' ' + tostr(status);