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 "funcrequest.h"
23 #include "metricsinfo.h"
24 #include "paragraph.h"
25 #include "WordLangTuple.h"
27 #include "frontends/Alert.h"
28 #include "frontends/LyXView.h"
30 #include "support/tostr.h"
34 using lyx::support::split;
35 using lyx::support::strToInt;
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 auto_ptr<InsetBase> InsetERT::clone() const
75 return auto_ptr<InsetBase>(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(*this).hideDialog();
112 void InsetERT::read(Buffer const & buf, LyXLex & lex)
114 bool token_found = false;
117 string const token = lex.getString();
118 if (token == "status") {
120 string const tmp_token = lex.getString();
122 if (tmp_token == "Inlined") {
124 } else if (tmp_token == "Collapsed") {
125 status(0, Collapsed);
127 // leave this as default!
133 lyxerr << "InsetERT::Read: Missing 'status'-tag!"
135 // take countermeasures
136 lex.pushToken(token);
140 #warning this should be really short lived only for compatibility to
141 #warning files written 07/08/2001 so this has to go before 1.2.0! (Jug)
144 string const token = lex.getString();
145 if (token == "collapsed") {
147 collapsed_ = lex.getBool();
149 // Take countermeasures
150 lex.pushToken(token);
154 inset.read(buf, lex);
157 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
158 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
159 font.setColor(LColor::latex);
161 ParagraphList::iterator pit = inset.paragraphs.begin();
162 ParagraphList::iterator pend = inset.paragraphs.end();
163 for (; pit != pend; ++pit) {
164 pos_type siz = pit->size();
165 for (pos_type i = 0; i < siz; ++i) {
166 pit->setFont(i, font);
173 status(0, Collapsed);
182 void InsetERT::write(Buffer const & buf, ostream & os) const
198 os << getInsetName() << "\n"
199 << "status "<< st << "\n";
201 //inset.writeParagraphData(buf, os);
202 string const layout(buf.params().getLyXTextClass().defaultLayoutName());
203 ParagraphList::iterator par = inset.paragraphs.begin();
204 ParagraphList::iterator end = inset.paragraphs.end();
205 for (; par != end; ++par) {
206 os << "\n\\begin_layout " << layout << "\n";
207 pos_type siz = par->size();
208 for (pos_type i = 0; i < siz; ++i) {
209 Paragraph::value_type c = par->getChar(i);
211 case Paragraph::META_INSET:
212 if (par->getInset(i)->lyxCode() != InsetOld::NEWLINE_CODE) {
213 lyxerr << "Element is not allowed in insertERT"
216 par->getInset(i)->write(buf, os);
221 os << "\n\\backslash \n";
228 os << "\n\\end_layout\n";
233 string const InsetERT::editMessage() const
235 return _("Opened ERT Inset");
239 bool InsetERT::insertInset(BufferView *, InsetOld *)
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 InsetOld::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 && hitButton(cmd)) {
297 updateStatus(bv, true);
299 FuncRequest cmd1 = cmd;
301 cmd1.y = ascent() + cmd.y - inset.ascent();
303 // inlined is special - the text appears above
305 if (status_ == Inlined)
306 inset.localDispatch(cmd1);
307 else if (!collapsed_ && (cmd.y > button_dim.y2)) {
308 cmd1.y -= height_collapsed();
309 inset.localDispatch(cmd1);
316 void InsetERT::lfunMouseMotion(FuncRequest const & cmd)
318 if (status_ == Inlined)
319 inset.localDispatch(cmd);
321 InsetCollapsable::localDispatch(cmd);
325 int InsetERT::latex(Buffer const &, ostream & os,
326 LatexRunParams const &) const
328 ParagraphList::iterator par = inset.paragraphs.begin();
329 ParagraphList::iterator end = inset.paragraphs.end();
333 pos_type siz = par->size();
334 for (pos_type i = 0; i < siz; ++i) {
335 // ignore all struck out text
336 if (isDeletedText(*par, i))
339 if (par->isNewline(i)) {
343 os << par->getChar(i);
357 int InsetERT::ascii(Buffer const &, ostream &, int /*linelen*/) const
363 int InsetERT::linuxdoc(Buffer const &, ostream & os) const
365 ParagraphList::iterator par = inset.paragraphs.begin();
366 ParagraphList::iterator end = inset.paragraphs.end();
370 pos_type siz = par->size();
371 for (pos_type i = 0; i < siz; ++i) {
372 if (par->isNewline(i)) {
376 os << par->getChar(i);
390 int InsetERT::docbook(Buffer const &, ostream & os, bool) const
392 ParagraphList::iterator par = inset.paragraphs.begin();
393 ParagraphList::iterator end = inset.paragraphs.end();
397 pos_type siz = par->size();
398 for (pos_type i = 0; i < siz; ++i) {
399 if (par->isNewline(i)) {
403 os << par->getChar(i);
417 InsetOld::RESULT InsetERT::localDispatch(FuncRequest const & cmd)
419 InsetOld::RESULT result = UNDISPATCHED;
420 BufferView * bv = cmd.view();
422 if (inset.paragraphs.begin()->empty()) {
426 switch (cmd.action) {
428 case LFUN_INSET_EDIT:
429 if (cmd.button() == mouse_button::button3)
431 if (status_ == Inlined) {
432 if (!bv->lockInset(this))
434 result = inset.localDispatch(cmd);
436 result = InsetCollapsable::localDispatch(cmd);
442 case LFUN_INSET_MODIFY: {
443 InsetERT::ERTStatus status_;
444 InsetERTMailer::string2params(cmd.argument, status_);
448 /* FIXME: I refuse to believe we have to live
449 * with ugliness like this ! Note that this
450 * rebreak *is* needed. Consider a change from
451 * Open (needfullrow) to Inlined (only the space
452 * taken by the text).
454 inset.getLyXText(cmd.view())->fullRebreak();
455 bv->updateInset(this);
460 case LFUN_MOUSE_PRESS:
465 case LFUN_MOUSE_MOTION:
466 lfunMouseMotion(cmd);
470 case LFUN_MOUSE_RELEASE:
471 lfunMouseRelease(cmd);
476 bv->owner()->setLayout(inset.paragraphs.begin()->layout()->name());
477 result = DISPATCHED_NOUPDATE;
481 result = InsetCollapsable::localDispatch(cmd);
484 switch (cmd.action) {
485 case LFUN_BREAKPARAGRAPH:
486 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
488 case LFUN_BACKSPACE_SKIP:
490 case LFUN_DELETE_SKIP:
491 case LFUN_DELETE_LINE_FORWARD:
503 string const InsetERT::get_new_label() const
506 pos_type const max_length = 15;
507 pos_type const p_siz = inset.paragraphs.begin()->size();
508 pos_type const n = min(max_length, p_siz);
511 for(; i < n && j < p_siz; ++j) {
512 if (inset.paragraphs.begin()->isInset(j))
514 la += inset.paragraphs.begin()->getChar(j);
517 if (inset.paragraphs.size() > 1 || (i > 0 && j < p_siz)) {
527 void InsetERT::setButtonLabel() const
529 if (status_ == Collapsed) {
530 setLabel(get_new_label());
537 bool InsetERT::checkInsertChar(LyXFont & /* font */)
540 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
542 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
543 font.setColor(LColor::latex);
549 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
553 inset.metrics(mi, dim);
555 InsetCollapsable::metrics(mi, dim);
556 // Make it stand out on its own as it is code, not part of running
558 if (isOpen() && !inlined())
559 dim.wid = mi.base.textwidth;
564 void InsetERT::draw(PainterInfo & pi, int x, int y) const
566 InsetCollapsable::draw(pi, x, y, inlined());
570 void InsetERT::set_latex_font(BufferView * /* bv */)
573 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
575 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
576 font.setColor(LColor::latex);
578 inset.getLyXText(bv)->setFont(bv, font, false);
583 // attention this function can be called with bv == 0
584 void InsetERT::status(BufferView * bv, ERTStatus const st) const
599 bv->unlockInset(const_cast<InsetERT *>(this));
603 bv->updateInset(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);