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 &, int /*linelen*/) const
371 int InsetERT::linuxdoc(Buffer const &, ostream & os) const
373 ParagraphList::iterator par = inset.paragraphs.begin();
374 ParagraphList::iterator end = inset.paragraphs.end();
378 pos_type siz = par->size();
379 for (pos_type i = 0; i < siz; ++i) {
380 if (par->isNewline(i)) {
384 os << par->getChar(i);
398 int InsetERT::docbook(Buffer const &, ostream & os, bool) const
400 ParagraphList::iterator par = inset.paragraphs.begin();
401 ParagraphList::iterator end = inset.paragraphs.end();
405 pos_type siz = par->size();
406 for (pos_type i = 0; i < siz; ++i) {
407 if (par->isNewline(i)) {
411 os << par->getChar(i);
426 InsetERT::priv_dispatch(FuncRequest const & cmd,
427 idx_type & idx, pos_type & pos)
429 DispatchResult result = UNDISPATCHED;
430 BufferView * bv = cmd.view();
432 if (inset.paragraphs.begin()->empty()) {
436 switch (cmd.action) {
438 case LFUN_INSET_EDIT:
439 if (cmd.button() == mouse_button::button3)
441 if (status_ == Inlined) {
442 if (!bv->lockInset(this))
444 result = inset.dispatch(cmd);
446 result = InsetCollapsable::priv_dispatch(cmd, idx, pos);
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);
470 case LFUN_MOUSE_PRESS:
475 case LFUN_MOUSE_MOTION:
476 lfunMouseMotion(cmd);
480 case LFUN_MOUSE_RELEASE:
481 lfunMouseRelease(cmd);
486 bv->owner()->setLayout(inset.paragraphs.begin()->layout()->name());
487 result = DISPATCHED_NOUPDATE;
491 result = InsetCollapsable::priv_dispatch(cmd, idx, pos);
494 switch (cmd.action) {
495 case LFUN_BREAKPARAGRAPH:
496 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
498 case LFUN_BACKSPACE_SKIP:
500 case LFUN_DELETE_SKIP:
501 case LFUN_DELETE_LINE_FORWARD:
513 string const InsetERT::get_new_label() const
516 pos_type const max_length = 15;
517 pos_type const p_siz = inset.paragraphs.begin()->size();
518 pos_type const n = min(max_length, p_siz);
521 for(; i < n && j < p_siz; ++j) {
522 if (inset.paragraphs.begin()->isInset(j))
524 la += inset.paragraphs.begin()->getChar(j);
527 if (inset.paragraphs.size() > 1 || (i > 0 && j < p_siz)) {
537 void InsetERT::setButtonLabel() const
539 if (status_ == Collapsed) {
540 setLabel(get_new_label());
547 bool InsetERT::checkInsertChar(LyXFont & /* font */)
550 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
552 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
553 font.setColor(LColor::latex);
559 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
563 inset.metrics(mi, dim);
565 InsetCollapsable::metrics(mi, dim);
566 // Make it stand out on its own as it is code, not part of running
568 if (isOpen() && !inlined())
569 dim.wid = mi.base.textwidth;
574 void InsetERT::draw(PainterInfo & pi, int x, int y) const
576 InsetCollapsable::draw(pi, x, y, inlined());
580 void InsetERT::set_latex_font(BufferView * /* bv */)
583 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
585 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
586 font.setColor(LColor::latex);
588 inset.getLyXText(bv)->setFont(bv, font, false);
593 // attention this function can be called with bv == 0
594 void InsetERT::status(BufferView * bv, ERTStatus const st) const
609 bv->unlockInset(const_cast<InsetERT *>(this));
613 bv->updateInset(this);
614 bv->buffer()->markDirty();
620 bool InsetERT::showInsetDialog(BufferView * bv) const
622 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
627 void InsetERT::open(BufferView * bv)
635 void InsetERT::close(BufferView * bv) const
637 if (status_ == Collapsed || status_ == Inlined)
640 status(bv, Collapsed);
645 InsetERT::selectNextWordToSpellcheck(BufferView * bv, float &) const
647 bv->unlockInset(const_cast<InsetERT *>(this));
648 return WordLangTuple();
652 void InsetERT::getDrawFont(LyXFont & font) const
654 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
656 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
657 font.setColor(LColor::latex);
661 string const InsetERTMailer::name_("ert");
663 InsetERTMailer::InsetERTMailer(InsetERT & inset)
668 string const InsetERTMailer::inset2string(Buffer const &) const
670 return params2string(inset_.status());
674 void InsetERTMailer::string2params(string const & in,
675 InsetERT::ERTStatus & status)
677 status = InsetERT::Collapsed;
680 string body = split(in, name, ' ');
685 status = static_cast<InsetERT::ERTStatus>(strToInt(body));
690 InsetERTMailer::params2string(InsetERT::ERTStatus status)
692 return name_ + ' ' + tostr(status);