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"
36 using namespace lyx::support;
49 labelfont = LyXFont(LyXFont::ALL_SANE);
52 labelfont.setColor(LColor::latex);
57 InsetERT::InsetERT(BufferParams const & bp, bool collapsed)
58 : InsetCollapsable(bp, collapsed)
68 InsetERT::InsetERT(InsetERT const & in)
69 : InsetCollapsable(in), status_(in.status_)
75 auto_ptr<InsetBase> InsetERT::clone() const
77 return auto_ptr<InsetBase>(new InsetERT(*this));
81 InsetERT::InsetERT(BufferParams const & bp,
82 Language const * l, string const & contents, bool collapsed)
83 : InsetCollapsable(bp, collapsed)
90 LyXFont font(LyXFont::ALL_INHERIT, l);
92 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
93 font.setColor(LColor::latex);
96 string::const_iterator cit = contents.begin();
97 string::const_iterator end = contents.end();
99 for (; cit != end; ++cit) {
100 inset.paragraphs.begin()->insertChar(pos++, *cit, font);
102 // the init has to be after the initialization of the paragraph
103 // because of the label settings (draw_label for ert insets).
108 InsetERT::~InsetERT()
110 InsetERTMailer(*this).hideDialog();
114 void InsetERT::read(Buffer const * buf, LyXLex & lex)
116 bool token_found = false;
119 string const token = lex.getString();
120 if (token == "status") {
122 string const tmp_token = lex.getString();
124 if (tmp_token == "Inlined") {
126 } else if (tmp_token == "Collapsed") {
127 status(0, Collapsed);
129 // leave this as default!
135 lyxerr << "InsetERT::Read: Missing 'status'-tag!"
137 // take countermeasures
138 lex.pushToken(token);
142 #warning this should be really short lived only for compatibility to
143 #warning files written 07/08/2001 so this has to go before 1.2.0! (Jug)
146 string const token = lex.getString();
147 if (token == "collapsed") {
149 collapsed_ = lex.getBool();
151 // Take countermeasures
152 lex.pushToken(token);
156 inset.read(buf, lex);
159 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
160 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
161 font.setColor(LColor::latex);
163 ParagraphList::iterator pit = inset.paragraphs.begin();
164 ParagraphList::iterator pend = inset.paragraphs.end();
165 for (; pit != pend; ++pit) {
166 pos_type siz = pit->size();
167 for (pos_type i = 0; i < siz; ++i) {
168 pit->setFont(i, font);
175 status(0, Collapsed);
184 void InsetERT::write(Buffer const * buf, ostream & os) const
200 os << getInsetName() << "\n"
201 << "status "<< st << "\n";
203 //inset.writeParagraphData(buf, os);
204 string const layout(buf->params.getLyXTextClass().defaultLayoutName());
205 ParagraphList::iterator par = inset.paragraphs.begin();
206 ParagraphList::iterator end = inset.paragraphs.end();
207 for (; par != end; ++par) {
208 os << "\n\\begin_layout " << layout << "\n";
209 pos_type siz = par->size();
210 for (pos_type i = 0; i < siz; ++i) {
211 Paragraph::value_type c = par->getChar(i);
213 case Paragraph::META_INSET:
214 if (par->getInset(i)->lyxCode() != InsetOld::NEWLINE_CODE) {
215 lyxerr << "Element is not allowed in insertERT"
218 par->getInset(i)->write(buf, os);
223 os << "\n\\backslash \n";
230 os << "\n\\end_layout\n";
235 string const InsetERT::editMessage() const
237 return _("Opened ERT Inset");
241 bool InsetERT::insertInset(BufferView *, InsetOld *)
247 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
250 #warning FIXME. More UI stupidity...
252 // if selectall is activated then the fontchange was an outside general
253 // fontchange and this messages is not needed
255 Alert::error(_("Cannot change font"),
256 _("You cannot change font settings inside TeX code."));
260 void InsetERT::updateStatus(BufferView * bv, bool swap) const
262 if (status_ != Inlined) {
264 status(bv, swap ? Open : Collapsed);
266 status(bv, swap ? Collapsed : Open);
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)
283 inset.localDispatch(cmd);
285 InsetCollapsable::localDispatch(cmd);
289 bool InsetERT::lfunMouseRelease(FuncRequest const & cmd)
291 BufferView * bv = cmd.view();
293 if (cmd.button() == mouse_button::button3) {
298 if (status_ != Inlined && hitButton(cmd)) {
299 updateStatus(bv, true);
301 FuncRequest cmd1 = cmd;
303 cmd1.y = ascent() + cmd.y - inset.ascent();
305 // inlined is special - the text appears above
307 if (status_ == Inlined)
308 inset.localDispatch(cmd1);
309 else if (!collapsed_ && (cmd.y > button_dim.y2)) {
310 cmd1.y -= height_collapsed();
311 inset.localDispatch(cmd1);
318 void InsetERT::lfunMouseMotion(FuncRequest const & cmd)
320 if (status_ == Inlined)
321 inset.localDispatch(cmd);
323 InsetCollapsable::localDispatch(cmd);
327 int InsetERT::latex(Buffer const *, ostream & os,
328 LatexRunParams const &) const
330 ParagraphList::iterator par = inset.paragraphs.begin();
331 ParagraphList::iterator end = inset.paragraphs.end();
335 pos_type siz = par->size();
336 for (pos_type i = 0; i < siz; ++i) {
337 // ignore all struck out text
338 if (isDeletedText(*par, i))
341 if (par->isNewline(i)) {
345 os << par->getChar(i);
359 int InsetERT::ascii(Buffer const *, ostream &, int /*linelen*/) const
365 int InsetERT::linuxdoc(Buffer const *, ostream & os) const
367 ParagraphList::iterator par = inset.paragraphs.begin();
368 ParagraphList::iterator end = inset.paragraphs.end();
372 pos_type siz = par->size();
373 for (pos_type i = 0; i < siz; ++i) {
374 if (par->isNewline(i)) {
378 os << par->getChar(i);
392 int InsetERT::docbook(Buffer const *, ostream & os, bool) const
394 ParagraphList::iterator par = inset.paragraphs.begin();
395 ParagraphList::iterator end = inset.paragraphs.end();
399 pos_type siz = par->size();
400 for (pos_type i = 0; i < siz; ++i) {
401 if (par->isNewline(i)) {
405 os << par->getChar(i);
419 InsetOld::RESULT InsetERT::localDispatch(FuncRequest const & cmd)
421 InsetOld::RESULT result = UNDISPATCHED;
422 BufferView * bv = cmd.view();
424 if (inset.paragraphs.begin()->empty()) {
428 switch (cmd.action) {
430 case LFUN_INSET_EDIT:
431 if (cmd.button() == mouse_button::button3)
433 if (status_ == Inlined) {
434 if (!bv->lockInset(this))
436 result = inset.localDispatch(cmd);
438 result = InsetCollapsable::localDispatch(cmd);
444 case LFUN_INSET_MODIFY: {
445 InsetERT::ERTStatus status_;
446 InsetERTMailer::string2params(cmd.argument, status_);
450 /* FIXME: I refuse to believe we have to live
451 * with ugliness like this ! Note that this
452 * rebreak *is* needed. Consider a change from
453 * Open (needfullrow) to Inlined (only the space
454 * taken by the text).
456 inset.getLyXText(cmd.view())->fullRebreak();
457 bv->updateInset(this);
462 case LFUN_MOUSE_PRESS:
467 case LFUN_MOUSE_MOTION:
468 lfunMouseMotion(cmd);
472 case LFUN_MOUSE_RELEASE:
473 lfunMouseRelease(cmd);
478 bv->owner()->setLayout(inset.paragraphs.begin()->layout()->name());
479 result = DISPATCHED_NOUPDATE;
483 result = InsetCollapsable::localDispatch(cmd);
486 switch (cmd.action) {
487 case LFUN_BREAKPARAGRAPH:
488 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
490 case LFUN_BACKSPACE_SKIP:
492 case LFUN_DELETE_SKIP:
493 case LFUN_DELETE_LINE_FORWARD:
505 string const InsetERT::get_new_label() const
508 pos_type const max_length = 15;
509 pos_type const p_siz = inset.paragraphs.begin()->size();
510 pos_type const n = min(max_length, p_siz);
513 for(; i < n && j < p_siz; ++j) {
514 if (inset.paragraphs.begin()->isInset(j))
516 la += inset.paragraphs.begin()->getChar(j);
519 if (p_siz > 1 || (i > 0 && j < p_siz)) {
529 void InsetERT::setButtonLabel() const
531 if (status_ == Collapsed) {
532 setLabel(get_new_label());
539 bool InsetERT::checkInsertChar(LyXFont & /* font */)
542 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
544 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
545 font.setColor(LColor::latex);
551 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
555 inset.metrics(mi, dim);
557 InsetCollapsable::metrics(mi, dim);
562 void InsetERT::draw(PainterInfo & pi, int x, int y) const
564 InsetCollapsable::draw(pi, x, y, inlined());
568 void InsetERT::set_latex_font(BufferView * /* bv */)
571 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
573 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
574 font.setColor(LColor::latex);
576 inset.getLyXText(bv)->setFont(bv, font, false);
581 // attention this function can be called with bv == 0
582 void InsetERT::status(BufferView * bv, ERTStatus const st) const
589 inset.setUpdateStatus(InsetText::INIT);
599 bv->unlockInset(const_cast<InsetERT *>(this));
603 bv->updateInset(const_cast<InsetERT *>(this));
604 bv->buffer()->markDirty();
610 bool InsetERT::showInsetDialog(BufferView * bv) const
612 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
617 void InsetERT::open(BufferView * bv)
625 void InsetERT::close(BufferView * bv) const
627 if (status_ == Collapsed || status_ == Inlined)
630 status(bv, Collapsed);
635 InsetERT::selectNextWordToSpellcheck(BufferView * bv, float &) const
637 bv->unlockInset(const_cast<InsetERT *>(this));
638 return WordLangTuple();
642 void InsetERT::getDrawFont(LyXFont & font) const
644 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
646 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
647 font.setColor(LColor::latex);
651 string const InsetERTMailer::name_("ert");
653 InsetERTMailer::InsetERTMailer(InsetERT & inset)
658 string const InsetERTMailer::inset2string(Buffer const &) const
660 return params2string(inset_.status());
664 void InsetERTMailer::string2params(string const & in,
665 InsetERT::ERTStatus & status)
667 status = InsetERT::Collapsed;
670 string body = split(in, name, ' ');
675 status = static_cast<InsetERT::ERTStatus>(strToInt(body));
680 InsetERTMailer::params2string(InsetERT::ERTStatus status)
682 return name_ + ' ' + tostr(status);