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"
24 #include "metricsinfo.h"
25 #include "paragraph.h"
26 #include "WordLangTuple.h"
28 #include "frontends/Alert.h"
29 #include "frontends/LyXView.h"
31 #include "support/tostr.h"
35 using lyx::support::split;
36 using lyx::support::strToInt;
49 LyXFont font(LyXFont::ALL_SANE);
52 font.setColor(LColor::latex);
59 InsetERT::InsetERT(BufferParams const & bp, bool collapsed)
60 : InsetCollapsable(bp, collapsed)
70 InsetERT::InsetERT(InsetERT const & in)
71 : InsetCollapsable(in), status_(in.status_)
77 auto_ptr<InsetBase> InsetERT::clone() const
79 return auto_ptr<InsetBase>(new InsetERT(*this));
83 InsetERT::InsetERT(BufferParams const & bp,
84 Language const * l, string const & contents, bool collapsed)
85 : InsetCollapsable(bp, collapsed)
92 LyXFont font(LyXFont::ALL_INHERIT, l);
94 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
95 font.setColor(LColor::latex);
98 string::const_iterator cit = contents.begin();
99 string::const_iterator end = contents.end();
101 for (; cit != end; ++cit) {
102 inset.paragraphs.begin()->insertChar(pos++, *cit, font);
104 // the init has to be after the initialization of the paragraph
105 // because of the label settings (draw_label for ert insets).
110 InsetERT::~InsetERT()
112 InsetERTMailer(*this).hideDialog();
116 void InsetERT::read(Buffer const & buf, LyXLex & lex)
118 bool token_found = false;
121 string const token = lex.getString();
122 if (token == "status") {
124 string const tmp_token = lex.getString();
126 if (tmp_token == "Inlined") {
128 } else if (tmp_token == "Collapsed") {
129 status(0, Collapsed);
131 // leave this as default!
137 lyxerr << "InsetERT::Read: Missing 'status'-tag!"
139 // take countermeasures
140 lex.pushToken(token);
144 #warning this should be really short lived only for compatibility to
145 #warning files written 07/08/2001 so this has to go before 1.2.0! (Jug)
148 string const token = lex.getString();
149 if (token == "collapsed") {
151 setCollapsed(lex.getBool());
153 // Take countermeasures
154 lex.pushToken(token);
158 inset.read(buf, lex);
161 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
162 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
163 font.setColor(LColor::latex);
165 ParagraphList::iterator pit = inset.paragraphs.begin();
166 ParagraphList::iterator pend = inset.paragraphs.end();
167 for (; pit != pend; ++pit) {
168 pos_type siz = pit->size();
169 for (pos_type i = 0; i < siz; ++i) {
170 pit->setFont(i, font);
179 status(0, Collapsed);
185 void InsetERT::write(Buffer const & buf, ostream & os) const
201 os << getInsetName() << "\n"
202 << "status "<< st << "\n";
204 //inset.writeParagraphData(buf, os);
205 string const layout(buf.params().getLyXTextClass().defaultLayoutName());
206 ParagraphList::iterator par = inset.paragraphs.begin();
207 ParagraphList::iterator end = inset.paragraphs.end();
208 for (; par != end; ++par) {
209 os << "\n\\begin_layout " << layout << "\n";
210 pos_type siz = par->size();
211 for (pos_type i = 0; i < siz; ++i) {
212 Paragraph::value_type c = par->getChar(i);
214 case Paragraph::META_INSET:
215 if (par->getInset(i)->lyxCode() != InsetOld::NEWLINE_CODE) {
216 lyxerr << "Element is not allowed in insertERT"
219 par->getInset(i)->write(buf, os);
224 os << "\n\\backslash \n";
231 os << "\n\\end_layout\n";
236 string const InsetERT::editMessage() const
238 return _("Opened ERT Inset");
242 bool InsetERT::insertInset(BufferView *, InsetOld *)
248 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
251 #warning FIXME. More UI stupidity...
253 // if selectall is activated then the fontchange was an outside general
254 // fontchange and this messages is not needed
256 Alert::error(_("Cannot change font"),
257 _("You cannot change font settings inside TeX code."));
261 void InsetERT::updateStatus(BufferView * bv, bool swap) const
263 if (status_ != Inlined) {
265 status(bv, swap ? Collapsed : Open);
267 status(bv, swap ? Open : Collapsed);
272 InsetOld::EDITABLE InsetERT::editable() const
274 if (status_ == Collapsed)
276 return HIGHLY_EDITABLE;
280 void InsetERT::lfunMousePress(FuncRequest const & cmd)
282 if (status_ == Inlined)
287 InsetCollapsable::priv_dispatch(cmd, idx, pos);
292 bool InsetERT::lfunMouseRelease(FuncRequest const & cmd)
294 BufferView * bv = cmd.view();
296 if (cmd.button() == mouse_button::button3) {
301 if (status_ != Inlined && hitButton(cmd)) {
302 updateStatus(bv, true);
304 FuncRequest cmd1 = cmd;
306 cmd1.y = ascent() + cmd.y - inset.ascent();
308 // inlined is special - the text appears above
309 if (status_ == Inlined)
310 inset.dispatch(cmd1);
311 else if (isOpen() && (cmd.y > buttonDim().y2)) {
312 cmd1.y -= height_collapsed();
313 inset.dispatch(cmd1);
320 void InsetERT::lfunMouseMotion(FuncRequest const & cmd)
322 if (status_ == Inlined)
327 InsetCollapsable::priv_dispatch(cmd, idx, pos);
332 int InsetERT::latex(Buffer const &, ostream & os,
333 LatexRunParams const &) const
335 ParagraphList::iterator par = inset.paragraphs.begin();
336 ParagraphList::iterator end = inset.paragraphs.end();
340 pos_type siz = par->size();
341 for (pos_type i = 0; i < siz; ++i) {
342 // ignore all struck out text
343 if (isDeletedText(*par, i))
346 if (par->isNewline(i)) {
350 os << par->getChar(i);
364 int InsetERT::ascii(Buffer const &, ostream &, int /*linelen*/) const
370 int InsetERT::linuxdoc(Buffer const &, ostream & os) const
372 ParagraphList::iterator par = inset.paragraphs.begin();
373 ParagraphList::iterator end = inset.paragraphs.end();
377 pos_type siz = par->size();
378 for (pos_type i = 0; i < siz; ++i) {
379 if (par->isNewline(i)) {
383 os << par->getChar(i);
397 int InsetERT::docbook(Buffer const &, ostream & os, bool) const
399 ParagraphList::iterator par = inset.paragraphs.begin();
400 ParagraphList::iterator end = inset.paragraphs.end();
404 pos_type siz = par->size();
405 for (pos_type i = 0; i < siz; ++i) {
406 if (par->isNewline(i)) {
410 os << par->getChar(i);
425 InsetERT::priv_dispatch(FuncRequest const & cmd,
426 idx_type & idx, pos_type & pos)
428 dispatch_result result = UNDISPATCHED;
429 BufferView * bv = cmd.view();
431 if (inset.paragraphs.begin()->empty()) {
435 switch (cmd.action) {
437 case LFUN_INSET_EDIT:
438 if (cmd.button() == mouse_button::button3)
440 if (status_ == Inlined) {
441 if (!bv->lockInset(this))
443 result = inset.dispatch(cmd);
445 result = InsetCollapsable::priv_dispatch(cmd, idx, pos);
451 case LFUN_INSET_MODIFY: {
452 InsetERT::ERTStatus status_;
453 InsetERTMailer::string2params(cmd.argument, status_);
457 /* FIXME: I refuse to believe we have to live
458 * with ugliness like this ! Note that this
459 * rebreak *is* needed. Consider a change from
460 * Open (needfullrow) to Inlined (only the space
461 * taken by the text).
463 inset.getLyXText(cmd.view())->fullRebreak();
464 bv->updateInset(this);
469 case LFUN_MOUSE_PRESS:
474 case LFUN_MOUSE_MOTION:
475 lfunMouseMotion(cmd);
479 case LFUN_MOUSE_RELEASE:
480 lfunMouseRelease(cmd);
485 bv->owner()->setLayout(inset.paragraphs.begin()->layout()->name());
486 result = DISPATCHED_NOUPDATE;
490 result = InsetCollapsable::priv_dispatch(cmd, idx, pos);
493 switch (cmd.action) {
494 case LFUN_BREAKPARAGRAPH:
495 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
497 case LFUN_BACKSPACE_SKIP:
499 case LFUN_DELETE_SKIP:
500 case LFUN_DELETE_LINE_FORWARD:
512 string const InsetERT::get_new_label() const
515 pos_type const max_length = 15;
516 pos_type const p_siz = inset.paragraphs.begin()->size();
517 pos_type const n = min(max_length, p_siz);
520 for(; i < n && j < p_siz; ++j) {
521 if (inset.paragraphs.begin()->isInset(j))
523 la += inset.paragraphs.begin()->getChar(j);
526 if (inset.paragraphs.size() > 1 || (i > 0 && j < p_siz)) {
536 void InsetERT::setButtonLabel() const
538 if (status_ == Collapsed) {
539 setLabel(get_new_label());
546 bool InsetERT::checkInsertChar(LyXFont & /* font */)
549 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
551 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
552 font.setColor(LColor::latex);
558 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
562 inset.metrics(mi, dim);
564 InsetCollapsable::metrics(mi, dim);
565 // Make it stand out on its own as it is code, not part of running
567 if (isOpen() && !inlined())
568 dim.wid = mi.base.textwidth;
573 void InsetERT::draw(PainterInfo & pi, int x, int y) const
575 InsetCollapsable::draw(pi, x, y, 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
608 bv->unlockInset(const_cast<InsetERT *>(this));
612 bv->updateInset(this);
613 bv->buffer()->markDirty();
619 bool InsetERT::showInsetDialog(BufferView * bv) const
621 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
626 void InsetERT::open(BufferView * bv)
634 void InsetERT::close(BufferView * bv) const
636 if (status_ == Collapsed || status_ == Inlined)
639 status(bv, Collapsed);
644 InsetERT::selectNextWordToSpellcheck(BufferView * bv, float &) const
646 bv->unlockInset(const_cast<InsetERT *>(this));
647 return WordLangTuple();
651 void InsetERT::getDrawFont(LyXFont & font) const
653 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
655 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
656 font.setColor(LColor::latex);
660 string const InsetERTMailer::name_("ert");
662 InsetERTMailer::InsetERTMailer(InsetERT & inset)
667 string const InsetERTMailer::inset2string(Buffer const &) const
669 return params2string(inset_.status());
673 void InsetERTMailer::string2params(string const & in,
674 InsetERT::ERTStatus & status)
676 status = InsetERT::Collapsed;
679 string body = split(in, name, ' ');
684 status = static_cast<InsetERT::ERTStatus>(strToInt(body));
689 InsetERTMailer::params2string(InsetERT::ERTStatus status)
691 return name_ + ' ' + tostr(status);