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 case Paragraph::META_HFILL:
212 lyxerr << "Element is not allowed in insertERT"
214 case Paragraph::META_NEWLINE:
215 os << "\n\\newline \n";
218 os << "\n\\backslash \n";
230 string const InsetERT::editMessage() const
232 return _("Opened ERT Inset");
236 bool InsetERT::insertInset(BufferView *, Inset *)
242 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
244 // if selectall is activated then the fontchange was an outside general
245 // fontchange and this messages is not needed
247 Alert::alert(_("Impossible operation!"),
248 _("Not permitted to change font-types inside ERT-insets!"),
253 void InsetERT::updateStatus(BufferView * bv, bool swap) const
255 if (status_ != Inlined) {
257 status(bv, swap ? Open : Collapsed);
259 status(bv, swap ? Collapsed : Open);
264 void InsetERT::edit(BufferView * bv, int x, int y, mouse_button::state button)
266 if (button == mouse_button::button3)
269 if (status_ == Inlined) {
270 if (!bv->lockInset(this))
272 inset.edit(bv, x, y, button);
274 InsetCollapsable::edit(bv, x, y, button);
281 Inset::EDITABLE InsetERT::editable() const
283 if (status_ == Collapsed)
285 return HIGHLY_EDITABLE;
289 void InsetERT::edit(BufferView * bv, bool front)
291 InsetCollapsable::edit(bv, front);
297 void InsetERT::lfunMousePress(FuncRequest const & cmd)
299 if (status_ == Inlined)
300 inset.localDispatch(cmd);
302 InsetCollapsable::localDispatch(cmd);
306 bool InsetERT::lfunMouseRelease(FuncRequest const & cmd)
308 BufferView * bv = cmd.view();
310 if (cmd.button() == mouse_button::button3) {
315 if (status_ != Inlined && (cmd.x >= 0) && (cmd.x < button_length) &&
316 (cmd.y >= button_top_y) && (cmd.y <= button_bottom_y)) {
317 updateStatus(bv, true);
319 LyXFont font(LyXFont::ALL_SANE);
320 FuncRequest cmd1 = cmd;
321 cmd1.y = ascent(bv, font) + cmd.y - inset.ascent(bv, font);
323 // inlined is special - the text appears above
325 if (status_ == Inlined)
326 inset.localDispatch(cmd1);
327 else if (!collapsed_ && (cmd.y > button_bottom_y)) {
328 cmd1.y -= ascent_collapsed() + descent_collapsed();
329 inset.localDispatch(cmd1);
336 void InsetERT::lfunMouseMotion(FuncRequest const & cmd)
338 if (status_ == Inlined)
339 inset.localDispatch(cmd);
341 InsetCollapsable::localDispatch(cmd);
345 int InsetERT::latex(Buffer const *, ostream & os, bool /*fragile*/,
346 bool /*free_spc*/) const
348 Paragraph * par = inset.paragraph();
351 pos_type siz = par->size();
352 for (pos_type i = 0; i < siz; ++i) {
353 // ignore all struck out text
354 if (isDeletedText(*par, i))
357 Paragraph::value_type c = par->getChar(i);
359 case Paragraph::META_NEWLINE:
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 Paragraph::value_type c = par->getChar(i);
394 case Paragraph::META_NEWLINE:
414 int InsetERT::docbook(Buffer const *, ostream & os, bool) const
416 Paragraph * par = inset.paragraph();
419 pos_type siz = par->size();
420 for (pos_type i = 0; i < siz; ++i) {
421 Paragraph::value_type c = par->getChar(i);
423 case Paragraph::META_NEWLINE:
443 Inset::RESULT InsetERT::localDispatch(FuncRequest const & cmd)
445 Inset::RESULT result = DISPATCHED_NOUPDATE;
446 BufferView * bv = cmd.view();
448 if (inset.paragraph()->empty()) {
452 switch (cmd.action) {
453 case LFUN_MOUSE_PRESS:
457 case LFUN_MOUSE_MOTION:
458 lfunMouseMotion(cmd);
461 case LFUN_MOUSE_RELEASE:
462 lfunMouseRelease(cmd);
466 bv->owner()->setLayout(inset.paragraph()->layout()->name());
470 result = InsetCollapsable::localDispatch(cmd);
473 switch (cmd.action) {
474 case LFUN_BREAKPARAGRAPH:
475 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
477 case LFUN_BACKSPACE_SKIP:
479 case LFUN_DELETE_SKIP:
480 case LFUN_DELETE_LINE_FORWARD:
492 string const InsetERT::get_new_label() const
495 pos_type const max_length = 15;
496 pos_type const p_siz = inset.paragraph()->size();
497 pos_type const n = min(max_length, p_siz);
500 for(; i < n && j < p_siz; ++j) {
501 if (inset.paragraph()->isInset(j))
503 la += inset.paragraph()->getChar(j);
506 if (inset.paragraph()->next() || (i > 0 && j < p_siz)) {
516 void InsetERT::setButtonLabel() const
518 if (status_ == Collapsed) {
519 setLabel(get_new_label());
526 bool InsetERT::checkInsertChar(LyXFont & /* font */)
529 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
531 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
532 font.setColor(LColor::latex);
538 int InsetERT::ascent(BufferView * bv, LyXFont const & font) const
541 return InsetCollapsable::ascent(bv, font);
543 return inset.ascent(bv, font);
547 int InsetERT::descent(BufferView * bv, LyXFont const & font) const
550 return InsetCollapsable::descent(bv, font);
552 return inset.descent(bv, font);
556 int InsetERT::width(BufferView * bv, LyXFont const & font) const
559 return InsetCollapsable::width(bv, font);
561 return inset.width(bv, font);
565 void InsetERT::draw(BufferView * bv, LyXFont const & f,
566 int baseline, float & x, bool cleared) const
571 Painter & pain = bv->painter();
573 button_length = width_collapsed();
574 button_top_y = -ascent(bv, f);
575 button_bottom_y = -ascent(bv, f) + ascent_collapsed() +
579 draw_collapsed(pain, baseline, x);
586 x += static_cast<float>(scroll());
588 if (!cleared && (inset.need_update == InsetText::FULL ||
589 inset.need_update == InsetText::INIT ||
591 top_baseline != baseline))
593 // we don't need anymore to clear here we just have to tell
594 // the underlying LyXText that it should do the RowClear!
595 inset.setUpdateStatus(bv, InsetText::FULL);
596 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
602 top_baseline = baseline;
604 int const bl = baseline - ascent(bv, f) + ascent_collapsed();
607 inset.draw(bv, f, baseline, x, cleared);
609 draw_collapsed(pain, bl, old_x);
611 bl + descent_collapsed() + inset.ascent(bv, f),
618 void InsetERT::set_latex_font(BufferView * /* bv */)
621 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
623 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
624 font.setColor(LColor::latex);
626 inset.getLyXText(bv)->setFont(bv, font, false);
631 // attention this function can be called with bv == 0
632 void InsetERT::status(BufferView * bv, ERTStatus const st) const
640 inset.setUpdateStatus(bv, InsetText::INIT);
650 bv->unlockInset(const_cast<InsetERT *>(this));
654 bv->updateInset(const_cast<InsetERT *>(this), false);
659 bool InsetERT::showInsetDialog(BufferView *) const
661 InsetERTMailer mailer(const_cast<InsetERT &>(*this));
667 void InsetERT::open(BufferView * bv)
675 void InsetERT::close(BufferView * bv) const
677 if (status_ == Collapsed || status_ == Inlined)
680 status(bv, Collapsed);
685 InsetERT::selectNextWordToSpellcheck(BufferView * bv, float &) const
687 bv->unlockInset(const_cast<InsetERT *>(this));
688 return WordLangTuple();
692 void InsetERT::getDrawFont(LyXFont & font) const
694 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
696 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
697 font.setColor(LColor::latex);
701 int InsetERT::getMaxWidth(BufferView * bv, UpdatableInset const * in) const
703 int w = InsetCollapsable::getMaxWidth(bv, in);
704 if (status_ != Inlined || w < 0)
706 LyXText * text = inset.getLyXText(bv);
707 int rw = text->firstRow()->width();
711 if (!text->firstRow()->next() && rw < w)
717 void InsetERT::update(BufferView * bv, LyXFont const & font,
720 if (inset.need_update & InsetText::INIT ||
721 inset.need_update & InsetText::FULL)
725 InsetCollapsable::update(bv, font, reinit);
729 InsetERTMailer::InsetERTMailer(InsetERT & inset)
730 : name_("ert"), inset_(inset)
734 string const InsetERTMailer::inset2string() const
736 return params2string(inset_.status());
740 void InsetERTMailer::string2params(string const & in,
741 InsetERT::ERTStatus & status)
743 status = InsetERT::Collapsed;
747 status = static_cast<InsetERT::ERTStatus>(strToInt(in));
752 InsetERTMailer::params2string(InsetERT::ERTStatus status)
754 return tostr(status);