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.paragraph()->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 Paragraph * par = inset.paragraph();
205 os << "\n\\layout " << layout << "\n";
206 pos_type siz = par->size();
207 for (pos_type i = 0; i < siz; ++i) {
208 Paragraph::value_type c = par->getChar(i);
210 case Paragraph::META_INSET:
211 if (par->getInset(i)->lyxCode() != Inset::NEWLINE_CODE) {
212 lyxerr << "Element is not allowed in insertERT"
215 par->getInset(i)->write(buf, os);
220 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 Paragraph * par = inset.paragraph();
355 pos_type siz = par->size();
356 for (pos_type i = 0; i < siz; ++i) {
357 // ignore all struck out text
358 if (isDeletedText(*par, i))
361 if (par->isNewline(i)) {
365 os << par->getChar(i);
379 int InsetERT::ascii(Buffer const *, ostream &, int /*linelen*/) const
385 int InsetERT::linuxdoc(Buffer const *, ostream & os) const
387 Paragraph * par = inset.paragraph();
390 pos_type siz = par->size();
391 for (pos_type i = 0; i < siz; ++i) {
392 if (par->isNewline(i)) {
396 os << par->getChar(i);
410 int InsetERT::docbook(Buffer const *, ostream & os, bool) const
412 Paragraph * par = inset.paragraph();
415 pos_type siz = par->size();
416 for (pos_type i = 0; i < siz; ++i) {
417 if (par->isNewline(i)) {
421 os << par->getChar(i);
435 Inset::RESULT InsetERT::localDispatch(FuncRequest const & cmd)
437 Inset::RESULT result = UNDISPATCHED;
438 BufferView * bv = cmd.view();
440 if (inset.paragraph()->empty()) {
444 switch (cmd.action) {
445 case LFUN_INSET_MODIFY: {
446 InsetERT::ERTStatus status_;
447 InsetERTMailer::string2params(cmd.argument, status_);
451 /* FIXME: I refuse to believe we have to live
452 * with ugliness like this ! Note that this
453 * rebreak *is* needed. Consider a change from
454 * Open (needfullrow) to Inlined (only the space
455 * taken by the text).
457 LyXText * t = inset.getLyXText(cmd.view());
458 t->need_break_row = t->rows().begin();
460 t->setCursorIntern(t->cursor.par(), t->cursor.pos());
461 inset.update(cmd.view(), true);
462 bv->updateInset(this);
467 case LFUN_MOUSE_PRESS:
472 case LFUN_MOUSE_MOTION:
473 lfunMouseMotion(cmd);
477 case LFUN_MOUSE_RELEASE:
478 lfunMouseRelease(cmd);
483 bv->owner()->setLayout(inset.paragraph()->layout()->name());
484 result = DISPATCHED_NOUPDATE;
488 result = InsetCollapsable::localDispatch(cmd);
491 switch (cmd.action) {
492 case LFUN_BREAKPARAGRAPH:
493 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
495 case LFUN_BACKSPACE_SKIP:
497 case LFUN_DELETE_SKIP:
498 case LFUN_DELETE_LINE_FORWARD:
510 string const InsetERT::get_new_label() const
513 pos_type const max_length = 15;
514 pos_type const p_siz = inset.paragraph()->size();
515 pos_type const n = min(max_length, p_siz);
518 for(; i < n && j < p_siz; ++j) {
519 if (inset.paragraph()->isInset(j))
521 la += inset.paragraph()->getChar(j);
524 if (inset.paragraph()->next() || (i > 0 && j < p_siz)) {
534 void InsetERT::setButtonLabel() const
536 if (status_ == Collapsed) {
537 setLabel(get_new_label());
544 bool InsetERT::checkInsertChar(LyXFont & /* font */)
547 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
549 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
550 font.setColor(LColor::latex);
556 int InsetERT::ascent(BufferView * bv, LyXFont const & font) const
559 return InsetCollapsable::ascent(bv, font);
561 return inset.ascent(bv, font);
565 int InsetERT::descent(BufferView * bv, LyXFont const & font) const
568 return InsetCollapsable::descent(bv, font);
570 return inset.descent(bv, font);
574 int InsetERT::width(BufferView * bv, LyXFont const & font) const
577 return InsetCollapsable::width(bv, font);
579 return inset.width(bv, font);
583 void InsetERT::draw(BufferView * bv, LyXFont const & f,
584 int baseline, float & x) const
586 InsetCollapsable::draw(bv, f, baseline, x, inlined());
590 void InsetERT::set_latex_font(BufferView * /* bv */)
593 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
595 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
596 font.setColor(LColor::latex);
598 inset.getLyXText(bv)->setFont(bv, font, false);
603 // attention this function can be called with bv == 0
604 void InsetERT::status(BufferView * bv, ERTStatus const st) const
611 inset.setUpdateStatus(bv, InsetText::INIT);
621 bv->unlockInset(const_cast<InsetERT *>(this));
625 bv->updateInset(const_cast<InsetERT *>(this));
626 bv->buffer()->markDirty();
632 bool InsetERT::showInsetDialog(BufferView * bv) const
634 InsetERTMailer mailer(const_cast<InsetERT &>(*this));
635 mailer.showDialog(bv);
640 void InsetERT::open(BufferView * bv)
648 void InsetERT::close(BufferView * bv) const
650 if (status_ == Collapsed || status_ == Inlined)
653 status(bv, Collapsed);
658 InsetERT::selectNextWordToSpellcheck(BufferView * bv, float &) const
660 bv->unlockInset(const_cast<InsetERT *>(this));
661 return WordLangTuple();
665 void InsetERT::getDrawFont(LyXFont & font) const
667 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
669 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
670 font.setColor(LColor::latex);
674 int InsetERT::getMaxWidth(BufferView * bv, UpdatableInset const * in) const
676 int w = InsetCollapsable::getMaxWidth(bv, in);
677 if (status_ != Inlined || w < 0)
679 LyXText * text = inset.getLyXText(bv);
680 int rw = text->rows().begin()->width();
684 if (text->rows().size() == 1 && rw < w)
690 void InsetERT::update(BufferView * bv, bool reinit)
692 if (inset.need_update & InsetText::INIT ||
693 inset.need_update & InsetText::FULL) {
697 InsetCollapsable::update(bv, reinit);
701 string const InsetERTMailer::name_("ert");
703 InsetERTMailer::InsetERTMailer(InsetERT & inset)
708 string const InsetERTMailer::inset2string() const
710 return params2string(inset_.status());
714 void InsetERTMailer::string2params(string const & in,
715 InsetERT::ERTStatus & status)
717 status = InsetERT::Collapsed;
720 string body = split(in, name, ' ');
725 status = static_cast<InsetERT::ERTStatus>(strToInt(body));
730 InsetERTMailer::params2string(InsetERT::ERTStatus status)
732 return name_ + ' ' + tostr(status);