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);
162 ParagraphList::iterator pit = inset.paragraphs.begin();
163 ParagraphList::iterator pend = inset.paragraphs.end();
164 for (; pit != pend; ++pit) {
165 pos_type siz = pit->size();
166 for (pos_type i = 0; i < siz; ++i) {
167 pit->setFont(i, font);
174 status(0, Collapsed);
183 void InsetERT::write(Buffer const * buf, ostream & os) const
199 os << getInsetName() << "\n"
200 << "status "<< st << "\n";
202 //inset.writeParagraphData(buf, os);
203 string const layout(buf->params.getLyXTextClass().defaultLayoutName());
204 ParagraphList::iterator par = inset.paragraphs.begin();
205 ParagraphList::iterator end = inset.paragraphs.end();
206 for (; par != end; ++par) {
207 os << "\n\\layout " << layout << "\n";
208 pos_type siz = par->size();
209 for (pos_type i = 0; i < siz; ++i) {
210 Paragraph::value_type c = par->getChar(i);
212 case Paragraph::META_INSET:
213 if (par->getInset(i)->lyxCode() != Inset::NEWLINE_CODE) {
214 lyxerr << "Element is not allowed in insertERT"
217 par->getInset(i)->write(buf, os);
222 os << "\n\\backslash \n";
233 string const InsetERT::editMessage() const
235 return _("Opened ERT Inset");
239 bool InsetERT::insertInset(BufferView *, Inset *)
245 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
248 #warning FIXME. More UI stupidity...
250 // if selectall is activated then the fontchange was an outside general
251 // fontchange and this messages is not needed
253 Alert::error(_("Cannot change font"),
254 _("You cannot change font settings inside TeX code."));
258 void InsetERT::updateStatus(BufferView * bv, bool swap) const
260 if (status_ != Inlined) {
262 status(bv, swap ? Open : Collapsed);
264 status(bv, swap ? Collapsed : Open);
269 void InsetERT::edit(BufferView * bv, int x, int y, mouse_button::state button)
271 if (button == mouse_button::button3)
274 if (status_ == Inlined) {
275 if (!bv->lockInset(this))
277 inset.edit(bv, x, y, button);
279 InsetCollapsable::edit(bv, x, y, button);
286 Inset::EDITABLE InsetERT::editable() const
288 if (status_ == Collapsed)
290 return HIGHLY_EDITABLE;
294 void InsetERT::edit(BufferView * bv, bool front)
296 InsetCollapsable::edit(bv, front);
302 void InsetERT::lfunMousePress(FuncRequest const & cmd)
304 if (status_ == Inlined)
305 inset.localDispatch(cmd);
307 InsetCollapsable::localDispatch(cmd);
311 bool InsetERT::lfunMouseRelease(FuncRequest const & cmd)
313 BufferView * bv = cmd.view();
315 if (cmd.button() == mouse_button::button3) {
320 if (status_ != Inlined && (cmd.x >= 0) && (cmd.x < button_length) &&
321 (cmd.y >= button_top_y) && (cmd.y <= button_bottom_y)) {
322 updateStatus(bv, true);
324 LyXFont font(LyXFont::ALL_SANE);
325 FuncRequest cmd1 = cmd;
326 cmd1.y = ascent(bv, font) + cmd.y - inset.ascent(bv, font);
328 // inlined is special - the text appears above
330 if (status_ == Inlined)
331 inset.localDispatch(cmd1);
332 else if (!collapsed_ && (cmd.y > button_bottom_y)) {
333 cmd1.y -= ascent_collapsed() + descent_collapsed();
334 inset.localDispatch(cmd1);
341 void InsetERT::lfunMouseMotion(FuncRequest const & cmd)
343 if (status_ == Inlined)
344 inset.localDispatch(cmd);
346 InsetCollapsable::localDispatch(cmd);
350 int InsetERT::latex(Buffer const *, ostream & os, bool /*fragile*/,
351 bool /*free_spc*/) const
353 ParagraphList::iterator par = inset.paragraphs.begin();
354 ParagraphList::iterator end = inset.paragraphs.end();
358 pos_type siz = par->size();
359 for (pos_type i = 0; i < siz; ++i) {
360 // ignore all struck out text
361 if (isDeletedText(*par, i))
364 if (par->isNewline(i)) {
368 os << par->getChar(i);
382 int InsetERT::ascii(Buffer const *, ostream &, int /*linelen*/) const
388 int InsetERT::linuxdoc(Buffer const *, ostream & os) const
390 ParagraphList::iterator par = inset.paragraphs.begin();
391 ParagraphList::iterator end = inset.paragraphs.end();
395 pos_type siz = par->size();
396 for (pos_type i = 0; i < siz; ++i) {
397 if (par->isNewline(i)) {
401 os << par->getChar(i);
415 int InsetERT::docbook(Buffer const *, ostream & os, bool) const
417 ParagraphList::iterator par = inset.paragraphs.begin();
418 ParagraphList::iterator end = inset.paragraphs.end();
422 pos_type siz = par->size();
423 for (pos_type i = 0; i < siz; ++i) {
424 if (par->isNewline(i)) {
428 os << par->getChar(i);
442 Inset::RESULT InsetERT::localDispatch(FuncRequest const & cmd)
444 Inset::RESULT result = UNDISPATCHED;
445 BufferView * bv = cmd.view();
447 if (inset.paragraphs.begin()->empty()) {
451 switch (cmd.action) {
452 case LFUN_INSET_MODIFY: {
453 InsetERT::ERTStatus status_;
454 InsetERTMailer::string2params(cmd.argument, status_);
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).
464 LyXText * t = inset.getLyXText(cmd.view());
465 t->need_break_row = t->rows().begin();
467 t->setCursorIntern(t->cursor.par(), t->cursor.pos());
468 inset.update(cmd.view(), true);
469 bv->updateInset(this);
474 case LFUN_MOUSE_PRESS:
479 case LFUN_MOUSE_MOTION:
480 lfunMouseMotion(cmd);
484 case LFUN_MOUSE_RELEASE:
485 lfunMouseRelease(cmd);
490 bv->owner()->setLayout(inset.paragraphs.begin()->layout()->name());
491 result = DISPATCHED_NOUPDATE;
495 result = InsetCollapsable::localDispatch(cmd);
498 switch (cmd.action) {
499 case LFUN_BREAKPARAGRAPH:
500 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
502 case LFUN_BACKSPACE_SKIP:
504 case LFUN_DELETE_SKIP:
505 case LFUN_DELETE_LINE_FORWARD:
517 string const InsetERT::get_new_label() const
520 pos_type const max_length = 15;
521 pos_type const p_siz = inset.paragraphs.begin()->size();
522 pos_type const n = min(max_length, p_siz);
525 for(; i < n && j < p_siz; ++j) {
526 if (inset.paragraphs.begin()->isInset(j))
528 la += inset.paragraphs.begin()->getChar(j);
531 if (boost::next(inset.paragraphs.begin()) != inset.paragraphs.end() ||
532 (i > 0 && j < p_siz)) {
542 void InsetERT::setButtonLabel() const
544 if (status_ == Collapsed) {
545 setLabel(get_new_label());
552 bool InsetERT::checkInsertChar(LyXFont & /* font */)
555 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
557 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
558 font.setColor(LColor::latex);
564 int InsetERT::ascent(BufferView * bv, LyXFont const & font) const
567 return InsetCollapsable::ascent(bv, font);
569 return inset.ascent(bv, font);
573 int InsetERT::descent(BufferView * bv, LyXFont const & font) const
576 return InsetCollapsable::descent(bv, font);
578 return inset.descent(bv, font);
582 int InsetERT::width(BufferView * bv, LyXFont const & font) const
585 return InsetCollapsable::width(bv, font);
587 return inset.width(bv, font);
591 void InsetERT::draw(BufferView * bv, LyXFont const & f,
592 int baseline, float & x) const
594 InsetCollapsable::draw(bv, f, baseline, x, inlined());
598 void InsetERT::set_latex_font(BufferView * /* bv */)
601 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
603 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
604 font.setColor(LColor::latex);
606 inset.getLyXText(bv)->setFont(bv, font, false);
611 // attention this function can be called with bv == 0
612 void InsetERT::status(BufferView * bv, ERTStatus const st) const
619 inset.setUpdateStatus(bv, InsetText::INIT);
629 bv->unlockInset(const_cast<InsetERT *>(this));
633 bv->updateInset(const_cast<InsetERT *>(this));
634 bv->buffer()->markDirty();
640 bool InsetERT::showInsetDialog(BufferView * bv) const
642 InsetERTMailer mailer(const_cast<InsetERT &>(*this));
643 mailer.showDialog(bv);
648 void InsetERT::open(BufferView * bv)
656 void InsetERT::close(BufferView * bv) const
658 if (status_ == Collapsed || status_ == Inlined)
661 status(bv, Collapsed);
666 InsetERT::selectNextWordToSpellcheck(BufferView * bv, float &) const
668 bv->unlockInset(const_cast<InsetERT *>(this));
669 return WordLangTuple();
673 void InsetERT::getDrawFont(LyXFont & font) const
675 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
677 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
678 font.setColor(LColor::latex);
682 int InsetERT::getMaxWidth(BufferView * bv, UpdatableInset const * in) const
684 int w = InsetCollapsable::getMaxWidth(bv, in);
685 if (status_ != Inlined || w < 0)
687 LyXText * text = inset.getLyXText(bv);
688 int rw = text->rows().begin()->width();
692 if (text->rows().size() == 1 && rw < w)
698 void InsetERT::update(BufferView * bv, bool reinit)
700 if (inset.need_update & InsetText::INIT ||
701 inset.need_update & InsetText::FULL) {
705 InsetCollapsable::update(bv, reinit);
709 string const InsetERTMailer::name_("ert");
711 InsetERTMailer::InsetERTMailer(InsetERT & inset)
716 string const InsetERTMailer::inset2string() const
718 return params2string(inset_.status());
722 void InsetERTMailer::string2params(string const & in,
723 InsetERT::ERTStatus & status)
725 status = InsetERT::Collapsed;
728 string body = split(in, name, ' ');
733 status = static_cast<InsetERT::ERTStatus>(strToInt(body));
738 InsetERTMailer::params2string(InsetERT::ERTStatus status)
740 return name_ + ' ' + tostr(status);