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/tostr.h"
36 using namespace lyx::support;
48 labelfont = LyXFont(LyXFont::ALL_SANE);
51 labelfont.setColor(LColor::latex);
56 InsetERT::InsetERT(BufferParams const & bp, bool collapsed)
57 : InsetCollapsable(bp, collapsed)
67 InsetERT::InsetERT(InsetERT const & in)
68 : InsetCollapsable(in), status_(in.status_)
74 InsetBase * InsetERT::clone() const
76 return new InsetERT(*this);
80 InsetERT::InsetERT(BufferParams const & bp,
81 Language const * l, string const & contents, bool collapsed)
82 : InsetCollapsable(bp, collapsed)
89 LyXFont font(LyXFont::ALL_INHERIT, l);
91 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
92 font.setColor(LColor::latex);
95 string::const_iterator cit = contents.begin();
96 string::const_iterator end = contents.end();
98 for (; cit != end; ++cit) {
99 inset.paragraphs.begin()->insertChar(pos++, *cit, font);
101 // the init has to be after the initialization of the paragraph
102 // because of the label settings (draw_label for ert insets).
107 InsetERT::~InsetERT()
109 InsetERTMailer mailer(*this);
114 void InsetERT::read(Buffer const * buf, LyXLex & lex)
116 bool token_found = false;
119 string const token = lex.getString();
120 if (token == "status") {
122 string const tmp_token = lex.getString();
124 if (tmp_token == "Inlined") {
126 } else if (tmp_token == "Collapsed") {
127 status(0, Collapsed);
129 // leave this as default!
135 lyxerr << "InsetERT::Read: Missing 'status'-tag!"
137 // take countermeasures
138 lex.pushToken(token);
142 #warning this should be really short lived only for compatibility to
143 #warning files written 07/08/2001 so this has to go before 1.2.0! (Jug)
146 string const token = lex.getString();
147 if (token == "collapsed") {
149 collapsed_ = lex.getBool();
151 // Take countermeasures
152 lex.pushToken(token);
156 inset.read(buf, lex);
159 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
160 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
161 font.setColor(LColor::latex);
163 ParagraphList::iterator pit = inset.paragraphs.begin();
164 ParagraphList::iterator pend = inset.paragraphs.end();
165 for (; pit != pend; ++pit) {
166 pos_type siz = pit->size();
167 for (pos_type i = 0; i < siz; ++i) {
168 pit->setFont(i, font);
175 status(0, Collapsed);
184 void InsetERT::write(Buffer const * buf, ostream & os) const
200 os << getInsetName() << "\n"
201 << "status "<< st << "\n";
203 //inset.writeParagraphData(buf, os);
204 string const layout(buf->params.getLyXTextClass().defaultLayoutName());
205 ParagraphList::iterator par = inset.paragraphs.begin();
206 ParagraphList::iterator end = inset.paragraphs.end();
207 for (; par != end; ++par) {
208 os << "\n\\layout " << layout << "\n";
209 pos_type siz = par->size();
210 for (pos_type i = 0; i < siz; ++i) {
211 Paragraph::value_type c = par->getChar(i);
213 case Paragraph::META_INSET:
214 if (par->getInset(i)->lyxCode() != Inset::NEWLINE_CODE) {
215 lyxerr << "Element is not allowed in insertERT"
218 par->getInset(i)->write(buf, os);
223 os << "\n\\backslash \n";
234 string const InsetERT::editMessage() const
236 return _("Opened ERT Inset");
240 bool InsetERT::insertInset(BufferView *, Inset *)
246 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
249 #warning FIXME. More UI stupidity...
251 // if selectall is activated then the fontchange was an outside general
252 // fontchange and this messages is not needed
254 Alert::error(_("Cannot change font"),
255 _("You cannot change font settings inside TeX code."));
259 void InsetERT::updateStatus(BufferView * bv, bool swap) const
261 if (status_ != Inlined) {
263 status(bv, swap ? Open : Collapsed);
265 status(bv, swap ? Collapsed : Open);
271 Inset::EDITABLE InsetERT::editable() const
273 if (status_ == Collapsed)
275 return HIGHLY_EDITABLE;
279 void InsetERT::lfunMousePress(FuncRequest const & cmd)
281 if (status_ == Inlined)
282 inset.localDispatch(cmd);
284 InsetCollapsable::localDispatch(cmd);
288 bool InsetERT::lfunMouseRelease(FuncRequest const & cmd)
290 BufferView * bv = cmd.view();
292 if (cmd.button() == mouse_button::button3) {
297 if (status_ != Inlined && (cmd.x >= 0) && (cmd.x < button_length) &&
298 (cmd.y >= button_top_y) && (cmd.y <= button_bottom_y)) {
299 updateStatus(bv, true);
301 LyXFont font(LyXFont::ALL_SANE);
302 FuncRequest cmd1 = cmd;
303 cmd1.y = ascent(bv, font) + cmd.y - inset.ascent(bv, font);
305 // inlined is special - the text appears above
307 if (status_ == Inlined)
308 inset.localDispatch(cmd1);
309 else if (!collapsed_ && (cmd.y > button_bottom_y)) {
310 cmd1.y -= height_collapsed();
311 inset.localDispatch(cmd1);
318 void InsetERT::lfunMouseMotion(FuncRequest const & cmd)
320 if (status_ == Inlined)
321 inset.localDispatch(cmd);
323 InsetCollapsable::localDispatch(cmd);
327 int InsetERT::latex(Buffer const *, ostream & os,
328 LatexRunParams const &) const
330 ParagraphList::iterator par = inset.paragraphs.begin();
331 ParagraphList::iterator end = inset.paragraphs.end();
335 pos_type siz = par->size();
336 for (pos_type i = 0; i < siz; ++i) {
337 // ignore all struck out text
338 if (isDeletedText(*par, i))
341 if (par->isNewline(i)) {
345 os << par->getChar(i);
359 int InsetERT::ascii(Buffer const *, ostream &, int /*linelen*/) const
365 int InsetERT::linuxdoc(Buffer const *, ostream & os) const
367 ParagraphList::iterator par = inset.paragraphs.begin();
368 ParagraphList::iterator end = inset.paragraphs.end();
372 pos_type siz = par->size();
373 for (pos_type i = 0; i < siz; ++i) {
374 if (par->isNewline(i)) {
378 os << par->getChar(i);
392 int InsetERT::docbook(Buffer const *, ostream & os, bool) const
394 ParagraphList::iterator par = inset.paragraphs.begin();
395 ParagraphList::iterator end = inset.paragraphs.end();
399 pos_type siz = par->size();
400 for (pos_type i = 0; i < siz; ++i) {
401 if (par->isNewline(i)) {
405 os << par->getChar(i);
419 Inset::RESULT InsetERT::localDispatch(FuncRequest const & cmd)
421 Inset::RESULT result = UNDISPATCHED;
422 BufferView * bv = cmd.view();
424 if (inset.paragraphs.begin()->empty()) {
428 switch (cmd.action) {
430 case LFUN_INSET_EDIT:
431 if (cmd.button() == mouse_button::button3)
433 if (status_ == Inlined) {
434 if (!bv->lockInset(this))
436 result = inset.localDispatch(cmd);
438 result = InsetCollapsable::localDispatch(cmd);
444 case LFUN_INSET_MODIFY: {
445 InsetERT::ERTStatus status_;
446 InsetERTMailer::string2params(cmd.argument, status_);
450 /* FIXME: I refuse to believe we have to live
451 * with ugliness like this ! Note that this
452 * rebreak *is* needed. Consider a change from
453 * Open (needfullrow) to Inlined (only the space
454 * taken by the text).
456 inset.getLyXText(cmd.view())->fullRebreak();
457 inset.update(cmd.view(), true);
458 bv->updateInset(this);
463 case LFUN_MOUSE_PRESS:
468 case LFUN_MOUSE_MOTION:
469 lfunMouseMotion(cmd);
473 case LFUN_MOUSE_RELEASE:
474 lfunMouseRelease(cmd);
479 bv->owner()->setLayout(inset.paragraphs.begin()->layout()->name());
480 result = DISPATCHED_NOUPDATE;
484 result = InsetCollapsable::localDispatch(cmd);
487 switch (cmd.action) {
488 case LFUN_BREAKPARAGRAPH:
489 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
491 case LFUN_BACKSPACE_SKIP:
493 case LFUN_DELETE_SKIP:
494 case LFUN_DELETE_LINE_FORWARD:
506 string const InsetERT::get_new_label() const
509 pos_type const max_length = 15;
510 pos_type const p_siz = inset.paragraphs.begin()->size();
511 pos_type const n = min(max_length, p_siz);
514 for(; i < n && j < p_siz; ++j) {
515 if (inset.paragraphs.begin()->isInset(j))
517 la += inset.paragraphs.begin()->getChar(j);
520 if (p_siz > 1 || (i > 0 && j < p_siz)) {
530 void InsetERT::setButtonLabel() const
532 if (status_ == Collapsed) {
533 setLabel(get_new_label());
540 bool InsetERT::checkInsertChar(LyXFont & /* font */)
543 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
545 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
546 font.setColor(LColor::latex);
552 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
555 inset.metrics(mi, dim);
557 InsetCollapsable::metrics(mi, dim);
561 void InsetERT::draw(PainterInfo & pi, int x, int y) const
563 InsetCollapsable::draw(pi, x, y, inlined());
567 void InsetERT::set_latex_font(BufferView * /* bv */)
570 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
572 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
573 font.setColor(LColor::latex);
575 inset.getLyXText(bv)->setFont(bv, font, false);
580 // attention this function can be called with bv == 0
581 void InsetERT::status(BufferView * bv, ERTStatus const st) const
588 inset.setUpdateStatus(InsetText::INIT);
598 bv->unlockInset(const_cast<InsetERT *>(this));
602 bv->updateInset(const_cast<InsetERT *>(this));
603 bv->buffer()->markDirty();
609 bool InsetERT::showInsetDialog(BufferView * bv) const
611 InsetERTMailer mailer(const_cast<InsetERT &>(*this));
612 mailer.showDialog(bv);
617 void InsetERT::open(BufferView * bv)
625 void InsetERT::close(BufferView * bv) const
627 if (status_ == Collapsed || status_ == Inlined)
630 status(bv, Collapsed);
635 InsetERT::selectNextWordToSpellcheck(BufferView * bv, float &) const
637 bv->unlockInset(const_cast<InsetERT *>(this));
638 return WordLangTuple();
642 void InsetERT::getDrawFont(LyXFont & font) const
644 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
646 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
647 font.setColor(LColor::latex);
651 int InsetERT::getMaxWidth(BufferView * bv, UpdatableInset const * in) const
653 int w = InsetCollapsable::getMaxWidth(bv, in);
654 if (status_ != Inlined || w < 0)
656 LyXText * text = inset.getLyXText(bv);
657 int rw = text->rows().begin()->width();
661 if (text->rows().size() == 1 && rw < w)
667 void InsetERT::update(BufferView * bv, bool reinit)
669 if (inset.need_update & InsetText::INIT ||
670 inset.need_update & InsetText::FULL) {
674 InsetCollapsable::update(bv, reinit);
678 string const InsetERTMailer::name_("ert");
680 InsetERTMailer::InsetERTMailer(InsetERT & inset)
685 string const InsetERTMailer::inset2string() const
687 return params2string(inset_.status());
691 void InsetERTMailer::string2params(string const & in,
692 InsetERT::ERTStatus & status)
694 status = InsetERT::Collapsed;
697 string body = split(in, name, ' ');
702 status = static_cast<InsetERT::ERTStatus>(strToInt(body));
707 InsetERTMailer::params2string(InsetERT::ERTStatus status)
709 return name_ + ' ' + tostr(status);