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
15 #include "insettext.h"
18 #include "BufferView.h"
20 #include "funcrequest.h"
27 #include "WordLangTuple.h"
29 #include "frontends/Alert.h"
30 #include "frontends/Dialogs.h"
31 #include "frontends/LyXView.h"
33 #include "support/LOstream.h"
46 labelfont = LyXFont(LyXFont::ALL_SANE);
49 labelfont.setColor(LColor::latex);
54 InsetERT::InsetERT(BufferParams const & bp, bool collapsed)
55 : InsetCollapsable(bp, collapsed)
65 InsetERT::InsetERT(InsetERT const & in, bool same_id)
66 : InsetCollapsable(in, same_id), status_(in.status_)
72 Inset * InsetERT::clone(Buffer const &, bool same_id) const
74 return new InsetERT(*const_cast<InsetERT *>(this), same_id);
78 InsetERT::InsetERT(BufferParams const & bp,
79 Language const * l, string const & contents, bool collapsed)
80 : InsetCollapsable(bp, collapsed)
87 LyXFont font(LyXFont::ALL_INHERIT, l);
89 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
90 font.setColor(LColor::latex);
93 string::const_iterator cit = contents.begin();
94 string::const_iterator end = contents.end();
96 for (; cit != end; ++cit) {
97 inset.paragraph()->insertChar(pos++, *cit, font);
99 // the init has to be after the initialization of the paragraph
100 // because of the label settings (draw_label for ert insets).
105 InsetERT::~InsetERT()
111 void InsetERT::read(Buffer const * buf, LyXLex & lex)
113 bool token_found = false;
116 string const token = lex.getString();
117 if (token == "status") {
119 string const tmp_token = lex.getString();
121 if (tmp_token == "Inlined") {
123 } else if (tmp_token == "Collapsed") {
124 status(0, Collapsed);
126 // leave this as default!
132 lyxerr << "InsetERT::Read: Missing 'status'-tag!"
134 // take countermeasures
135 lex.pushToken(token);
139 #warning this should be really short lived only for compatibility to
140 #warning files written 07/08/2001 so this has to go before 1.2.0! (Jug)
143 string const token = lex.getString();
144 if (token == "collapsed") {
146 collapsed_ = lex.getBool();
148 // Take countermeasures
149 lex.pushToken(token);
153 inset.read(buf, lex);
156 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
157 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
158 font.setColor(LColor::latex);
159 Paragraph * par = inset.paragraph();
161 pos_type siz = par->size();
162 for (pos_type i = 0; i < siz; ++i) {
163 par->setFont(i, font);
171 status(0, Collapsed);
180 void InsetERT::write(Buffer const * buf, ostream & os) const
196 os << getInsetName() << "\n"
197 << "status "<< st << "\n";
199 //inset.writeParagraphData(buf, os);
200 string const layout(buf->params.getLyXTextClass().defaultLayoutName());
201 Paragraph * par = inset.paragraph();
203 os << "\n\\layout " << layout << "\n";
204 pos_type siz = par->size();
205 for (pos_type i = 0; i < siz; ++i) {
206 Paragraph::value_type c = par->getChar(i);
208 case Paragraph::META_INSET:
209 case Paragraph::META_HFILL:
210 lyxerr << "Element is not allowed in insertERT"
212 case Paragraph::META_NEWLINE:
213 os << "\n\\newline \n";
216 os << "\n\\backslash \n";
228 string const InsetERT::editMessage() const
230 return _("Opened ERT Inset");
234 bool InsetERT::insertInset(BufferView *, Inset *)
240 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
242 // if selectall is activated then the fontchange was an outside general
243 // fontchange and this messages is not needed
245 Alert::alert(_("Impossible operation!"),
246 _("Not permitted to change font-types inside ERT-insets!"),
251 void InsetERT::updateStatus(BufferView * bv, bool swap) const
253 if (status_ != Inlined) {
255 status(bv, swap ? Open : Collapsed);
257 status(bv, swap ? Collapsed : Open);
262 void InsetERT::edit(BufferView * bv, int x, int y, mouse_button::state button)
264 if (button == mouse_button::button3)
267 if (status_ == Inlined) {
268 if (!bv->lockInset(this))
270 inset.edit(bv, x, y, button);
272 InsetCollapsable::edit(bv, x, y, button);
279 Inset::EDITABLE InsetERT::editable() const
281 if (status_ == Collapsed)
283 return HIGHLY_EDITABLE;
287 void InsetERT::edit(BufferView * bv, bool front)
289 InsetCollapsable::edit(bv, front);
295 void InsetERT::lfunMousePress(FuncRequest const & cmd)
297 if (status_ == Inlined)
298 inset.localDispatch(cmd);
300 InsetCollapsable::localDispatch(cmd);
304 bool InsetERT::lfunMouseRelease(FuncRequest const & cmd)
306 BufferView * bv = cmd.view();
308 if (cmd.button() == mouse_button::button3) {
313 if (status_ != Inlined && (cmd.x >= 0) && (cmd.x < button_length) &&
314 (cmd.y >= button_top_y) && (cmd.y <= button_bottom_y)) {
315 updateStatus(bv, true);
317 LyXFont font(LyXFont::ALL_SANE);
318 FuncRequest cmd1 = cmd;
319 cmd1.y = ascent(bv, font) + cmd.y - inset.ascent(bv, font);
321 // inlined is special - the text appears above
323 if (status_ == Inlined)
324 inset.localDispatch(cmd1);
325 else if (!collapsed_ && (cmd.y > button_bottom_y)) {
326 cmd1.y -= ascent_collapsed() + descent_collapsed();
327 inset.localDispatch(cmd1);
334 void InsetERT::lfunMouseMotion(FuncRequest const & cmd)
336 if (status_ == Inlined)
337 inset.localDispatch(cmd);
339 InsetCollapsable::localDispatch(cmd);
343 int InsetERT::latex(Buffer const *, ostream & os, bool /*fragile*/,
344 bool /*free_spc*/) const
346 Paragraph * par = inset.paragraph();
349 pos_type siz = par->size();
350 for (pos_type i = 0; i < siz; ++i) {
351 // ignore all struck out text
352 if (isDeletedText(par, i))
355 Paragraph::value_type c = par->getChar(i);
357 case Paragraph::META_NEWLINE:
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 Paragraph::value_type c = par->getChar(i);
392 case Paragraph::META_NEWLINE:
412 int InsetERT::docbook(Buffer const *, ostream & os, bool) const
414 Paragraph * par = inset.paragraph();
417 pos_type siz = par->size();
418 for (pos_type i = 0; i < siz; ++i) {
419 Paragraph::value_type c = par->getChar(i);
421 case Paragraph::META_NEWLINE:
441 Inset::RESULT InsetERT::localDispatch(FuncRequest const & cmd)
443 Inset::RESULT result = DISPATCHED_NOUPDATE;
444 BufferView * bv = cmd.view();
446 if (inset.paragraph()->empty()) {
450 switch (cmd.action) {
451 case LFUN_MOUSE_PRESS:
455 case LFUN_MOUSE_MOTION:
456 lfunMouseMotion(cmd);
459 case LFUN_MOUSE_RELEASE:
460 lfunMouseRelease(cmd);
464 bv->owner()->setLayout(inset.paragraph()->layout()->name());
468 result = InsetCollapsable::localDispatch(cmd);
471 switch (cmd.action) {
472 case LFUN_BREAKPARAGRAPH:
473 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
475 case LFUN_BACKSPACE_SKIP:
477 case LFUN_DELETE_SKIP:
478 case LFUN_DELETE_LINE_FORWARD:
490 string const InsetERT::get_new_label() const
493 pos_type const max_length = 15;
494 pos_type const p_siz = inset.paragraph()->size();
495 pos_type const n = min(max_length, p_siz);
498 for(; i < n && j < p_siz; ++j) {
499 if (inset.paragraph()->isInset(j))
501 la += inset.paragraph()->getChar(j);
504 if (inset.paragraph()->next() || (i > 0 && j < p_siz)) {
514 void InsetERT::setButtonLabel() const
516 if (status_ == Collapsed) {
517 setLabel(get_new_label());
524 bool InsetERT::checkInsertChar(LyXFont & /* font */)
527 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
529 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
530 font.setColor(LColor::latex);
536 int InsetERT::ascent(BufferView * bv, LyXFont const & font) const
539 return InsetCollapsable::ascent(bv, font);
541 return inset.ascent(bv, font);
545 int InsetERT::descent(BufferView * bv, LyXFont const & font) const
548 return InsetCollapsable::descent(bv, font);
550 return inset.descent(bv, font);
554 int InsetERT::width(BufferView * bv, LyXFont const & font) const
557 return InsetCollapsable::width(bv, font);
559 return inset.width(bv, font);
563 void InsetERT::draw(BufferView * bv, LyXFont const & f,
564 int baseline, float & x, bool cleared) const
566 Painter & pain = bv->painter();
568 button_length = width_collapsed();
569 button_top_y = -ascent(bv, f);
570 button_bottom_y = -ascent(bv, f) + ascent_collapsed() +
574 draw_collapsed(pain, baseline, x);
581 x += static_cast<float>(scroll());
583 if (!cleared && (inset.need_update == InsetText::FULL ||
584 inset.need_update == InsetText::INIT ||
586 top_baseline != baseline))
588 // we don't need anymore to clear here we just have to tell
589 // the underlying LyXText that it should do the RowClear!
590 inset.setUpdateStatus(bv, InsetText::FULL);
591 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
597 top_baseline = baseline;
599 int const bl = baseline - ascent(bv, f) + ascent_collapsed();
602 inset.draw(bv, f, baseline, x, cleared);
604 draw_collapsed(pain, bl, old_x);
606 bl + descent_collapsed() + inset.ascent(bv, f),
613 void InsetERT::set_latex_font(BufferView * /* bv */)
616 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
618 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
619 font.setColor(LColor::latex);
621 inset.getLyXText(bv)->setFont(bv, font, false);
626 // attention this function can be called with bv == 0
627 void InsetERT::status(BufferView * bv, ERTStatus const st) const
635 inset.setUpdateStatus(bv, InsetText::INIT);
645 bv->unlockInset(const_cast<InsetERT *>(this));
649 bv->updateInset(const_cast<InsetERT *>(this), false);
654 bool InsetERT::showInsetDialog(BufferView * bv) const
656 bv->owner()->getDialogs().showERT(const_cast<InsetERT *>(this));
661 void InsetERT::open(BufferView * bv)
669 void InsetERT::close(BufferView * bv) const
671 if (status_ == Collapsed || status_ == Inlined)
674 status(bv, Collapsed);
679 InsetERT::selectNextWordToSpellcheck(BufferView * bv, float &) const
681 bv->unlockInset(const_cast<InsetERT *>(this));
682 return WordLangTuple();
686 void InsetERT::getDrawFont(LyXFont & font) const
688 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
690 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
691 font.setColor(LColor::latex);
695 int InsetERT::getMaxWidth(BufferView * bv, UpdatableInset const * in) const
697 int w = InsetCollapsable::getMaxWidth(bv, in);
698 if (status_ != Inlined || w < 0)
700 LyXText * text = inset.getLyXText(bv);
701 int rw = text->firstRow()->width();
705 if (!text->firstRow()->next() && rw < w)
711 void InsetERT::update(BufferView * bv, LyXFont const & font,
714 if (inset.need_update & InsetText::INIT ||
715 inset.need_update & InsetText::FULL)
719 InsetCollapsable::update(bv, font, reinit);