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 "BufferView.h"
18 #include "funcrequest.h"
22 #include "metricsinfo.h"
23 #include "paragraph.h"
24 #include "WordLangTuple.h"
26 #include "frontends/Alert.h"
27 #include "frontends/LyXView.h"
29 #include "support/tostr.h"
31 using namespace lyx::support;
45 labelfont = LyXFont(LyXFont::ALL_SANE);
48 labelfont.setColor(LColor::latex);
53 InsetERT::InsetERT(BufferParams const & bp, bool collapsed)
54 : InsetCollapsable(bp, collapsed)
64 InsetERT::InsetERT(InsetERT const & in)
65 : InsetCollapsable(in), status_(in.status_)
71 auto_ptr<InsetBase> InsetERT::clone() const
73 return auto_ptr<InsetBase>(new InsetERT(*this));
77 InsetERT::InsetERT(BufferParams const & bp,
78 Language const * l, string const & contents, bool collapsed)
79 : InsetCollapsable(bp, collapsed)
86 LyXFont font(LyXFont::ALL_INHERIT, l);
88 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
89 font.setColor(LColor::latex);
92 string::const_iterator cit = contents.begin();
93 string::const_iterator end = contents.end();
95 for (; cit != end; ++cit) {
96 inset.paragraphs.begin()->insertChar(pos++, *cit, font);
98 // the init has to be after the initialization of the paragraph
99 // because of the label settings (draw_label for ert insets).
104 InsetERT::~InsetERT()
106 InsetERTMailer(*this).hideDialog();
110 void InsetERT::read(Buffer const & buf, LyXLex & lex)
112 bool token_found = false;
115 string const token = lex.getString();
116 if (token == "status") {
118 string const tmp_token = lex.getString();
120 if (tmp_token == "Inlined") {
122 } else if (tmp_token == "Collapsed") {
123 status(0, Collapsed);
125 // leave this as default!
131 lyxerr << "InsetERT::Read: Missing 'status'-tag!"
133 // take countermeasures
134 lex.pushToken(token);
138 #warning this should be really short lived only for compatibility to
139 #warning files written 07/08/2001 so this has to go before 1.2.0! (Jug)
142 string const token = lex.getString();
143 if (token == "collapsed") {
145 collapsed_ = lex.getBool();
147 // Take countermeasures
148 lex.pushToken(token);
152 inset.read(buf, lex);
155 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
156 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
157 font.setColor(LColor::latex);
159 ParagraphList::iterator pit = inset.paragraphs.begin();
160 ParagraphList::iterator pend = inset.paragraphs.end();
161 for (; pit != pend; ++pit) {
162 pos_type siz = pit->size();
163 for (pos_type i = 0; i < siz; ++i) {
164 pit->setFont(i, font);
171 status(0, Collapsed);
180 void InsetERT::write(Buffer const & buf, ostream & os) const
196 os << getInsetName() << "\n"
197 << "status "<< st << "\n";
199 //inset.writeParagraphData(buf, os);
200 string const layout(buf.params().getLyXTextClass().defaultLayoutName());
201 ParagraphList::iterator par = inset.paragraphs.begin();
202 ParagraphList::iterator end = inset.paragraphs.end();
203 for (; par != end; ++par) {
204 os << "\n\\begin_layout " << layout << "\n";
205 pos_type siz = par->size();
206 for (pos_type i = 0; i < siz; ++i) {
207 Paragraph::value_type c = par->getChar(i);
209 case Paragraph::META_INSET:
210 if (par->getInset(i)->lyxCode() != InsetOld::NEWLINE_CODE) {
211 lyxerr << "Element is not allowed in insertERT"
214 par->getInset(i)->write(buf, os);
219 os << "\n\\backslash \n";
226 os << "\n\\end_layout\n";
231 string const InsetERT::editMessage() const
233 return _("Opened ERT Inset");
237 bool InsetERT::insertInset(BufferView *, InsetOld *)
243 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
246 #warning FIXME. More UI stupidity...
248 // if selectall is activated then the fontchange was an outside general
249 // fontchange and this messages is not needed
251 Alert::error(_("Cannot change font"),
252 _("You cannot change font settings inside TeX code."));
256 void InsetERT::updateStatus(BufferView * bv, bool swap) const
258 if (status_ != Inlined) {
260 status(bv, swap ? Open : Collapsed);
262 status(bv, swap ? Collapsed : Open);
268 InsetOld::EDITABLE InsetERT::editable() const
270 if (status_ == Collapsed)
272 return HIGHLY_EDITABLE;
276 void InsetERT::lfunMousePress(FuncRequest const & cmd)
278 if (status_ == Inlined)
279 inset.localDispatch(cmd);
281 InsetCollapsable::localDispatch(cmd);
285 bool InsetERT::lfunMouseRelease(FuncRequest const & cmd)
287 BufferView * bv = cmd.view();
289 if (cmd.button() == mouse_button::button3) {
294 if (status_ != Inlined && hitButton(cmd)) {
295 updateStatus(bv, true);
297 FuncRequest cmd1 = cmd;
299 cmd1.y = ascent() + cmd.y - inset.ascent();
301 // inlined is special - the text appears above
303 if (status_ == Inlined)
304 inset.localDispatch(cmd1);
305 else if (!collapsed_ && (cmd.y > button_dim.y2)) {
306 cmd1.y -= height_collapsed();
307 inset.localDispatch(cmd1);
314 void InsetERT::lfunMouseMotion(FuncRequest const & cmd)
316 if (status_ == Inlined)
317 inset.localDispatch(cmd);
319 InsetCollapsable::localDispatch(cmd);
323 int InsetERT::latex(Buffer const &, ostream & os,
324 LatexRunParams const &) const
326 ParagraphList::iterator par = inset.paragraphs.begin();
327 ParagraphList::iterator end = inset.paragraphs.end();
331 pos_type siz = par->size();
332 for (pos_type i = 0; i < siz; ++i) {
333 // ignore all struck out text
334 if (isDeletedText(*par, i))
337 if (par->isNewline(i)) {
341 os << par->getChar(i);
355 int InsetERT::ascii(Buffer const &, ostream &, int /*linelen*/) const
361 int InsetERT::linuxdoc(Buffer const &, ostream & os) const
363 ParagraphList::iterator par = inset.paragraphs.begin();
364 ParagraphList::iterator end = inset.paragraphs.end();
368 pos_type siz = par->size();
369 for (pos_type i = 0; i < siz; ++i) {
370 if (par->isNewline(i)) {
374 os << par->getChar(i);
388 int InsetERT::docbook(Buffer const &, ostream & os, bool) const
390 ParagraphList::iterator par = inset.paragraphs.begin();
391 ParagraphList::iterator end = inset.paragraphs.end();
395 pos_type siz = par->size();
396 for (pos_type i = 0; i < siz; ++i) {
397 if (par->isNewline(i)) {
401 os << par->getChar(i);
415 InsetOld::RESULT InsetERT::localDispatch(FuncRequest const & cmd)
417 InsetOld::RESULT result = UNDISPATCHED;
418 BufferView * bv = cmd.view();
420 if (inset.paragraphs.begin()->empty()) {
424 switch (cmd.action) {
426 case LFUN_INSET_EDIT:
427 if (cmd.button() == mouse_button::button3)
429 if (status_ == Inlined) {
430 if (!bv->lockInset(this))
432 result = inset.localDispatch(cmd);
434 result = InsetCollapsable::localDispatch(cmd);
440 case LFUN_INSET_MODIFY: {
441 InsetERT::ERTStatus status_;
442 InsetERTMailer::string2params(cmd.argument, status_);
446 /* FIXME: I refuse to believe we have to live
447 * with ugliness like this ! Note that this
448 * rebreak *is* needed. Consider a change from
449 * Open (needfullrow) to Inlined (only the space
450 * taken by the text).
452 inset.getLyXText(cmd.view())->fullRebreak();
453 bv->updateInset(this);
458 case LFUN_MOUSE_PRESS:
463 case LFUN_MOUSE_MOTION:
464 lfunMouseMotion(cmd);
468 case LFUN_MOUSE_RELEASE:
469 lfunMouseRelease(cmd);
474 bv->owner()->setLayout(inset.paragraphs.begin()->layout()->name());
475 result = DISPATCHED_NOUPDATE;
479 result = InsetCollapsable::localDispatch(cmd);
482 switch (cmd.action) {
483 case LFUN_BREAKPARAGRAPH:
484 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
486 case LFUN_BACKSPACE_SKIP:
488 case LFUN_DELETE_SKIP:
489 case LFUN_DELETE_LINE_FORWARD:
501 string const InsetERT::get_new_label() const
504 pos_type const max_length = 15;
505 pos_type const p_siz = inset.paragraphs.begin()->size();
506 pos_type const n = min(max_length, p_siz);
509 for(; i < n && j < p_siz; ++j) {
510 if (inset.paragraphs.begin()->isInset(j))
512 la += inset.paragraphs.begin()->getChar(j);
515 if (inset.paragraphs.size() > 1 || (i > 0 && j < p_siz)) {
525 void InsetERT::setButtonLabel() const
527 if (status_ == Collapsed) {
528 setLabel(get_new_label());
535 bool InsetERT::checkInsertChar(LyXFont & /* font */)
538 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
540 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
541 font.setColor(LColor::latex);
547 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
551 inset.metrics(mi, dim);
553 InsetCollapsable::metrics(mi, dim);
554 // Make it stand out on its own as it is code, not part of running
556 if (isOpen() && !inlined())
557 dim.wid = mi.base.textwidth;
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
597 bv->unlockInset(const_cast<InsetERT *>(this));
601 bv->updateInset(this);
602 bv->buffer()->markDirty();
608 bool InsetERT::showInsetDialog(BufferView * bv) const
610 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
615 void InsetERT::open(BufferView * bv)
623 void InsetERT::close(BufferView * bv) const
625 if (status_ == Collapsed || status_ == Inlined)
628 status(bv, Collapsed);
633 InsetERT::selectNextWordToSpellcheck(BufferView * bv, float &) const
635 bv->unlockInset(const_cast<InsetERT *>(this));
636 return WordLangTuple();
640 void InsetERT::getDrawFont(LyXFont & font) const
642 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
644 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
645 font.setColor(LColor::latex);
649 string const InsetERTMailer::name_("ert");
651 InsetERTMailer::InsetERTMailer(InsetERT & inset)
656 string const InsetERTMailer::inset2string(Buffer const &) const
658 return params2string(inset_.status());
662 void InsetERTMailer::string2params(string const & in,
663 InsetERT::ERTStatus & status)
665 status = InsetERT::Collapsed;
668 string body = split(in, name, ' ');
673 status = static_cast<InsetERT::ERTStatus>(strToInt(body));
678 InsetERTMailer::params2string(InsetERT::ERTStatus status)
680 return name_ + ' ' + tostr(status);