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)
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;
239 void InsetERT::lfunMousePress(FuncRequest const & cmd)
241 if (status_ == Inlined)
246 InsetCollapsable::priv_dispatch(cmd, idx, pos);
251 bool InsetERT::lfunMouseRelease(FuncRequest const & cmd)
253 BufferView * bv = cmd.view();
255 if (cmd.button() == mouse_button::button3) {
260 if (status_ != Inlined && hitButton(cmd)) {
263 FuncRequest cmd1 = cmd;
265 cmd1.y = ascent() + cmd.y - inset.ascent();
267 // inlined is special - the text appears above
268 if (status_ == Inlined)
269 inset.dispatch(cmd1);
270 else if (isOpen() && cmd.y > buttonDim().y2) {
271 cmd1.y -= height_collapsed();
272 inset.dispatch(cmd1);
279 void InsetERT::lfunMouseMotion(FuncRequest const & cmd)
281 if (status_ == Inlined)
286 InsetCollapsable::priv_dispatch(cmd, idx, pos);
291 int InsetERT::latex(Buffer const &, ostream & os,
292 OutputParams const &) const
294 ParagraphList::iterator par = inset.paragraphs().begin();
295 ParagraphList::iterator end = inset.paragraphs().end();
299 pos_type siz = par->size();
300 for (pos_type i = 0; i < siz; ++i) {
301 // ignore all struck out text
302 if (isDeletedText(*par, i))
305 if (par->isNewline(i)) {
309 os << par->getChar(i);
323 int InsetERT::plaintext(Buffer const &, ostream &,
324 OutputParams const & /*runparams*/) const
330 int InsetERT::linuxdoc(Buffer const &, ostream & os,
331 OutputParams const &)const
333 ParagraphList::iterator par = inset.paragraphs().begin();
334 ParagraphList::iterator end = inset.paragraphs().end();
338 pos_type siz = par->size();
339 for (pos_type i = 0; i < siz; ++i) {
340 if (par->isNewline(i)) {
344 os << par->getChar(i);
358 int InsetERT::docbook(Buffer const &, ostream & os,
359 OutputParams const &) 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 void InsetERT::edit(BufferView * bv, bool left)
388 if (status_ == Inlined)
389 inset.edit(bv, left);
391 InsetCollapsable::edit(bv, left);
397 InsetERT::priv_dispatch(FuncRequest const & cmd, idx_type & idx, pos_type & pos)
399 BufferView * bv = cmd.view();
401 switch (cmd.action) {
403 case LFUN_INSET_MODIFY: {
404 InsetERTMailer::string2params(cmd.argument, status_);
407 return DispatchResult(true, true);
410 case LFUN_MOUSE_PRESS:
412 return DispatchResult(true, true);
414 case LFUN_MOUSE_MOTION:
415 lfunMouseMotion(cmd);
416 return DispatchResult(true, true);
418 case LFUN_MOUSE_RELEASE:
419 lfunMouseRelease(cmd);
420 return DispatchResult(true, true);
423 bv->owner()->setLayout(inset.paragraphs().begin()->layout()->name());
424 return DispatchResult(true);
427 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
432 void InsetERT::setButtonLabel() const
434 setLabel(status_ == Collapsed ? getNewLabel(_("ERT")) : _("ERT"));
438 bool InsetERT::insetAllowed(InsetOld::Code code) const
440 return code == InsetOld::NEWLINE_CODE;
444 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
446 LyXFont tmpfont = mi.base.font;
447 getDrawFont(mi.base.font);
448 InsetCollapsable::metrics(mi, dim);
449 mi.base.font = tmpfont;
454 void InsetERT::draw(PainterInfo & pi, int x, int y) const
456 LyXFont tmpfont = pi.base.font;
457 getDrawFont(pi.base.font);
458 InsetCollapsable::draw(pi, x, y);
459 pi.base.font = tmpfont;
463 void InsetERT::setStatus(CollapseStatus st)
470 bool InsetERT::showInsetDialog(BufferView * bv) const
472 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
477 void InsetERT::getDrawFont(LyXFont & font) const
479 font = LyXFont(LyXFont::ALL_INHERIT, latex_language);
480 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
481 font.setColor(LColor::latex);
485 string const InsetERTMailer::name_("ert");
487 InsetERTMailer::InsetERTMailer(InsetERT & inset)
492 string const InsetERTMailer::inset2string(Buffer const &) const
494 return params2string(inset_.status());
498 void InsetERTMailer::string2params(string const & in,
499 InsetCollapsable::InsetCollapsable::CollapseStatus & status)
501 status = InsetCollapsable::Collapsed;
504 string body = split(in, name, ' ');
509 status = static_cast<InsetCollapsable::CollapseStatus>(strToInt(body));
514 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
516 return name_ + ' ' + tostr(status);