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 "WordLangTuple.h"
25 #include "frontends/Alert.h"
26 #include "frontends/LyXView.h"
28 #include "support/tostr.h"
30 using namespace lyx::support;
43 labelfont = LyXFont(LyXFont::ALL_SANE);
46 labelfont.setColor(LColor::latex);
51 InsetERT::InsetERT(BufferParams const & bp, bool collapsed)
52 : InsetCollapsable(bp, collapsed)
62 InsetERT::InsetERT(InsetERT const & in)
63 : InsetCollapsable(in), status_(in.status_)
69 auto_ptr<InsetBase> InsetERT::clone() const
71 return auto_ptr<InsetBase>(new InsetERT(*this));
75 InsetERT::InsetERT(BufferParams const & bp,
76 Language const * l, string const & contents, bool collapsed)
77 : InsetCollapsable(bp, collapsed)
84 LyXFont font(LyXFont::ALL_INHERIT, l);
86 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
87 font.setColor(LColor::latex);
90 string::const_iterator cit = contents.begin();
91 string::const_iterator end = contents.end();
93 for (; cit != end; ++cit) {
94 inset.paragraphs.begin()->insertChar(pos++, *cit, font);
96 // the init has to be after the initialization of the paragraph
97 // because of the label settings (draw_label for ert insets).
102 InsetERT::~InsetERT()
104 InsetERTMailer(*this).hideDialog();
108 void InsetERT::read(Buffer const & buf, LyXLex & lex)
110 bool token_found = false;
113 string const token = lex.getString();
114 if (token == "status") {
116 string const tmp_token = lex.getString();
118 if (tmp_token == "Inlined") {
120 } else if (tmp_token == "Collapsed") {
121 status(0, Collapsed);
123 // leave this as default!
129 lyxerr << "InsetERT::Read: Missing 'status'-tag!"
131 // take countermeasures
132 lex.pushToken(token);
136 #warning this should be really short lived only for compatibility to
137 #warning files written 07/08/2001 so this has to go before 1.2.0! (Jug)
140 string const token = lex.getString();
141 if (token == "collapsed") {
143 collapsed_ = lex.getBool();
145 // Take countermeasures
146 lex.pushToken(token);
150 inset.read(buf, lex);
153 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
154 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
155 font.setColor(LColor::latex);
157 ParagraphList::iterator pit = inset.paragraphs.begin();
158 ParagraphList::iterator pend = inset.paragraphs.end();
159 for (; pit != pend; ++pit) {
160 pos_type siz = pit->size();
161 for (pos_type i = 0; i < siz; ++i) {
162 pit->setFont(i, font);
169 status(0, Collapsed);
178 void InsetERT::write(Buffer const & buf, ostream & os) const
194 os << getInsetName() << "\n"
195 << "status "<< st << "\n";
197 //inset.writeParagraphData(buf, os);
198 string const layout(buf.params.getLyXTextClass().defaultLayoutName());
199 ParagraphList::iterator par = inset.paragraphs.begin();
200 ParagraphList::iterator end = inset.paragraphs.end();
201 for (; par != end; ++par) {
202 os << "\n\\begin_layout " << layout << "\n";
203 pos_type siz = par->size();
204 for (pos_type i = 0; i < siz; ++i) {
205 Paragraph::value_type c = par->getChar(i);
207 case Paragraph::META_INSET:
208 if (par->getInset(i)->lyxCode() != InsetOld::NEWLINE_CODE) {
209 lyxerr << "Element is not allowed in insertERT"
212 par->getInset(i)->write(buf, os);
217 os << "\n\\backslash \n";
224 os << "\n\\end_layout\n";
229 string const InsetERT::editMessage() const
231 return _("Opened ERT Inset");
235 bool InsetERT::insertInset(BufferView *, InsetOld *)
241 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
244 #warning FIXME. More UI stupidity...
246 // if selectall is activated then the fontchange was an outside general
247 // fontchange and this messages is not needed
249 Alert::error(_("Cannot change font"),
250 _("You cannot change font settings inside TeX code."));
254 void InsetERT::updateStatus(BufferView * bv, bool swap) const
256 if (status_ != Inlined) {
258 status(bv, swap ? Open : Collapsed);
260 status(bv, swap ? Collapsed : Open);
266 InsetOld::EDITABLE InsetERT::editable() const
268 if (status_ == Collapsed)
270 return HIGHLY_EDITABLE;
274 void InsetERT::lfunMousePress(FuncRequest const & cmd)
276 if (status_ == Inlined)
277 inset.localDispatch(cmd);
279 InsetCollapsable::localDispatch(cmd);
283 bool InsetERT::lfunMouseRelease(FuncRequest const & cmd)
285 BufferView * bv = cmd.view();
287 if (cmd.button() == mouse_button::button3) {
292 if (status_ != Inlined && hitButton(cmd)) {
293 updateStatus(bv, true);
295 FuncRequest cmd1 = cmd;
297 cmd1.y = ascent() + cmd.y - inset.ascent();
299 // inlined is special - the text appears above
301 if (status_ == Inlined)
302 inset.localDispatch(cmd1);
303 else if (!collapsed_ && (cmd.y > button_dim.y2)) {
304 cmd1.y -= height_collapsed();
305 inset.localDispatch(cmd1);
312 void InsetERT::lfunMouseMotion(FuncRequest const & cmd)
314 if (status_ == Inlined)
315 inset.localDispatch(cmd);
317 InsetCollapsable::localDispatch(cmd);
321 int InsetERT::latex(Buffer const &, ostream & os,
322 LatexRunParams const &) const
324 ParagraphList::iterator par = inset.paragraphs.begin();
325 ParagraphList::iterator end = inset.paragraphs.end();
329 pos_type siz = par->size();
330 for (pos_type i = 0; i < siz; ++i) {
331 // ignore all struck out text
332 if (isDeletedText(*par, i))
335 if (par->isNewline(i)) {
339 os << par->getChar(i);
353 int InsetERT::ascii(Buffer const &, ostream &, int /*linelen*/) const
359 int InsetERT::linuxdoc(Buffer const &, ostream & os) const
361 ParagraphList::iterator par = inset.paragraphs.begin();
362 ParagraphList::iterator end = inset.paragraphs.end();
366 pos_type siz = par->size();
367 for (pos_type i = 0; i < siz; ++i) {
368 if (par->isNewline(i)) {
372 os << par->getChar(i);
386 int InsetERT::docbook(Buffer const &, ostream & os, bool) const
388 ParagraphList::iterator par = inset.paragraphs.begin();
389 ParagraphList::iterator end = inset.paragraphs.end();
393 pos_type siz = par->size();
394 for (pos_type i = 0; i < siz; ++i) {
395 if (par->isNewline(i)) {
399 os << par->getChar(i);
413 InsetOld::RESULT InsetERT::localDispatch(FuncRequest const & cmd)
415 InsetOld::RESULT result = UNDISPATCHED;
416 BufferView * bv = cmd.view();
418 if (inset.paragraphs.begin()->empty()) {
422 switch (cmd.action) {
424 case LFUN_INSET_EDIT:
425 if (cmd.button() == mouse_button::button3)
427 if (status_ == Inlined) {
428 if (!bv->lockInset(this))
430 result = inset.localDispatch(cmd);
432 result = InsetCollapsable::localDispatch(cmd);
438 case LFUN_INSET_MODIFY: {
439 InsetERT::ERTStatus status_;
440 InsetERTMailer::string2params(cmd.argument, status_);
444 /* FIXME: I refuse to believe we have to live
445 * with ugliness like this ! Note that this
446 * rebreak *is* needed. Consider a change from
447 * Open (needfullrow) to Inlined (only the space
448 * taken by the text).
450 inset.getLyXText(cmd.view())->fullRebreak();
451 bv->updateInset(this);
456 case LFUN_MOUSE_PRESS:
461 case LFUN_MOUSE_MOTION:
462 lfunMouseMotion(cmd);
466 case LFUN_MOUSE_RELEASE:
467 lfunMouseRelease(cmd);
472 bv->owner()->setLayout(inset.paragraphs.begin()->layout()->name());
473 result = DISPATCHED_NOUPDATE;
477 result = InsetCollapsable::localDispatch(cmd);
480 switch (cmd.action) {
481 case LFUN_BREAKPARAGRAPH:
482 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
484 case LFUN_BACKSPACE_SKIP:
486 case LFUN_DELETE_SKIP:
487 case LFUN_DELETE_LINE_FORWARD:
499 string const InsetERT::get_new_label() const
502 pos_type const max_length = 15;
503 pos_type const p_siz = inset.paragraphs.begin()->size();
504 pos_type const n = min(max_length, p_siz);
507 for(; i < n && j < p_siz; ++j) {
508 if (inset.paragraphs.begin()->isInset(j))
510 la += inset.paragraphs.begin()->getChar(j);
513 if (inset.paragraphs.size() > 1 || (i > 0 && j < p_siz)) {
523 void InsetERT::setButtonLabel() const
525 if (status_ == Collapsed) {
526 setLabel(get_new_label());
533 bool InsetERT::checkInsertChar(LyXFont & /* font */)
536 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
538 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
539 font.setColor(LColor::latex);
545 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
549 inset.metrics(mi, dim);
551 InsetCollapsable::metrics(mi, dim);
552 // Make it stand out on its own as it is code, not part of running
554 if (isOpen() && !inlined())
555 dim.wid = mi.base.textwidth;
560 void InsetERT::draw(PainterInfo & pi, int x, int y) const
562 InsetCollapsable::draw(pi, x, y, inlined());
566 void InsetERT::set_latex_font(BufferView * /* bv */)
569 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
571 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
572 font.setColor(LColor::latex);
574 inset.getLyXText(bv)->setFont(bv, font, false);
579 // attention this function can be called with bv == 0
580 void InsetERT::status(BufferView * bv, ERTStatus const st) const
595 bv->unlockInset(const_cast<InsetERT *>(this));
599 bv->updateInset(this);
600 bv->buffer()->markDirty();
606 bool InsetERT::showInsetDialog(BufferView * bv) const
608 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
613 void InsetERT::open(BufferView * bv)
621 void InsetERT::close(BufferView * bv) const
623 if (status_ == Collapsed || status_ == Inlined)
626 status(bv, Collapsed);
631 InsetERT::selectNextWordToSpellcheck(BufferView * bv, float &) const
633 bv->unlockInset(const_cast<InsetERT *>(this));
634 return WordLangTuple();
638 void InsetERT::getDrawFont(LyXFont & font) const
640 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
642 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
643 font.setColor(LColor::latex);
647 string const InsetERTMailer::name_("ert");
649 InsetERTMailer::InsetERTMailer(InsetERT & inset)
654 string const InsetERTMailer::inset2string(Buffer const &) const
656 return params2string(inset_.status());
660 void InsetERTMailer::string2params(string const & in,
661 InsetERT::ERTStatus & status)
663 status = InsetERT::Collapsed;
666 string body = split(in, name, ' ');
671 status = static_cast<InsetERT::ERTStatus>(strToInt(body));
676 InsetERTMailer::params2string(InsetERT::ERTStatus status)
678 return name_ + ' ' + tostr(status);