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;
44 labelfont = LyXFont(LyXFont::ALL_SANE);
47 labelfont.setColor(LColor::latex);
52 InsetERT::InsetERT(BufferParams const & bp, bool collapsed)
53 : InsetCollapsable(bp, collapsed)
63 InsetERT::InsetERT(InsetERT const & in)
64 : InsetCollapsable(in), status_(in.status_)
70 auto_ptr<InsetBase> InsetERT::clone() const
72 return auto_ptr<InsetBase>(new InsetERT(*this));
76 InsetERT::InsetERT(BufferParams const & bp,
77 Language const * l, string const & contents, bool collapsed)
78 : InsetCollapsable(bp, collapsed)
85 LyXFont font(LyXFont::ALL_INHERIT, l);
87 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
88 font.setColor(LColor::latex);
91 string::const_iterator cit = contents.begin();
92 string::const_iterator end = contents.end();
94 for (; cit != end; ++cit) {
95 inset.paragraphs.begin()->insertChar(pos++, *cit, font);
97 // the init has to be after the initialization of the paragraph
98 // because of the label settings (draw_label for ert insets).
103 InsetERT::~InsetERT()
105 InsetERTMailer(*this).hideDialog();
109 void InsetERT::read(Buffer const & buf, LyXLex & lex)
111 bool token_found = false;
114 string const token = lex.getString();
115 if (token == "status") {
117 string const tmp_token = lex.getString();
119 if (tmp_token == "Inlined") {
121 } else if (tmp_token == "Collapsed") {
122 status(0, Collapsed);
124 // leave this as default!
130 lyxerr << "InsetERT::Read: Missing 'status'-tag!"
132 // take countermeasures
133 lex.pushToken(token);
137 #warning this should be really short lived only for compatibility to
138 #warning files written 07/08/2001 so this has to go before 1.2.0! (Jug)
141 string const token = lex.getString();
142 if (token == "collapsed") {
144 collapsed_ = lex.getBool();
146 // Take countermeasures
147 lex.pushToken(token);
151 inset.read(buf, lex);
154 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
155 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
156 font.setColor(LColor::latex);
158 ParagraphList::iterator pit = inset.paragraphs.begin();
159 ParagraphList::iterator pend = inset.paragraphs.end();
160 for (; pit != pend; ++pit) {
161 pos_type siz = pit->size();
162 for (pos_type i = 0; i < siz; ++i) {
163 pit->setFont(i, font);
170 status(0, Collapsed);
179 void InsetERT::write(Buffer const & buf, ostream & os) const
195 os << getInsetName() << "\n"
196 << "status "<< st << "\n";
198 //inset.writeParagraphData(buf, os);
199 string const layout(buf.params.getLyXTextClass().defaultLayoutName());
200 ParagraphList::iterator par = inset.paragraphs.begin();
201 ParagraphList::iterator end = inset.paragraphs.end();
202 for (; par != end; ++par) {
203 os << "\n\\begin_layout " << layout << "\n";
204 pos_type siz = par->size();
205 for (pos_type i = 0; i < siz; ++i) {
206 Paragraph::value_type c = par->getChar(i);
208 case Paragraph::META_INSET:
209 if (par->getInset(i)->lyxCode() != InsetOld::NEWLINE_CODE) {
210 lyxerr << "Element is not allowed in insertERT"
213 par->getInset(i)->write(buf, os);
218 os << "\n\\backslash \n";
225 os << "\n\\end_layout\n";
230 string const InsetERT::editMessage() const
232 return _("Opened ERT Inset");
236 bool InsetERT::insertInset(BufferView *, InsetOld *)
242 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
245 #warning FIXME. More UI stupidity...
247 // if selectall is activated then the fontchange was an outside general
248 // fontchange and this messages is not needed
250 Alert::error(_("Cannot change font"),
251 _("You cannot change font settings inside TeX code."));
255 void InsetERT::updateStatus(BufferView * bv, bool swap) const
257 if (status_ != Inlined) {
259 status(bv, swap ? Open : Collapsed);
261 status(bv, swap ? Collapsed : Open);
267 InsetOld::EDITABLE InsetERT::editable() const
269 if (status_ == Collapsed)
271 return HIGHLY_EDITABLE;
275 void InsetERT::lfunMousePress(FuncRequest const & cmd)
277 if (status_ == Inlined)
278 inset.localDispatch(cmd);
280 InsetCollapsable::localDispatch(cmd);
284 bool InsetERT::lfunMouseRelease(FuncRequest const & cmd)
286 BufferView * bv = cmd.view();
288 if (cmd.button() == mouse_button::button3) {
293 if (status_ != Inlined && hitButton(cmd)) {
294 updateStatus(bv, true);
296 FuncRequest cmd1 = cmd;
298 cmd1.y = ascent() + cmd.y - inset.ascent();
300 // inlined is special - the text appears above
302 if (status_ == Inlined)
303 inset.localDispatch(cmd1);
304 else if (!collapsed_ && (cmd.y > button_dim.y2)) {
305 cmd1.y -= height_collapsed();
306 inset.localDispatch(cmd1);
313 void InsetERT::lfunMouseMotion(FuncRequest const & cmd)
315 if (status_ == Inlined)
316 inset.localDispatch(cmd);
318 InsetCollapsable::localDispatch(cmd);
322 int InsetERT::latex(Buffer const &, ostream & os,
323 LatexRunParams const &) const
325 ParagraphList::iterator par = inset.paragraphs.begin();
326 ParagraphList::iterator end = inset.paragraphs.end();
330 pos_type siz = par->size();
331 for (pos_type i = 0; i < siz; ++i) {
332 // ignore all struck out text
333 if (isDeletedText(*par, i))
336 if (par->isNewline(i)) {
340 os << par->getChar(i);
354 int InsetERT::ascii(Buffer const &, ostream &, int /*linelen*/) const
360 int InsetERT::linuxdoc(Buffer const &, ostream & os) const
362 ParagraphList::iterator par = inset.paragraphs.begin();
363 ParagraphList::iterator end = inset.paragraphs.end();
367 pos_type siz = par->size();
368 for (pos_type i = 0; i < siz; ++i) {
369 if (par->isNewline(i)) {
373 os << par->getChar(i);
387 int InsetERT::docbook(Buffer const &, ostream & os, bool) const
389 ParagraphList::iterator par = inset.paragraphs.begin();
390 ParagraphList::iterator end = inset.paragraphs.end();
394 pos_type siz = par->size();
395 for (pos_type i = 0; i < siz; ++i) {
396 if (par->isNewline(i)) {
400 os << par->getChar(i);
414 InsetOld::RESULT InsetERT::localDispatch(FuncRequest const & cmd)
416 InsetOld::RESULT result = UNDISPATCHED;
417 BufferView * bv = cmd.view();
419 if (inset.paragraphs.begin()->empty()) {
423 switch (cmd.action) {
425 case LFUN_INSET_EDIT:
426 if (cmd.button() == mouse_button::button3)
428 if (status_ == Inlined) {
429 if (!bv->lockInset(this))
431 result = inset.localDispatch(cmd);
433 result = InsetCollapsable::localDispatch(cmd);
439 case LFUN_INSET_MODIFY: {
440 InsetERT::ERTStatus status_;
441 InsetERTMailer::string2params(cmd.argument, status_);
445 /* FIXME: I refuse to believe we have to live
446 * with ugliness like this ! Note that this
447 * rebreak *is* needed. Consider a change from
448 * Open (needfullrow) to Inlined (only the space
449 * taken by the text).
451 inset.getLyXText(cmd.view())->fullRebreak();
452 bv->updateInset(this);
457 case LFUN_MOUSE_PRESS:
462 case LFUN_MOUSE_MOTION:
463 lfunMouseMotion(cmd);
467 case LFUN_MOUSE_RELEASE:
468 lfunMouseRelease(cmd);
473 bv->owner()->setLayout(inset.paragraphs.begin()->layout()->name());
474 result = DISPATCHED_NOUPDATE;
478 result = InsetCollapsable::localDispatch(cmd);
481 switch (cmd.action) {
482 case LFUN_BREAKPARAGRAPH:
483 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
485 case LFUN_BACKSPACE_SKIP:
487 case LFUN_DELETE_SKIP:
488 case LFUN_DELETE_LINE_FORWARD:
500 string const InsetERT::get_new_label() const
503 pos_type const max_length = 15;
504 pos_type const p_siz = inset.paragraphs.begin()->size();
505 pos_type const n = min(max_length, p_siz);
508 for(; i < n && j < p_siz; ++j) {
509 if (inset.paragraphs.begin()->isInset(j))
511 la += inset.paragraphs.begin()->getChar(j);
514 if (inset.paragraphs.size() > 1 || (i > 0 && j < p_siz)) {
524 void InsetERT::setButtonLabel() const
526 if (status_ == Collapsed) {
527 setLabel(get_new_label());
534 bool InsetERT::checkInsertChar(LyXFont & /* font */)
537 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
539 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
540 font.setColor(LColor::latex);
546 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
550 inset.metrics(mi, dim);
552 InsetCollapsable::metrics(mi, dim);
553 // Make it stand out on its own as it is code, not part of running
555 if (isOpen() && !inlined())
556 dim.wid = mi.base.textwidth;
561 void InsetERT::draw(PainterInfo & pi, int x, int y) const
563 InsetCollapsable::draw(pi, x, y, inlined());
567 void InsetERT::set_latex_font(BufferView * /* bv */)
570 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
572 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
573 font.setColor(LColor::latex);
575 inset.getLyXText(bv)->setFont(bv, font, false);
580 // attention this function can be called with bv == 0
581 void InsetERT::status(BufferView * bv, ERTStatus const st) const
596 bv->unlockInset(const_cast<InsetERT *>(this));
600 bv->updateInset(this);
601 bv->buffer()->markDirty();
607 bool InsetERT::showInsetDialog(BufferView * bv) const
609 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
614 void InsetERT::open(BufferView * bv)
622 void InsetERT::close(BufferView * bv) const
624 if (status_ == Collapsed || status_ == Inlined)
627 status(bv, Collapsed);
632 InsetERT::selectNextWordToSpellcheck(BufferView * bv, float &) const
634 bv->unlockInset(const_cast<InsetERT *>(this));
635 return WordLangTuple();
639 void InsetERT::getDrawFont(LyXFont & font) const
641 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
643 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
644 font.setColor(LColor::latex);
648 string const InsetERTMailer::name_("ert");
650 InsetERTMailer::InsetERTMailer(InsetERT & inset)
655 string const InsetERTMailer::inset2string(Buffer const &) const
657 return params2string(inset_.status());
661 void InsetERTMailer::string2params(string const & in,
662 InsetERT::ERTStatus & status)
664 status = InsetERT::Collapsed;
667 string body = split(in, name, ' ');
672 status = static_cast<InsetERT::ERTStatus>(strToInt(body));
677 InsetERTMailer::params2string(InsetERT::ERTStatus status)
679 return name_ + ' ' + tostr(status);