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.
16 #include "bufferparams.h"
17 #include "BufferView.h"
19 #include "funcrequest.h"
23 #include "metricsinfo.h"
24 #include "paragraph.h"
25 #include "WordLangTuple.h"
27 #include "frontends/Alert.h"
28 #include "frontends/LyXView.h"
30 #include "support/tostr.h"
32 using namespace lyx::support;
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)
66 : InsetCollapsable(in), status_(in.status_)
72 auto_ptr<InsetBase> InsetERT::clone() const
74 return auto_ptr<InsetBase>(new InsetERT(*this));
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.paragraphs.begin()->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()
107 InsetERTMailer(*this).hideDialog();
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);
160 ParagraphList::iterator pit = inset.paragraphs.begin();
161 ParagraphList::iterator pend = inset.paragraphs.end();
162 for (; pit != pend; ++pit) {
163 pos_type siz = pit->size();
164 for (pos_type i = 0; i < siz; ++i) {
165 pit->setFont(i, font);
172 status(0, Collapsed);
181 void InsetERT::write(Buffer const & buf, ostream & os) const
197 os << getInsetName() << "\n"
198 << "status "<< st << "\n";
200 //inset.writeParagraphData(buf, os);
201 string const layout(buf.params().getLyXTextClass().defaultLayoutName());
202 ParagraphList::iterator par = inset.paragraphs.begin();
203 ParagraphList::iterator end = inset.paragraphs.end();
204 for (; par != end; ++par) {
205 os << "\n\\begin_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() != InsetOld::NEWLINE_CODE) {
212 lyxerr << "Element is not allowed in insertERT"
215 par->getInset(i)->write(buf, os);
220 os << "\n\\backslash \n";
227 os << "\n\\end_layout\n";
232 string const InsetERT::editMessage() const
234 return _("Opened ERT Inset");
238 bool InsetERT::insertInset(BufferView *, InsetOld *)
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);
269 InsetOld::EDITABLE InsetERT::editable() const
271 if (status_ == Collapsed)
273 return HIGHLY_EDITABLE;
277 void InsetERT::lfunMousePress(FuncRequest const & cmd)
279 if (status_ == Inlined)
280 inset.localDispatch(cmd);
282 InsetCollapsable::localDispatch(cmd);
286 bool InsetERT::lfunMouseRelease(FuncRequest const & cmd)
288 BufferView * bv = cmd.view();
290 if (cmd.button() == mouse_button::button3) {
295 if (status_ != Inlined && hitButton(cmd)) {
296 updateStatus(bv, true);
298 FuncRequest cmd1 = cmd;
300 cmd1.y = ascent() + cmd.y - inset.ascent();
302 // inlined is special - the text appears above
304 if (status_ == Inlined)
305 inset.localDispatch(cmd1);
306 else if (!collapsed_ && (cmd.y > button_dim.y2)) {
307 cmd1.y -= height_collapsed();
308 inset.localDispatch(cmd1);
315 void InsetERT::lfunMouseMotion(FuncRequest const & cmd)
317 if (status_ == Inlined)
318 inset.localDispatch(cmd);
320 InsetCollapsable::localDispatch(cmd);
324 int InsetERT::latex(Buffer const &, ostream & os,
325 LatexRunParams const &) const
327 ParagraphList::iterator par = inset.paragraphs.begin();
328 ParagraphList::iterator end = inset.paragraphs.end();
332 pos_type siz = par->size();
333 for (pos_type i = 0; i < siz; ++i) {
334 // ignore all struck out text
335 if (isDeletedText(*par, i))
338 if (par->isNewline(i)) {
342 os << par->getChar(i);
356 int InsetERT::ascii(Buffer const &, ostream &, int /*linelen*/) const
362 int InsetERT::linuxdoc(Buffer const &, ostream & os) const
364 ParagraphList::iterator par = inset.paragraphs.begin();
365 ParagraphList::iterator end = inset.paragraphs.end();
369 pos_type siz = par->size();
370 for (pos_type i = 0; i < siz; ++i) {
371 if (par->isNewline(i)) {
375 os << par->getChar(i);
389 int InsetERT::docbook(Buffer const &, ostream & os, bool) const
391 ParagraphList::iterator par = inset.paragraphs.begin();
392 ParagraphList::iterator end = inset.paragraphs.end();
396 pos_type siz = par->size();
397 for (pos_type i = 0; i < siz; ++i) {
398 if (par->isNewline(i)) {
402 os << par->getChar(i);
416 InsetOld::RESULT InsetERT::localDispatch(FuncRequest const & cmd)
418 InsetOld::RESULT result = UNDISPATCHED;
419 BufferView * bv = cmd.view();
421 if (inset.paragraphs.begin()->empty()) {
425 switch (cmd.action) {
427 case LFUN_INSET_EDIT:
428 if (cmd.button() == mouse_button::button3)
430 if (status_ == Inlined) {
431 if (!bv->lockInset(this))
433 result = inset.localDispatch(cmd);
435 result = InsetCollapsable::localDispatch(cmd);
441 case LFUN_INSET_MODIFY: {
442 InsetERT::ERTStatus status_;
443 InsetERTMailer::string2params(cmd.argument, status_);
447 /* FIXME: I refuse to believe we have to live
448 * with ugliness like this ! Note that this
449 * rebreak *is* needed. Consider a change from
450 * Open (needfullrow) to Inlined (only the space
451 * taken by the text).
453 inset.getLyXText(cmd.view())->fullRebreak();
454 bv->updateInset(this);
459 case LFUN_MOUSE_PRESS:
464 case LFUN_MOUSE_MOTION:
465 lfunMouseMotion(cmd);
469 case LFUN_MOUSE_RELEASE:
470 lfunMouseRelease(cmd);
475 bv->owner()->setLayout(inset.paragraphs.begin()->layout()->name());
476 result = DISPATCHED_NOUPDATE;
480 result = InsetCollapsable::localDispatch(cmd);
483 switch (cmd.action) {
484 case LFUN_BREAKPARAGRAPH:
485 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
487 case LFUN_BACKSPACE_SKIP:
489 case LFUN_DELETE_SKIP:
490 case LFUN_DELETE_LINE_FORWARD:
502 string const InsetERT::get_new_label() const
505 pos_type const max_length = 15;
506 pos_type const p_siz = inset.paragraphs.begin()->size();
507 pos_type const n = min(max_length, p_siz);
510 for(; i < n && j < p_siz; ++j) {
511 if (inset.paragraphs.begin()->isInset(j))
513 la += inset.paragraphs.begin()->getChar(j);
516 if (inset.paragraphs.size() > 1 || (i > 0 && j < p_siz)) {
526 void InsetERT::setButtonLabel() const
528 if (status_ == Collapsed) {
529 setLabel(get_new_label());
536 bool InsetERT::checkInsertChar(LyXFont & /* font */)
539 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
541 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
542 font.setColor(LColor::latex);
548 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
552 inset.metrics(mi, dim);
554 InsetCollapsable::metrics(mi, dim);
555 // Make it stand out on its own as it is code, not part of running
557 if (isOpen() && !inlined())
558 dim.wid = mi.base.textwidth;
563 void InsetERT::draw(PainterInfo & pi, int x, int y) const
565 InsetCollapsable::draw(pi, x, y, inlined());
569 void InsetERT::set_latex_font(BufferView * /* bv */)
572 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
574 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
575 font.setColor(LColor::latex);
577 inset.getLyXText(bv)->setFont(bv, font, false);
582 // attention this function can be called with bv == 0
583 void InsetERT::status(BufferView * bv, ERTStatus const st) const
598 bv->unlockInset(const_cast<InsetERT *>(this));
602 bv->updateInset(this);
603 bv->buffer()->markDirty();
609 bool InsetERT::showInsetDialog(BufferView * bv) const
611 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
616 void InsetERT::open(BufferView * bv)
624 void InsetERT::close(BufferView * bv) const
626 if (status_ == Collapsed || status_ == Inlined)
629 status(bv, Collapsed);
634 InsetERT::selectNextWordToSpellcheck(BufferView * bv, float &) const
636 bv->unlockInset(const_cast<InsetERT *>(this));
637 return WordLangTuple();
641 void InsetERT::getDrawFont(LyXFont & font) const
643 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
645 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
646 font.setColor(LColor::latex);
650 string const InsetERTMailer::name_("ert");
652 InsetERTMailer::InsetERTMailer(InsetERT & inset)
657 string const InsetERTMailer::inset2string(Buffer const &) const
659 return params2string(inset_.status());
663 void InsetERTMailer::string2params(string const & in,
664 InsetERT::ERTStatus & status)
666 status = InsetERT::Collapsed;
669 string body = split(in, name, ' ');
674 status = static_cast<InsetERT::ERTStatus>(strToInt(body));
679 InsetERTMailer::params2string(InsetERT::ERTStatus status)
681 return name_ + ' ' + tostr(status);