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.
14 #include "insettext.h"
15 #include "metricsinfo.h"
18 #include "BufferView.h"
20 #include "funcrequest.h"
27 #include "WordLangTuple.h"
29 #include "frontends/Alert.h"
30 #include "frontends/Dialogs.h"
31 #include "frontends/LyXView.h"
33 #include "support/LOstream.h"
34 #include "support/LAssert.h"
35 #include "support/tostr.h"
37 using namespace lyx::support;
50 labelfont = LyXFont(LyXFont::ALL_SANE);
53 labelfont.setColor(LColor::latex);
58 InsetERT::InsetERT(BufferParams const & bp, bool collapsed)
59 : InsetCollapsable(bp, collapsed)
69 InsetERT::InsetERT(InsetERT const & in)
70 : InsetCollapsable(in), status_(in.status_)
76 auto_ptr<InsetBase> InsetERT::clone() const
78 return auto_ptr<InsetBase>(new InsetERT(*this));
82 InsetERT::InsetERT(BufferParams const & bp,
83 Language const * l, string const & contents, bool collapsed)
84 : InsetCollapsable(bp, collapsed)
91 LyXFont font(LyXFont::ALL_INHERIT, l);
93 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
94 font.setColor(LColor::latex);
97 string::const_iterator cit = contents.begin();
98 string::const_iterator end = contents.end();
100 for (; cit != end; ++cit) {
101 inset.paragraphs.begin()->insertChar(pos++, *cit, font);
103 // the init has to be after the initialization of the paragraph
104 // because of the label settings (draw_label for ert insets).
109 InsetERT::~InsetERT()
111 InsetERTMailer(*this).hideDialog();
115 void InsetERT::read(Buffer const & buf, LyXLex & lex)
117 bool token_found = false;
120 string const token = lex.getString();
121 if (token == "status") {
123 string const tmp_token = lex.getString();
125 if (tmp_token == "Inlined") {
127 } else if (tmp_token == "Collapsed") {
128 status(0, Collapsed);
130 // leave this as default!
136 lyxerr << "InsetERT::Read: Missing 'status'-tag!"
138 // take countermeasures
139 lex.pushToken(token);
143 #warning this should be really short lived only for compatibility to
144 #warning files written 07/08/2001 so this has to go before 1.2.0! (Jug)
147 string const token = lex.getString();
148 if (token == "collapsed") {
150 collapsed_ = lex.getBool();
152 // Take countermeasures
153 lex.pushToken(token);
157 inset.read(buf, lex);
160 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
161 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
162 font.setColor(LColor::latex);
164 ParagraphList::iterator pit = inset.paragraphs.begin();
165 ParagraphList::iterator pend = inset.paragraphs.end();
166 for (; pit != pend; ++pit) {
167 pos_type siz = pit->size();
168 for (pos_type i = 0; i < siz; ++i) {
169 pit->setFont(i, font);
176 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 ? Open : Collapsed);
267 status(bv, swap ? Collapsed : Open);
273 InsetOld::EDITABLE InsetERT::editable() const
275 if (status_ == Collapsed)
277 return HIGHLY_EDITABLE;
281 void InsetERT::lfunMousePress(FuncRequest const & cmd)
283 if (status_ == Inlined)
284 inset.localDispatch(cmd);
286 InsetCollapsable::localDispatch(cmd);
290 bool InsetERT::lfunMouseRelease(FuncRequest const & cmd)
292 BufferView * bv = cmd.view();
294 if (cmd.button() == mouse_button::button3) {
299 if (status_ != Inlined && hitButton(cmd)) {
300 updateStatus(bv, true);
302 FuncRequest cmd1 = cmd;
304 cmd1.y = ascent() + cmd.y - inset.ascent();
306 // inlined is special - the text appears above
308 if (status_ == Inlined)
309 inset.localDispatch(cmd1);
310 else if (!collapsed_ && (cmd.y > button_dim.y2)) {
311 cmd1.y -= height_collapsed();
312 inset.localDispatch(cmd1);
319 void InsetERT::lfunMouseMotion(FuncRequest const & cmd)
321 if (status_ == Inlined)
322 inset.localDispatch(cmd);
324 InsetCollapsable::localDispatch(cmd);
328 int InsetERT::latex(Buffer const &, ostream & os,
329 LatexRunParams const &) const
331 ParagraphList::iterator par = inset.paragraphs.begin();
332 ParagraphList::iterator end = inset.paragraphs.end();
336 pos_type siz = par->size();
337 for (pos_type i = 0; i < siz; ++i) {
338 // ignore all struck out text
339 if (isDeletedText(*par, i))
342 if (par->isNewline(i)) {
346 os << par->getChar(i);
360 int InsetERT::ascii(Buffer const &, ostream &, int /*linelen*/) const
366 int InsetERT::linuxdoc(Buffer const &, ostream & os) const
368 ParagraphList::iterator par = inset.paragraphs.begin();
369 ParagraphList::iterator end = inset.paragraphs.end();
373 pos_type siz = par->size();
374 for (pos_type i = 0; i < siz; ++i) {
375 if (par->isNewline(i)) {
379 os << par->getChar(i);
393 int InsetERT::docbook(Buffer const &, ostream & os, bool) const
395 ParagraphList::iterator par = inset.paragraphs.begin();
396 ParagraphList::iterator end = inset.paragraphs.end();
400 pos_type siz = par->size();
401 for (pos_type i = 0; i < siz; ++i) {
402 if (par->isNewline(i)) {
406 os << par->getChar(i);
420 InsetOld::RESULT InsetERT::localDispatch(FuncRequest const & cmd)
422 InsetOld::RESULT result = UNDISPATCHED;
423 BufferView * bv = cmd.view();
425 if (inset.paragraphs.begin()->empty()) {
429 switch (cmd.action) {
431 case LFUN_INSET_EDIT:
432 if (cmd.button() == mouse_button::button3)
434 if (status_ == Inlined) {
435 if (!bv->lockInset(this))
437 result = inset.localDispatch(cmd);
439 result = InsetCollapsable::localDispatch(cmd);
445 case LFUN_INSET_MODIFY: {
446 InsetERT::ERTStatus status_;
447 InsetERTMailer::string2params(cmd.argument, status_);
451 /* FIXME: I refuse to believe we have to live
452 * with ugliness like this ! Note that this
453 * rebreak *is* needed. Consider a change from
454 * Open (needfullrow) to Inlined (only the space
455 * taken by the text).
457 inset.getLyXText(cmd.view())->fullRebreak();
458 bv->updateInset(this);
463 case LFUN_MOUSE_PRESS:
468 case LFUN_MOUSE_MOTION:
469 lfunMouseMotion(cmd);
473 case LFUN_MOUSE_RELEASE:
474 lfunMouseRelease(cmd);
479 bv->owner()->setLayout(inset.paragraphs.begin()->layout()->name());
480 result = DISPATCHED_NOUPDATE;
484 result = InsetCollapsable::localDispatch(cmd);
487 switch (cmd.action) {
488 case LFUN_BREAKPARAGRAPH:
489 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
491 case LFUN_BACKSPACE_SKIP:
493 case LFUN_DELETE_SKIP:
494 case LFUN_DELETE_LINE_FORWARD:
506 string const InsetERT::get_new_label() const
509 pos_type const max_length = 15;
510 pos_type const p_siz = inset.paragraphs.begin()->size();
511 pos_type const n = min(max_length, p_siz);
514 for(; i < n && j < p_siz; ++j) {
515 if (inset.paragraphs.begin()->isInset(j))
517 la += inset.paragraphs.begin()->getChar(j);
520 if (inset.paragraphs.size() > 1 || (i > 0 && j < p_siz)) {
530 void InsetERT::setButtonLabel() const
532 if (status_ == Collapsed) {
533 setLabel(get_new_label());
540 bool InsetERT::checkInsertChar(LyXFont & /* font */)
543 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
545 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
546 font.setColor(LColor::latex);
552 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
556 inset.metrics(mi, dim);
558 InsetCollapsable::metrics(mi, dim);
559 // Make it stand out on its own as it is code, not part of running
561 if (isOpen() && !inlined())
562 dim.wid = mi.base.textwidth;
567 void InsetERT::draw(PainterInfo & pi, int x, int y) const
569 InsetCollapsable::draw(pi, x, y, inlined());
573 void InsetERT::set_latex_font(BufferView * /* bv */)
576 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
578 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
579 font.setColor(LColor::latex);
581 inset.getLyXText(bv)->setFont(bv, font, false);
586 // attention this function can be called with bv == 0
587 void InsetERT::status(BufferView * bv, ERTStatus const st) const
602 bv->unlockInset(const_cast<InsetERT *>(this));
606 bv->updateInset(this);
607 bv->buffer()->markDirty();
613 bool InsetERT::showInsetDialog(BufferView * bv) const
615 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
620 void InsetERT::open(BufferView * bv)
628 void InsetERT::close(BufferView * bv) const
630 if (status_ == Collapsed || status_ == Inlined)
633 status(bv, Collapsed);
638 InsetERT::selectNextWordToSpellcheck(BufferView * bv, float &) const
640 bv->unlockInset(const_cast<InsetERT *>(this));
641 return WordLangTuple();
645 void InsetERT::getDrawFont(LyXFont & font) const
647 LyXFont f(LyXFont::ALL_INHERIT, latex_language);
649 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
650 font.setColor(LColor::latex);
654 string const InsetERTMailer::name_("ert");
656 InsetERTMailer::InsetERTMailer(InsetERT & inset)
661 string const InsetERTMailer::inset2string(Buffer const &) const
663 return params2string(inset_.status());
667 void InsetERTMailer::string2params(string const & in,
668 InsetERT::ERTStatus & status)
670 status = InsetERT::Collapsed;
673 string body = split(in, name, ' ');
678 status = static_cast<InsetERT::ERTStatus>(strToInt(body));
683 InsetERTMailer::params2string(InsetERT::ERTStatus status)
685 return name_ + ' ' + tostr(status);