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)
246 // if selectall is activated then the fontchange was an outside general
247 // fontchange and this messages is not needed
249 Alert::alert(_("Impossible operation!"),
250 _("Not permitted to change font-types inside ERT-insets!"),
255 void InsetERT::updateStatus(BufferView * bv, bool swap) const
257 if (status_ != Inlined) {
259 status(bv, swap ? Open : Collapsed);
261 status(bv, swap ? Collapsed : Open);
266 void InsetERT::edit(BufferView * bv, int x, int y, mouse_button::state button)
268 if (button == mouse_button::button3)
271 if (status_ == Inlined) {
272 if (!bv->lockInset(this))
274 inset.edit(bv, x, y, button);
276 InsetCollapsable::edit(bv, x, y, button);
283 Inset::EDITABLE InsetERT::editable() const
285 if (status_ == Collapsed)
287 return HIGHLY_EDITABLE;
291 void InsetERT::edit(BufferView * bv, bool front)
293 InsetCollapsable::edit(bv, front);
299 void InsetERT::lfunMousePress(FuncRequest const & cmd)
301 if (status_ == Inlined)
302 inset.localDispatch(cmd);
304 InsetCollapsable::localDispatch(cmd);
308 bool InsetERT::lfunMouseRelease(FuncRequest const & cmd)
310 BufferView * bv = cmd.view();
312 if (cmd.button() == mouse_button::button3) {
317 if (status_ != Inlined && (cmd.x >= 0) && (cmd.x < button_length) &&
318 (cmd.y >= button_top_y) && (cmd.y <= button_bottom_y)) {
319 updateStatus(bv, true);
321 LyXFont font(LyXFont::ALL_SANE);
322 FuncRequest cmd1 = cmd;
323 cmd1.y = ascent(bv, font) + cmd.y - inset.ascent(bv, font);
325 // inlined is special - the text appears above
327 if (status_ == Inlined)
328 inset.localDispatch(cmd1);
329 else if (!collapsed_ && (cmd.y > button_bottom_y)) {
330 cmd1.y -= ascent_collapsed() + descent_collapsed();
331 inset.localDispatch(cmd1);
338 void InsetERT::lfunMouseMotion(FuncRequest const & cmd)
340 if (status_ == Inlined)
341 inset.localDispatch(cmd);
343 InsetCollapsable::localDispatch(cmd);
347 int InsetERT::latex(Buffer const *, ostream & os, bool /*fragile*/,
348 bool /*free_spc*/) const
350 Paragraph * par = inset.paragraph();
353 pos_type siz = par->size();
354 for (pos_type i = 0; i < siz; ++i) {
355 // ignore all struck out text
356 if (isDeletedText(*par, i))
359 if (par->isNewline(i)) {
363 os << par->getChar(i);
377 int InsetERT::ascii(Buffer const *, ostream &, int /*linelen*/) const
383 int InsetERT::linuxdoc(Buffer const *, ostream & os) const
385 Paragraph * par = inset.paragraph();
388 pos_type siz = par->size();
389 for (pos_type i = 0; i < siz; ++i) {
390 if (par->isNewline(i)) {
394 os << par->getChar(i);
408 int InsetERT::docbook(Buffer const *, ostream & os, bool) const
410 Paragraph * par = inset.paragraph();
413 pos_type siz = par->size();
414 for (pos_type i = 0; i < siz; ++i) {
415 if (par->isNewline(i)) {
419 os << par->getChar(i);
433 Inset::RESULT InsetERT::localDispatch(FuncRequest const & cmd)
435 Inset::RESULT result = UNDISPATCHED;
436 BufferView * bv = cmd.view();
438 if (inset.paragraph()->empty()) {
442 switch (cmd.action) {
443 case LFUN_INSET_MODIFY: {
444 InsetERT::ERTStatus status_;
445 InsetERTMailer::string2params(cmd.argument, status_);
448 // FIXME: how on holy earth do you actually get
449 // this thing to reinit the bloody insettext
450 // and change the size of this inset !!?!
451 bv->updateInset(this);
456 case LFUN_MOUSE_PRESS:
461 case LFUN_MOUSE_MOTION:
462 lfunMouseMotion(cmd);
466 case LFUN_MOUSE_RELEASE:
467 lfunMouseRelease(cmd);
472 bv->owner()->setLayout(inset.paragraph()->layout()->name());
473 result = DISPATCHED_NOUPDATE;
477 result = InsetCollapsable::localDispatch(cmd);
480 switch (cmd.action) {
481 case LFUN_BREAKPARAGRAPH:
482 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
484 case LFUN_BACKSPACE_SKIP:
486 case LFUN_DELETE_SKIP:
487 case LFUN_DELETE_LINE_FORWARD:
499 string const InsetERT::get_new_label() const
502 pos_type const max_length = 15;
503 pos_type const p_siz = inset.paragraph()->size();
504 pos_type const n = min(max_length, p_siz);
507 for(; i < n && j < p_siz; ++j) {
508 if (inset.paragraph()->isInset(j))
510 la += inset.paragraph()->getChar(j);
513 if (inset.paragraph()->next() || (i > 0 && j < p_siz)) {
523 void InsetERT::setButtonLabel() const
525 if (status_ == Collapsed) {
526 setLabel(get_new_label());
533 bool InsetERT::checkInsertChar(LyXFont & /* font */)
536 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
538 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
539 font.setColor(LColor::latex);
545 int InsetERT::ascent(BufferView * bv, LyXFont const & font) const
548 return InsetCollapsable::ascent(bv, font);
550 return inset.ascent(bv, font);
554 int InsetERT::descent(BufferView * bv, LyXFont const & font) const
557 return InsetCollapsable::descent(bv, font);
559 return inset.descent(bv, font);
563 int InsetERT::width(BufferView * bv, LyXFont const & font) const
566 return InsetCollapsable::width(bv, font);
568 return inset.width(bv, font);
572 void InsetERT::draw(BufferView * bv, LyXFont const & f,
573 int baseline, float & x) const
575 InsetCollapsable::draw(bv, f, baseline, x, inlined());
579 void InsetERT::set_latex_font(BufferView * /* bv */)
582 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
584 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
585 font.setColor(LColor::latex);
587 inset.getLyXText(bv)->setFont(bv, font, false);
592 // attention this function can be called with bv == 0
593 void InsetERT::status(BufferView * bv, ERTStatus const st) const
600 inset.setUpdateStatus(bv, InsetText::INIT);
610 bv->unlockInset(const_cast<InsetERT *>(this));
614 bv->updateInset(const_cast<InsetERT *>(this));
615 bv->buffer()->markDirty();
621 bool InsetERT::showInsetDialog(BufferView * bv) const
623 InsetERTMailer mailer(const_cast<InsetERT &>(*this));
624 mailer.showDialog(bv);
629 void InsetERT::open(BufferView * bv)
637 void InsetERT::close(BufferView * bv) const
639 if (status_ == Collapsed || status_ == Inlined)
642 status(bv, Collapsed);
647 InsetERT::selectNextWordToSpellcheck(BufferView * bv, float &) const
649 bv->unlockInset(const_cast<InsetERT *>(this));
650 return WordLangTuple();
654 void InsetERT::getDrawFont(LyXFont & font) const
656 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
658 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
659 font.setColor(LColor::latex);
663 int InsetERT::getMaxWidth(BufferView * bv, UpdatableInset const * in) const
665 int w = InsetCollapsable::getMaxWidth(bv, in);
666 if (status_ != Inlined || w < 0)
668 LyXText * text = inset.getLyXText(bv);
669 int rw = text->firstRow()->width();
673 if (!text->firstRow()->next() && rw < w)
679 void InsetERT::update(BufferView * bv, bool reinit)
681 if (inset.need_update & InsetText::INIT ||
682 inset.need_update & InsetText::FULL) {
686 InsetCollapsable::update(bv, reinit);
690 string const InsetERTMailer::name_("ert");
692 InsetERTMailer::InsetERTMailer(InsetERT & inset)
697 string const InsetERTMailer::inset2string() const
699 return params2string(inset_.status());
703 void InsetERTMailer::string2params(string const & in,
704 InsetERT::ERTStatus & status)
706 status = InsetERT::Collapsed;
709 string body = split(in, name, ' ');
714 status = static_cast<InsetERT::ERTStatus>(strToInt(body));
719 InsetERTMailer::params2string(InsetERT::ERTStatus status)
721 return name_ + ' ' + tostr(status);