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.
17 #include "bufferparams.h"
18 #include "BufferView.h"
20 #include "dispatchresult.h"
21 #include "funcrequest.h"
26 #include "metricsinfo.h"
27 #include "paragraph.h"
29 #include "frontends/Alert.h"
30 #include "frontends/LyXView.h"
32 #include "support/tostr.h"
36 using lyx::support::split;
37 using lyx::support::strToInt;
50 LyXFont font(LyXFont::ALL_SANE);
53 font.setColor(LColor::latex);
60 InsetERT::InsetERT(BufferParams const & bp, bool collapsed)
61 : InsetCollapsable(bp, collapsed)
63 status_ = collapsed ? Collapsed : Open;
68 InsetERT::InsetERT(InsetERT const & in)
69 : InsetCollapsable(in), status_(in.status_)
75 auto_ptr<InsetBase> InsetERT::clone() const
77 return auto_ptr<InsetBase>(new InsetERT(*this));
81 InsetERT::InsetERT(BufferParams const & bp,
82 Language const * l, string const & contents, bool collapsed)
83 : InsetCollapsable(bp, collapsed)
85 status_ = collapsed ? Collapsed : Open;
87 LyXFont font(LyXFont::ALL_INHERIT, l);
89 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
90 font.setColor(LColor::latex);
93 string::const_iterator cit = contents.begin();
94 string::const_iterator end = contents.end();
96 for (; cit != end; ++cit) {
97 inset.paragraphs().begin()->insertChar(pos++, *cit, font);
99 // the init has to be after the initialization of the paragraph
100 // because of the label settings (draw_label for ert insets).
105 InsetERT::~InsetERT()
107 InsetERTMailer(*this).hideDialog();
111 void InsetERT::read(Buffer const & buf, LyXLex & lex)
113 bool token_found = false;
116 string const token = lex.getString();
117 if (token == "status") {
119 string const tmp_token = lex.getString();
121 if (tmp_token == "Inlined") {
123 } else if (tmp_token == "Collapsed") {
126 // leave this as default!
132 lyxerr << "InsetERT::Read: Missing 'status'-tag!"
134 // take countermeasures
135 lex.pushToken(token);
138 inset.read(buf, lex);
141 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
142 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
143 font.setColor(LColor::latex);
145 ParagraphList::iterator pit = inset.paragraphs().begin();
146 ParagraphList::iterator pend = inset.paragraphs().end();
147 for (; pit != pend; ++pit) {
148 pos_type siz = pit->size();
149 for (pos_type i = 0; i < siz; ++i) {
150 pit->setFont(i, font);
165 void InsetERT::write(Buffer const & buf, ostream & os) const
181 os << getInsetName() << "\n" << "status "<< st << "\n";
183 //inset.writeParagraphData(buf, os);
184 string const layout(buf.params().getLyXTextClass().defaultLayoutName());
185 ParagraphList::iterator par = inset.paragraphs().begin();
186 ParagraphList::iterator end = inset.paragraphs().end();
187 for (; par != end; ++par) {
188 os << "\n\\begin_layout " << layout << "\n";
189 pos_type siz = par->size();
190 for (pos_type i = 0; i < siz; ++i) {
191 Paragraph::value_type c = par->getChar(i);
193 case Paragraph::META_INSET:
194 if (par->getInset(i)->lyxCode() != InsetOld::NEWLINE_CODE) {
195 lyxerr << "Element is not allowed in insertERT"
198 par->getInset(i)->write(buf, os);
203 os << "\n\\backslash \n";
210 os << "\n\\end_layout\n";
215 string const InsetERT::editMessage() const
217 return _("Opened ERT Inset");
221 bool InsetERT::insertInset(BufferView *, InsetOld *)
227 void InsetERT::updateStatus(bool swap) const
229 if (status_ != Inlined) {
231 status(swap ? Collapsed : Open);
233 status(swap ? Open : Collapsed);
238 InsetOld::EDITABLE InsetERT::editable() const
240 return (status_ == Collapsed) ? IS_EDITABLE : HIGHLY_EDITABLE;
244 void InsetERT::lfunMousePress(FuncRequest const & cmd)
246 if (status_ == Inlined)
251 InsetCollapsable::priv_dispatch(cmd, idx, pos);
256 bool InsetERT::lfunMouseRelease(FuncRequest const & cmd)
258 BufferView * bv = cmd.view();
260 if (cmd.button() == mouse_button::button3) {
265 if (status_ != Inlined && hitButton(cmd)) {
268 FuncRequest cmd1 = cmd;
270 cmd1.y = ascent() + cmd.y - inset.ascent();
272 // inlined is special - the text appears above
273 if (status_ == Inlined)
274 inset.dispatch(cmd1);
275 else if (isOpen() && cmd.y > buttonDim().y2) {
276 cmd1.y -= height_collapsed();
277 inset.dispatch(cmd1);
284 void InsetERT::lfunMouseMotion(FuncRequest const & cmd)
286 if (status_ == Inlined)
291 InsetCollapsable::priv_dispatch(cmd, idx, pos);
296 int InsetERT::latex(Buffer const &, ostream & os,
297 OutputParams const &) const
299 ParagraphList::iterator par = inset.paragraphs().begin();
300 ParagraphList::iterator end = inset.paragraphs().end();
304 pos_type siz = par->size();
305 for (pos_type i = 0; i < siz; ++i) {
306 // ignore all struck out text
307 if (isDeletedText(*par, i))
310 if (par->isNewline(i)) {
314 os << par->getChar(i);
328 int InsetERT::plaintext(Buffer const &, ostream &,
329 OutputParams const & /*runparams*/) const
335 int InsetERT::linuxdoc(Buffer const &, ostream & os,
336 OutputParams const &)const
338 ParagraphList::iterator par = inset.paragraphs().begin();
339 ParagraphList::iterator end = inset.paragraphs().end();
343 pos_type siz = par->size();
344 for (pos_type i = 0; i < siz; ++i) {
345 if (par->isNewline(i)) {
349 os << par->getChar(i);
363 int InsetERT::docbook(Buffer const &, ostream & os,
364 OutputParams const &) 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 void InsetERT::edit(BufferView * bv, bool left)
393 if (status_ == Inlined) {
394 inset.edit(bv, left);
396 InsetCollapsable::edit(bv, left);
404 InsetERT::priv_dispatch(FuncRequest const & cmd, idx_type & idx, pos_type & pos)
406 BufferView * bv = cmd.view();
408 if (inset.paragraphs().begin()->empty())
411 switch (cmd.action) {
413 case LFUN_INSET_MODIFY: {
414 InsetERT::ERTStatus status_;
415 InsetERTMailer::string2params(cmd.argument, status_);
418 return DispatchResult(true, true);
421 case LFUN_MOUSE_PRESS:
423 return DispatchResult(true, true);
425 case LFUN_MOUSE_MOTION:
426 lfunMouseMotion(cmd);
427 return DispatchResult(true, true);
429 case LFUN_MOUSE_RELEASE:
430 lfunMouseRelease(cmd);
431 return DispatchResult(true, true);
434 bv->owner()->setLayout(inset.paragraphs().begin()->layout()->name());
435 return DispatchResult(true);
437 case LFUN_BREAKPARAGRAPH:
438 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
440 case LFUN_BACKSPACE_SKIP:
442 case LFUN_DELETE_SKIP:
443 case LFUN_DELETE_LINE_FORWARD:
446 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
449 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
454 void InsetERT::setButtonLabel() const
456 setLabel(status_ == Collapsed ? getNewLabel(_("ERT")) : _("ERT"));
460 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
464 inset.metrics(mi, dim);
466 InsetCollapsable::metrics(mi, dim);
467 // Make it stand out on its own as it is code, not part of running
469 if (isOpen() && !inlined())
470 dim.wid = mi.base.textwidth;
475 void InsetERT::draw(PainterInfo & pi, int x, int y) const
477 InsetCollapsable::draw(pi, x, y, inlined());
481 void InsetERT::setLatexFont(BufferView * /*bv*/)
484 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
485 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
486 font.setColor(LColor::latex);
487 inset.text_.setFont(bv, font, false);
492 void InsetERT::status(ERTStatus const st) const
514 bool InsetERT::showInsetDialog(BufferView * bv) const
516 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
521 void InsetERT::open()
528 void InsetERT::close() const
530 if (status_ == Collapsed || status_ == Inlined)
537 void InsetERT::getDrawFont(LyXFont & font) const
539 font = LyXFont(LyXFont::ALL_INHERIT, latex_language);
540 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
541 font.setColor(LColor::latex);
545 string const InsetERTMailer::name_("ert");
547 InsetERTMailer::InsetERTMailer(InsetERT & inset)
552 string const InsetERTMailer::inset2string(Buffer const &) const
554 return params2string(inset_.status());
558 void InsetERTMailer::string2params(string const & in,
559 InsetERT::ERTStatus & status)
561 status = InsetERT::Collapsed;
564 string body = split(in, name, ' ');
569 status = static_cast<InsetERT::ERTStatus>(strToInt(body));
573 string const InsetERTMailer::params2string(InsetERT::ERTStatus status)
575 return name_ + ' ' + tostr(status);