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::setFont(BufferView *, LyXFont const &, bool, bool selectall)
230 #warning FIXME. More UI stupidity...
232 // if selectall is activated then the fontchange was an outside general
233 // fontchange and this messages is not needed
235 Alert::error(_("Cannot change font"),
236 _("You cannot change font settings inside TeX code."));
240 void InsetERT::updateStatus(bool swap) const
242 if (status_ != Inlined) {
244 status(swap ? Collapsed : Open);
246 status(swap ? Open : Collapsed);
251 InsetOld::EDITABLE InsetERT::editable() const
253 return (status_ == Collapsed) ? IS_EDITABLE : HIGHLY_EDITABLE;
257 void InsetERT::lfunMousePress(FuncRequest const & cmd)
259 if (status_ == Inlined)
264 InsetCollapsable::priv_dispatch(cmd, idx, pos);
269 bool InsetERT::lfunMouseRelease(FuncRequest const & cmd)
271 BufferView * bv = cmd.view();
273 if (cmd.button() == mouse_button::button3) {
278 if (status_ != Inlined && hitButton(cmd)) {
281 FuncRequest cmd1 = cmd;
283 cmd1.y = ascent() + cmd.y - inset.ascent();
285 // inlined is special - the text appears above
286 if (status_ == Inlined)
287 inset.dispatch(cmd1);
288 else if (isOpen() && cmd.y > buttonDim().y2) {
289 cmd1.y -= height_collapsed();
290 inset.dispatch(cmd1);
297 void InsetERT::lfunMouseMotion(FuncRequest const & cmd)
299 if (status_ == Inlined)
304 InsetCollapsable::priv_dispatch(cmd, idx, pos);
309 int InsetERT::latex(Buffer const &, ostream & os,
310 OutputParams const &) const
312 ParagraphList::iterator par = inset.paragraphs.begin();
313 ParagraphList::iterator end = inset.paragraphs.end();
317 pos_type siz = par->size();
318 for (pos_type i = 0; i < siz; ++i) {
319 // ignore all struck out text
320 if (isDeletedText(*par, i))
323 if (par->isNewline(i)) {
327 os << par->getChar(i);
341 int InsetERT::plaintext(Buffer const &, ostream &,
342 OutputParams const & /*runparams*/) const
348 int InsetERT::linuxdoc(Buffer const &, ostream & os,
349 OutputParams const &)const
351 ParagraphList::iterator par = inset.paragraphs.begin();
352 ParagraphList::iterator end = inset.paragraphs.end();
356 pos_type siz = par->size();
357 for (pos_type i = 0; i < siz; ++i) {
358 if (par->isNewline(i)) {
362 os << par->getChar(i);
376 int InsetERT::docbook(Buffer const &, ostream & os,
377 OutputParams const &) const
379 ParagraphList::iterator par = inset.paragraphs.begin();
380 ParagraphList::iterator end = inset.paragraphs.end();
384 pos_type siz = par->size();
385 for (pos_type i = 0; i < siz; ++i) {
386 if (par->isNewline(i)) {
390 os << par->getChar(i);
404 void InsetERT::edit(BufferView * bv, bool left)
406 if (status_ == Inlined) {
407 inset.edit(bv, left);
409 InsetCollapsable::edit(bv, left);
417 InsetERT::priv_dispatch(FuncRequest const & cmd, idx_type & idx, pos_type & pos)
419 BufferView * bv = cmd.view();
421 if (inset.paragraphs.begin()->empty())
424 switch (cmd.action) {
426 case LFUN_INSET_MODIFY: {
427 InsetERT::ERTStatus status_;
428 InsetERTMailer::string2params(cmd.argument, status_);
431 return DispatchResult(true, true);
434 case LFUN_MOUSE_PRESS:
436 return DispatchResult(true, true);
438 case LFUN_MOUSE_MOTION:
439 lfunMouseMotion(cmd);
440 return DispatchResult(true, true);
442 case LFUN_MOUSE_RELEASE:
443 lfunMouseRelease(cmd);
444 return DispatchResult(true, true);
447 bv->owner()->setLayout(inset.paragraphs.begin()->layout()->name());
448 return DispatchResult(true);
450 case LFUN_BREAKPARAGRAPH:
451 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
453 case LFUN_BACKSPACE_SKIP:
455 case LFUN_DELETE_SKIP:
456 case LFUN_DELETE_LINE_FORWARD:
459 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
462 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
467 string const InsetERT::get_new_label() const
470 pos_type const max_length = 15;
471 pos_type const p_siz = inset.paragraphs.begin()->size();
472 pos_type const n = min(max_length, p_siz);
475 for( ; i < n && j < p_siz; ++j) {
476 if (inset.paragraphs.begin()->isInset(j))
478 la += inset.paragraphs.begin()->getChar(j);
481 if (inset.paragraphs.size() > 1 || (i > 0 && j < p_siz)) {
491 void InsetERT::setButtonLabel() const
493 setLabel(status_ == Collapsed ? get_new_label() : _("ERT"));
497 bool InsetERT::checkInsertChar(LyXFont & /* font */)
500 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
501 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
502 font.setColor(LColor::latex);
508 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
512 inset.metrics(mi, dim);
514 InsetCollapsable::metrics(mi, dim);
515 // Make it stand out on its own as it is code, not part of running
517 if (isOpen() && !inlined())
518 dim.wid = mi.base.textwidth;
523 void InsetERT::draw(PainterInfo & pi, int x, int y) const
525 InsetCollapsable::draw(pi, x, y, inlined());
529 void InsetERT::set_latex_font(BufferView * /*bv*/)
532 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
533 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
534 font.setColor(LColor::latex);
535 inset.text_.setFont(bv, font, false);
540 void InsetERT::status(ERTStatus const st) const
562 bool InsetERT::showInsetDialog(BufferView * bv) const
564 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
569 void InsetERT::open()
576 void InsetERT::close() const
578 if (status_ == Collapsed || status_ == Inlined)
585 void InsetERT::getDrawFont(LyXFont & font) const
587 font = LyXFont(LyXFont::ALL_INHERIT, latex_language);
588 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
589 font.setColor(LColor::latex);
593 string const InsetERTMailer::name_("ert");
595 InsetERTMailer::InsetERTMailer(InsetERT & inset)
600 string const InsetERTMailer::inset2string(Buffer const &) const
602 return params2string(inset_.status());
606 void InsetERTMailer::string2params(string const & in,
607 InsetERT::ERTStatus & status)
609 status = InsetERT::Collapsed;
612 string body = split(in, name, ' ');
617 status = static_cast<InsetERT::ERTStatus>(strToInt(body));
621 string const InsetERTMailer::params2string(InsetERT::ERTStatus status)
623 return name_ + ' ' + tostr(status);