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);
428 InsetERT::priv_dispatch(FuncRequest const & cmd,
429 idx_type & idx, pos_type & pos)
431 DispatchResult result = DispatchResult(false);
432 BufferView * bv = cmd.view();
434 if (inset.paragraphs.begin()->empty()) {
438 switch (cmd.action) {
440 case LFUN_INSET_EDIT:
441 if (cmd.button() == mouse_button::button3)
443 if (status_ == Inlined) {
444 if (!bv->lockInset(this))
446 result = inset.dispatch(cmd);
448 // Is the following line correct? Ab
450 result = InsetCollapsable::priv_dispatch(cmd, idx, pos);
456 case LFUN_INSET_MODIFY: {
457 InsetERT::ERTStatus status_;
458 InsetERTMailer::string2params(cmd.argument, status_);
462 /* FIXME: I refuse to believe we have to live
463 * with ugliness like this ! Note that this
464 * rebreak *is* needed. Consider a change from
465 * Open (needfullrow) to Inlined (only the space
466 * taken by the text).
468 inset.getLyXText(cmd.view())->fullRebreak();
469 bv->updateInset(this);
470 result = DispatchResult(true, true);
474 case LFUN_MOUSE_PRESS:
476 result = DispatchResult(true, true);
479 case LFUN_MOUSE_MOTION:
480 lfunMouseMotion(cmd);
481 result = DispatchResult(true, true);
484 case LFUN_MOUSE_RELEASE:
485 lfunMouseRelease(cmd);
486 result = DispatchResult(true, true);
490 bv->owner()->setLayout(inset.paragraphs.begin()->layout()->name());
491 result = DispatchResult(true);
495 result = InsetCollapsable::priv_dispatch(cmd, idx, pos);
498 switch (cmd.action) {
499 case LFUN_BREAKPARAGRAPH:
500 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
502 case LFUN_BACKSPACE_SKIP:
504 case LFUN_DELETE_SKIP:
505 case LFUN_DELETE_LINE_FORWARD:
517 string const InsetERT::get_new_label() const
520 pos_type const max_length = 15;
521 pos_type const p_siz = inset.paragraphs.begin()->size();
522 pos_type const n = min(max_length, p_siz);
525 for(; i < n && j < p_siz; ++j) {
526 if (inset.paragraphs.begin()->isInset(j))
528 la += inset.paragraphs.begin()->getChar(j);
531 if (inset.paragraphs.size() > 1 || (i > 0 && j < p_siz)) {
541 void InsetERT::setButtonLabel() const
543 if (status_ == Collapsed) {
544 setLabel(get_new_label());
551 bool InsetERT::checkInsertChar(LyXFont & /* font */)
554 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
556 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
557 font.setColor(LColor::latex);
563 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
567 inset.metrics(mi, dim);
569 InsetCollapsable::metrics(mi, dim);
570 // Make it stand out on its own as it is code, not part of running
572 if (isOpen() && !inlined())
573 dim.wid = mi.base.textwidth;
578 void InsetERT::draw(PainterInfo & pi, int x, int y) const
580 InsetCollapsable::draw(pi, x, y, inlined());
584 void InsetERT::set_latex_font(BufferView * /* bv */)
587 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
589 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
590 font.setColor(LColor::latex);
592 inset.getLyXText(bv)->setFont(bv, font, false);
597 // attention this function can be called with bv == 0
598 void InsetERT::status(BufferView * bv, ERTStatus const st) const
613 bv->unlockInset(const_cast<InsetERT *>(this));
617 bv->updateInset(this);
618 bv->buffer()->markDirty();
624 bool InsetERT::showInsetDialog(BufferView * bv) const
626 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
631 void InsetERT::open(BufferView * bv)
639 void InsetERT::close(BufferView * bv) const
641 if (status_ == Collapsed || status_ == Inlined)
644 status(bv, Collapsed);
648 void InsetERT::getDrawFont(LyXFont & font) const
650 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
652 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
653 font.setColor(LColor::latex);
657 string const InsetERTMailer::name_("ert");
659 InsetERTMailer::InsetERTMailer(InsetERT & inset)
664 string const InsetERTMailer::inset2string(Buffer const &) const
666 return params2string(inset_.status());
670 void InsetERTMailer::string2params(string const & in,
671 InsetERT::ERTStatus & status)
673 status = InsetERT::Collapsed;
676 string body = split(in, name, ' ');
681 status = static_cast<InsetERT::ERTStatus>(strToInt(body));
686 InsetERTMailer::params2string(InsetERT::ERTStatus status)
688 return name_ + ' ' + tostr(status);