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"
24 #include "metricsinfo.h"
25 #include "paragraph.h"
26 #include "WordLangTuple.h"
28 #include "frontends/Alert.h"
29 #include "frontends/LyXView.h"
31 #include "support/tostr.h"
35 using lyx::support::split;
36 using lyx::support::strToInt;
49 LyXFont font(LyXFont::ALL_SANE);
52 font.setColor(LColor::latex);
59 InsetERT::InsetERT(BufferParams const & bp, bool collapsed)
60 : InsetCollapsable(bp, collapsed)
70 InsetERT::InsetERT(InsetERT const & in)
71 : InsetCollapsable(in), status_(in.status_)
77 auto_ptr<InsetBase> InsetERT::clone() const
79 return auto_ptr<InsetBase>(new InsetERT(*this));
83 InsetERT::InsetERT(BufferParams const & bp,
84 Language const * l, string const & contents, bool collapsed)
85 : InsetCollapsable(bp, collapsed)
92 LyXFont font(LyXFont::ALL_INHERIT, l);
94 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
95 font.setColor(LColor::latex);
98 string::const_iterator cit = contents.begin();
99 string::const_iterator end = contents.end();
101 for (; cit != end; ++cit) {
102 inset.paragraphs.begin()->insertChar(pos++, *cit, font);
104 // the init has to be after the initialization of the paragraph
105 // because of the label settings (draw_label for ert insets).
110 InsetERT::~InsetERT()
112 InsetERTMailer(*this).hideDialog();
116 void InsetERT::read(Buffer const & buf, LyXLex & lex)
118 bool token_found = false;
121 string const token = lex.getString();
122 if (token == "status") {
124 string const tmp_token = lex.getString();
126 if (tmp_token == "Inlined") {
128 } else if (tmp_token == "Collapsed") {
129 status(0, Collapsed);
131 // leave this as default!
137 lyxerr << "InsetERT::Read: Missing 'status'-tag!"
139 // take countermeasures
140 lex.pushToken(token);
144 #warning this should be really short lived only for compatibility to
145 #warning files written 07/08/2001 so this has to go before 1.2.0! (Jug)
148 string const token = lex.getString();
149 if (token == "collapsed") {
151 setCollapsed(lex.getBool());
153 // Take countermeasures
154 lex.pushToken(token);
158 inset.read(buf, lex);
161 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
162 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
163 font.setColor(LColor::latex);
165 ParagraphList::iterator pit = inset.paragraphs.begin();
166 ParagraphList::iterator pend = inset.paragraphs.end();
167 for (; pit != pend; ++pit) {
168 pos_type siz = pit->size();
169 for (pos_type i = 0; i < siz; ++i) {
170 pit->setFont(i, font);
179 status(0, Collapsed);
185 void InsetERT::write(Buffer const & buf, ostream & os) const
201 os << getInsetName() << "\n"
202 << "status "<< st << "\n";
204 //inset.writeParagraphData(buf, os);
205 string const layout(buf.params().getLyXTextClass().defaultLayoutName());
206 ParagraphList::iterator par = inset.paragraphs.begin();
207 ParagraphList::iterator end = inset.paragraphs.end();
208 for (; par != end; ++par) {
209 os << "\n\\begin_layout " << layout << "\n";
210 pos_type siz = par->size();
211 for (pos_type i = 0; i < siz; ++i) {
212 Paragraph::value_type c = par->getChar(i);
214 case Paragraph::META_INSET:
215 if (par->getInset(i)->lyxCode() != InsetOld::NEWLINE_CODE) {
216 lyxerr << "Element is not allowed in insertERT"
219 par->getInset(i)->write(buf, os);
224 os << "\n\\backslash \n";
231 os << "\n\\end_layout\n";
236 string const InsetERT::editMessage() const
238 return _("Opened ERT Inset");
242 bool InsetERT::insertInset(BufferView *, InsetOld *)
248 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
251 #warning FIXME. More UI stupidity...
253 // if selectall is activated then the fontchange was an outside general
254 // fontchange and this messages is not needed
256 Alert::error(_("Cannot change font"),
257 _("You cannot change font settings inside TeX code."));
261 void InsetERT::updateStatus(BufferView * bv, bool swap) const
263 if (status_ != Inlined) {
265 status(bv, swap ? Collapsed : Open);
267 status(bv, swap ? Open : Collapsed);
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
306 if (status_ == Inlined)
307 inset.localDispatch(cmd1);
308 else if (isOpen() && (cmd.y > buttonDim().y2)) {
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 InsetOld::RESULT InsetERT::localDispatch(FuncRequest const & cmd)
420 InsetOld::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 inset.getLyXText(cmd.view())->fullRebreak();
456 bv->updateInset(this);
461 case LFUN_MOUSE_PRESS:
466 case LFUN_MOUSE_MOTION:
467 lfunMouseMotion(cmd);
471 case LFUN_MOUSE_RELEASE:
472 lfunMouseRelease(cmd);
477 bv->owner()->setLayout(inset.paragraphs.begin()->layout()->name());
478 result = DISPATCHED_NOUPDATE;
482 result = InsetCollapsable::localDispatch(cmd);
485 switch (cmd.action) {
486 case LFUN_BREAKPARAGRAPH:
487 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
489 case LFUN_BACKSPACE_SKIP:
491 case LFUN_DELETE_SKIP:
492 case LFUN_DELETE_LINE_FORWARD:
504 string const InsetERT::get_new_label() const
507 pos_type const max_length = 15;
508 pos_type const p_siz = inset.paragraphs.begin()->size();
509 pos_type const n = min(max_length, p_siz);
512 for(; i < n && j < p_siz; ++j) {
513 if (inset.paragraphs.begin()->isInset(j))
515 la += inset.paragraphs.begin()->getChar(j);
518 if (inset.paragraphs.size() > 1 || (i > 0 && j < p_siz)) {
528 void InsetERT::setButtonLabel() const
530 if (status_ == Collapsed) {
531 setLabel(get_new_label());
538 bool InsetERT::checkInsertChar(LyXFont & /* font */)
541 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
543 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
544 font.setColor(LColor::latex);
550 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
554 inset.metrics(mi, dim);
556 InsetCollapsable::metrics(mi, dim);
557 // Make it stand out on its own as it is code, not part of running
559 if (isOpen() && !inlined())
560 dim.wid = mi.base.textwidth;
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
600 bv->unlockInset(const_cast<InsetERT *>(this));
604 bv->updateInset(this);
605 bv->buffer()->markDirty();
611 bool InsetERT::showInsetDialog(BufferView * bv) const
613 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
618 void InsetERT::open(BufferView * bv)
626 void InsetERT::close(BufferView * bv) const
628 if (status_ == Collapsed || status_ == Inlined)
631 status(bv, Collapsed);
636 InsetERT::selectNextWordToSpellcheck(BufferView * bv, float &) const
638 bv->unlockInset(const_cast<InsetERT *>(this));
639 return WordLangTuple();
643 void InsetERT::getDrawFont(LyXFont & font) const
645 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
647 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
648 font.setColor(LColor::latex);
652 string const InsetERTMailer::name_("ert");
654 InsetERTMailer::InsetERTMailer(InsetERT & inset)
659 string const InsetERTMailer::inset2string(Buffer const &) const
661 return params2string(inset_.status());
665 void InsetERTMailer::string2params(string const & in,
666 InsetERT::ERTStatus & status)
668 status = InsetERT::Collapsed;
671 string body = split(in, name, ' ');
676 status = static_cast<InsetERT::ERTStatus>(strToInt(body));
681 InsetERTMailer::params2string(InsetERT::ERTStatus status)
683 return name_ + ' ' + tostr(status);