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/std_sstream.h"
40 using std::istringstream;
42 using std::ostringstream;
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);
88 string::const_iterator cit = contents.begin();
89 string::const_iterator end = contents.end();
91 for (; cit != end; ++cit) {
92 inset.paragraphs().begin()->insertChar(pos++, *cit, font);
94 // the init has to be after the initialization of the paragraph
95 // because of the label settings (draw_label for ert insets).
100 InsetERT::~InsetERT()
102 InsetERTMailer(*this).hideDialog();
106 void InsetERT::read(Buffer const & buf, LyXLex & lex)
108 bool token_found = false;
111 string const token = lex.getString();
112 if (token == "status") {
114 string const tmp_token = lex.getString();
116 if (tmp_token == "Inlined") {
118 } else if (tmp_token == "Collapsed") {
121 // leave this as default!
127 lyxerr << "InsetERT::Read: Missing 'status'-tag!"
129 // take countermeasures
130 lex.pushToken(token);
133 inset.read(buf, lex);
145 void InsetERT::write(Buffer const & buf, ostream & os) const
161 os << getInsetName() << "\n" << "status "<< st << "\n";
163 //inset.writeParagraphData(buf, os);
164 string const layout(buf.params().getLyXTextClass().defaultLayoutName());
165 ParagraphList::iterator par = inset.paragraphs().begin();
166 ParagraphList::iterator end = inset.paragraphs().end();
167 for (; par != end; ++par) {
168 os << "\n\\begin_layout " << layout << "\n";
169 pos_type siz = par->size();
170 for (pos_type i = 0; i < siz; ++i) {
171 Paragraph::value_type c = par->getChar(i);
173 case Paragraph::META_INSET:
174 if (par->getInset(i)->lyxCode() != InsetOld::NEWLINE_CODE) {
175 lyxerr << "Element is not allowed in insertERT"
178 par->getInset(i)->write(buf, os);
183 os << "\n\\backslash \n";
190 os << "\n\\end_layout\n";
195 string const InsetERT::editMessage() const
197 return _("Opened ERT Inset");
201 void InsetERT::updateStatus(bool swap) const
203 if (status_ != Inlined) {
205 status_ = swap ? Collapsed : Open;
207 status_ = swap ? Open : Collapsed;
213 void InsetERT::lfunMousePress(FuncRequest const & cmd)
215 if (status_ == Inlined)
220 InsetCollapsable::priv_dispatch(cmd, idx, pos);
225 bool InsetERT::lfunMouseRelease(FuncRequest const & cmd)
227 BufferView * bv = cmd.view();
229 if (cmd.button() == mouse_button::button3) {
234 if (status_ != Inlined && hitButton(cmd)) {
237 FuncRequest cmd1 = cmd;
239 cmd1.y = ascent() + cmd.y - inset.ascent();
241 // inlined is special - the text appears above
242 if (status_ == Inlined)
243 inset.dispatch(cmd1);
244 else if (isOpen() && cmd.y > buttonDim().y2) {
245 cmd1.y -= height_collapsed();
246 inset.dispatch(cmd1);
253 void InsetERT::lfunMouseMotion(FuncRequest const & cmd)
255 if (status_ == Inlined)
260 InsetCollapsable::priv_dispatch(cmd, idx, pos);
265 int InsetERT::latex(Buffer const &, ostream & os,
266 OutputParams const &) const
268 ParagraphList::iterator par = inset.paragraphs().begin();
269 ParagraphList::iterator end = inset.paragraphs().end();
273 pos_type siz = par->size();
274 for (pos_type i = 0; i < siz; ++i) {
275 // ignore all struck out text
276 if (isDeletedText(*par, i))
279 if (par->isNewline(i)) {
283 os << par->getChar(i);
297 int InsetERT::plaintext(Buffer const &, ostream &,
298 OutputParams const & /*runparams*/) const
304 int InsetERT::linuxdoc(Buffer const &, ostream & os,
305 OutputParams const &)const
307 ParagraphList::iterator par = inset.paragraphs().begin();
308 ParagraphList::iterator end = inset.paragraphs().end();
312 pos_type siz = par->size();
313 for (pos_type i = 0; i < siz; ++i) {
314 if (par->isNewline(i)) {
318 os << par->getChar(i);
332 int InsetERT::docbook(Buffer const &, ostream & os,
333 OutputParams const &) const
335 ParagraphList::iterator par = inset.paragraphs().begin();
336 ParagraphList::iterator end = inset.paragraphs().end();
340 pos_type siz = par->size();
341 for (pos_type i = 0; i < siz; ++i) {
342 if (par->isNewline(i)) {
346 os << par->getChar(i);
360 void InsetERT::edit(BufferView * bv, bool left)
362 if (status_ == Inlined)
363 inset.edit(bv, left);
365 InsetCollapsable::edit(bv, left);
371 InsetERT::priv_dispatch(FuncRequest const & cmd, idx_type & idx, pos_type & pos)
373 switch (cmd.action) {
375 case LFUN_MOUSE_PRESS:
377 return DispatchResult(true, true);
379 case LFUN_MOUSE_MOTION:
380 lfunMouseMotion(cmd);
381 return DispatchResult(true, true);
383 case LFUN_MOUSE_RELEASE:
384 lfunMouseRelease(cmd);
385 return DispatchResult(true, true);
387 case LFUN_INSET_MODIFY:
388 InsetERTMailer::string2params(cmd.argument, status_);
390 return DispatchResult(true, true);
397 case LFUN_FREEFONT_APPLY:
398 case LFUN_FREEFONT_UPDATE:
405 case LFUN_FONT_STATE:
407 return DispatchResult(true);
410 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
415 void InsetERT::setButtonLabel() const
417 setLabel(status_ == Collapsed ? getNewLabel(_("ERT")) : _("ERT"));
421 bool InsetERT::insetAllowed(InsetOld::Code code) const
423 return code == InsetOld::NEWLINE_CODE;
427 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
429 LyXFont tmpfont = mi.base.font;
430 getDrawFont(mi.base.font);
431 InsetCollapsable::metrics(mi, dim);
432 mi.base.font = tmpfont;
437 void InsetERT::draw(PainterInfo & pi, int x, int y) const
439 LyXFont tmpfont = pi.base.font;
440 getDrawFont(pi.base.font);
441 InsetCollapsable::draw(pi, x, y);
442 pi.base.font = tmpfont;
446 void InsetERT::setStatus(CollapseStatus st)
453 bool InsetERT::showInsetDialog(BufferView * bv) const
455 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
460 void InsetERT::getDrawFont(LyXFont & font) const
462 font = LyXFont(LyXFont::ALL_INHERIT, latex_language);
463 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
464 font.setColor(LColor::latex);
468 string const InsetERTMailer::name_("ert");
470 InsetERTMailer::InsetERTMailer(InsetERT & inset)
475 string const InsetERTMailer::inset2string(Buffer const &) const
477 return params2string(inset_.status());
481 void InsetERTMailer::string2params(string const & in,
482 InsetCollapsable::CollapseStatus & status)
484 status = InsetCollapsable::Collapsed;
486 istringstream data(in);
493 lyxerr << "InsetERTMailer::string2params(" << in << ")\n"
494 << "Missing identifier \"" << name_ << '"' << std::endl;
501 status = static_cast<InsetCollapsable::CollapseStatus>(s);
506 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
509 data << name_ << ' ' << status;