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 "dispatchresult.h"
20 #include "funcrequest.h"
25 #include "metricsinfo.h"
26 #include "paragraph.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 &,
365 LatexRunParams const & /*runparams*/) const
371 int InsetERT::linuxdoc(Buffer const &, ostream & os,
372 LatexRunParams const &)const
374 ParagraphList::iterator par = inset.paragraphs.begin();
375 ParagraphList::iterator end = inset.paragraphs.end();
379 pos_type siz = par->size();
380 for (pos_type i = 0; i < siz; ++i) {
381 if (par->isNewline(i)) {
385 os << par->getChar(i);
399 int InsetERT::docbook(Buffer const &, ostream & os,
400 LatexRunParams const &) const
402 ParagraphList::iterator par = inset.paragraphs.begin();
403 ParagraphList::iterator end = inset.paragraphs.end();
407 pos_type siz = par->size();
408 for (pos_type i = 0; i < siz; ++i) {
409 if (par->isNewline(i)) {
413 os << par->getChar(i);
427 void InsetERT::edit(BufferView * bv, bool left)
429 if (status_ == Inlined) {
430 if (!bv->lockInset(this))
432 inset.edit(bv, left);
434 InsetCollapsable::edit(bv, left);
442 InsetERT::priv_dispatch(FuncRequest const & cmd,
443 idx_type & idx, pos_type & pos)
445 BufferView * bv = cmd.view();
447 if (inset.paragraphs.begin()->empty())
450 switch (cmd.action) {
452 case LFUN_INSET_MODIFY: {
453 InsetERT::ERTStatus status_;
454 InsetERTMailer::string2params(cmd.argument, status_);
458 /* FIXME: I refuse to believe we have to live
459 * with ugliness like this ! Note that this
460 * rebreak *is* needed. Consider a change from
461 * Open (needfullrow) to Inlined (only the space
462 * taken by the text).
464 inset.getLyXText(cmd.view())->fullRebreak();
465 bv->updateInset(this);
466 return DispatchResult(true, true);
469 case LFUN_MOUSE_PRESS:
471 return DispatchResult(true, true);
473 case LFUN_MOUSE_MOTION:
474 lfunMouseMotion(cmd);
475 return DispatchResult(true, true);
477 case LFUN_MOUSE_RELEASE:
478 lfunMouseRelease(cmd);
479 return DispatchResult(true, true);
482 bv->owner()->setLayout(inset.paragraphs.begin()->layout()->name());
483 return DispatchResult(true);
485 case LFUN_BREAKPARAGRAPH:
486 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
488 case LFUN_BACKSPACE_SKIP:
490 case LFUN_DELETE_SKIP:
491 case LFUN_DELETE_LINE_FORWARD:
494 return DispatchResult(false);
497 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
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);
633 void InsetERT::getDrawFont(LyXFont & font) const
635 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
637 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
638 font.setColor(LColor::latex);
642 string const InsetERTMailer::name_("ert");
644 InsetERTMailer::InsetERTMailer(InsetERT & inset)
649 string const InsetERTMailer::inset2string(Buffer const &) const
651 return params2string(inset_.status());
655 void InsetERTMailer::string2params(string const & in,
656 InsetERT::ERTStatus & status)
658 status = InsetERT::Collapsed;
661 string body = split(in, name, ' ');
666 status = static_cast<InsetERT::ERTStatus>(strToInt(body));
671 InsetERTMailer::params2string(InsetERT::ERTStatus status)
673 return name_ + ' ' + tostr(status);