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"
27 #include "WordLangTuple.h"
29 #include "frontends/Alert.h"
30 #include "frontends/LyXView.h"
32 #include "support/tostr.h"
36 using lyx::support::split;
37 using lyx::support::strToInt;
50 LyXFont font(LyXFont::ALL_SANE);
53 font.setColor(LColor::latex);
60 InsetERT::InsetERT(BufferParams const & bp, bool collapsed)
61 : InsetCollapsable(bp, collapsed)
71 InsetERT::InsetERT(InsetERT const & in)
72 : InsetCollapsable(in), status_(in.status_)
78 auto_ptr<InsetBase> InsetERT::clone() const
80 return auto_ptr<InsetBase>(new InsetERT(*this));
84 InsetERT::InsetERT(BufferParams const & bp,
85 Language const * l, string const & contents, bool collapsed)
86 : InsetCollapsable(bp, collapsed)
93 LyXFont font(LyXFont::ALL_INHERIT, l);
95 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
96 font.setColor(LColor::latex);
99 string::const_iterator cit = contents.begin();
100 string::const_iterator end = contents.end();
102 for (; cit != end; ++cit) {
103 inset.paragraphs.begin()->insertChar(pos++, *cit, font);
105 // the init has to be after the initialization of the paragraph
106 // because of the label settings (draw_label for ert insets).
111 InsetERT::~InsetERT()
113 InsetERTMailer(*this).hideDialog();
117 void InsetERT::read(Buffer const & buf, LyXLex & lex)
119 bool token_found = false;
122 string const token = lex.getString();
123 if (token == "status") {
125 string const tmp_token = lex.getString();
127 if (tmp_token == "Inlined") {
129 } else if (tmp_token == "Collapsed") {
130 status(0, Collapsed);
132 // leave this as default!
138 lyxerr << "InsetERT::Read: Missing 'status'-tag!"
140 // take countermeasures
141 lex.pushToken(token);
145 #warning this should be really short lived only for compatibility to
146 #warning files written 07/08/2001 so this has to go before 1.2.0! (Jug)
149 string const token = lex.getString();
150 if (token == "collapsed") {
152 setCollapsed(lex.getBool());
154 // Take countermeasures
155 lex.pushToken(token);
159 inset.read(buf, lex);
162 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
163 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
164 font.setColor(LColor::latex);
166 ParagraphList::iterator pit = inset.paragraphs.begin();
167 ParagraphList::iterator pend = inset.paragraphs.end();
168 for (; pit != pend; ++pit) {
169 pos_type siz = pit->size();
170 for (pos_type i = 0; i < siz; ++i) {
171 pit->setFont(i, font);
180 status(0, Collapsed);
186 void InsetERT::write(Buffer const & buf, ostream & os) const
202 os << getInsetName() << "\n"
203 << "status "<< st << "\n";
205 //inset.writeParagraphData(buf, os);
206 string const layout(buf.params().getLyXTextClass().defaultLayoutName());
207 ParagraphList::iterator par = inset.paragraphs.begin();
208 ParagraphList::iterator end = inset.paragraphs.end();
209 for (; par != end; ++par) {
210 os << "\n\\begin_layout " << layout << "\n";
211 pos_type siz = par->size();
212 for (pos_type i = 0; i < siz; ++i) {
213 Paragraph::value_type c = par->getChar(i);
215 case Paragraph::META_INSET:
216 if (par->getInset(i)->lyxCode() != InsetOld::NEWLINE_CODE) {
217 lyxerr << "Element is not allowed in insertERT"
220 par->getInset(i)->write(buf, os);
225 os << "\n\\backslash \n";
232 os << "\n\\end_layout\n";
237 string const InsetERT::editMessage() const
239 return _("Opened ERT Inset");
243 bool InsetERT::insertInset(BufferView *, InsetOld *)
249 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
252 #warning FIXME. More UI stupidity...
254 // if selectall is activated then the fontchange was an outside general
255 // fontchange and this messages is not needed
257 Alert::error(_("Cannot change font"),
258 _("You cannot change font settings inside TeX code."));
262 void InsetERT::updateStatus(BufferView * bv, bool swap) const
264 if (status_ != Inlined) {
266 status(bv, swap ? Collapsed : Open);
268 status(bv, swap ? Open : Collapsed);
273 InsetOld::EDITABLE InsetERT::editable() const
275 if (status_ == Collapsed)
277 return HIGHLY_EDITABLE;
281 void InsetERT::lfunMousePress(FuncRequest const & cmd)
283 if (status_ == Inlined)
288 InsetCollapsable::priv_dispatch(cmd, idx, pos);
293 bool InsetERT::lfunMouseRelease(FuncRequest const & cmd)
295 BufferView * bv = cmd.view();
297 if (cmd.button() == mouse_button::button3) {
302 if (status_ != Inlined && hitButton(cmd)) {
303 updateStatus(bv, true);
305 FuncRequest cmd1 = cmd;
307 cmd1.y = ascent() + cmd.y - inset.ascent();
309 // inlined is special - the text appears above
310 if (status_ == Inlined)
311 inset.dispatch(cmd1);
312 else if (isOpen() && (cmd.y > buttonDim().y2)) {
313 cmd1.y -= height_collapsed();
314 inset.dispatch(cmd1);
321 void InsetERT::lfunMouseMotion(FuncRequest const & cmd)
323 if (status_ == Inlined)
328 InsetCollapsable::priv_dispatch(cmd, idx, pos);
333 int InsetERT::latex(Buffer const &, ostream & os,
334 LatexRunParams const &) const
336 ParagraphList::iterator par = inset.paragraphs.begin();
337 ParagraphList::iterator end = inset.paragraphs.end();
341 pos_type siz = par->size();
342 for (pos_type i = 0; i < siz; ++i) {
343 // ignore all struck out text
344 if (isDeletedText(*par, i))
347 if (par->isNewline(i)) {
351 os << par->getChar(i);
365 int InsetERT::ascii(Buffer const &, ostream &,
366 LatexRunParams const & /*runparams*/) const
372 int InsetERT::linuxdoc(Buffer const &, ostream & os,
373 LatexRunParams const &)const
375 ParagraphList::iterator par = inset.paragraphs.begin();
376 ParagraphList::iterator end = inset.paragraphs.end();
380 pos_type siz = par->size();
381 for (pos_type i = 0; i < siz; ++i) {
382 if (par->isNewline(i)) {
386 os << par->getChar(i);
400 int InsetERT::docbook(Buffer const &, ostream & os,
401 LatexRunParams const &) const
403 ParagraphList::iterator par = inset.paragraphs.begin();
404 ParagraphList::iterator end = inset.paragraphs.end();
408 pos_type siz = par->size();
409 for (pos_type i = 0; i < siz; ++i) {
410 if (par->isNewline(i)) {
414 os << par->getChar(i);
429 InsetERT::priv_dispatch(FuncRequest const & cmd,
430 idx_type & idx, pos_type & pos)
432 DispatchResult result = DispatchResult(false);
433 BufferView * bv = cmd.view();
435 if (inset.paragraphs.begin()->empty()) {
439 switch (cmd.action) {
441 case LFUN_INSET_EDIT:
442 if (cmd.button() == mouse_button::button3)
444 if (status_ == Inlined) {
445 if (!bv->lockInset(this))
447 result = inset.dispatch(cmd);
449 result = InsetCollapsable::priv_dispatch(cmd, idx, pos);
455 case LFUN_INSET_MODIFY: {
456 InsetERT::ERTStatus status_;
457 InsetERTMailer::string2params(cmd.argument, status_);
461 /* FIXME: I refuse to believe we have to live
462 * with ugliness like this ! Note that this
463 * rebreak *is* needed. Consider a change from
464 * Open (needfullrow) to Inlined (only the space
465 * taken by the text).
467 inset.getLyXText(cmd.view())->fullRebreak();
468 bv->updateInset(this);
469 result = DispatchResult(true, true);
473 case LFUN_MOUSE_PRESS:
475 result = DispatchResult(true, true);
478 case LFUN_MOUSE_MOTION:
479 lfunMouseMotion(cmd);
480 result = DispatchResult(true, true);
483 case LFUN_MOUSE_RELEASE:
484 lfunMouseRelease(cmd);
485 result = DispatchResult(true, true);
489 bv->owner()->setLayout(inset.paragraphs.begin()->layout()->name());
490 result = DispatchResult(true);
494 result = InsetCollapsable::priv_dispatch(cmd, idx, pos);
497 switch (cmd.action) {
498 case LFUN_BREAKPARAGRAPH:
499 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
501 case LFUN_BACKSPACE_SKIP:
503 case LFUN_DELETE_SKIP:
504 case LFUN_DELETE_LINE_FORWARD:
516 string const InsetERT::get_new_label() const
519 pos_type const max_length = 15;
520 pos_type const p_siz = inset.paragraphs.begin()->size();
521 pos_type const n = min(max_length, p_siz);
524 for(; i < n && j < p_siz; ++j) {
525 if (inset.paragraphs.begin()->isInset(j))
527 la += inset.paragraphs.begin()->getChar(j);
530 if (inset.paragraphs.size() > 1 || (i > 0 && j < p_siz)) {
540 void InsetERT::setButtonLabel() const
542 if (status_ == Collapsed) {
543 setLabel(get_new_label());
550 bool InsetERT::checkInsertChar(LyXFont & /* font */)
553 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
555 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
556 font.setColor(LColor::latex);
562 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
566 inset.metrics(mi, dim);
568 InsetCollapsable::metrics(mi, dim);
569 // Make it stand out on its own as it is code, not part of running
571 if (isOpen() && !inlined())
572 dim.wid = mi.base.textwidth;
577 void InsetERT::draw(PainterInfo & pi, int x, int y) const
579 InsetCollapsable::draw(pi, x, y, inlined());
583 void InsetERT::set_latex_font(BufferView * /* bv */)
586 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
588 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
589 font.setColor(LColor::latex);
591 inset.getLyXText(bv)->setFont(bv, font, false);
596 // attention this function can be called with bv == 0
597 void InsetERT::status(BufferView * bv, ERTStatus const st) const
612 bv->unlockInset(const_cast<InsetERT *>(this));
616 bv->updateInset(this);
617 bv->buffer()->markDirty();
623 bool InsetERT::showInsetDialog(BufferView * bv) const
625 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
630 void InsetERT::open(BufferView * bv)
638 void InsetERT::close(BufferView * bv) const
640 if (status_ == Collapsed || status_ == Inlined)
643 status(bv, Collapsed);
648 InsetERT::selectNextWordToSpellcheck(BufferView * bv, float &) const
650 bv->unlockInset(const_cast<InsetERT *>(this));
651 return WordLangTuple();
655 void InsetERT::getDrawFont(LyXFont & font) const
657 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
659 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
660 font.setColor(LColor::latex);
664 string const InsetERTMailer::name_("ert");
666 InsetERTMailer::InsetERTMailer(InsetERT & inset)
671 string const InsetERTMailer::inset2string(Buffer const &) const
673 return params2string(inset_.status());
677 void InsetERTMailer::string2params(string const & in,
678 InsetERT::ERTStatus & status)
680 status = InsetERT::Collapsed;
683 string body = split(in, name, ' ');
688 status = static_cast<InsetERT::ERTStatus>(strToInt(body));
693 InsetERTMailer::params2string(InsetERT::ERTStatus status)
695 return name_ + ' ' + tostr(status);