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
14 #include "insettext.h"
17 #include "BufferView.h"
19 #include "funcrequest.h"
26 #include "WordLangTuple.h"
28 #include "frontends/Alert.h"
29 #include "frontends/Dialogs.h"
30 #include "frontends/LyXView.h"
32 #include "support/LOstream.h"
33 #include "support/LAssert.h"
34 #include "support/tostr.h"
47 labelfont = LyXFont(LyXFont::ALL_SANE);
50 labelfont.setColor(LColor::latex);
55 InsetERT::InsetERT(BufferParams const & bp, bool collapsed)
56 : InsetCollapsable(bp, collapsed)
66 InsetERT::InsetERT(InsetERT const & in)
67 : InsetCollapsable(in), status_(in.status_)
73 Inset * InsetERT::clone(Buffer const &) const
75 return new InsetERT(*this);
79 InsetERT::InsetERT(BufferParams const & bp,
80 Language const * l, string const & contents, bool collapsed)
81 : InsetCollapsable(bp, collapsed)
88 LyXFont font(LyXFont::ALL_INHERIT, l);
90 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
91 font.setColor(LColor::latex);
94 string::const_iterator cit = contents.begin();
95 string::const_iterator end = contents.end();
97 for (; cit != end; ++cit) {
98 inset.paragraphs.begin()->insertChar(pos++, *cit, font);
100 // the init has to be after the initialization of the paragraph
101 // because of the label settings (draw_label for ert insets).
106 InsetERT::~InsetERT()
108 InsetERTMailer mailer(*this);
113 void InsetERT::read(Buffer const * buf, LyXLex & lex)
115 bool token_found = false;
118 string const token = lex.getString();
119 if (token == "status") {
121 string const tmp_token = lex.getString();
123 if (tmp_token == "Inlined") {
125 } else if (tmp_token == "Collapsed") {
126 status(0, Collapsed);
128 // leave this as default!
134 lyxerr << "InsetERT::Read: Missing 'status'-tag!"
136 // take countermeasures
137 lex.pushToken(token);
141 #warning this should be really short lived only for compatibility to
142 #warning files written 07/08/2001 so this has to go before 1.2.0! (Jug)
145 string const token = lex.getString();
146 if (token == "collapsed") {
148 collapsed_ = lex.getBool();
150 // Take countermeasures
151 lex.pushToken(token);
155 inset.read(buf, lex);
158 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
159 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
160 font.setColor(LColor::latex);
162 ParagraphList::iterator pit = inset.paragraphs.begin();
163 ParagraphList::iterator pend = inset.paragraphs.end();
164 for (; pit != pend; ++pit) {
165 pos_type siz = pit->size();
166 for (pos_type i = 0; i < siz; ++i) {
167 pit->setFont(i, font);
174 status(0, Collapsed);
183 void InsetERT::write(Buffer const * buf, ostream & os) const
199 os << getInsetName() << "\n"
200 << "status "<< st << "\n";
202 //inset.writeParagraphData(buf, os);
203 string const layout(buf->params.getLyXTextClass().defaultLayoutName());
204 ParagraphList::iterator par = inset.paragraphs.begin();
205 ParagraphList::iterator end = inset.paragraphs.end();
206 for (; par != end; ++par) {
207 os << "\n\\layout " << layout << "\n";
208 pos_type siz = par->size();
209 for (pos_type i = 0; i < siz; ++i) {
210 Paragraph::value_type c = par->getChar(i);
212 case Paragraph::META_INSET:
213 if (par->getInset(i)->lyxCode() != Inset::NEWLINE_CODE) {
214 lyxerr << "Element is not allowed in insertERT"
217 par->getInset(i)->write(buf, os);
222 os << "\n\\backslash \n";
233 string const InsetERT::editMessage() const
235 return _("Opened ERT Inset");
239 bool InsetERT::insertInset(BufferView *, Inset *)
245 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
248 #warning FIXME. More UI stupidity...
250 // if selectall is activated then the fontchange was an outside general
251 // fontchange and this messages is not needed
253 Alert::error(_("Cannot change font"),
254 _("You cannot change font settings inside TeX code."));
258 void InsetERT::updateStatus(BufferView * bv, bool swap) const
260 if (status_ != Inlined) {
262 status(bv, swap ? Open : Collapsed);
264 status(bv, swap ? Collapsed : Open);
270 Inset::EDITABLE InsetERT::editable() const
272 if (status_ == Collapsed)
274 return HIGHLY_EDITABLE;
278 void InsetERT::lfunMousePress(FuncRequest const & cmd)
280 if (status_ == Inlined)
281 inset.localDispatch(cmd);
283 InsetCollapsable::localDispatch(cmd);
287 bool InsetERT::lfunMouseRelease(FuncRequest const & cmd)
289 BufferView * bv = cmd.view();
291 if (cmd.button() == mouse_button::button3) {
296 if (status_ != Inlined && (cmd.x >= 0) && (cmd.x < button_length) &&
297 (cmd.y >= button_top_y) && (cmd.y <= button_bottom_y)) {
298 updateStatus(bv, true);
300 LyXFont font(LyXFont::ALL_SANE);
301 FuncRequest cmd1 = cmd;
302 cmd1.y = ascent(bv, font) + cmd.y - inset.ascent(bv, font);
304 // inlined is special - the text appears above
306 if (status_ == Inlined)
307 inset.localDispatch(cmd1);
308 else if (!collapsed_ && (cmd.y > button_bottom_y)) {
309 cmd1.y -= height_collapsed();
310 inset.localDispatch(cmd1);
317 void InsetERT::lfunMouseMotion(FuncRequest const & cmd)
319 if (status_ == Inlined)
320 inset.localDispatch(cmd);
322 InsetCollapsable::localDispatch(cmd);
326 int InsetERT::latex(Buffer const *, ostream & os,
327 LatexRunParams const &) const
329 ParagraphList::iterator par = inset.paragraphs.begin();
330 ParagraphList::iterator end = inset.paragraphs.end();
334 pos_type siz = par->size();
335 for (pos_type i = 0; i < siz; ++i) {
336 // ignore all struck out text
337 if (isDeletedText(*par, i))
340 if (par->isNewline(i)) {
344 os << par->getChar(i);
358 int InsetERT::ascii(Buffer const *, ostream &, int /*linelen*/) const
364 int InsetERT::linuxdoc(Buffer const *, ostream & os) const
366 ParagraphList::iterator par = inset.paragraphs.begin();
367 ParagraphList::iterator end = inset.paragraphs.end();
371 pos_type siz = par->size();
372 for (pos_type i = 0; i < siz; ++i) {
373 if (par->isNewline(i)) {
377 os << par->getChar(i);
391 int InsetERT::docbook(Buffer const *, ostream & os, bool) const
393 ParagraphList::iterator par = inset.paragraphs.begin();
394 ParagraphList::iterator end = inset.paragraphs.end();
398 pos_type siz = par->size();
399 for (pos_type i = 0; i < siz; ++i) {
400 if (par->isNewline(i)) {
404 os << par->getChar(i);
418 Inset::RESULT InsetERT::localDispatch(FuncRequest const & cmd)
420 Inset::RESULT result = UNDISPATCHED;
421 BufferView * bv = cmd.view();
423 if (inset.paragraphs.begin()->empty()) {
427 switch (cmd.action) {
429 case LFUN_INSET_EDIT:
430 if (cmd.button() == mouse_button::button3)
432 if (status_ == Inlined) {
433 if (!bv->lockInset(this))
435 result = inset.localDispatch(cmd);
437 result = InsetCollapsable::localDispatch(cmd);
443 case LFUN_INSET_MODIFY: {
444 InsetERT::ERTStatus status_;
445 InsetERTMailer::string2params(cmd.argument, status_);
449 /* FIXME: I refuse to believe we have to live
450 * with ugliness like this ! Note that this
451 * rebreak *is* needed. Consider a change from
452 * Open (needfullrow) to Inlined (only the space
453 * taken by the text).
455 LyXText * t = inset.getLyXText(cmd.view());
456 t->need_break_row = t->rows().begin();
458 t->setCursorIntern(t->cursor.par(), t->cursor.pos());
459 inset.update(cmd.view(), true);
460 bv->updateInset(this);
465 case LFUN_MOUSE_PRESS:
470 case LFUN_MOUSE_MOTION:
471 lfunMouseMotion(cmd);
475 case LFUN_MOUSE_RELEASE:
476 lfunMouseRelease(cmd);
481 bv->owner()->setLayout(inset.paragraphs.begin()->layout()->name());
482 result = DISPATCHED_NOUPDATE;
486 result = InsetCollapsable::localDispatch(cmd);
489 switch (cmd.action) {
490 case LFUN_BREAKPARAGRAPH:
491 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
493 case LFUN_BACKSPACE_SKIP:
495 case LFUN_DELETE_SKIP:
496 case LFUN_DELETE_LINE_FORWARD:
508 string const InsetERT::get_new_label() const
511 pos_type const max_length = 15;
512 pos_type const p_siz = inset.paragraphs.begin()->size();
513 pos_type const n = min(max_length, p_siz);
516 for(; i < n && j < p_siz; ++j) {
517 if (inset.paragraphs.begin()->isInset(j))
519 la += inset.paragraphs.begin()->getChar(j);
522 if (boost::next(inset.paragraphs.begin()) != inset.paragraphs.end() ||
523 (i > 0 && j < p_siz)) {
533 void InsetERT::setButtonLabel() const
535 if (status_ == Collapsed) {
536 setLabel(get_new_label());
543 bool InsetERT::checkInsertChar(LyXFont & /* font */)
546 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
548 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
549 font.setColor(LColor::latex);
555 void InsetERT::dimension(BufferView * bv, LyXFont const & font,
556 Dimension & dim) const
559 inset.dimension(bv, font, dim);
561 InsetCollapsable::dimension(bv, font, dim);
565 void InsetERT::draw(PainterInfo & pi, int x, int y) const
567 InsetCollapsable::draw(pi, x, y, inlined());
571 void InsetERT::set_latex_font(BufferView * /* bv */)
574 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
576 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
577 font.setColor(LColor::latex);
579 inset.getLyXText(bv)->setFont(bv, font, false);
584 // attention this function can be called with bv == 0
585 void InsetERT::status(BufferView * bv, ERTStatus const st) const
592 inset.setUpdateStatus(bv, InsetText::INIT);
602 bv->unlockInset(const_cast<InsetERT *>(this));
606 bv->updateInset(const_cast<InsetERT *>(this));
607 bv->buffer()->markDirty();
613 bool InsetERT::showInsetDialog(BufferView * bv) const
615 InsetERTMailer mailer(const_cast<InsetERT &>(*this));
616 mailer.showDialog(bv);
621 void InsetERT::open(BufferView * bv)
629 void InsetERT::close(BufferView * bv) const
631 if (status_ == Collapsed || status_ == Inlined)
634 status(bv, Collapsed);
639 InsetERT::selectNextWordToSpellcheck(BufferView * bv, float &) const
641 bv->unlockInset(const_cast<InsetERT *>(this));
642 return WordLangTuple();
646 void InsetERT::getDrawFont(LyXFont & font) const
648 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
650 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
651 font.setColor(LColor::latex);
655 int InsetERT::getMaxWidth(BufferView * bv, UpdatableInset const * in) const
657 int w = InsetCollapsable::getMaxWidth(bv, in);
658 if (status_ != Inlined || w < 0)
660 LyXText * text = inset.getLyXText(bv);
661 int rw = text->rows().begin()->width();
665 if (text->rows().size() == 1 && rw < w)
671 void InsetERT::update(BufferView * bv, bool reinit)
673 if (inset.need_update & InsetText::INIT ||
674 inset.need_update & InsetText::FULL) {
678 InsetCollapsable::update(bv, reinit);
682 string const InsetERTMailer::name_("ert");
684 InsetERTMailer::InsetERTMailer(InsetERT & inset)
689 string const InsetERTMailer::inset2string() const
691 return params2string(inset_.status());
695 void InsetERTMailer::string2params(string const & in,
696 InsetERT::ERTStatus & status)
698 status = InsetERT::Collapsed;
701 string body = split(in, name, ' ');
706 status = static_cast<InsetERT::ERTStatus>(strToInt(body));
711 InsetERTMailer::params2string(InsetERT::ERTStatus status)
713 return name_ + ' ' + tostr(status);